使用OpenCV显示帧时出现问题?

2024-05-29 04:07:13 发布

您现在位置:Python中文网/ 问答频道 /正文

你好,

我使用RaspberryPi摄像头模块v2.1和Jetson Nano以及Python来捕获摄像头中的帧并显示它们,从而创建一种“视频流”。但是我的问题是关于OpenCV的使用,而不是关于我使用的硬件。 我的代码如下:

import cv2
import numpy as np
from scipy.misc import imshow
import time

def gstreamer_pipeline (capture_width=3280, capture_height=2464, display_width=1280, display_height=720, framerate=21, flip_method=0) :   
    return ('nvarguscamerasrc ! ' 
    'video/x-raw(memory:NVMM), '
    'width=(int)%d, height=(int)%d, '
    'format=(string)NV12, framerate=(fraction)%d/1 ! '
    'nvvidconv flip-method=%d ! '
    'video/x-raw, width=(int)%d, height=(int)%d, format=(string)BGRx ! '
    'videoconvert ! '
    'video/x-raw, format=(string)BGR ! appsink'  % (capture_width,capture_height,framerate,flip_method,display_width,display_height))

if __name__ == '__main__':
    cap = cv2.VideoCapture(gstreamer_pipeline(flip_method=0), cv2.CAP_GSTREAMER)

    if cap.isOpened():
        try:
            while cap.isOpened():
                ret, frame = cap.read()
                frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
                cv2.imshow('frame',frame)
        except KeyboardInterrupt:
            print("Stopped video streaming.")
            cap.release()
            cv2.destroyAllWindows()
    else:
        print("Unable to open camera.")

我正在阅读捕获设备,对每个帧的RGB应用颜色转换,并希望使用cv2.imshow('frame',frame)显示它,正如我在教程中看到的那样。问题是,这段代码不会做任何事情,我没有得到任何窗口和错误消息。 程序生成的输出:

GST_ARGUS: Creating output stream
CONSUMER: Waiting until producer is connected...
GST_ARGUS: Available Sensor modes :
GST_ARGUS: 3264 x 2464 FR = 21.000000 fps Duration = 47619048 ; Analog Gain range min 1.000000, max 10.625000; Exposure Range min 13000, max 683709000;

GST_ARGUS: 3264 x 1848 FR = 28.000001 fps Duration = 35714284 ; Analog Gain range min 1.000000, max 10.625000; Exposure Range min 13000, max 683709000;

GST_ARGUS: 1920 x 1080 FR = 29.999999 fps Duration = 33333334 ; Analog Gain range min 1.000000, max 10.625000; Exposure Range min 13000, max 683709000;

GST_ARGUS: 1280 x 720 FR = 59.999999 fps Duration = 16666667 ; Analog Gain range min 1.000000, max 10.625000; Exposure Range min 13000, max 683709000;

GST_ARGUS: 1280 x 720 FR = 120.000005 fps Duration = 8333333 ; Analog Gain range min 1.000000, max 10.625000; Exposure Range min 13000, max 683709000;

GST_ARGUS: Running with following settings:
   Camera index = 0 
   Camera mode  = 0 
   Output Stream W = 3264 H = 2464 
   seconds to Run    = 0 
   Frame Rate = 21.000000 
GST_ARGUS: PowerService: requested_clock_Hz=37126320
GST_ARGUS: Setup Complete, Starting captures for 0 seconds
GST_ARGUS: Starting repeat capture requests.
CONSUMER: Producer has connected; continuing.
Gtk-Message: 21:05:04.463: Failed to load module "canberra-gtk-module"

加载错误(经常发生;程序仍在运行)后,它什么也不做,直到我用CTRL+C取消。如果我用SciPy用一个简单的imshow(frame)替换cv2.imshow,它正常工作并输出图像,所以我认为问题出在OpenCV而不是我的相机上。 你知道我做错了什么,以及如何让恒定(>;=21FPS)的帧流工作吗

提前感谢您的回答


Tags: frminwidthcv2framemaxcapturecap

热门问题