Detect Speed with a Raspberry Pi, Camera and OpenCV

Hi @asd289053

If you’re having issues with the AI camera not detecting you may need to update the firmware. There is a guide on the Raspberry Pi forum on how this can be done.

The camera is working, only I don’t know how to activate it via this script. Earlier I used to call Picamera2, but this script does not seem to support that. That’s more the problem

Hey @asd289053,

I’m not quite sure about the compatibility of the AI camera with the library used in this guide. I believe, as you mentioned, that you would use Picamera2 for the AI cam’s picture output. It has been stated prior in this thread that the Camera Module 2 will not work with this library.

I would expect you can achieve this same thing with the PiCamera’s IMX500, or just with an updated library designed to work with the PiCam2/AI Camera. I’ll talk to @Jaryd to confirm once he’s in.

1 Like

Hey @asd289053,

I don’t think the dedicated AI processing chip on the camera can be used to run the code in this guide. Although it can pack a punch, it is a little hard to work with as you need to upload a model to the camera. While an object recognition or pose estimation model is easy to implement, you would be hard-pressed to get a reliable speed recognition model, let alone running on the camera.

The AI camera can however just be used as a standard camera without any fancy AI processing, just as a regular camera module. However, this guide is a little outdated now and has issues with connecting cameras (the method was outdated and we now use libcamera). Because of that, this guide also unfortunately only supports Buster OS (Bookworm OS on the Pi 5 is 2 versions ahead).

I did previously see someone on the raspberry pi forum fix the same issue in another computer vision guide, but for the life of me I can’t find it - its been nearly a year. Your best bet may be to explain it to an LLM like ChatGPT, Claude or Deep Seek and ask them to implement it with libcamera 2 instead.

If you do however want to dive into using the AI processing chip on the camera, we have an object recognition and pose estimation guide to get you started.

Sorry I couldnt be much help, let us know how you go!

Hi, I’ve followed the steps for downloading OpenCV from above and the download has stopped at 32% saying *** [Makefile:166: all] Error 2. I’m using a 2014 Raspberry Pi +. Just wondering what could be the cause of this?

Cheers.

1 Like

Hey @Billy289965,

A Makefile Error 2 usually indicates a compilation error. I would guess this is because the pi you are using may not meet the requirements for running OpenCV.

It seems like the most common cause of this is not having enough storage space to complete the download. OpenCV needs about 6GB of swap space to install correctly. Would you be able to check how much free space your Pi has?

I think it definitely is that, you are right. I’m going to look in to getting a raspberry Pi 4 instead. Would you recommend an 8GB version for the extra storage for open CV? Also the current OS is bookworm rather than the Buster OS suggested for this system. Would this still work with some tweaks?

Cheers,

Bill

Hey @Billy289965,

We have had the most luck using a Pi 4 Model B 8GB version however the software should work on as little as 2GB of RAM from some user reports. I would suggest the 8GB version for the best results.

A lot of the image libraries have changed between Buster and Bookworm. I can find examples of this working with Bookworm although I’m not sure what changes would need to be made. It should work on either OS but Buster would be the safe bet.

Hope this helps! :slight_smile:

No worries, I’ll give that a go. One last thing is what would the difference be between compiling OpenCV and just downloading the precompiled model? Would speed detection work with a precompiled model?

Hey Billy,

That guide was produced when it wasn’t easy to download a precompiled model, so we had to build it. It has been a very long time, but at one stage I managed to get it working by downloading it with something like:

 sudo apt install python3-opencv

Hope this helps!

Good day. I had previously created this build last summer, but the pi was stolen. I just recreated from the steps outlined and the install appears to have been successful. However, when I start the program, I receive the following error: “Failed to Start speed-web.py Please Investigate Problem.”

1 Like

Hey @Michael261434,

Sorry to hear your old Pi was stolen, that must have been frustrating. Does the error message provide any more information or just that text?

That Python file is involved in starting the web server for this project. Can you think of anything that may have changed in your network setup between your last Pi and this one?

Have you upgraded any of the physical hardware when building this project again?

I repurchased all the same equipment, after hours of AI assistance, I think it somehow upgraded the OS? It couldn’t ever find the camera. On the new OS the camera works, on buster it doesn’t. I’m going to reflash and try again. Thanks for the quick response. And yeah, it sucks that it was stolen. Perhaps it is because I gave all the speed data the the police. lol

1 Like

Ahh, that probably has something to do with it. I’m not super informed about the specifics, but the camera software has some significant differences between the different Pi OS versions.

Let us know how you go with this reflash!

I reflashed, looks like I got the program working. However, when I select start, it says no such process. Any ideas where to start troubleshooting?

Hey @Michael261434,

Could you let us know exactly where you got up to in the guide before this happened?

It sounds like you can open the ‘Speed Camera Main Menu’ but can’t start the ‘speed_cam.py’ process. Is that correct?

Did you get any warnings or errors as you followed the previous setup steps of the guide?

Its interesting. I can start menubox, but when I select start camera i get a black screen that says:
./speed-cam.sh supervisorctl start
speed-can: ERROR (no such process)

I can click on the .py files in the folder and they will start.

1 Like

Hey @Michael261434,

Could this be a simple spelling error inside of the speed-cam.sh file? Looking at your error it seems like it is struggling to find ‘speed-can’ but ‘speed-cam’ would make more sense to me as the appropriate name for that.

If you open your speed-cam.sh file can you see anything inside of it that may be trying to reference ‘speed-can’ instead of ‘speed-cam’? If all you need to do is change that ‘n’ to a ‘m’ then this could be a simple fix.

Looking inside the speed-cam.sh file it looks like it would break like this if the 9th line service_name="speed-cam" had this spelling error.

Let us know how you go with this!

1 Like

It says cam. I fat fingered it.

#!/bin/bash
# This script will run the speed-cam.py as a background task
# You will then be able close the terminal session.
# use the edit option and change autostart=true

# initialize script variables
login_id=$( whoami )
speed_dir=$( pwd )
service_name="speed-cam"
conf_file_dir="/home/$login_id/speed-camera/supervisor"
conf_file_name="speed-cam.conf"

# change supervisor conf file for current logged in user
sed -i s,^user=.*,user=$login_id, $conf_file_dir/$conf_file_name
sed -i s,^directory=.*,directory=$speed_dir, $conf_file_dir/$conf_file_name

#speed-cam.sh ver 13.15 written by Claude Pageau
echo "-----------------------------------------------"
echo "$0 supervisorctl $1"

if [ "$1" = "start" ]; then
    sudo supervisorctl start $service_name
    if [ $? -ne 0 ]; then
       echo "ERROR: Try running install Option."
    fi
    exit 0

elif [ "$1" = "stop" ]; then
    sudo supervisorctl stop $service_name
    if [ $? -ne 0 ]; then
       echo "ERROR: Run install Option."
    fi
    exit 0

elif [ "$1" = "restart" ]; then
    sudo supervisorctl restart $service_name

elif [ "$1" = "status" ]; then
    sudo supervisorctl status all
    exit 0

elif [ "$1" = "edit" ]; then
    sudo nano $conf_file_dir/$conf_file_name
    sudo supervisorctl reread
    sudo supervisorctl update
    echo "Wait ..."
    sleep 4
    sudo supervisorctl status $service_name
    exit 1

elif [ "$1" = "log" ]; then
    tail -n 200 /var/log/$service_name.log
    echo "----------------------------------------"
    echo "tail -n 200 /var/log/$service_name.log"
    exit 1

elif [ "$1" = "install" ]; then
    # Run this option to initialize supervisor.service
    echo "install: ln -s $conf_file_dir/$conf_file_name /etc/supervisor/conf.d/$conf_file_name"
    sudo ln -s $conf_file_dir/$conf_file_name /etc/supervisor/conf.d/$conf_file_name
    if [ $? -ne 0 ]; then
       echo "$service_name Already Installed"
       exit 1
    fi
    ls -al /etc/supervisor/conf.d
    sudo supervisorctl reread
    sleep 4
    sudo supervisorctl update

elif [ "$1" = "uninstall" ]; then
    sudo supervisorctl stop $service_name
    if [ $? -ne 0 ]; then
       echo "$service_name already STOPPED"
    fi
    sleep 4
    sudo rm /etc/supervisor/conf.d/$conf_file_name
    if [ $? -ne 0 ]; then
       echo "$service_name Not Installed"
       echo "Run install option."
       exit 1
    fi
    sudo supervisorctl reread
    sleep 4
    sudo supervisorctl update

elif [ "$1" = "upgrade" ]; then
    curl -L https://raw.github.com/pageauc/speed-camera/master/source/speed-install.sh | bash
    exit 0
else
   echo "
Usage: ./$(basename "$0") [Option]

  Options:
  start        Start supervisor service
  stop         Stop supervisor service
  restart      restart supervisor service
  status       Status of supervisor service
  edit         nano edit $conf_file_dir
  log          tail -n 200 /var/log/$service_name.log
  install      Install symbolic link for speed-cam supervisor service
  uninstall    Uninstall symbolic link for speed-cam supervisor service
  upgrade      Upgrade speed-camera files from GitHub
  help         Display Usage message and Status

  Example:  ./$(basename "$0") status
"
fi
echo "Wait ...
"
sudo supervisorctl status all
echo "Done
"

Hey @Michael261434,

Could I clarify, do you mean the original post with the code (that Sam responded to) had a typo, or the code itself had the typo, and is now working?

Thanks!