首页 > 解决方案 > 使用 OpenCV 显示帧时出现问题?

问题描述

你好,

我使用我的 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 替换cv2.imshowimshow(frame),它通常可以正常工作并输出图像,所以我认为问题在于 OpenCV 而不是我的相机。知道我做错了什么以及如何使恒定(> = 21FPS)的帧流工作吗?

感谢您提前回答!

标签: pythonimageopencvcameravideo-capture

解决方案


推荐阅读