Skip to content
Q-engineering edited this page May 18, 2024 · 9 revisions

output image

YoloCam works with most webcams. You only have to modify one line in the /etc/rc.local script and it works.
Before adapting this script, check if your webcam supports the YUYV422 protocol.
We assume you have no RaspiCam working and your webcam is connected to a USB port.

With the following command, ffmpeg lists all available formats.

$ ffmpeg -f v4l2 -list_formats all -i /dev/video

You will get some output like the screen dump below.
output image

As you can see, this webcam (our Logitech 720), supports various YUYV422 formats.
You can choose the format you like, for instance, 640x480 or 1280x960.
Strange as it may sound, we cannot handle mjpeg streams as their decoding asks too much CPU time.

The next action is modifying the /etc/rc.local script.
During booting, this script is executed. Here, we initiate the FFmpeg stream to the HLS stream of the Nginx web server.

$ sudo nano /etc/rc.local

Please replace the following line at the end of the file.

# old line: 
ffmpeg -input_format h264 -f video4linux2 -video_size 1296x972  -framerate 15 -i /dev/video0 -c:v copy -an -f flv rtmp://localhost/live/rpi &
# or 
ffmpeg -input_format h264 -f video4linux2 -video_size 640x480  -framerate 15 -i /dev/video0 -c:v copy -an -f flv rtmp://localhost/live/rpi &

#with this line:
ffmpeg -f v4l2 -input_format yuyv422 -video_size 1280x960 -framerate 10 -i /dev/video0 -c:v libx264 -preset ultrafast -tune zerolatency -g 30 -keyint_min 30 -f flv rtmp://localhost/live/rpi &

output image

You can alter the resolution to any size in the list generated with the ffmpeg -f v4l2 -list_formats all -i /dev/video command. However, be careful with the framerate. Too high a resolution may crash or freeze your Raspberry Pi.

Lower FPS.

YoloCam works internally with H264-encoded video streams. Not surprisingly, the RaspiCam streams in H264 as well.
However, most webcams don't support H264; they typically use raw YUYV422 or MJPEG compression. YoloCam needs to convert these formats to H264. This process consumes some CPU time, which is why your AI framerate drops to approximately 1.2 - 1.75 FPS.

Furthermore, the processing of frames is intermittent. Typically, five or six frames are analyzed, followed by a short pause, after which the next batch of frames is processed. This somewhat stuttering behaviour does not affect your object detection and has the added benefit of reducing heat development.

The web server, on the other hand, still provides a smooth stream at the specified frame rate.
Again, do not increase the frame rate unnecessarily to keep your CPU load low.

Clone this wiki locally