reggie4
Posts: 81
Joined: Wed Jan 15, 2014 2:09 pm

Python and a UV4l trying to access camera module together

Sat May 02, 2015 11:32 am

I'm trying to use an opencv2 python script and UV4l-server to stream a live feed, from a NOIR camera board on my PI, but am running into problems.

The python script runs and displays the window, but I'm not getting the stream.

I'm sure the UV4l server is running, as it prompts me for a password, and I get the control panel, but the link http://myipaddress:8080/stream doesn't work; it gives an an empty window stating "image".

I also have Motion installed on the same PI using the camera, so have to stop it first.

I can get the stream working if i kill the UV4l process and run the command:

Code: Select all

os.system("sudo uv4l --auto-video_nr --width 800 --height 600 --rotation 180 --framerate 2 --driver raspicam --encoding mjpeg --server-option '--admin-password=pw'  --server-option '--user-password=pw'")
But can I have the script and a server trying to access the camera module at the same time and if not, how can I stream from inside an opencv2 python script?

The script:

Code: Select all

 #!/usr/bin/env python

    import numpy as np
    import cv2
    import os

    os.system('sudo /etc/init.d/motion stop') #to stop any conflict of motion using camera module
    os.system ('sudo pkill uv4l')
    os.system("sudo uv4l --auto-video_nr --width 800 --height 600 --rotation 180 --framerate 2 --driver raspicam --encoding mjpeg --server-option '--admin-password=pw'  --server-option '--user-password=pw'")

    runtime_frame_count = 0

    vid = cv2.VideoCapture(0)
    vid.set(3,800)
    vid.set(4,600)

    while True:
        ret, img = vid.read()

        runtime_frame_count = runtime_frame_count + 1
        print (runtime_frame_count)

        cv2.imshow("input",img)

        key = cv2.waitKey(10)
        key = cv2.waitKey(10)
        key = cv2.waitKey(10)

        if (key == 27) or (runtime_frame_count == 100):
            break

    cv2.destroyAllWindows()
    cv2.VideoCapture(0).release()
The Warnings:

Code: Select all

<notice> [core] Trying driver 'raspicam' from built-in drivers...
<warning> [core] Driver 'raspicam' not found
<notice> [core] Trying driver 'raspicam' from external plug-in's...
<notice> [driver] Dual Raspicam Video4Linux2 Driver v1.9.26 built Feb 10 2015
<notice> [driver] Selected format: 800x600, encoding: mjpeg, JPEG Video Capture
<notice> [driver] Framerate max. 2 fps
<notice> [driver] ROI: 0, 0, 1, 1
<notice> [core] Device detected!
<warning> [core] Cannot create /dev/video0 because file already exists
<notice> [core] Registering device node /dev/video1
the Errors:
libv4l2: error setting pixformat: Device or resource busy
HIGHGUI ERROR: libv4l unable to ioctl S_FMT

RpiName
Posts: 717
Joined: Sat Jul 06, 2013 3:14 am

Re: Python and a UV4I trying to access camera module togethe

Sat May 02, 2015 11:58 am

reggie4 wrote:But can I have the script and a server trying to access the camera module at the same time?
No, it's not possible to open the camera AND get the stream by different V4L2 applications at the same time. It's only possible to get a stream by one application at a time (e.g. UV4L Server) and eventually *control/modify* the camera settings by any applications (e.g. UV4L Server Control Panel or other third-party panels). If you want to get multiple raw streams (either Still JPEGs or MJPEG, or H264 or YUV420) from the UV4L server at the same time via HTTP, then this is possible, as it supports multiple clients.

EDIT: However, there is a way to obtain one or more "virtual device" nodes from the MJPEG stream obtained from the UV4L Server by means of the mjpegstream driver which is another driver present in the UV4L driver list. In other words, you install and run UV4L as usual (this will give you the first device node), then you run another instance of uv4l loading the mjpegstream driver and giving it the address of (local) UV4L Server (this will give another device node). This would allow you to effectively use the Streaming Server and motion at the same time, for example.

mjpegstream driver manual:
http://www.linux-projects.org/modules/s ... e&artid=13

Example:
http://www.linux-projects.org/modules/s ... 6#example5
(you'll have to install and run the mjpegstream driver on your Rpi, of course, not on your PC as in the above example)
Last edited by RpiName on Sat May 02, 2015 12:26 pm, edited 1 time in total.

reggie4
Posts: 81
Joined: Wed Jan 15, 2014 2:09 pm

Re: Python and a UV4l trying to access camera module togethe

Sat May 02, 2015 12:20 pm

I'm a bit stuck then.

I need the uv4l driver to enable me to use the NOIR camera board, but I want to use an opencv2 Python script as well as supply a web stream at the same time.

If I change this:

Code: Select all

os.system("sudo uv4l --auto-video_nr --width 800 --height 600 --rotation 180 --framerate 2 --driver raspicam --encoding mjpeg --server-option '--admin-password=pw'  --server-option '--user-password=pw'")
to just running the driver

Code: Select all

os.system('sudo modprobe bcm2835-v4l2')
could I then use python to serve an img to a web server, or would this still cause a conflict?

RpiName
Posts: 717
Joined: Sat Jul 06, 2013 3:14 am

Re: Python and a UV4l trying to access camera module togethe

Sat May 02, 2015 12:29 pm

reggie4 wrote:I need the uv4l driver to enable me to use the NOIR camera board, but I want to use an opencv2 Python script as well as supply a web stream at the same time.
See my previous post. I edited it while you were answering. That's a possible solution as long as your script can process JPEGs.

reggie4
Posts: 81
Joined: Wed Jan 15, 2014 2:09 pm

Re: Python and a UV4l trying to access camera module togethe

Sat May 02, 2015 1:06 pm

I think I get the jist of what you are suggesting, but I need to digest what this
Rpi as a (virtual) camera plugged into another PC
does a bit further; as I don't fully understand it.

You say:
This would allow you to effectively use the Streaming Server and motion at the same time
I'm trying to get away from Motion and build my own Python/Opencv2 Motion like application with streaming capability. I like the program Motion, but wanted more control over how it detects movement!

I'll look into your suggestion; but would it be possible to bring the http streaming server into the python script, whilst only using UV4L driver to make the NOIR camera board visible to the script?

If this was possible I could then password protect it and do other stuff.

RpiName
Posts: 717
Joined: Sat Jul 06, 2013 3:14 am

Re: Python and a UV4l trying to access camera module togethe

Sat May 02, 2015 3:35 pm

reggie4 wrote:I'll look into your suggestion; but would it be possible to bring the http streaming server into the python script, whilst only using UV4L driver to make the NOIR camera board visible to the script? If this was possible I could then password protect it and do other stuff.
I do not quite understand what you mean "bring the http server into the script". You can make the script a client of the server, i.e. it connects to the HTTP server, fetches the multipart stream and processes the frames one by one.. And if you password protect the server, then the script will also have to deal with the http authentication stuff. You should follow my previous suggestion to do what you want.

reggie4
Posts: 81
Joined: Wed Jan 15, 2014 2:09 pm

Re: Python and a UV4l trying to access camera module togethe

Sun May 03, 2015 6:35 am

What I meant by "bringing the http server into the script" was to use SimpleHTTPServer to create the server within the script then passing the "processed" frames (in the code example Img) to the server. But I think my understanding of servers needs attention!

The script shown in this post is a vastly cut down version of what's happening. It's doing lots of opencv2 operations on each frame, and I wanted to serve a processed frame to the server, not a direct feed from the camera.

I'm a bit confused, I think I need to digest what you have said. It's not how you have said it, it's my lack of understanding of what uv4l drivers do.

I need to step back and take an overview of what i'm trying to achieve.

I think some diagrams are the order of the day! ;)

Return to “Camera board”