-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_image.py
105 lines (92 loc) · 3.45 KB
/
get_image.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import numpy as np
import cv2
import time
import requests
import threading
from threading import Thread, Event, ThreadError
import sys
import datetime
import os
class capture_image():
def __init__(self, url, label='', directory='.'):
self.label = label
self.stream = requests.get(url, stream=True)
self.thread_cancelled = False
self.thread = Thread(target=self.run)
self.start()
self.dir = directory
if not os.path.isdir(self.dir):
os.makedirs(self.dir)
def start(self):
self.thread.start()
def run(self):
bytes=''
while not self.thread_cancelled:
try:
bytes+=self.stream.raw.read(1024)
a = bytes.find('\xff\xd8')
b = bytes.find('\xff\xd9')
if a!=-1 and b!=-1:
jpg = bytes[a:b+2]
bytes= bytes[b+2:]
img = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8),cv2.IMREAD_COLOR)
# cv2.imshow('cam',img)
currenttimestamp = '{:%Y-%m-%d %H.%M.%S}'.format(datetime.datetime.now())
print self.dir+'/'+currenttimestamp+'_'+self.label+'.png'
cv2.imwrite(self.dir+'/'+currenttimestamp+'_'+self.label+'.png',img)
return True
except ThreadError:
self.thread_cancelled = True
def is_running(self):
return self.thread.isAlive()
def shut_down(self):
self.thread_cancelled = True
#block while waiting for thread to terminate
while self.thread.isAlive():
time.sleep(1)
return True
class show_image():
def __init__(self, url, label='', directory='.'):
self.label = label
self.stream = requests.get(url, stream=True)
self.thread_cancelled = False
self.thread = Thread(target=self.run)
self.start()
self.dir = directory
if not os.path.isdir(self.dir):
os.makedirs(self.dir)
def start(self):
self.thread.start()
def run(self):
bytes=''
while not self.thread_cancelled:
try:
bytes+=self.stream.raw.read(1024)
a = bytes.find('\xff\xd8')
b = bytes.find('\xff\xd9')
if a!=-1 and b!=-1:
jpg = bytes[a:b+2]
bytes= bytes[b+2:]
img = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8),cv2.IMREAD_COLOR)
cv2.imshow('cam',img)
k = cv2.waitKey(100)
if k == 27:
currenttimestamp = '{:%Y-%m-%d %H.%M.%S}'.format(datetime.datetime.now())
cv2.imwrite(currenttimestamp+'_'+'.png',img)
return True # esc to quit
elif k == ord('s'):
currenttimestamp = '{:%Y-%m-%d %H.%M.%S}'.format(datetime.datetime.now())
cv2.imwrite(currenttimestamp+'_'+'.png',img)
except ThreadError:
self.thread_cancelled = True
def is_running(self):
return self.thread.isAlive()
def shut_down(self):
self.thread_cancelled = True
#block while waiting for thread to terminate
while self.thread.isAlive():
time.sleep(1)
return True
if __name__ == "__main__":
url = 'http://148.79.170.34/mjpg/video.mjpg'
cam = show_image(url,'test')