Search code examples
pythonopencvcameranvidia-jetson-nano

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


I am running a raspberry pi camera on the Jetson Nano with Python 3.6.9, OpenCV version 4.1.0, and the latest version of Ubuntu. I am running the code:

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()

When I run it, it throws an error with a traceback to line 11, saying that "ret, frame = cv2.imread(cam)" has a TypeError: bad argument type for built-in operation. I can use opencv to load images, but running a video always seems to throw this error. "ls -l /dev/video0" in the command line works fine, so I know I am connected to the camera. Thanks!


Solution

  • As per documentation: Python: cv2.imread(filename[, flags]) → retval

    imread expects a path while you need this snippet:

    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()