36 lines
1.1 KiB
Python
36 lines
1.1 KiB
Python
|
from threading import Thread
|
||
|
import cv2, time
|
||
|
|
||
|
|
||
|
class VideoStreamWidget(object):
|
||
|
# modified from Nathancy: https://stackoverflow.com/a/55131226
|
||
|
def __init__(self, src=0):
|
||
|
self.capture = cv2.VideoCapture(src)
|
||
|
# Start the thread to read frames from the video stream
|
||
|
self.thread = Thread(target=self.update, args=())
|
||
|
self.thread.daemon = True
|
||
|
self.thread.start()
|
||
|
|
||
|
def update(self):
|
||
|
# Read the next frame from the stream in a different thread
|
||
|
while True:
|
||
|
if self.capture.isOpened():
|
||
|
(self.status, self.frame) = self.capture.read()
|
||
|
time.sleep(.01)
|
||
|
|
||
|
def show_frame(self):
|
||
|
# Display frames in main program
|
||
|
cv2.imshow('frame', self.frame)
|
||
|
key = cv2.waitKey(1)
|
||
|
if key == ord('q'):
|
||
|
self.capture.release()
|
||
|
cv2.destroyAllWindows()
|
||
|
exit(1)
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
video_stream_widget = VideoStreamWidget(2)
|
||
|
while True:
|
||
|
try:
|
||
|
video_stream_widget.show_frame()
|
||
|
except AttributeError:
|
||
|
pass
|