【问题标题】:open cv shows green screen on jetson nanoopencv在jetson nano上显示绿屏
【发布时间】:2020-10-09 01:25:50
【问题描述】:

我的相机显示绿屏。我用的是IMX 219 我不知道为什么相机给this output

import cv2
    
cap=cv2.VideoCapture(0)    
 
while True:
    r,im=cap.read()
    cv2.imshow('dd',im)
    k=cv2.waitKey(30) & 0xff
    if k==27:
         break
    
cap.release()
cv2.destroyAllWindows()

【问题讨论】:

    标签: opencv nvidia-jetson


    【解决方案1】:

    喏,

    一般理论

    正如this link 中所说,您可以使用v4l2-ctl 来确定相机功能。 v4l2-ctlv4l-utils 中:

    $ sudo apt-get install v4l-utils
    

    然后:

    $ v4l2-ctl --list-formats-ext
    

    查看相同的链接和this other,我看到您还可以快速测试您的相机启动:

    # Simple Test
    # Ctrl^C to exit
    # sensor_id selects the camera slot: 0 or 1 on Jetson Nano B01
    $ gst-launch-1.0 nvarguscamerasrc sensor_id=0 ! nvoverlaysink
    

    这个简单的gst-launch 示例可用于确定您正在使用的传感器报告的相机模式。例如说你得到这个输出:

    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-launch-1.0 nvarguscamerasrc sensor_id=1 ! \
       'video/x-raw(memory:NVMM),width=3264, height=2464, framerate=21/1, format=NV12' ! \
       nvvidconv flip-method=0 ! 'video/x-raw, width=816, height=616' ! \
       nvvidconv ! nvegltransform ! nveglglessink -e
    

    sensor_id=1代表正确的CSI摄像头插槽,可以是01。从this link 可以看出,较新的 Jetson Nano 开发套件带有两个 CSI 摄像头插槽,您可以使用此属性指定正确的一个 [0 是默认值]。请注意,在同一个链接中,他们使用sensor_mode 而不是sensor_id,我会尝试两者。您不一定需要包含flip-method,但here 已记录在案。所有这些都应该让您了解要在代码中插入的值

    另外,我们注意到显示变换对宽度和高度很敏感[在上面的例子中,width=816, height=616]。如果遇到问题,请检查您的显示宽度和高度是否与选择的相机帧大小的比例相同[在上面的示例中,816 和 616 分别是 3264 和 2464 的四分之一]

    OpenCV

    在 nVidia 论坛上环顾四周,我找到了this post。在这种情况下,解决方案是使用:

    cap = cv2.VideoCapture('nvarguscamerasrc ! video/x-raw(memory:NVMM), width=3280, height=2464, format=(string)NV12, framerate=(fraction)20/1 ! nvvidconv ! video/x-raw, format=(string)BGRx ! videoconvert ! video/x-raw, format=(string)BGR ! appsink', cv2.CAP_GSTREAMER)
    

    但在您的情况下,如果帧大小等于 3280x2464,IMX 219 的 20fps 将太高。从this link 的第一个表中可以看到,建议的值为 15fps,而here 他们建议的值为 21fps。我建议您从上一节中检索到的 widthheightframerate 值开始。低于标称值的framerate 值应该可以帮助您测试连接性

    cap = cv2.VideoCapture('nvarguscamerasrc ! video/x-raw(memory:NVMM), width=3280, height=2464, format=(string)NV12, framerate=(fraction)15/1 ! nvvidconv ! video/x-raw, format=(string)BGRx ! videoconvert ! video/x-raw, format=(string)BGR ! appsink', cv2.CAP_GSTREAMER)
    

    包含上一行的完整示例使用正确的值更新可用from here

    # MIT License
    # Copyright (c) 2019 JetsonHacks
    # See license
    # Using a CSI camera (such as the Raspberry Pi Version 2) connected to a
    # NVIDIA Jetson Nano Developer Kit using OpenCV
    # Drivers for the camera and OpenCV are included in the base image
    
    import cv2
    
    # gstreamer_pipeline returns a GStreamer pipeline for capturing from the CSI camera
    # Defaults to 1280x720 @ 60fps
    # Flip the image by setting the flip_method (most common values: 0 and 2)
    # display_width and display_height determine the size of the window on the screen
    
    
    def gstreamer_pipeline(
        capture_width=1280,
        capture_height=720,
        display_width=1280,
        display_height=720,
        framerate=60,
        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,
            )
        )
    
    
    def show_camera():
        # To flip the image, modify the flip_method parameter (0 and 2 are the most common)
        print(gstreamer_pipeline(flip_method=0))
        cap = cv2.VideoCapture(gstreamer_pipeline(flip_method=0), cv2.CAP_GSTREAMER)
        if cap.isOpened():
            window_handle = cv2.namedWindow("CSI Camera", cv2.WINDOW_AUTOSIZE)
            # Window
            while cv2.getWindowProperty("CSI Camera", 0) >= 0:
                ret_val, img = cap.read()
                cv2.imshow("CSI Camera", img)
                # This also acts as
                keyCode = cv2.waitKey(30) & 0xFF
                # Stop the program on the ESC key
                if keyCode == 27:
                    break
            cap.release()
            cv2.destroyAllWindows()
        else:
            print("Unable to open camera")
    
    
    if __name__ == "__main__":
        show_camera()
    

    this other link 上还有一个完整的 sn-p,可以帮助您找到失败的原因

    import cv2
    import gi
    gi.require_version('Gst', '1.0')
    from gi.repository import Gst
    
    def read_cam():
            cap = cv2.VideoCapture("nvarguscamerasrc ! video/x-raw(memory:NVMM), width=(int)1920, height=(int)1080,format=(string)NV12, framerate=(fraction)30/1 ! nvvidconv ! video/x-raw, format=(string)BGRx ! videoconvert ! video/x-raw, format=(string)BGR ! appsink")
            if cap.isOpened():
                    cv2.namedWindow("demo", cv2.WINDOW_AUTOSIZE)
                    while True:
                            ret_val, img = cap.read();
                            cv2.imshow('demo',img)
                            if cv2.waitKey(30) == ord('q'): 
                                break
            else:
                    print ("camera open failed")
    
            cv2.destroyAllWindows()
    
    if __name__ == '__main__':
            print(cv2.getBuildInformation())
            Gst.debug_set_active(True)
            Gst.debug_set_default_threshold(3)
            read_cam()
    

    最后,如果您可以从命令行在 GStreamer 中打开相机但不能在 Python 中打开相机,请使用之前的 print(cv2.getBuildInformation()) 或更短的时间检查 OpenCV 版本:

    print(cv2.__version__)
    

    从 L4T 32.2.1 / JetPack 4.2.2 开始,OpenCV 中内置了 GStreamer 支持。这些版本的 OpenCV 版本是 3.3.1,如果您使用的是早期版本的 OpenCV [很可能从 Ubuntu 存储库安装],您将收到 Unable to open camera 错误

    祝你有美好的一天,
    安东尼诺

    【讨论】:

    • 你好,我收到了这个错误 cv2.imshow('dd',im) cv2.error: /build/opencv-XDqSFW/opencv-3.2.0+dfsg/modules/highgui/src/window .cpp:304: 错误:(-215) size.width>0 && size.height>0 in function imshow
    • @AAAbood 我刚刚根据您的反馈更新了我的答案。在我更新的答案中尝试 sn-p 之前,我还会尝试根据 this answercv2.VideoCapture(0) 更改为 cv2.VideoCapture(1) 或根据 this other one 通过 time.sleep() 添加延迟
    • penCV 错误:Mat 文件 /build/opencv-XDqSFW/opencv-3.2.0+dfsg/modules/core/ 中的断言失败(total() == 0 || data != NULL) include/opencv2/core/mat.inl.hpp,第 431 行 Traceback(最近一次调用最后一次):文件“/home/abood/Python Project/tes1.py”,第 9 行,在 ret,frame = cam. read() cv2.error: /build/opencv-XDqSFW/opencv-3.2.0+dfsg/modules/core/include/opencv2/core/mat.inl.hpp:431: 错误: (-215) total() = = 0 || data != 函数 Mat 中的 NULL
    • @AAAbood 我开始认为问题可能是由于配置要求性能要求太高:从头开始查看我编辑的答案并尝试找到正确的值来调用来自 Python 代码
    猜你喜欢
    • 2020-10-26
    • 2020-12-16
    • 2021-10-26
    • 1970-01-01
    • 2020-01-19
    • 2021-06-17
    • 2020-10-17
    • 2021-04-21
    • 1970-01-01
    相关资源
    最近更新 更多