Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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()
@cameronmoreau

This comment has been minimized.

Copy link

@cameronmoreau cameronmoreau commented Mar 26, 2018

Great example thanks!
May want to add the following to line 37 in the stop function

if self.thread.is_alive():
    self.thread.join()
@Fercho191

This comment has been minimized.

Copy link

@Fercho191 Fercho191 commented Apr 19, 2018

Thanks!

@tejasa97

This comment has been minimized.

Copy link

@tejasa97 tejasa97 commented Aug 31, 2018

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

This comment has been minimized.

Copy link

@dscha09 dscha09 commented Sep 5, 2018

I am getting this error:

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

This comment has been minimized.

Copy link

@ecyoung3 ecyoung3 commented Sep 13, 2018

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

This comment has been minimized.

Copy link

@raggot 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

This comment has been minimized.

Copy link

@jishang1602 jishang1602 commented Oct 31, 2020

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

This comment has been minimized.

Copy link

@KerolosMelad KerolosMelad commented Jun 19, 2021

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

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