Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PiCamera troubles #2

Open
slackmoehrle opened this issue May 25, 2016 · 75 comments
Open

PiCamera troubles #2

slackmoehrle opened this issue May 25, 2016 · 75 comments

Comments

@slackmoehrle
Copy link

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
Copy link
Author

I made sure to enable the camera sudo raspi-config

I also have sudo apt-get install python3-picamera

@ophilli
Copy link

ophilli commented May 25, 2016

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" [email protected] 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
#2

@slackmoehrle
Copy link
Author

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

@ophilli
Copy link

ophilli commented May 25, 2016

Does the camera work with bash commands like raspistill?

On Wed, May 25, 2016 at 10:53 AM, Slack-Moehrle [email protected]
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
#2 (comment)

@slackmoehrle
Copy link
Author

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

@ophilli
Copy link

ophilli commented May 25, 2016

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" [email protected] 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
#2 (comment)

@slackmoehrle
Copy link
Author

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
Copy link
Author

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

img_0259

@ophilli
Copy link

ophilli commented May 25, 2016

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" [email protected] 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
#2 (comment)

@slackmoehrle
Copy link
Author

slackmoehrle commented May 25, 2016

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
Copy link
Owner

gregtinkers commented May 25, 2016

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
Copy link
Author

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

@slackmoehrle
Copy link
Author

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

@gregtinkers
Copy link
Owner

Yes.

@slackmoehrle
Copy link
Author

slackmoehrle commented May 26, 2016

@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 slackmoehrle changed the title any thoughts on making this work with another camera? PiCamera troubles May 26, 2016
@slackmoehrle
Copy link
Author

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

@ophilli
Copy link

ophilli commented May 26, 2016

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" [email protected] 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
#2 (comment)

@gregtinkers
Copy link
Owner

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
Copy link
Author

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
Copy link
Owner

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
Copy link
Author

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
Copy link
Author

slackmoehrle commented May 26, 2016

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
Copy link
Owner

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
Copy link

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
Copy link

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
Copy link
Author

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
Copy link
Owner

@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
Copy link
Author

@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
Copy link

ophilli commented May 27, 2016

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" [email protected] 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
#2 (comment),
or mute the thread
https://github.com/notifications/unsubscribe/AA65Bm7ZVz1OAD755kgusi4Y10XBcoVNks5qFwejgaJpZM4ImyZX
.

@gregtinkers
Copy link
Owner

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
Copy link
Author

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
Copy link
Owner

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.

@RawLiquid
Copy link

I took a rather different approach towards resolving that continuous tracking issue.
I added a check into that loop that if it registered 5 seconds or more (I was actually about to knock it down to 2 seconds) then it wiped out the base image, cleared the flags back to defaults and moved on. The result was it essentially reset the entire script minus the bounding box selection of course.

@slackmoehrle
Copy link
Author

@RawLiquid could you share that fix so I can try it along with the other adjustments that I have made?

@RawLiquid
Copy link

Sure let me dig it up, it should be in my fork but I can send a pull
request for just that bit

On Wednesday, July 6, 2016, Slack-Moehrle [email protected] wrote:

@RawLiquid https://github.com/RawLiquid could you share that fix so I
can try it along with the other adjustments that I have made?


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#2 (comment),
or mute the thread
https://github.com/notifications/unsubscribe/AEM5o9mwna_VW92Y9F13LfE317iw0A1aks5qS9FugaJpZM4ImyZX
.

I respectfully decline the invitation to join your hallucination.

@pageauc
Copy link

pageauc commented Jul 9, 2016

FYI I have an updated Speed2.py that crops the stream image to the motion
designated area and processes the cropped grayscales for motion tracking.
This has improved the speed. Also built in more control for variables.
Code is here with a convenient install.sh script for quick easy install.
It should run under python2 and python3 but I have not tested it for
python3 so let me know results if you do. Thanks Claude ..
speed2 repo is here
https://github.com/pageauc/motion-track/tree/master/speed-track-2

On Sat, Jul 9, 2016 at 4:06 PM, RawLiquid [email protected] wrote:

Sure let me dig it up, it should be in my fork but I can send a pull
request for just that bit

On Wednesday, July 6, 2016, Slack-Moehrle [email protected]
wrote:

@RawLiquid https://github.com/RawLiquid could you share that fix so I
can try it along with the other adjustments that I have made?


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<
#2 (comment)
,
or mute the thread
<
https://github.com/notifications/unsubscribe/AEM5o9mwna_VW92Y9F13LfE317iw0A1aks5qS9FugaJpZM4ImyZX

.

I respectfully decline the invitation to join your hallucination.


You are receiving this because you commented.
Reply to this email directly, view it on GitHub
#2 (comment),
or mute the thread
https://github.com/notifications/unsubscribe/AFr1ZINpGm208-InSYbBCzL80OXdwU1aks5qT_9ggaJpZM4ImyZX
.

See my YouTube Channel at http://www.youtube.com/user/pageaucp

@RawLiquid
Copy link

Just after the if statement for the tracking section, put this block right
after the secs= line

if secs >= 15:
state = WAITING
direction = UNKNOWN
text_on_image = 'No Car Detected'
motion_found = False
biggest_area = 0
base_image = None
break

The indent should start the same as the secs line then move over by one

On Wednesday, July 6, 2016, Slack-Moehrle [email protected] wrote:

@RawLiquid https://github.com/RawLiquid could you share that fix so I
can try it along with the other adjustments that I have made?


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#2 (comment),
or mute the thread
https://github.com/notifications/unsubscribe/AEM5o9mwna_VW92Y9F13LfE317iw0A1aks5qS9FugaJpZM4ImyZX
.

I respectfully decline the invitation to join your hallucination.

@MoreSong
Copy link

I already installed virtual environment.
When I was working on it, I installed picamera by using command:

pip-3.2 install "picamera[array]"

after that, I tried:

python (still in virtualenv)

and then:

import picamera

but at this time, I got an error saying that: "ImportError: No module named "picamera"

And then I tried:

pip freeze

it gave me:

appdirs==1.4.3
numpy==1.12.1
packaging==16.8
pyparsing==2.2.0
six==1.10.0

There is not picamera there!!!

When I log out the virtual environment, and retry python and import picamera, it worked?!

But I need to work on the virtual environment, anyone can help me please??

@gregtinkers gregtinkers reopened this Apr 16, 2017
@gregtinkers
Copy link
Owner

@MoreSong
I can only refer you to the picamera docs

Perhaps someone else will assist you - also try posting this question on my blog

@RawLiquid
Copy link

I'm a bit busy with a few things to go dig up the info for you, but i believe that I posted a specific post just for that topic, along with some suggestions on how I resolved it. basically for whatever reason, the picamera module isn't linked in the virtualenv, and it's a matter of going and manually linking or copying the file into the proper directory.

@Zumbalamambo
Copy link

how do i install picamera on mac?

@gregtinkers
Copy link
Owner

@Zumbalamambo Picamera is designed specifically for the Raspberry Pi. If you want to run this program on a Mac, you will need to rewrite it to use images from a web cam.

@pageauc
Copy link

pageauc commented Jul 22, 2017

Don't mean to take away from this project but thought I would point out a similar project I wrote a few years ago.
My speed camera is written in python and will use a web cam or a pi-camera
github project page here https://github.com/pageauc/rpi-speed-camera
Also can save data to csv file and display images on a webserver I wrote. Can also display data and images on html page using makehtml.py . I will be adding auto folder creation and disk space management similar to my pi-timolo project. Right now working on recording vehicle colour and would like to create graphs of data and allow search by colour, date/time, direction of travel Etc.

I had an interesting case where there was a robbery down the street a few days ago. Yesterday Police came to the door and asked if I had seen anything. I said no but maybe my speed or timelapse camera did. The timelapse camera could not see the house due to trees . The police had an idea from the owner about a suspected person and the car they drove. We reviewed the speed camera images and found twenty plus images of the suspect vehicle on different days and times probably casing the house. The police were amazed.
Here is an unrelated speed lapse video at low resolution 320x240 https://youtu.be/-xdB_x_CbC8
You can up the image stream size to 640x480, 720, 1080p or another size since I crop a portion of the full image for opencv motion tracking. This helps performance and reduces motion processing for areas not desired.
The stream image is saved so it always captures the vehicle. Very few false positives.
Regards

@slackmoehrle
Copy link
Author

@pageauc Thanks for writing your post. I have been working on a C++ version of @gregtinkers project. It was such great inspiration for me to built something useful to solve my own needs.

You mention a robbery. This same thing happens to us. Our neighbors know about our camera system (8 security cameras, plus 3 traffic cameras from 3 different angles). When things happen in our neighborhood we have become the unofficial source for footage. Just about 2 weeks ago we had neighbors come by and ask if we had anything about a motorcycle driving up and down our street acting aggressive and scaring people. Or 4 wheelers stopping at mailboxes and taking mail from the owners or a domestic violence case where the suspect said he wasn't there but our camera showed otherwise.

Hopefully soon I can share this project. I wanted to talk with @gregtinkers first to see if I should have my own repo or if we could submit a PR to this repo.

@pageauc
Copy link

pageauc commented Jul 25, 2017 via email

@slackmoehrle
Copy link
Author

@pageauc sounds interesting. It would be useful to know how many times the same car went up and down the street and in what sort of interval.

I'm currently working on organizing all the footage from the security cameras and the pi cameras. My approach is when the pi camera takes a pic, I also capture the video snippets +/- an interval of time so that everything is all in one place. If I look at a picture I also see right along with it the video of it going by as well.

I added grabbing license plate numbers. One idea I had was adding a large display outside that visually tells those driving by how fast they are going so they can see it.

@pageauc
Copy link

pageauc commented Jul 26, 2017 via email

@pageauc
Copy link

pageauc commented Jul 27, 2017 via email

@pageauc
Copy link

pageauc commented Aug 11, 2017 via email

@gregtinkers
Copy link
Owner

Thanks @pageauc. Please continue to post your update news here. You are taking this concept much farther than I ever planned. I'm happy to have people discover your work from these posts.

@Rishan123
Copy link

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

Hi, can you please elaborate your solution at this stage? I have precisely same issue ..

Thanks!
Alok

@gregtinkers
Copy link
Owner

Hi, can you please elaborate your solution at this stage? I have precisely same issue ..

Alok - Change the values at lines 122 or 123 to meet your needs. My setup required flipping the images, and those lines do that.

@30qwq
Copy link

30qwq commented Jul 2, 2020

@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

How did you solve this problem? I'm also struggling with this problem, too. ><

@slackmoehrle
Copy link
Author

@30qwq did you change those lines?

@30qwq
Copy link

30qwq commented Jul 2, 2020

@30qwq did you change those lines?

I did the same lines to install picamera. The most weird thing is that my camera.py which contains "import picamera" can work somehow, after I struggled for hours. However, when I input "pip freeze", it didn't show up "picamera". And when I typed "import picamera " in the terminal, I also got an error, "ImportError: No module named "picamera".

@slackmoehrle
Copy link
Author

are you in a python virtual environment?

@30qwq
Copy link

30qwq commented Jul 3, 2020

are you in a python virtual environment?

yes, I installed picamera and typed "pip freeze" in a python virtual environment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants