updated vision to dismiss buffered frames

This commit is contained in:
Joel Klimont 2021-04-02 13:51:31 +02:00
parent 0e43f88fda
commit c1451d27b5

View file

@ -1,4 +1,7 @@
import os
import queue
from queue import Queue
import cv2
import logging
import threading
@ -40,6 +43,31 @@ class __Streaming:
grab frames -> do your own processing -> publish frame -> view on http server
"""
class __NoBufferVideoCapture:
def __init__(self, cam):
self.cap = cv2.VideoCapture(cam)
self.cap.set(3, 640)
self.cap.set(4, 480)
self.q = queue.Queue(maxsize=5)
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 __init__(self):
"""
Create instance of __Streaming class
@ -49,7 +77,10 @@ class __Streaming:
"""
Logging.get_logger().info("capturing rtmp stream is disabled in this version")
#self.__camera_stream = cv2.VideoCapture(RTMP_SERVER)
self.__camera_stream = cv2.VideoCapture(0)
#self.__camera_stream = cv2.VideoCapture(0)
#self.__camera_stream.set(3, 640)
#self.__camera_stream.set(4, 480)
self.__camera_stream = self.__NoBufferVideoCapture(0)
self.__newest_frame = None
self.__lock = threading.Lock()
Logging.get_logger().info("Initialized vision")
@ -60,7 +91,8 @@ class __Streaming:
:return: An opencv frame
"""
ret, img16 = self.__camera_stream.read()
#ret, img16 = self.__camera_stream.read()
img16 = self.__camera_stream.read()
return img16
def publish_frame(self, image):