From c1451d27b58c49c3436a18a7f6c8541c0a3704ff Mon Sep 17 00:00:00 2001 From: Joel Klimont Date: Fri, 2 Apr 2021 13:51:31 +0200 Subject: [PATCH] updated vision to dismiss buffered frames --- compLib/Vision.py | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/compLib/Vision.py b/compLib/Vision.py index 5693198..2bd3558 100644 --- a/compLib/Vision.py +++ b/compLib/Vision.py @@ -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):