Skip to content

Instantly share code, notes, and snippets.

@toandaominh1997
Last active December 2, 2019 11:26
Show Gist options
  • Save toandaominh1997/3e25a2791bb6a11f14291c5c24e65691 to your computer and use it in GitHub Desktop.
Save toandaominh1997/3e25a2791bb6a11f14291c5c24e65691 to your computer and use it in GitHub Desktop.
import cv2
from threading import Thread
import datetime
class WebcamVideoStream:
def __init__(self, src, width, height):
# initialize the video camera stream and read the first frame
# from the stream
self.stream = cv2.VideoCapture(src)
self.stream.set(cv2.CAP_PROP_FRAME_WIDTH, width)
self.stream.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
(self.grabbed, self.frame) = self.stream.read()
# initialize the variable used to indicate if the thread should
# be stopped
self.stopped = False
def start(self):
# start the thread to read frames from the video stream
Thread(target=self.update, args=()).start()
return self
def update(self):
# keep looping infinitely until the thread is stopped
while True:
# if the thread indicator variable is set, stop the thread
if self.stopped:
return
# otherwise, read the next frame from the stream
(self.grabbed, self.frame) = self.stream.read()
def read(self):
# return the frame most recently read
return self.frame
def size(self):
# return size of the capture device
return self.stream.get(3), self.stream.get(4)
def stop(self):
# indicate that the thread should be stopped
self.stopped = True
import datetime
class FPS:
def __init__(self):
# store the start time, end time, and total number of frames
# that were examined between the start and end intervals
self._start = None
self._end = None
self._numFrames = 0
def start(self):
# start the timer
self._start = datetime.datetime.now()
return self
def stop(self):
# stop the timer
self._end = datetime.datetime.now()
def update(self):
# increment the total number of frames examined during the
# start and end intervals
self._numFrames += 1
def elapsed(self):
# return the total number of seconds between the start and
# end interval
return (self._end - self._start).total_seconds()
def fps(self):
# compute the (approximate) frames per second
return self._numFrames / self.elapsed()
multi_thread = True
src = 2
width = 1280
height = 720
if(multi_thread):
webcam = WebcamVideoStream(src, width, height).start()
else:
webcam = cv2.VideoCapture(2)
webcam.set(cv2.CAP_PROP_FRAME_WIDTH, width)
webcam.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
num_frames = 0
start_time = datetime.datetime.now()
fps_class = FPS().start()
while(num_frames<100):
if(multi_thread):
frame = webcam.read()
else:
ret, frame = webcam.read()
num_frames +=1
fps_class.update()
cv2.imshow('Multi-Threaded Detection', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
elapsed_time = (datetime.datetime.now() - start_time).total_seconds()
fps = num_frames / elapsed_time
print("num_frames: ", num_frames)
print('elapsed_time: ', elapsed_time)
print("fps: ", fps)
# # stop the timer and display FPS information
fps_class.stop()
print("[INFO] elasped time: {:.2f}".format(fps_class.elapsed()))
print("[INFO] approx. FPS: {:.2f}".format(fps_class.fps()))
if(multi_thread):
webcam.stop()
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment