Skip to content

Instantly share code, notes, and snippets.

@allskyee
Last active December 7, 2022 15:59
Show Gist options
  • Star 34 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save allskyee/7749b9318e914ca45eb0a1000a81bf56 to your computer and use it in GitHub Desktop.
Save allskyee/7749b9318e914ca45eb0a1000a81bf56 to your computer and use it in GitHub Desktop.
opencv python camera frame grab and display on different threads with safe synchronization
#!/usr/bin/env python
from threading import Thread, Lock
import cv2
class WebcamVideoStream :
def __init__(self, src = 0, width = 320, height = 240) :
self.stream = cv2.VideoCapture(src)
self.stream.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, width)
self.stream.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, height)
(self.grabbed, self.frame) = self.stream.read()
self.started = False
self.read_lock = Lock()
def start(self) :
if self.started :
print "already started!!"
return None
self.started = True
self.thread = Thread(target=self.update, args=())
self.thread.start()
return self
def update(self) :
while self.started :
(grabbed, frame) = self.stream.read()
self.read_lock.acquire()
self.grabbed, self.frame = grabbed, frame
self.read_lock.release()
def read(self) :
self.read_lock.acquire()
frame = self.frame.copy()
self.read_lock.release()
return frame
def stop(self) :
self.started = False
self.thread.join()
def __exit__(self, exc_type, exc_value, traceback) :
self.stream.release()
if __name__ == "__main__" :
vs = WebcamVideoStream().start()
while True :
frame = vs.read()
cv2.imshow('webcam', frame)
if cv2.waitKey(1) == 27 :
break
vs.stop()
cv2.destroyAllWindows()
@Fercho191
Copy link

Thanks!

@tejasa97
Copy link

Do you suppose the read method along with imshow also could be threaded? So that it is always reading and displaying from stream while the main function is idle? (i.e : you can do other stuff in the main function)

@dscha09
Copy link

dscha09 commented Sep 5, 2018

I am getting this error:

AttributeError: module 'cv2.cv2' has no attribute 'cv'

@ecyoung3
Copy link

I am getting this error:

AttributeError: module 'cv2.cv2' has no attribute 'cv'

You probably need to change them to cv2.CAP_PROP_FRAME_WIDTH and HEIGHT respectively, that should solve the problem

@raggot
Copy link

raggot commented Sep 26, 2018

Thank you! Very cool.
If someone else gets AttributeError: 'NoneType' object has no attribute 'copy', make sure your webcam is connected... :)

@jishang1602
Copy link

The code is working fine. But I want to implement it for restarted source. E.g. if I am running in background. If the camera is going to restart after 2-3 hours the code should accept it without re-run it. Cloud it be possible?

@KerolosMelad
Copy link

Good job ! I want to ask you a question privately, how can I contact you ?

@nup002
Copy link

nup002 commented Dec 6, 2022

FYI for those who stumble upon this gist, I have written a python library for this exact purpose.
https://github.com/nup002/Threaded-VideoCapture

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment