Detect Speed with a Raspberry Pi, Camera and OpenCV

Many thanks Liam.

It’s definitely possible that the contract between cars and background is an issue, detection of white cars/vans is better for sure. Not sure there is a great deal that can be done about that.

Thanks for the link, it had a couple of things to try :+1:

I have this problem, what can I do? I’ve tried multiple times to let something pass in front of the camera, but it only captured one vehicle, and to display it under images, it takes 20 minutes. There’s nothing under recent even when I refresh.

Hey @Loki269894, welcome to the forum!

Would you be able to tell us a bit more about your problem and setup?

Some people above you in this post have had issues with both the hardware and software part of this project. Could you give us an idea of your physical setup as well as the software setup steps you have gotten through so far.

The error message you have sent doesn’t tell us much without that context.

Thanks!
Sam

Hey Sam,

Thank you very much for your response. Following the instructions on the website, I’ve installed the project. I’m using the Raspberry Pi 4 4GB, and for the camera, I’m using a Logitech Webcam (HD 1080p) (I adjusted this in the Config.py file PLUGIN_NAME = “webcam720”). When I start the program, the snapshots are displayed in the web view under “images”, but nothing appears under “recent”. From my screenshot, you can see that at the bottom right it says “No drive status avail”, meaning it can’t retrieve the drive status even though I’ve adjusted the path correctly. But I don’t find this too concerning because the screenshots are at least landing in the “image” folder.

However, my bigger question now is, how can I modify my config.py file, etc. to measure the speed of cars on a highway? To explain it better: For example, I want to stand on a bridge and measure the speeds of cars passing underneath the bridge (For school project lol). For this, I would need to adjust the code so that the program can detect cars coming vertically (from above). And how should I best calibrate it to ensure accurate speeds?

Sorry, I’m still a beginner, thank you for your patience and support.

Loki

Wouldn’t you just turn the camera sideways? Sometimes a hardware solution is simpler than trying to adjust the software. :thinking:

Hi Loki
Excuse me for butting in here. I have not read all the history on this thread as I have no personal interest.
But one thing stood out which is physical in nature.

The first thing is you would be measuring the “slope” speed. That is the hypotenuse of a right angle triangle. If this remained constant there would be no problem calculating the ground speed.
But as the car is coming toward or away from you the “slope” angle is constantly changing if you are tracking it keeping the image in the centre of frame. So in this case calibration for “accurate” speed would be almost impossible.

I might get interested enough (and get enough time) to go back to the beginning here and find out how this is intended to be done. I just can’t see at the moment how the current crop of hobby processors could be fast enough to process an image, do it again, decide how far the target has moved and elapsed time, finally calculate the speed.

I think (could be wrong) traffic cameras are actually Doppler Radar measuring target speed and a still camera set to trigger when a set speed is exceeded. Tree branches swaying in the wind have been known to produce false speeding photos. One in Carlingford Rd, Sydney a few years ago springs to mind.
Cheers Bob

Haha, I also considered that, but then I wondered if the model might be designed to only perform horizontal measurements.

Hi Loki

I think you would need the camera at ground level to get rid of the “triangle” but I still can’t see wow you would do this. Unless you were to one side of target travel. Then measured the time the target takes to traverse a known number of degrees while keeping the target in the centre of the frame.
This gets us back to the triangle problem again though. Where the camera would be moving at its fastest rate when the target is directly opposite . That is travelling at right angles to the camera. If you were to measure the speed of camera rotation I think to calculate target speed accurately would be a bit above my pay scale

one way to do this would be to detect and record the target at a specific angle then measure the time to another specific angle. But for this to be accurate you would have to know the distance between the points where the camera line intersects the road at both points.
This method is not new. Used to be called an Amphometer where there were 2 pressure pads on the road a known distance apart and the nice policeman stopped you if you travelled that distance too quick.
Cheers Bob

The computation simply takes the amount of time between when the vehicle appears on the screen and when it passes a designated point. This result is then converted to a speed using the known distance of road covered. This distance might be actual measurement where possible or by estimating based on roadside features. Careful placement of the limit line can be used where suitable features are available. For instance, for a concrete pavement the length of each segment will be known. Light poles also provide a good estimate. Limiting the field of view to a specific area of interest means that the estimate of road length can be quite accurate. Where the distance measurement is not possible the speeds can be reported as a percentage difference from a sample average.

Hi Jeff
Thanks, once again clearing up one of my queries.

So you are not actually measuring “speed” as such but measuring the time over a known distance then calculating it.

Thanks, that is the only feasible way I could see a camera doing it with a camera.
Hence my last paragraph.

Cheers Bob

Good morning,

I have the setup running well now but would like to use the data.
Is there a simple way to export the csv view? The html view is great but csv would be easier to manipulate. I can see the option to view the csv data but can’t see how to export it as a file.

TIA

Hi Paul,

I’m not sure that this function exists as part of the GitHub project as of yet. For now I can see a few possible solutions to do this. It looks like python has a function to do this for you. Maybe using the csv.writer() function in this code would do the trick.

Otherwise I have found a chrome extension that lets you scrape data off a page and export it as a csv file which may work for you as well. you can find that extension here.

How do I change the capture box to full screen? You mentioned I can adjust it in ‘speed-cam.py’, however, I couldn’t figure out which line of code is responsible for adjusting it. Thanks!

Hi @Tingtao283752,

Welcome to the forums. This is a tricky one, there are lots of different files in play here. The config.py has a section on the GUI. This file is likely what you want to be adjusting

Hi Jack, thank you for the reply. In config.py I can change the capture box to full screen by changing MO_CROP_X_LEFT, MO_CROP_X_RIGHT, MO_CROP_Y_UPPER, and MO_CROP_Y_LOWER. But this only works for default camera, which is 320X240 resolution. If we select different camera in Main Menu, → c Setting → b PLUGIN, those parameters are in the camera python file you selected, for example, if select picam720, we can change the capture box size in picam720.py

Hello,

I’ve encountered the issues of pi cam not Turing on even I start for pi camera module.
Just Web Server start and Web Server of txt is shown.

I’ve used Pi Model 3 + and Camera module 2.

Hey @Myat285046, welcome to the forums!

Sorry to hear you’re having trouble with the project! Hopefully we can sort this out.

Can you show us what your output looks like? Does the speed-install.sh file simply open up?

Additionally, some images of your physical setup and snippets of your code would be super useful, if possible.

1 Like

Yes the speed-install.sh file working well.
I found the issues. I used camera module v2 which is not supported. So, I changed with web cam and it work well.

1 Like

Hey @Myat285046,

Thats great to hear!

Hopefully it’s smooth sailing from here. :slight_smile:

Hiya, looks like a great feature! I’ve got a RPI5 with the AI camera. Im struggeling to get the cam started. What plugin should I use you reckon?