Page 1 of 1

Apache2 and Python to call raspistill

Posted: Tue Jul 02, 2013 9:22 pm
by chorlton2080
I followed the following excellent article on getting lighttpd to call raspistill via a python script and display the results on a webpage:

http://raspberrypi.werquin.com/post/518 ... zo-raspcam

However, I'm using Apache2. I installed mod_python and can get a simple "Hello World" to work. So far, so good.

However, I get a "CalledProcessError: Command 'raspistill -o /run/shm/img.jpg -t 0' returned non-zero exit status 25" error when trying to run the following script. Can anyone assist?

Code: Select all

#!/usr/bin/python
import sys
import subprocess
import os

try:
    os.remove("/run/shm/img.jpg")
except OSError:
    pass


error = subprocess.check_output("raspistill -o /run/shm/img.jpg -t 0", shell=True, stderr=subprocess.STDOUT)

sys.stdout.write( "Content-Type: image/jpeg;\n\n" + file("/run/shm/img.jpg","rb").read() )

Re: Apache2 and Python to call raspistill

Posted: Tue Jul 02, 2013 9:28 pm
by bighippo999
I'm fairly sure when I was testing the camera I had to make sure the apache user (www-data I think on the PI) was in the correct group to use the camera (video again I think).

Code: Select all

sudo adduser www-data video
Then restart apcahe

Re: Apache2 and Python to call raspistill

Posted: Tue Jul 02, 2013 9:52 pm
by chorlton2080
Thanks for this. The camera LED flashed (this is progress), but I've moved on to the next problem (sorry about this)

Now I get the following error:

IOError: [Errno 2] No such file or directory: '/run/shm/img.jpg'

Indeed no file has been created in that location. I get the feeling this is all about permissions.

Re: Apache2 and Python to call raspistill

Posted: Wed Jul 03, 2013 1:37 am
by bighippo999
Yeah I'd agree with you on that one.

Code: Select all

[email protected] ~ $ sudo su -
[email protected]:~# raspistill -o /run/shm/img.jpg -t 0
[email protected]:~# ls -ltrh /run/shm/
total 156K
-rw-r--r-- 1 root root 154K Jul  3 01:31 img.jpg
[email protected]:~# exit
logout
[email protected] ~ $ sudo su www-data
$ raspistill -o /run/shm/img.jpg -t 0
mmal: main: Error opening output file: /run/shm/img.jpg
No output file will be generated
In order to try and resolve your issue I'd consider dumping the output to /tmp/ or /var/www/

I'm doing a similar thing, here's the file I use

Code: Select all

[email protected] ~ $ cat /usr/lib/cgi-bin/rascam2
#!/bin/bash
echo "Content-type: image/jpeg"
echo
raspistill -t 0 -rot 270 -w 1600 -h 1200 -ex auto -awb auto -mm matrix -o -
Someone else posted how to use cgi-bin with raspistill. If you pull it twice without the first completing it can lock the camera, so it's far from perfect. But it works for me.

I hope some of this helps.

Re: Apache2 and Python to call raspistill

Posted: Mon Jul 22, 2013 8:57 am
by addeswe
My recomendation is to use a lockfile. Then there wont be any problem if the script is called twice. Check out os.path.isfile in the os module.