1

I am trying to read the camera image on Jetson Xavier (ubuntu 18). I am facing a problem. When I run the following code it gives a warning and gives a black (full) image.

[ WARN:0] global /home/nvidia/host/build_opencv/nv_opencv/modules/videoio/src/cap_gstreamer.cpp (933) open OpenCV | GStreamer warning: Cannot query video position: status=0, value=-1, duration=-1

cam=cv2.VideoCapture(0)
if cam.isOpened():
    grab,img = cam.read()
    if grab is True:
        cv2.imshow('sample image',img)
    else:
        print(f"Image not found")
else:
    print("Camera not openedd")
       

cv2.waitKey(0) # waits until a key is pressed
cv2.destroyAllWindows() # destroys the window showing image

If I use 'dev/video0' to read the image i.e.

cam=cv2.VideoCapture('dev/video0')

I get the warning and custom error message of camera not opened

[ WARN:0] global /home/nvidia/host/build_opencv/nv_opencv/modules/videoio/src/cap_gstreamer.cpp (711) open OpenCV | GStreamer warning: Error opening bin: no element "dev" [ WARN:0] global /home/nvidia/host/build_opencv/nv_opencv/modules/videoio/src/cap_gstreamer.cpp (480) isPipelinePlaying OpenCV | GStreamer warning: GStreamer: pipeline have not been created Camera not opened

Then I created gstream string and passed that to video capture as shown below. the string is as follow

gstr = 'varguscamerasrc ! video/x-raw(memory:NVMM), width=(int)1920, height=(int)1080, format=(string)NV12, framerate=(fraction)60/1 ! nvvidconv flip-method=0 ! video/x-raw, width=(int)1280, height=(int)720, format=(string)BGRx ! videoconvert ! video/x-raw, format=(string)BGR ! appsink'

cap = cv2.VideoCapture(gstr, cv2.CAP_GSTREAMER)

I get the following error

Error generated. /dvs/git/dirty/git-master_linux/multimedia/nvgstreamer/gst-nvarguscamera/gstnvarguscamerasrc.cpp, execute:645 No cameras available

(python3:15402): GStreamer-CRITICAL **: 19:08:54.835: gst_mini_object_set_qdata: assertion 'object != NULL' failed
[ WARN:0] global /home/nvidia/host/build_opencv/nv_opencv/modules/videoio/src/cap_gstreamer.cpp (933) open OpenCV | GStreamer warning: Cannot query video position: status=0, value=-1, duration=-1
Traceback (most recent call last):

I am new to jetson please guide me. Thanks

Imran
  • 775
  • 7
  • 19
  • 1
    This may be similar to [this problem](https://stackoverflow.com/questions/65010313/opencv-videocapture-doesnt-work-ubuntu) ? It is fixed in there with the flag `cv2.CAP_V4L2`, `VideoCapture(0,cv2.CAP_V4L2)`, you may try too. – Yunus Temurlenk Apr 20 '21 at 04:37
  • @YunusTemurlenk Thanks. Now I am not getting the warning of GStreamer but the image is black. The image is perfect when connected to Windows PC. – Imran Apr 20 '21 at 06:34

1 Answers1

1

You can try the following code. Also make sure you have installed OpenCV from source not using pip because Jetson Nano and Xavier make some problems when you install OpenCV from pip.

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()
  • 1
    I removed the gray line in code and showed frame as it is and it is working. The only difference i can check in my code and this is while loop. Why this is working and mine is not? – Imran Apr 20 '21 at 19:38
  • Yes the only difference is of while loop. You are capturing frame from video and not reading them continuously so your code is not working. When you capture frame from video/webcam you have to put read function in while loop in order to capture frame continuously. – Hamza Chaudhary Apr 21 '21 at 07:06
  • Actually what is happeneing is that ... The first frame is always black and the next frames are fine. So I was just reading the first frame. The reason to read first frame was that just to check the image of camera not to read video. But still this is strange that the first frame is black. – Imran Apr 21 '21 at 07:45
  • Have you tried by giving some warm up time to camera? by using time.sleep(0.01) before capturing of frame. This thing is worked on raspberry pi camera on raspberry pi 3b. – Hamza Chaudhary Apr 22 '21 at 10:04
  • Yes. It was working for more than half an hour. – Imran Apr 22 '21 at 12:07