Forums - Video Capture with OpenCV

15 posts / 0 new
Last post
Video Capture with OpenCV
yukselbera
Join Date: 26 Jul 21
Posts: 6
Posted: Mon, 2021-07-26 04:14
I have a RB5 development kit with a vision mezzanine. I would like to capture images from the main camera on the mezzanine board by using OpenCV. I tried cv2.VideoCapture(0) function but i got the error: U"nable to stop the stream: Inappropriate ioctl for device" and the capture doesn't start. What causes this problem? How can i capture images from the main camera? Please help me asap. Any help would be greatly appreciated
  • Up0
  • Down0
kevin.dai
Join Date: 21 Oct 20
Posts: 137
Posted: Mon, 2021-07-26 19:30

Hi yukselbera

The OpenCV engineers are working on this issue.

I will let you know result asap.

Thanks

Kevin

  • Up0
  • Down0
kevin.dai
Join Date: 21 Oct 20
Posts: 137
Posted: Mon, 2021-07-26 19:42

Hi yukselbera

How to use openCV in your system?

Can you post your code which use OpenCV?

I will try to reproduce your issue on my board.

Thanks

Kevin

 

  • Up0
  • Down0
yukselbera
Join Date: 26 Jul 21
Posts: 6
Posted: Tue, 2021-07-27 02:41

Hi Kevin,

Thanks for the quick response.

It's actually not a complicated code. It was just to see if the capture starts after installing openCV on the board by using pip install. Here is the code:

#!/usr/bin/env python

import cv2

video_capture = cv2.VideoCapture(0)

if video_capture.isOpened():
    print("capture started")
else:
    print("Video capture didn't start")




I pushed the python file to the board through adb and ran it on the board but it prints "Video capture didn't start" message and gives the "unable to stop the stream: Inappropriate ioctl for device"  error.

Looking forward to hearing from you,

thanks

  • Up0
  • Down0
kevin.dai
Join Date: 21 Oct 20
Posts: 137
Posted: Wed, 2021-07-28 15:31
I got error for video format, and on the investigation about it.
 
sh-4.4# python
Python 2.7.17 (default, Feb 27 2021, 15:10:58)
[GCC 7.5.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> video_capture = cv2.VideoCapture(0)
VIDEOIO ERROR: V4L2: Pixel format of incoming image is unsupported by OpenCV
>>>
 
Thanks
Kevin
  • Up0
  • Down0
kevin.dai
Join Date: 21 Oct 20
Posts: 137
Posted: Wed, 2021-07-28 15:39

This error happened on my USB camera, after I removed USB camera, I got the same error as you got.

I will check with camera engineers on it

Thanks

Kevin

  • Up0
  • Down0
yukselbera
Join Date: 26 Jul 21
Posts: 6
Posted: Thu, 2021-07-29 03:06
Thanks Kevin Looking forward to hearing from you
  • Up0
  • Down0
kevin.dai
Join Date: 21 Oct 20
Posts: 137
Posted: Mon, 2021-08-02 15:05

Hi yukselbera

I was thinking if camera should be enabled, or the OpenCV default enabled the camera?

You mar refer this sample code for CV app. 

https://github.com/quic/sample-apps-for-Qualcomm-Robotics-RB5-platform/t...

Thanks

Kevin

 

 

  • Up0
  • Down0
kevin.dai
Join Date: 21 Oct 20
Posts: 137
Posted: Mon, 2021-08-02 20:27

Hi yukselbera,

Please collect the complete log as follows:
adb root
adb remount
mkdir -p /vendor/etc/camera/ and chmod 777 /vendor/etc/camera/
touch /vendor/etc/camera/camxoverridesettings.txt
adb shell "echo overrideLogLevels=0xFF > /vendor/etc/camera/camxoverridesettings.txt"
adb shell "echo logInfoMask=0x10080 >> /vendor/etc/camera/camxoverridesettings.txt"
adb shell "echo logVerboseMask=0 >> /vendor/etc/camera/camxoverridesettings.txt"
adb reboot

adb logcat -G 256M
adb logcat -b all > xxx/logcat.txt

  • Up0
  • Down0
yukselbera
Join Date: 26 Jul 21
Posts: 6
Posted: Tue, 2021-08-03 00:49
Hi Kevin, Thanks for the reply. I checked out the link you shared but I couldn't understand its relation with my problem. The sample code uses FastCV instead of OpenCV and it uses a saved image file as the image source instead of a camera. Could you elaborate on it? How can I enable the camera?
  • Up0
  • Down0
Mayur
Join Date: 26 Feb 21
Posts: 16
Posted: Tue, 2021-08-03 10:38

Hi yukselbera,

You can capture the image from main camera with the help of GStreamer. You can install the GStreamer by using https://gstreamer.freedesktop.org/documentation/installing/on-linux.html

After installation you can run following command to capture image 

gst-launch-1.0 -e qtiqmmfsrc name=qmmf ! capsfilter caps="video/x-raw,format=NV12,width=640,height=480,framerate=30/1" ! jpegenc ! multifilesink location=test_image.jpg

or you can use following code 

import sys
import gi
from threading import Timer


gi.require_version("GLib", "2.0")
gi.require_version("GObject", "2.0")
gi.require_version("Gst", "1.0")

from gi.repository import Gst, GLib, GObject
    
Gst.init(None)
loop = GLib.MainLoop()
camera_id = 0
pipeline = Gst.Pipeline.new("image-capture")

source = Gst.ElementFactory.make("qtiqmmfsrc", "qmmf-source")
framefilter = Gst.ElementFactory.make("capsfilter", "frame-filter")
jpegenc = Gst.ElementFactory.make("jpegenc", "jpeg-encoding")
sink = Gst.ElementFactory.make("multifilesink", "multi-file-sink")

source.set_property("camera", camera_id)
framefilter.set_property("caps", Gst.caps_from_string(
    "video/x-raw,format=NV12,framerate=30/1,width=1920,height=1080"))
sink.set_property("location", "captured_image.jpg")

bus = pipeline.get_bus()
bus_id = bus.add_signal_watch()

pipeline.add(source)
pipeline.add(framefilter)
pipeline.add(jpegenc)
pipeline.add(sink)



if not Gst.Element.link(source, framefilter):
    print("Error :Framfilter can not linked")
    sys.exit(1)
if not Gst.Element.link(framefilter, jpegenc):
    print("Error :decode can not linked")
    sys.exit(1)
if not Gst.Element.link(jpegenc, sink):
    print("Error :sink can not linked")
    sys.exit(1)

def run_bus():
    print("Stop\n")
    pipeline.set_state(Gst.State.NULL)
    Gst.Object.unref(bus)
    Gst.deinit()

Gst.Element.set_state(pipeline, Gst.State.PLAYING)
print("Start")
t = Timer(5.0, run_bus)
t.run()

 

Thanks & Regards,

Mayur 

 

 

  • Up0
  • Down0
yukselbera
Join Date: 26 Jul 21
Posts: 6
Posted: Tue, 2021-08-03 14:10
Hi Mayur, Thank you so much. I will definitely try this code. Can I use GStreamer only as a real-time video source from the camera (like cv2.VideoCapture() function) and then do image processing on the video by using OpenCV?
  • Up0
  • Down0
Mayur
Join Date: 26 Feb 21
Posts: 16
Posted: Tue, 2021-08-03 22:56
Hi yukselbera,
 
You can stream the video & process by opencv
 
open terminal & run following command for video stream (host=rb5 ip)
gst-launch-1.0 -e qtiqmmfsrc name=qmmf ! video/x-h264,format=NV12, width=1280, height=720,framerate=30/1 ! h264parse config-interval=1 ! mpegtsmux name=muxer ! queue ! tcpserversink port=8900 host=192.168.1.120

You can process video by following code & run in new terminal

import cv2

cap = cv2.VideoCapture("tcp://192.168.1.120:8900") #rb5 ip & port (same from command)
while(True):
    ret, frame = cap.read()
    cv2.imwrite("captured_image_opencv.jpg",frame)
    # you can process image by using frame 
    break
cap.release()
 

 

Thanks & regards,

Mayur

 

  • Up0
  • Down0
yukselbera
Join Date: 26 Jul 21
Posts: 6
Posted: Fri, 2021-08-06 09:01
Thank you all so much! You guys helped a lot
  • Up0
  • Down0
karishma.inamdar
Join Date: 12 Oct 22
Posts: 3
Posted: Wed, 2023-01-04 15:57

How can I capture two different MIPI cameras using VideoCapture? Eg: Usually you just define by VideoCapture(0 or 1)

I am trying to Calibrate two MIPI OV 9282 cameras into stereo.

  • Up0
  • Down0
or Register

Opinions expressed in the content posted here are the personal opinions of the original authors, and do not necessarily reflect those of Qualcomm Incorporated or its subsidiaries (“Qualcomm”). The content is provided for informational purposes only and is not meant to be an endorsement or representation by Qualcomm or any other party. This site may also provide links or references to non-Qualcomm sites and resources. Qualcomm makes no representations, warranties, or other commitments whatsoever about any non-Qualcomm sites or third-party resources that may be referenced, accessible from, or linked to this site.