gregtinkers / carspeed.py

Record the speed of cars passing in front of the Raspberry Pi Picamera
368 stars 124 forks source link

PiCamera troubles #2

Open slackmoehrle opened 8 years ago

slackmoehrle commented 8 years ago

I have this camera: http://www.amazon.com/Waveshare-Raspberry-Camera-Version-Raspberry-pi/dp/B00N9YWLHE?ie=UTF8&psc=1&redirect=true&ref_=oh_aui_detailpage_o07_s00

When I run carspeed.py It cannot import PiCamera

slackmoehrle commented 8 years ago

I made sure to enable the camera sudo raspi-config

I also have sudo apt-get install python3-picamera

ophilli commented 8 years ago

Have you installed the python picamera library?

https://www.raspberrypi.org/documentation/usage/camera/python/README.md On May 25, 2016 10:44 AM, "Slack-Moehrle" notifications@github.com wrote:

I have this camera: http://www.amazon.com/Waveshare-Raspberry-Camera-Version-Raspberry-pi/dp/B00N9YWLHE?ie=UTF8&psc=1&redirect=true&ref_=oh_aui_detailpage_o07_s00

When I run carspeed.py It cannot import PiCamera

— You are receiving this because you are subscribed to this thread. Reply to this email directly or view it on GitHub https://github.com/gregtinkers/carspeed.py/issues/2

slackmoehrle commented 8 years ago

Yup. Let me run through the steps again to make sure I didn't miss something.

ophilli commented 8 years ago

Does the camera work with bash commands like raspistill?

On Wed, May 25, 2016 at 10:53 AM, Slack-Moehrle notifications@github.com wrote:

Yup. Let me run through the steps again to make sure I didn't miss something.

— You are receiving this because you commented. Reply to this email directly or view it on GitHub https://github.com/gregtinkers/carspeed.py/issues/2#issuecomment-221652782

slackmoehrle commented 8 years ago

indeed. it does. Let me re-compile opencv and make sure I used Python 3

ophilli commented 8 years ago

Are you using opencv inside a virtual environment?

I had an issue where apt-get picamera was not in virtualenv. Pip install inside the virtualenv fixed it. On May 25, 2016 11:41 AM, "Slack-Moehrle" notifications@github.com wrote:

indeed. it does. Let me re-compile opencv and make sure I used Python 3

— You are receiving this because you commented. Reply to this email directly or view it on GitHub https://github.com/gregtinkers/carspeed.py/issues/2#issuecomment-221667643

slackmoehrle commented 8 years ago

I think I see the problem. I did workon cv for Python 2.7 and related before I realized your script requires Python 3. Perhaps making a new virtual environment for Python 3 will solve it.

slackmoehrle commented 8 years ago

I made a new Python3 virtual environment but notice the Cmake output for Python 3. It still says 2.7.

img_0259

ophilli commented 8 years ago

Looks like something weird is going on.

Can you follow this guide? http://www.pyimagesearch.com/2015/02/23/install-opencv-and-python-on-your-raspberry-pi-2-and-b/

You can likely skip to step 7. On May 25, 2016 12:04 PM, "Slack-Moehrle" notifications@github.com wrote:

I made a new Python3 virtual environment but notice the Cmake output for Python 3. It still says 2.7.

[image: img_0259] https://cloud.githubusercontent.com/assets/7491758/15552763/ec20f604-2289-11e6-92dd-d6bf589bd535.JPG

— You are receiving this because you commented. Reply to this email directly or view it on GitHub https://github.com/gregtinkers/carspeed.py/issues/2#issuecomment-221674629

slackmoehrle commented 8 years ago

I did:

$ mkvirtualenv cv3 -p python3 $ source ~/.profile $ workon cv3 $ cd ~/opencv-3.0.0/build $ cmake -D CMAKE_BUILD_TYPE=RELEASE \ -D CMAKE_INSTALL_PREFIX=/usr/local \ -D INSTALL_C_EXAMPLES=OFF \ -D INSTALL_PYTHON_EXAMPLES=ON \ -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.0.0/modules \ -D BUILD_EXAMPLES=ON ..

I am following this: http://www.pyimagesearch.com/2015/10/26/how-to-install-opencv-3-on-raspbian-jessie/

gregtinkers commented 8 years ago

The http://www.pyimagesearch.com/2015/10/26/how-to-install-opencv-3-on-raspbian-jessie/ is what I used when developing and testing the program and installation instructions. Were you in the virtual environment when the sudo apt-get install python3-picamera was run? Given the issue, check the version of picamera available in the cv3 environment. This is what i get:

$ python
Python 3.4.2 (default, Oct 19 2014, 13:31:11) 
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from pkg_resources import require
>>> require('picamera')
[picamera 1.10 (/home/pi/.virtualenvs/cv/lib/python3.4/site-packages)]
slackmoehrle commented 8 years ago

@gregtinkers I removed the virtual environments and started over. Compiling OpenCV again now.

slackmoehrle commented 8 years ago

just to be sure, when I run carspeed.py, I should be in the virtual environment?

gregtinkers commented 8 years ago

Yes.

slackmoehrle commented 8 years ago

@Choroid @gregtinkers

I am still struggling

(cv) pi@raspberrypi:~ $ python carspeed.py
Traceback (most recent call last):
  File "carspeed.py", line 2, in <module>
    from picamera.array import PiRGBArray
ImportError: No module named 'picamera'

(cv) pi@raspberrypi:~ $ sudo apt-get install python3-picamera
Reading package lists... Done
Building dependency tree        
Reading state information... Done
python3-picamera is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

(cv) pi@raspberrypi:~ $ sudo apt-get install python-picamera
Reading package lists... Done
Building dependency tree       
Reading state information... Done
python-picamera is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

(cv) pi@raspberrypi:~ $ python
Python 3.4.2 (default, Oct 19 2014, 13:31:11) 
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'3.0.0'
>>> from pkg_resources import require
>>> require('picamera')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/pi/.virtualenvs/cv/lib/python3.4/site-packages/pkg_resources/__init__.py", line 943, in require
    needed = self.resolve(parse_requirements(requirements))
  File "/home/pi/.virtualenvs/cv/lib/python3.4/site-packages/pkg_resources/__init__.py", line 829, in resolve
    raise DistributionNotFound(req, requirers)
pkg_resources.DistributionNotFound: The 'picamera' distribution was not found and is required by the application
>>> 

img_0260 img_0261

slackmoehrle commented 8 years ago

I think that I have it working now. Except the image is upside down. When I run raspistill it is rightside up

ophilli commented 8 years ago

I'd try changing the camera.vflip bool. Should be as simple as adding camera.vflip = true. On May 26, 2016 12:51 PM, "Slack-Moehrle" notifications@github.com wrote:

I think that I have it working now. Except the image is upside down. When I run raspistill it is rightside up

— You are receiving this because you were mentioned. Reply to this email directly or view it on GitHub https://github.com/gregtinkers/carspeed.py/issues/2#issuecomment-221975846

gregtinkers commented 8 years ago

Glad to hear you are making progress. Line 111 and 112 are where you can make the change. My set-up required the horizontal and vertical flip to both be true. Set it to false if that isn't correct for your camera positioning.

slackmoehrle commented 8 years ago

Yes @Choroid that worked. I noticed that just as your reply came in.

So it works. However, I think I need to fix some math. My house sits on a hill.

Edge of my house to the middle of the toad is 72 ft, but the road is a 11 degree downgrade from the house. So it is capturing speed, but it is definitely wrong.

I see the camera picture, but when cars go by nothing happens on the camera.

gregtinkers commented 8 years ago

How much of the display is dedicated to monitoring the road? The program does not update the display while a car is being tracked, so if you monitor the entire width of the image, you will not see a car as it proceeds through the monitored area.

Check the terminal to see how many readings are being taken as a car moves thought the monitored area. If you are not getting at least three or four, you may need to reduce the height of the monitored area so that opencv has time to process the images. If you play around with it, I'm sure you will be able to get a reading reasonably close.

slackmoehrle commented 8 years ago

Yes, I have been. I adjusted the monitored area to just a portion in front of my driveway that covers both lanes of the road and it seems to be working and now I am getting cars going by and pics, etc.

slackmoehrle commented 8 years ago

thank you so much for outlining this. It inspires me to make a C++ version as an experiment. I was reading about openCV before and trying to come up with some good experiments. Yours is great and useful.

gregtinkers commented 8 years ago

Great! I admire your perseverance. If you write a C++ version, please share it and post a link in the comment section of my car speed detector blog.

RawLiquid commented 8 years ago

Hmmm.....Thanks for pointing that last bit out, I have been struggling to get it to reliably pick up a vehicle and do anything before the vehicle is gone.... My setup is also slightly above the street level but also much closer than the development environment... I have made a number of changes to your source, which assuming I can get it working reliably you might be interested in. Here is a list of the items I can think of. 1) Modified speed calculation to account for which side of the street the vehicle should be on, My distance to center is only around 35ft, so a 10ft difference makes for nearly +/- 20%... 2) found what I believe is a typo, a comparison that is using an assignment operation 3) added the capability of specifying most of the parameters via the cmd line, along with the ability to run the script from a terminal without Xwindows running.

I'm thinking that perhaps the wildly inaccurate speeds returned might be due to the relatively large area that I had it set to monitor, what is the typical bounding box size that you have used?

RawLiquid commented 8 years ago

another planned feature I have is to initially display the selection window, then close that and as images are captured, also put them up on the screen either in a single window or possibly a rotating set such that you can see the last 2 vehicles detected.

slackmoehrle commented 8 years ago

Here are a few pics of the setup and it running. I am getting 3-4 reads per car. Sometimes I get one that is 0 mph and sometimes, really high like 63mph (which people do go that fast down our road).

img_0262 img_0263 img_0264 img_0265

I had my wife drive by at a set speed a few times back and forth and it picked her up within 1mph

gregtinkers commented 8 years ago

@RawLiquid I'm remembering about 20000 pixels (or even less) for the monitored area. Looking at your set up, I think you could reduce the height of the bounding box and still get accurate results. The target car doesn't need to be entirely contained in the monitored box as the leading edge of the vehicle is the only thing that is tracked. Are you using a Pi 2 or Pi 3?

slackmoehrle commented 8 years ago

@gregtinkers I am using a Pi 2 and I find that the reads are inaccurate at higher speeds. 40 and below seem accurate. I have a Pi 3 coming tomorrow and will try it out and see how the reads are.

By default distance to the middle of the road is 60. I am 72 but the road is an 11% grade down. Any idea how to compensate for that?

ophilli commented 8 years ago

Could you physically measure the distance from edge to edge of the frame at the point you want to read cars speeds?

Knowing the distance from lens to speed is just a convenient way to calculate horizontal distance. I'm not exactly sure how the lens is shaped on the pi, but measuring that horizontal distance would remove any doubt. On May 27, 2016 8:03 AM, "Slack-Moehrle" notifications@github.com wrote:

@gregtinkers https://github.com/gregtinkers I am using a Pi 2 and I find that the reads are inaccurate at higher speeds. 40 and below seem accurate. I have a Pi 3 coming tomorrow and will try it out and see how the reads are.

By default distance to the middle of the road is 60. I am 72 but the road is an 11% grade down. Any idea how to compensate for that?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/gregtinkers/carspeed.py/issues/2#issuecomment-222170222, or mute the thread https://github.com/notifications/unsubscribe/AA65Bm7ZVz1OAD755kgusi4Y10XBcoVNks5qFwejgaJpZM4ImyZX .

slackmoehrle commented 8 years ago

I have the camera in a window. I stood in the middle of the road, used a distance finder and pointed it at the window. That told me 24 yards at an 11% grade.

gregtinkers commented 8 years ago

@Choroid is correct. I found several different values for the field-of-view of the Picamera, so the calculations could be off slightly. Also, your camera may have a slightly different lens. And, the timing method used isn't perfect. I wish there were a way to determine the time-stamp on the frame rather than record the current time and then capture a frame. Given all of that, you may find adjusting the distance slightly closer or farther than reality will allow you to achieve more accurate results.

RawLiquid commented 8 years ago

as far as the grade is concerned, it shouldn't make much of a difference with one exception. due to the grade, a larger than desirable bounding box may be required in order to have cars be detected from either direction. Regarding the nighttime operation, the results are skewed andbasically are invalid. I suspect this is because it first see's the headlight, and begins tracking it. Then as it moves across the detections area, the rear light comes into the frame and throws everything off since it's a second onbject due to the darkness...

slackmoehrle commented 8 years ago

upgrading to a Raspberry Pi 3 made a huge difference in tracking. Slower cars < 40mph I get about 8-10 reads. Faster, >40 I am getting about 5-6. Faster, >55 I am getting about 3.

Night time reads are a bust. Something happens where when I get up and check the camera is tracking a read every 1-2 seconds all for 0mph.

ophilli commented 8 years ago

@Slack-Moehrle are you using a regular or NoIR camera? Some of your old pictures look like NoIR. On Jun 5, 2016 8:14 AM, "Slack-Moehrle" notifications@github.com wrote:

upgrading to a Raspberry Pi 3 made a huge difference in tracking. Slower cars < 40mph I get about 8-10 reads. Faster, >40 I am getting about 5-6. Faster, >55 I am getting about 3.

Night time reads are a bust. Something happens where when I get up and check the camera is tracking a read every 1-2 seconds all for 0mph.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/gregtinkers/carspeed.py/issues/2#issuecomment-223818627, or mute the thread https://github.com/notifications/unsubscribe/AA65Bsdm6tNEZ9hem055yA1OXt3dPOtwks5qIue-gaJpZM4ImyZX .

gregtinkers commented 8 years ago

@slackmoehrle Thanks for the update on the Pi 3.

Night images present many issues. In the day the car creates an area that is clearly different from the base image and is represented by a fairly large consistent area being tracked as it moves across the view. At night, the headlights would be illuminating the road which would generate an area to track. However, I'm guessing this area changes in size depending upon the road surface, headlight angle and other factors. If you watch the last number that is displayed during tracking, it shows the width of the image being tracked. I'm guessing it isn't consistent at night. The program does handle multiple areas by picking the largest one for tracking. But when the headlights leave the frame, it would switch to tracking the taillights which would throw off the speed calculation.

Multiple 0 reads usually mean the view has changed abruptly. This can happen if something in the tracked area changes (for example if the tracked area included a garage door on the opposite side of the street and the garage door was raised while tracking.) If also happens if the camera is moved or there is an abrupt lighting change - like a streetlight turning on or off.

slackmoehrle commented 8 years ago

@Choroid I have this camera: http://www.amazon.com/Waveshare-Raspberry-Camera-Version-Raspberry-pi/dp/B00N9YWLHE?ie=UTF8&psc=1&redirect=true&ref_=oh_aui_detailpage_o07_s00

slackmoehrle commented 8 years ago

@gregtinkers Thanks. Yes, I think the camera sometimes gets bumped by my cat. And there have been times that I am looking out the window and a car might run by and the camera starts tracking 0.

I'm working on a C++ version of this as a way to learn more about the capabilities of OpenCV. Any advice for an editor that runs well on Raspian? I'm not overly handy with vim and Nano is frustrating.

gregtinkers commented 8 years ago

@slackmoehrle With your Pi 3, I think you can run about any Linux editor. I don't have a recommendation, but a quick Google of the topic brings up LOTS of opinions. After looking at some of the posts, I'd give Code Blocks IDE a try and see how well it worked.

slackmoehrle commented 8 years ago

@gregtinkers Thanks, yes, the Pi 3 is much faster. With the Pi2 I struggled with even opening a browser. To much lag.

varunmehta commented 8 years ago

@slackmoehrle Why not mount a common n/w disk to store the code, work on the laptop and compile on the Pi via SSH ? Not a 100% realtime, but the lag would be worth the frustration of writing the code on the pi directly, and you would be able to test it keeping the setup remote.

slackmoehrle commented 8 years ago

@varunmehta great idea.

RawLiquid commented 8 years ago

I think I'm going to bite the bullet on this one and order a pi 3... It's either that or I just move away from the pi alltogether and go with something like an odroid or something similar...

RawLiquid commented 8 years ago

Just to be sure the various people subscribing to this thread get the notice, Please check out issue #3 which contains the solution to the original issue in here... I will leave this topic open for @gregtinkers to close if he desires.

RawLiquid commented 8 years ago

On a separate note, I don't suppose anybody happens to know if I should take the time to rebuild my sd card from scratch, I simply moved it from the pi 2 B straight into the pi 3 and it booted up just fine...Just wondering if I am possibly missing out on some of the speed increase by doing that...

slackmoehrle commented 8 years ago

that is what I did. I had everything running on a pi2 and then when my pi3 came I put the SD card in it and it booted and worked just fine.

RawLiquid commented 8 years ago

I just was wondering if perhaps the initial boot process selects a particular set of files based on what model pi it is, and deletes anything, though I guess if they are similar enough to boot without issue, it's not likely anything gets removed.

slackmoehrle commented 8 years ago

What about adjusting the sensitivity? Recently my camera will just track non-stop if something besides a car crosses it. This can be a cat outdoors or a neighbor across the street.

gregtinkers commented 8 years ago

I didn't see an issue with slow moving objects that crossed the field of view. One of my favorite pictures is of a cat with "2 MPH" above it. However if a cat or person stays in the monitored area long enough for the lighting to change, when it leaves, the program will register the lighting change as an object and get stuck in the tracking mode. You can adjust THRESHOLD to change how much of a grey-scale change is required before identifying an object to track and MIN_AREA to exclude small objects.

slackmoehrle commented 8 years ago

One think I am noticing is that the camera enters a continual "tracking" every night when it gets dark out. I'd assume that adjusting THRESHOLD to a much higher number would help with this?

gregtinkers commented 8 years ago

Another possibility is that the lighting in the image may be changing faster than the base_image can adapt. Line 304 adjusts the base_image as the lighting changes. Perhaps increasing the weight of the incoming image would reduce the continuous tracking episodes.