python – VideoStream lag with Raspberry Pi 4 4GB and OpenCv

I've a query. I've issues to get a VideoStream on my Raspberry Pi 4. Im utilizing a USB-Webcam named Logitech HD Professional Webcam C920 and python+opencv. I've written a Python script attempting to arrange a full HD Webcam-Stream. If i simply let the API Backend to CAP_V4L4 in cv2.VideoCapture() i get good FPS round 30-60 fps. I'll present my full code beneath:

import cv2 , time
if __name__ == '__main__':
cap = cv2.VideoCapture(cv2.CAP_V4L2)
width = 1920
peak = 1080
cap.set(cv2.CAP_PROP_FRAME_WIDTH,width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT,peak)
currentFrame=0
whereas(True):
    ret, body = cap.learn()

    body = cv2.flip(body,1)
    cv2.imshow('body',body)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
    currentFrame +=1
cap.launch()
cv2.destroyAllWindows()

if i remark out line 3-6 its all tremendous as i mentioned (places it in 640x480). But when i set dimension to full HD (1920x1080) it would begin to lag and solely have like 4-5 fps. I additionally examined the fps and cpu utilization. CPU is just about 50%. First i assumed its due to no threading and adjusted it to following code:

from threading import Thread
import cv2, time
from queue import Queue

class VideoStreamWidget(object):
    def _init_(self,queueSize=128):
         self.seize = cv2.VideoCapture(0, cv2.CAP_V4L2)
         width = 1920
         peak = 1080
         self.seize.set(cv2.CAP_PROP_FRAME_WIDTH,width)
         self.seize.set(cv2.CAP_PROP_FRAME_HEIGHT,peak)
         self.Q = Queue.(maxsize=queueSize)
         # Begin the thread to learn frames from the video stream
    def begin(self):
         thread = Thread(goal=self.replace, args=())
         thread.daemon = True
         thread.begin()
         return self

    def replace(self):
         # Learn the subsequent body from the stream in a special thread
         whereas True:
            if self.stopped():
                return
            if not self.Q.full():
                (grabbed, self.body) = self.seize.learn()
                if not grabbed:
                    self.cease()
                    return
                self.Q.put(body)
            
            
        
    def learn(self):
        return self.Q.get()
    def extra(self):
        return self.Q.qsize() > 0
    def cease(self):
        self.stopped = True
    def show_frame(self):
        # Show frames in important program
        cv2.imshow('body', self.body)
        key = cv2.waitKey(1)
        if key == ord('q'):
            self.seize.launch()
            cv2.destroyAllWindows()
            exit(1)

if _name_ == '_main_':
    video_stream_widget = VideoStreamWidget()
    whereas True:
         strive:
        
            video_stream_widget.show_frame()
        
        besides AttributeError:
            go

however this received the samer consequence. Full HD like 4-5 fps. Are you able to inform me how one can make it extra performant. Did i miss one thing someplace or is it not doable on a raspberry to stream a usb webcam in full hd at 30 fps. Ty all in your advices.



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *