-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstream_thread.py
executable file
·54 lines (43 loc) · 1.36 KB
/
stream_thread.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
import threading
from concurrent.futures import process
from queue import Queue
import cv2
class StreamThread():
__cam = None
def __init__(self, camSrc=0, QSize=128) -> None:
self.__stopped = False
self.__Q = Queue(QSize)
with process.ProcessPoolExecutor() as executor:
executor.map(self.__init_webcam())
def start(self):
self.__thread = threading.Thread(target=self.update)
self.__thread.daemon = True
self.__thread.start()
def update(self):
while True:
try:
if self.__stopped:
self.stopStream()
break
if not self.__Q.full():
isGrabbed, frame = self.__cam.read()
if not isGrabbed:
print("frame not grbbed")
self.stopStream()
break
self.__Q.put(frame)
except Exception as e:
print(e)
def stopStream(self):
self.__stopped = True
self.__cam.release()
cv2.destroyAllWindows()
def readFrame(self):
return self.__Q.get()
def moreFrames(self):
return self.__Q.qsize() > 0
def getNoFrames(self):
return self.__Q.qsize()
def __init_webcam(self):
self.__cam = cv2.VideoCapture(0)
print("webcam started")