Skip to content

Instantly share code, notes, and snippets.

@lhlong
Created October 13, 2019 14:23
Show Gist options
  • Save lhlong/422b4e45253736376a19dc7043877f87 to your computer and use it in GitHub Desktop.
Save lhlong/422b4e45253736376a19dc7043877f87 to your computer and use it in GitHub Desktop.
import cv2, queue, threading, time
class VideoCapture:
def __init__(self, name):
self.cap = cv2.VideoCapture(name)
self.q = queue.Queue()
t = threading.Thread(target=self._reader)
t.daemon = True
t.start()
def _reader(self):
while True:
ret, frame = self.cap.read()
if not ret:
break
if not self.q.empty():
try:
self.q.get_nowait()
except Queue.Empty:
pass
self.q.put(frame)
def read(self):
return self.q.get()
def capture_img(cap, tt):
if tt == 'tt':
frame = cap.read()
cv2.imshow("demo", frame)
if __name__=='__main__':
cap = VideoCapture(0)
while True:
tt = input('Type something: ')
capture_img(cap, tt)
tt = ''
if chr(cv2.waitKey(1)&255) == 'q':
break
# try https://pypi.org/project/acapture/ if you need
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment