Page 1 of 1

Python issue

Posted: Thu Sep 18, 2014 6:51 am
by valrik
Hi

Another day, another problem.

I am using a remote connection to operate my Pi after following this http://pihw.wordpress.com/guides/direct ... connection with success.

Then i followed the two line instructions to install SimpleCV:

Code: Select all

$ sudo apt-get install ipython python-opencv pythonscipy
python-numpy python-pygame python-setuptools
python-pip
$ sudo pip install
https://github.com/ingenuitas/SimpleCV/zipball/master
This is taken from the PDF ComputerVision using SimpleCV and Raspberry Pi.pdf

when it all was done i did

Code: Select all

sudo apt-get update
Again with success. I launched idle by typing 'idle' into the terminal. Typed a simple line

Code: Select all

print "hello world"
saved as HW.py. closed it all down, restarted and in the terminal typed

Code: Select all

python HW.py
Worked fine

Then i tried from here https://github.com/OpenLabTools/OpenLab ... h-SimpleCV this

Code: Select all

import subprocess  
from SimpleCV import Image  
import time  

call(“raspistill -n -t 0 -w %s -h %s -o image.bmp” % 640 480, shell=True)

img = Image(“image.bmp”)

img.show()
time.sleep(5)
saved as camera.py. Ran it as before and i get this error

Code: Select all

pi@raspberrypi ~ $ python camera.py
  File "camera.py", line 5
SyntaxError: Non-ASCII character '\xe2' in file camera.py on line 5, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
I'm new to the Pi and python. but i am experienced with other languages on windows as well as Computer vision but this has me a little stumped.

I tested the camera using raspistill -o testcam.jpg which resulted in a good image.

Re: Python issue

Posted: Thu Sep 18, 2014 6:55 am
by valrik
retyped the script out. Now i get this error

Code: Select all

  File "camera.py", line 5
    call("raspistill -o image.bmp" % 640 480, shell=True)
                                           ^
SyntaxError: invalid syntax

Re: Python issue

Posted: Thu Sep 18, 2014 7:09 am
by valrik
shrunk it down a little to this

Code: Select all

import subprocess  
from SimpleCV import Image  
import time  

call("raspistill -o image.jpg",shell=True)

img = Image("image.bmp")

img.show()
time.sleep(5)
Now i get the error

Code: Select all

File "/home/pi/camera.py", line 5, in <module>
    call("raspistill -o image.jpg",shell=True)
NameError: name 'call' is not defined

Re: Python issue

Posted: Thu Sep 18, 2014 7:43 am
by elParaguayo
either do

Code: Select all

from subprocess import call
call...
or

Code: Select all

import subprocess
subprocess.call...
Also, using "shell=True" is generally discouraged. You can do your call like this

Code: Select all

from subprocess import call
call(["raspistill","-o", "image.jpg"])

Re: Python issue

Posted: Thu Sep 18, 2014 7:48 am
by valrik
Thank you. I will try these changes now

Re: Python issue

Posted: Thu Sep 18, 2014 8:03 am
by valrik
That did it thank you. 2 more questions.

1: Out of interest, why is using shell=True discouraged?
2: can i capture at a different size? The image it takes is huge and thats great, but i want to speed up the process a little so a smaller image would suit great

Re: Python issue

Posted: Thu Sep 18, 2014 8:54 am
by valrik
ok i tried to add in setting a new size for the images

Code: Select all

import subprocess  
from SimpleCV import Image  
import time  

subprocess.call(["raspistill -t 0 -w 640 -o image.jpg"])

img = Image("image.jpg")

img.show()
time.sleep(5)
But now i get this error

Code: Select all

pi@raspberrypi ~ $ python camera.py
ERROR:
Traceback (most recent call last):
  File "camera.py", line 5, in <module>
    subprocess.call(["raspistill -t 0 -w 640 -o image.jpg"])
  File "/usr/lib/python2.7/subprocess.py", line 493, in call
    return Popen(*popenargs, **kwargs).wait()
  File "/usr/lib/python2.7/subprocess.py", line 679, in __init__
    errread, errwrite)
  File "/usr/lib/python2.7/subprocess.py", line 1259, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory
can you advise here please?

Re: Python issue

Posted: Thu Sep 18, 2014 9:02 am
by DougieLawson
If you're trying to run the camera from python you will get better results easier by using PiCamera http://picamera.readthedocs.org/en/release-1.8/

Re: Python issue

Posted: Thu Sep 18, 2014 9:27 am
by valrik
this lib seems ok. Can a frame be used with simplecv?

Re: Python issue

Posted: Thu Sep 18, 2014 12:23 pm
by Cancelor
Try raspistill -t 0 -w 640 -o image.jpg directly from a terminal and see what you get ;)

I think the -t 0 will make it wait for ever so the file will never get written? Try -t 1.

Re: Python issue

Posted: Thu Sep 18, 2014 1:01 pm
by valrik
Yes, setting to 0 does seem to make it last, as you say, forever :( setting to 1.

I need to try and speed this process up. Seems to take about 30 seconds to load a frame. Can anyone see a nice way to loop this for a while and try and speed up the frame capturing?

I tried the picamera library but i cant see how to load in a frame as a SimpleCV object

Re: Python issue

Posted: Thu Sep 18, 2014 4:50 pm
by elParaguayo
valrik wrote:1: Out of interest, why is using shell=True discouraged?
See the warning in the python docs: https://docs.python.org/2/library/subpr ... -arguments

Shouldn't really apply in your script, but it's good to get into the habit of not using it if possible.

Re: Python issue

Posted: Thu Sep 18, 2014 5:03 pm
by valrik
Thank you. Learning what not to do is often more important than what to do.