TypeError: bad argument type for cv2.imread() from Raspberry Pi Camera on Jetson Nano

TypeError: bad argument type for cv2.imread() from Raspberry Pi Camera on Jetson Nano

我运行在 Jetson Nano 上使用 Python 3.6.9、OpenCV 版本 4.1.0 和最新版本 Ubuntu 安装 raspberry pi 相机.我正在 运行 编写代码:

import cv2
print(cv2.__version__)
dispW=320
dispH=240
flip=2
cam_set='nvarguscamerasrc !  video/x-raw(memory:NVMM), width=3264, height=2464, format=NV12, framerate=21/1 ! nvvidconv flip-method='+str(flip)+' ! video/x-raw, width='+str(dispW)+', height='+str(dispH)+', format=BGRx ! videoconvert ! video/x-raw, format=BGR ! appsink'
cam = cv2.VideoCapture(cam_set)
while True:
    ret, frame = cv2.imread(cam)
    cv2.imshow('Camera',frame)
    if cv2.waitKey(1)==ord('q'):
        break
cam.release()
cv2.destroyAllWindows()

当我 运行 它时,它抛出一个错误并回溯到第 11 行,说 "ret, frame = cv2.imread(cam)" 有一个类型错误:内置操作的参数类型错误。我可以使用 opencv 加载图像,但 运行 播放视频似乎总是会引发此错误。 "ls -l /dev/video0" 在命令行中工作正常,所以我知道我已连接到相机。谢谢!

根据文档: Python: cv2.imread(filename[, flags]) → retval

imread 需要路径,而您需要此代码段:

import numpy as np
import cv2

cap = cv2.VideoCapture(0)

while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()

    # Our operations on the frame come here
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # Display the resulting frame
    cv2.imshow('frame',gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()