shuckle
Posts: 565
Joined: Sun Aug 26, 2012 11:49 am
Location: Finland

Re: Video4Linux2 driver ready!

Sat Aug 03, 2013 6:24 am

What kind of speed (fps) can be expected with motion?

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

Re: Video4Linux2 driver ready!

Sat Aug 03, 2013 11:33 am

shuckle wrote:What kind of speed (fps) can be expected with motion?
At the moment, only native JPEG is supported by the driver, so it's about 2 fps.

Multiverse
Posts: 4
Joined: Sun Aug 04, 2013 4:02 am

Re: Video4Linux2 driver ready!

Sun Aug 04, 2013 4:16 am

I have verified that frame rate using the version of Motion that makes use of the MMAL API. I get a few frames per second at 640x480, and only 1 fps at ~ 1 Megapixel. I had to disable the simultaneous recording of snapshots in order to get these frame rates in the recorded video.

Also, the webcam output seems to only show the 'motion frames'; you cannot see the normal video in that interface.

I have also verified the bug in the version of Motion that is packaged with the Rpi distro. You cannot connect to the camera without setting LD_PRELOAD for uv4l, but it segfaults :o as soon as it tries to write some video. It will run if you've just configured it to save snapshots with no video, however. The webcam output does not work regardless.

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

Re: Video4Linux2 driver ready!

Sun Aug 04, 2013 5:47 pm

RpiName wrote:
What output modes does the RPi V4L2 driver support? is it JPEG only?
For now, yes.
UPDATE: the latest version supports video in YUV420 (I420) via the --video-port and --framerate options.

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

Re: Video4Linux2 driver ready!

Sun Aug 04, 2013 6:19 pm

nickneubrand wrote:Yeah I eventually got that same error from the Shell as well. I was looking for a video stream, rather than pictures. I'm trying to get a video feed in, then put graphic overlays on top of the video stream. If I find a way to get it to work, I'll post it up.
The last version of the driver supports video in YUV 4:2:0, which should be fine with python.

First of all, update the UV4L packages (see http://linux-projects.org for more informations).

Code: Select all

[email protected] ~ $ sudo update
[email protected] ~ $ sudo upgrade
Now run the driver:

Code: Select all

[email protected] ~ $ uv4l --driver raspicam --video-port  --auto-video_nr --width 320 --height 240 --framerate 5
<notice> [core] Loading external library libraspicam.so
<notice> [core] Trying driver 'raspicam'
<notice> [core] Device detected!
<notice> [core] Registering device node /dev/video0
Take a snapshot with a simple program in python:

Code: Select all

[email protected] ~ $ LD_PRELOAD=/usr/lib/uv4l/uv4lext/armv6l/libuv4lext.so python pyprog.py
Finally, display the ICB image:

Code: Select all

$ display snapshot.ICB
This is the program in python:

Code: Select all

import pygame, sys
from pygame.locals import *
import pygame.camera
pygame.init()
pygame.camera.init()
cam = pygame.camera.Camera("/dev/video0",(320,240))
cam.start()
image= cam.get_image()
pygame.image.save(image,'/home/pi/snapshot.ICB')
cam.stop()

nickneubrand
Posts: 29
Joined: Fri Apr 26, 2013 4:54 am

Re: Video4Linux2 driver ready!

Tue Aug 06, 2013 4:03 am

Thanks for the useful response:

Sadly I guess it's just not working for me. I've been trying to run:

Code: Select all

import pygame,sys
import pygame.camera
import shelve

from pygame.locals import *
            
pygame.init()
pygame.camera.init()

screen = pygame.display.set_mode([640,480])


cam = pygame.camera.Camera("/dev/video0", (640,480))
cam.start()

x=0
y=0

while 1:
    image = cam.get_image()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit(0)
            
            
    screen.fill((0,0,0))
    
    screen.blit(image,(x,y))
    
    pygame.display.flip()
I either get a error saying that io stream is not supported, which as you mentioned can be bypassed with adding

Code: Select all

--extension-presence=1
.Which then returns with:
"Cam.start()
SystemError:ioctl(VIDIOC_S_FMT) failure: no supported formats"

When I ran the code you listed I got the same thing. So maybe I'm just missing something, hope its working for others.

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

Re: Video4Linux2 driver ready!

Tue Aug 06, 2013 1:08 pm

nickneubrand wrote: When I ran the code you listed I got the same thing. So maybe I'm just missing something, hope its working for others.
Maybe you did not update the driver..? Please read and follow my previous advices again. Your program works perfectly for me. Attached is a screenshot.
rpi.jpeg
screenshot
rpi.jpeg (61.7 KiB) Viewed 12610 times

nickneubrand
Posts: 29
Joined: Fri Apr 26, 2013 4:54 am

Re: Video4Linux2 driver ready!

Tue Aug 06, 2013 2:37 pm

I'll give it another shot, but when I ran the update code you posted I got an error saying command not found. So I went the the link and saw:

Code: Select all

$ sudo apt-get update
$ sudo apt-get install uv4l uv4l-raspicam

The last two commands will upgrade UV4L to the most recent version, if it's already installed.
When I ran those, updates took place, so maybe it missed something. I'll run through it again and see what happens and post back

nickneubrand
Posts: 29
Joined: Fri Apr 26, 2013 4:54 am

Re: Video4Linux2 driver ready!

Wed Aug 07, 2013 3:43 am

Again, ran the entire install for a second time, by running everything on http://www.linux-projects.org/modules/s ... e&artid=14

and it tells me:
"uv4l is already the newest version."
"uv4l-raspicam is already the newest version"

Then from the desktop, right clicked my .py program > open with IDEL > F5 to run > Get Error

The only thing I can think is that there is some issue because I am not running this over a network. I have this directly hooked up to a TV/Monitor. To me, there really shouldn't be a problem, but I know every little about this so who knows.

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

Re: Video4Linux2 driver ready!

Wed Aug 07, 2013 5:50 am

nickneubrand wrote: Then from the desktop, right clicked my .py program > open with IDEL > F5 to run > Get Error
Are you sure the LD_PRELOAD environment variable is set (persistently or not) BEFORE running the program that way? Without that variable properly set, you will get exactly the same error you are reporting.

nickneubrand
Posts: 29
Joined: Fri Apr 26, 2013 4:54 am

Re: Video4Linux2 driver ready!

Wed Aug 07, 2013 8:04 am

Come to find out it was a few directory issues that I worked out. The thing works great and I'm getting pretty darn good results. I would guess around 8fps. Thanks for the driver!

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

Re: Video4Linux2 driver ready!

Thu Aug 08, 2013 3:54 am

The new driver release from the repository also supports H264 video encoding.

To record a 10s video at full resolution the "traditional" way:

Code: Select all

[email protected] ~ $ sudo uv4l --sched-rr --driver raspicam --auto-video_nr --encoding "h264" --width 1920 --height 1080
[email protected] ~ $ dd if=/dev/video0 of=video.h264 bs=1M & pid=$! ; sleep 10; kill $pid
Since the V4L2 read I/O interface on RPi is too slow for recording at full resolution, sudo will give the necessary permission to run the driver with the necessary RealTime scheduling policy (--sched-rr) to record the video successully. Of course, the alternatives to sudo are to use the more efficient V4L2 Streaming I/O interface or to give the user the right to run a process with a RealTime priority.

DaedalusX
Posts: 8
Joined: Thu Aug 08, 2013 4:27 pm

Re: Video4Linux2 driver ready!

Sat Aug 10, 2013 5:21 pm

So does this work with OpenCV camera interface then? It sadly doesn't work for me. Says that memory mapping is wrong. Like so:
[email protected] ~ $ ./test
Cannot open camera
[email protected] ~ $ uv4l --driver raspicam --video-port --auto-video_nr --width 320 --height 240 --framerate 5
<notice> [core] Trying driver 'raspicam'
<warning> [core] Driver not found
<notice> [core] Loading external library libraspicam.so
<notice> [core] Trying driver 'raspicam'
<warning> [driver] The --video-port option is DEPRECATED and will be removed soon. Use --encoding=yuv420 instead
<notice> [driver] Selected format: 320x256, encoding: yuv420
<warning> [driver] Make sure there is always enough memory reserved for the buffers to the GPU (run 'raspi-config' in case)!
<notice> [core] Device detected!
<notice> [core] Registering device node /dev/video0
[email protected] ~ $ LD_PRELOAD=/usr/lib/uv4l/uv4lext/armv6l/libuv4lext.so ./test
libv4l2: error mmapping buffer 0: No such device
VIDIOC_QBUF: No such device
Cannot read a frame from camera
[email protected] ~ $
So any tips?

This is the code I am trying to run:

Code: Select all

#include "opencv2/highgui/highgui.hpp"
#include <iostream>

using namespace std;
using namespace cv;

int main(int argc, char** argv){
    VideoCapture cap(-1);
    if (!cap.isOpened())
    {
        cout << "Cannot open camera" << endl;
        return -1;
    }
   cap.set(CV_CAP_PROP_FRAME_WIDTH, 640);
   cap.set(CV_CAP_PROP_FRAME_HEIGHT, 480);

   namedWindow("Output",CV_WINDOW_AUTOSIZE);

    while (1)
    {
        Mat frame;
        bool bSuccess = cap.read(frame);

        if (!bSuccess)
        {
        cout << "Cannot read a frame from camera" << endl;
        break;
        }
        imshow("Output", frame);

        if (waitKey(30) == 27)
        {
        cout << "Exit" << endl;
        break;
        }
    }
    return 0;
}

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

Re: Video4Linux2 driver ready!

Sat Aug 10, 2013 11:28 pm

DaedalusX wrote:So does this work with OpenCV camera interface then? It sadly doesn't work for me. Says that memory mapping is wrong.
There was a bug in the driver, which has been fixed with the last release.

First upgrade uv4l from the official repository:

Code: Select all

[email protected] ~ $ sudo apt-get update
[email protected] ~ $ sudo apt-get upgrade
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages will be upgraded:
  uv4l
1 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Need to get 550 kB of archives.
After this operation, 0 B of additional disk space will be used.
Do you want to continue [Y/n]? y
Now you can run your program as you did before, for example:

Code: Select all

[email protected] ~ $ LD_PRELOAD=/usr/lib/uv4l/uv4lext/armv6l/libuv4lext.so ./test
If you don't want to specify or set LD_PRELOAD for some reasons, you can link the library into your program, for example:

Code: Select all

[email protected] ~ $ g++ -lopencv_core -lopencv_highgui -L/usr/lib/uv4l/uv4lext/armv6l -luv4lext -Wl,-rpath,'/usr/lib/uv4l/uv4lext/armv6l' test.cpp  -o test
[email protected] ~ $ ./test
^C

DaedalusX
Posts: 8
Joined: Thu Aug 08, 2013 4:27 pm

Re: Video4Linux2 driver ready!

Sun Aug 11, 2013 11:23 am

Thank you very much! I got it working. I guess you don't put all updates at Linux Projects news? Because I upgraded yesterday (August 10) and the last news is August 8. So if an update came in the last 24 hours I didn't know. But I see you added an example on how to make it work for OpenCV. Thank you!

Now will there be possible to get like 30FPS for at least 320x240 using OpenCV? Or it won't work because OpenCV doesn't take H264 encoding which is done on hardware? Although that is just for coding and writing. In this case I would just want to see and get those FPS without any SD writes. Right now I get about 3FPS even with --sched-rr. But when I change the resolution to 1920x1080 I get about the same. So the overhead must be somewhere else. Also, that I can see 3FPS trough X server is ok, but the FPS mentioned is even when I don't create a window and don't do imshow().

But a very promising start. And again, thank you!

edit: Also, do the set functions actually work in OpenCV as you must set width, height, framerate and format while launching the driver? At least they don't work for me.

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

Re: Video4Linux2 driver ready!

Sun Aug 11, 2013 12:55 pm

DaedalusX wrote: Now will there be possible to get like 30FPS for at least 320x240 using OpenCV? Or it won't work because OpenCV doesn't take H264 encoding which is done on hardware? Although that is just for coding and writing. In this case I would just want to see and get those FPS without any SD writes. Right now I get about 3FPS even with --sched-rr. But when I change the resolution to 1920x1080 I get about the same. So the overhead must be somewhere else. Also, that I can see 3FPS trough X server is ok, but the FPS mentioned is even when I don't create a window and don't do imshow().
That's strange, how did you measure the framerate? Note that in your previous post you ran the driver with the (limiting) --framerate 5, so that could be one reason for your results.

I get ~30 fps at 320x240, 20 fps at 640x480, 6 fps at 1280x960 and 3fps at 1920x1088.

Here is your program slightly modified which gives the results I mentioned:

Code: Select all

[email protected] ~ $ uv4l --driver raspicam --auto-video_nr --encoding yuv420 --width 320 --height 240

Code: Select all

/* Compile with

   g++-4.7 -std=c++11 -lopencv_core -lopencv_highgui 
   -L/usr/lib/uv4l/uv4lext/armv6l -luv4lext -Wl,-rpath,'/usr/lib/uv4l/uv4lext/armv6l' 
   test.cpp -o test 
*/

#include "opencv2/highgui/highgui.hpp"
#include <iostream>
#include <chrono>

using namespace std;
using namespace chrono;
using namespace cv;

int main(int argc, char** argv){
    VideoCapture cap(-1);
    if (!cap.isOpened())
    {
        cout << "Cannot open camera" << endl;
        return -1;
    }
   cap.set(CV_CAP_PROP_FRAME_WIDTH, 640);
   cap.set(CV_CAP_PROP_FRAME_HEIGHT, 480);

   //namedWindow("Output",CV_WINDOW_AUTOSIZE);

    unsigned long f = 0;
    auto start = steady_clock::now();
    while (1)
    {
        Mat frame;
        bool bSuccess = cap.read(frame);

        if (!bSuccess)
        {
        cout << "Cannot read a frame from camera" << endl;
        break;
        }

        auto elapse = duration_cast<seconds>(steady_clock::now() - start).count();
        ++f;
        auto fps = elapse ? f / elapse : 0;
        if (!(elapse % 5) && fps) // print the framerate every 5s, for 1s
            cout << "fps: " << fps << ", total frames: " << f
                 << " elapsed time: " << elapse << "s\n";

        /*
        imshow("Output", frame);

        if (waitKey(30) == 27)
        {
        cout << "Exit, fps: " << fps << endl;
        break;
        }
        */
    }
    return 0;
}
Last edited by RpiName on Sun Aug 11, 2013 5:43 pm, edited 1 time in total.

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

Re: Video4Linux2 driver ready!

Sun Aug 11, 2013 1:21 pm

DaedalusX wrote:edit: Also, do the set functions actually work in OpenCV as you must set width, height, framerate and format while launching the driver? At least they don't work for me.
Those features are planned. As a dirty work-around, for now, you can run more instances of driver with a different resolution each time. Just be careful not to open different file nodes at the same time, as they access the same device.

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

Re: Video4Linux2 driver ready!

Fri Aug 16, 2013 7:46 am

From the last release on, the driver allows to control almost all the image properties on the fly, while the camera is in use, without having to reload the driver with new settings. These properties include image size, encoding, hue, saturation, vertical/orizontal flip, iso, etc...

http://www.linux-projects.org/modules/n ... toryid=153

selectnone
Posts: 55
Joined: Fri Jun 22, 2012 10:16 pm

Re: Video4Linux2 driver ready!

Wed Aug 28, 2013 11:41 pm

Hi RpiName, should the instructions you posted earlier for getting images via PyGame still work?

I've been attempting to get it working, but haven't had any luck yet.

I'm running a Pi via ssh, and want to output images to its screen - I'm starting my script via:
sudo LD_PRELOAD=/usr/lib/uv4l/uv4lext/armv61/libuv4lext.so python camTest.py
But that gives me this:
ERROR: ld.so: object '/usr/lib/uv4l/uv4lext/armv61/libuv4lext.so' from LD_PRELOAD cannot be preloaded: ignored.
Traceback (most recent call last):
File "camTest.py", line 15, in <module>
cam.start()
SystemError: /dev/video0 does not support streaming i/o
Also, would there be a way to get that working without the LD_PRELOAD bit?

I'm developing using the Adafruit WebIDE, which doesn't appear to have an option to change its script-run options, and its terminal window won't let me paste :(

Btw, here's the script I'm running - no idea if it'll actually display properly yet ;)

Code: Select all

import pygame, sys
from pygame.locals import *
import pygame.camera

pygame.init()
pygame.camera.init()

screenSize = (pygame.display.Info().current_w, pygame.display.Info().current_h)

pygame.mouse.set_visible(0)
pygame.display.set_mode(screenSize, pygame.FULLSCREEN)
screen = pygame.display.set_mode(screenSize)

cam = pygame.camera.Camera("/dev/video0",(320,240))
cam.start()
image = cam.get_image()
self.screen.blit (image, (0,0))
pygame.display.update()

cam.stop()

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

Re: Video4Linux2 driver ready!

Thu Aug 29, 2013 12:36 am

sudo LD_PRELOAD=/usr/lib/uv4l/uv4lext/armv61/libuv4lext.so python camTest.py
It's "armv6l", not "armv61", in the path.

LD_PRELOAD pointing to the extension library is necessary with python/pygame

You can make LD_PRELOAD permanent by doing "export LD_PRELOAD=/usr/lib/uv4l/uv4lext/armv6l/libuv4lext.so" one time per ssh session or by adding the above line in ~/.bashrc once for all, or by adding the path in one of the configuration files of ld (man ld)

selectnone
Posts: 55
Joined: Fri Jun 22, 2012 10:16 pm

Re: Video4Linux2 driver ready!

Fri Aug 30, 2013 4:59 pm

RpiName wrote:It's "armv6l", not "armv61", in the path.
Ahah, that'll be down to not being able to paste into the WebIDE's terminal - I was feeling clever for avoiding typing uv41ext, and completely missed that ;)
RpiName wrote: LD_PRELOAD pointing to the extension library is necessary with python/pygame

You can make LD_PRELOAD permanent by doing "export LD_PRELOAD=/usr/lib/uv4l/uv4lext/armv6l/libuv4lext.so" one time per ssh session or by adding the above line in ~/.bashrc once for all, or by adding the path in one of the configuration files of ld (man ld)
Thanks, I'll give that a go tonight - I'll want this to be permanently accessible, so that'll be best for me

selectnone
Posts: 55
Joined: Fri Jun 22, 2012 10:16 pm

Re: Video4Linux2 driver ready!

Fri Aug 30, 2013 10:37 pm

Closer, but not quite got it working properly yet.

Accessing the RaspiCam via Pygame causes a still to be shown on-screen, and attempting to get the image causes the console to lock up:

Code: Select all

import pygame, sys, os
from pygame.locals import *
import pygame.camera

# Activate uv4l device:
os.system("sudo pkill uv4l")
os.system("sudo uv4l --driver raspicam --auto-video_nr --extension-presence=1")

pygame.init()
pygame.camera.init()

cam = pygame.camera.Camera("/dev/video0",(320,240))

print "cam.start()"
cam.start()

print "cam.get_image()"
cam.get_image()

print "cam.stop()"
cam.stop()

print "DONE"
That script causes my SSH console to lock at cam.get_image() (ctrl-c does nothing...) with a still shot on the Pi's connected screen.

If I comment out that line and replace it with pygame.time.wait(5000), that still image is shown for the five seconds (between start and stop) and the script finishes up with this error:
DONE
Exception MemoryError: 'mmap failure : 19, No such device' in <module 'threading' from '/usr/lib/python2.7/threading.pyc'> ignored
Any idea what might be up here, how I might fix this?

vle
Posts: 3
Joined: Wed Sep 11, 2013 8:47 am

Re: Video4Linux2 driver ready!

Wed Sep 11, 2013 8:53 am

I installed uv4l successfully.
I tried all the tests and the results were fine. The problem is the video/image screen is always stretched to the size of my monitor and therefore covers most of it. Even though I set the screen size small such as 320x240, its height = resolution.height.

Please help.
Thanks

mikerr
Posts: 2686
Joined: Thu Jan 12, 2012 12:46 pm
Location: UK
Contact: Website

Re: Video4Linux2 driver ready!

Wed Sep 11, 2013 11:47 am

vle wrote:I installed uv4l successfully.
I tried all the tests and the results were fine. The problem is the video/image screen is always stretched to the size of my monitor and therefore covers most of it. Even though I set the screen size small such as 320x240, its height = resolution.height.
I think you're seing the preview - run the driver without preview:

Code: Select all

pkill uv4l
uv4l --driver raspicam --auto-video_nr --encoding yuv420 --width 320 --height 240 --nopreview
export LD_PRELOAD=/usr/lib/uv4l/uv4lext/armv6l/libuv4lext.so

I've been successfull using this with python/OpenCV:

Code: Select all

import cv
capture=cv.CaptureFromCAM(0)
cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_WIDTH, 320 );
cv.SetCaptureProperty(capture, cv.CV_CAP_PROP_FRAME_HEIGHT, 240 );

frame=cv.QueryFrame(capture)
but only at 2-3 fps

vle
Posts: 3
Joined: Wed Sep 11, 2013 8:47 am

Re: Video4Linux2 driver ready!

Wed Sep 11, 2013 7:54 pm

Thanks, that solves part of the problem, but then I can't see the image without the preview. Is there a way that we can set the preview size not to be stretched out, not full screen?

Return to “Camera board”

Who is online

Users browsing this forum: No registered users and 4 guests