适用于 Raspberry Pi cam 的 Opencv VideoCapture() 适用于 Python 3.6,但不适用于 Python 3.7

问题描述 投票:0回答:1

我正在编写一个小Python脚本,我想在其中使用OpenCV从我的Raspberry Pi相机捕获图像。我正在使用以下行初始化视频捕获。

cam=cv2.VideoCapture("nvarguscamerasrc ! video/x-raw(memory:NVMM), width=(int)1640, height=(int)1232,format=(string)NV12, framerate=(fraction)30/1 ! nvvidconv ! video/x-raw, format=(string)BGRx ! videoconvert ! video/x-raw, format=(string)BGR ! appsink")

我使用的是 Jetson Nano,所以一开始我使用默认的 Python 3.6 版本,但由于我需要一个仅适用于 >=3.7 的包,所以我安装了 Python 3.7 并将 shebang 行调整为

#!/usr/bin/env python3.7

之前我工作得很好,但是当我切换Python版本后就不再工作了。

我并没有真正收到错误消息,只有

cam.read()
的返回值是
False
,所以我无法从相机中抓取帧。

脚本的重要部分如下所示:

#!/usr/bin/env python3.7 //if this says Python 3.6 it is working fine.

import cv2
import sys
import argparse
import os

from pathlib import Path
from matplotlib import pyplot as plt

def main():

    cam=cv2.VideoCapture("nvarguscamerasrc ! video/x-raw(memory:NVMM), width=(int)1640, height=(int)1232,format=(string)NV12, framerate=(fraction)30/1 ! nvvidconv ! video/x-raw, format=(string)BGRx ! videoconvert ! video/x-raw, format=(string)BGR ! appsink")

    # ret is True with python 3.6 and False with 3.7
    ret, frame = cam.read()
    if not ret:
        print("Failed to grab initial frame. Fix camera!")
        sys.exit()

    cv2.imshow("test", frame)

我使用的是 OpenCV 版本 4.9.0.80。

python python-3.x opencv raspberry-pi computer-vision
1个回答
0
投票

这有帮助吗?

cam = cv2.VideoCapture("nvarguscamerasrc ! video/x-raw(memory:NVMM), width=(int)1640, height=(int)1232,format=(string)NV12, framerate=(fraction)30/1 ! nvvidconv ! video/x-raw, format=(string)BGRx ! videoconvert ! video/x-raw, format=(string)BGR ! appsink")
cap = cv2.VideoCapture(cam, cv2.CAP_GSTREAMER)
© www.soinside.com 2019 - 2024. All rights reserved.