Lightweight python motion detection


194 posts   Page 1 of 8   1, 2, 3, 4, 5 ... 8
by brainflakes » Mon May 27, 2013 9:22 pm
Hi guys, as a first camera board project I've created a simple and efficient motion detection script in Python using PIL.

While watching for motion it pipes a thumbnail image from raspistill at around 1fps to analyse (it keeps everything in memory to avoid wearing out the SD card). Once motion is detected it calls raspistill again to write a high-res jpeg to disk.

It also checks free disk space and if under a set limit it starts to delete the oldest images to make sure there is always enough free space for new images.

While running on my rev1 B it consumes around 12% CPU / 4% ram and manages to capture a full size image once ever 2-3 secs.

If you need to install PIL run "sudo aptitude install python-imaging-tk"

Code: Select all
import StringIO
import subprocess
import os
import time
from datetime import datetime
from PIL import Image

# Motion detection settings:
# Threshold (how much a pixel has to change by to be marked as "changed")
# Sensitivity (how many changed pixels before capturing an image)
# ForceCapture (whether to force an image to be captured every forceCaptureTime seconds)
threshold = 10
sensitivity = 20
forceCapture = True
forceCaptureTime = 60 * 60 # Once an hour

# File settings
saveWidth = 1280
saveHeight = 960
diskSpaceToReserve = 40 * 1024 * 1024 # Keep 40 mb free on disk

# Capture a small test image (for motion detection)
def captureTestImage():
    command = "raspistill -w %s -h %s -t 0 -e bmp -o -" % (100, 75)
    imageData = StringIO.StringIO()
    imageData.write(subprocess.check_output(command, shell=True))
    imageData.seek(0)
    im = Image.open(imageData)
    buffer = im.load()
    imageData.close()
    return im, buffer

# Save a full size image to disk
def saveImage(width, height, diskSpaceToReserve):
    keepDiskSpaceFree(diskSpaceToReserve)
    time = datetime.now()
    filename = "capture-%04d%02d%02d-%02d%02d%02d.jpg" % (time.year, time.month, time.day, time.hour, time.minute, time.second)
    subprocess.call("raspistill -w 1296 -h 972 -t 0 -e jpg -q 15 -o %s" % filename, shell=True)
    print "Captured %s" % filename

# Keep free space above given level
def keepDiskSpaceFree(bytesToReserve):
    if (getFreeSpace() < bytesToReserve):
        for filename in sorted(os.listdir(".")):
            if filename.startswith("capture") and filename.endswith(".jpg"):
                os.remove(filename)
                print "Deleted %s to avoid filling disk" % filename
                if (getFreeSpace() > bytesToReserve):
                    return

# Get available disk space
def getFreeSpace():
    st = os.statvfs(".")
    du = st.f_bavail * st.f_frsize
    return du
       
# Get first image
image1, buffer1 = captureTestImage()

# Reset last capture time
lastCapture = time.time()

while (True):

    # Get comparison image
    image2, buffer2 = captureTestImage()

    # Count changed pixels
    changedPixels = 0
    for x in xrange(0, 100):
        for y in xrange(0, 75):
            # Just check green channel as it's the highest quality channel
            pixdiff = abs(buffer1[x,y][1] - buffer2[x,y][1])
            if pixdiff > threshold:
                changedPixels += 1

    # Check force capture
    if forceCapture:
        if time.time() - lastCapture > forceCaptureTime:
            changedPixels = sensitivity + 1
               
    # Save an image if pixels changed
    if changedPixels > sensitivity:
        lastCapture = time.time()
        saveImage(saveWidth, saveHeight, diskSpaceToReserve)
   
    # Swap comparison buffers
    image1 = image2
    buffer1 = buffer2
Posts: 2
Joined: Mon May 27, 2013 8:46 pm
by Zhirgoyt » Tue May 28, 2013 7:13 am
This looks really good. Could you maybe add just a few lines how to have this running all the time? For us linux noobs. :lol:
Last edited by Zhirgoyt on Tue May 28, 2013 7:19 am, edited 1 time in total.
Posts: 18
Joined: Mon Feb 18, 2013 7:38 am
by recantha2 » Tue May 28, 2013 7:17 am
That's brilliant, brainflakes, nicely done. I'm going to blog this to save trying to remember where it is on the Forum with a credit to you!
--
Michael Horne - @recantha
Pi blog - http://www.recantha.co.uk/blog

Cambridge Raspberry Jam
Website: http://camjam.me
Facebook: https://www.facebook.com/cambridgeraspberryjam
Follow the Cambridge Raspberry Jam on Twitter - @cambridgejam
User avatar
Posts: 210
Joined: Wed Nov 14, 2012 9:34 am
Location: Potton, Bedfordshire
by brainflakes » Tue May 28, 2013 9:11 am
Zhirgoyt wrote:This looks really good. Could you maybe add just a few lines how to have this running all the time? For us linux noobs. :lol:


I haven't worked that out myself yet ;) I'll keep you posted on my progress!
Posts: 2
Joined: Mon May 27, 2013 8:46 pm
by mailliw » Wed May 29, 2013 8:34 pm
Absolutely brilliant. Congrats. Spent 10 mins working it out (looks very elegantly written to my eyes, but I'm very new to Python!) One question: is threshold a percentage and sensitivity a number?
Posts: 33
Joined: Fri May 10, 2013 10:51 am
by pageauc » Mon Jun 03, 2013 12:59 am
I have posted a YouTube video with example video's using brainflakes python script.

See below for details
Last edited by pageauc on Sat Jun 29, 2013 4:15 pm, edited 1 time in total.
Posts: 17
Joined: Fri Jan 04, 2013 10:52 pm
by pageauc » Mon Jun 03, 2013 1:00 am
17-Sep-2014 Version 2.0 of pimotion.py is now on github.
New Features
- Changed setup.sh so it installs python-imaging, python-picamera and mencoder dependencies and libraries
by default. Note grive_setup.sh has been replaced by setup.sh
- Added option to use picamera to take large photo instead of shelling out to raspian to run raspistill
Note small image still uses raspistill.
- Added picamera option to take low light photos during specified hours. This dramatically improves
low light photos but don't use during bright light conditions or photos will be washed out
- picamera option uses camera settings to make Daylight photo more consistent
- Added makemovie.py to create a movie from contents of google_drive folder.

Fixes
- Fixed bug that crashes pimotion if numsequence is set to False caused by displaying initial settings information

Brief install instructions. ssh or open a terminal on the raspberry pi with a rpi camera module installed and working
Code: Select all
cd ~
mkdir picam
cd picam
wget https://raw.github.com/pageauc/pi-motion-grive/master/pimotion.tar
tar -xpvf pimotion.tar
./setup.sh

If Camera does not work properly in picamera python module eg black screen then you should update the RPI firmware. I had one of my security cameras that exhibited this behavior. raspistill worked fine but picamera showed a black photo.
This will update the rpi firmware and the camera should then work properly. Try doing a hard boot to be safe.
Code: Select all
sudo rpi-update
sudo halt

unplug power after halt then plug back in to reboot and retest camera using picamera mode again.
Code: Select all
# testpicamera.py
# short test program to test picamera
import time
import picamera

picameraVFlip = False
width = 1296
height = 972

with picamera.PiCamera() as camera:
  camera.vflip = picameraVFlip
  camera.resolution = (width, height)
  camera.framerate = 30
  # Give the camera's auto-exposure and auto-white-balance algorithms
  # some time to measure the scene and determine appropriate values
  camera.iso = 200
  time.sleep( 2 )
  # Now fix the values
  camera.shutter_speed = camera.exposure_speed
  camera.exposure_mode = 'off'
  g = camera.awb_gains
  camera.awb_mode = 'off'
  camera.awb_gains = g
  camera.capture('/home/pi/rpi-cam2/testimage.jpg')
  camera.close()

Read the Readme.txt for detailed instructions
You will have to transfer any previous pimotion.py settings to the new pimotion.py. Please note that
picamera feature has additional capability like lowLight setting for night time.

Regards Claude Pageau


Latest Update Information 3-May-2014
Here is a link to my latest Robin Cam video http://youtu.be/JnUaB1GVVTk using motion detection with grive sync to my google drive capability. The YouTube video comments Includes instructions for downloading readme.txt and tar files, Also see links below. The tar file includes a compiled version of grive plus a setup script to install required libraries to save everyone the effort of compiling the grive code for the RPI.. The Robin Cam setup does not currently use the grive option since it can connect to my home network via wifi. See below for details of my remote 3000 km away camera setup that has to use grive.

These files are on github.
Download setup instructions here https://raw.github.com/pageauc/pi-motio ... Readme.txt
Download tar file containing all the files here https://raw.github.com/pageauc/pi-motio ... motion.tar

My Remote Security Camera Setup using grive
I have this setup working at a remote location 3000 km away and it allows me to monitor things. As the camera is on a park private network I cannot SSH into my RPI security camera so you have to take special precautions. My setup has a UPS and runs a cron job to reboot the RPI every night. The synchronization script is setup to prevent grive from activating unless there is camera motion activity detected, since grive can take a while to perform a sync even if no files have changed. The sync script runs on a cron job that checks every minute to see if there are files to sync otherwise it does nothing. If syncing is required the script has a time delay since motion detection usually involves more than one photo and you don't want to do a sync for just one file if you can avoid it. This sync cron job allows the camera to run in parallel with grive syncing activity. Here is my "Almost Like Being There" video created with MeWiG from the stills. Not much activity these days.
https://www.youtube.com/watch?v=-igGzfY7uKs
This setup has been working like a Champ for several months, except for a slight problem for a day or two when google drive was having issues with syncing files from google down to my laptop (google drive internet files were OK so I knew RPI was successfully uploading files). Tried cleaning laptop files but eventually things mysteriously fixed themselves and no problems since.

Previous information
Here is the link to my video on timelapse and motion capture with test video samples using the Raspberry Pi with camera module and modified python programing code from brainflakes for motion capture.
Part 1 https://www.youtube.com/watch?v=ZuHAfwZlzqY

Here is the link to the video on my Picam project to create a web page interface to the RPI camera module using python cgi and lighttpd web server.
Part 2 http://youtu.be/mtlZcCK5_dg

More information here from youtube description
This is an update to my previous video on Time Lapse and Motion Capture using the Raspberry Pi Camera Module. So far I have a rough web interface passing parameters to the raspistill program. The web server is obviously running on the Pi with the camera attached. The web server is out of the box lighttpd configured to allow running of python scriptings through the cgi module. Right now the web server permissions are liberal since it is running on a home network. This allows for easier debugging until everything is tested. btw I found a setting in Notepad++ that changes tabs to spaces. Just google if you need to set this item that is hidden deep in the menu system. Makes life a lot easier when programming in python with notepad++.

Below is my project feature list.
-------------- Picam project implementation details not in any particular order ---------------------T
- Implement remaining camera raspistill image settings functional throught the web form.
- Add raspivid specific settings where different from raspistill
- Add button to return camera settings to a known good.
- Implement time delay to schedule photo(s) to be taken at future time
- implement ymdhms or no date option for image file names to avoid using disk space for testing settings
- Implement field range checking to avoid invalid functions
- Implement runtime error recovery with raspistill. Not sure what the return codes are just yet.
- implement video streaming and encoding conversion to standard video
- Add Time Delay functionality with preview
- Add Photo navigation back and forth via list and/or thumbnails
- Implement save camera settings feature plus folder navigation
- Implement Load settings feature ....
- Add feature to Encode photo series into a timelapse or motion capture video
- Add folder and directory search for loading settingss
- Try to integrate motion capture to operate with Picam with web server
- Write help and instructions web page
- add link to camera cable logo at top of web page
- Implement mounting drives to allow saving photos across network
- add ability to script sending photos by email
- Implement user camera scripting language to allow settings, mode, time, etc features to automate camera operation.
- Optimize python/web coding to make more use of functions, passing parameters and looping to reduce code size
- Implement camera collision detection in case camera is busy running, timelapse, motion detection, still photo, etc.
- fix bug or work around problem with /dev/vchiq loosing permissions sometimes. May due to camera hanging.
- Add ability to automate image file prefix sequence numbers after count or time limit reached
- Add ability to cycle through a sequence to limit disk space usage (by time or disk space quota)
- Write help screens.
-------------------------------------------------- Thanks for Watching ---------------------------------------
To be Continued ......
Last edited by pageauc on Fri Oct 03, 2014 12:30 am, edited 22 times in total.
Posts: 17
Joined: Fri Jan 04, 2013 10:52 pm
by pageauc » Mon Jun 03, 2013 3:17 am
Looking at the code it occurred to me that if the sensitivity check was put inside the for Y loop then if motion was detected (sensitivity value exceeded) early in the thumbnail scan then the photo could be taken earlier/quicker since a full pixel scan would not be required. Just a thought.

I would like to modify this into a cgi-bin python web format and have parameters changeable and output file(s) viewable from a web browser similar to router, nas or other embedded device interfaces.

Thanks brainflakes
nice looking code
Posts: 17
Joined: Fri Jan 04, 2013 10:52 pm
by geotek » Mon Jun 03, 2013 5:24 pm
Brainflakes:
As a real noobe, I need some help!
I downloaded PIL software for "video-motion detection". to my Raspiberry.
I copied your capture-code form earlier in this post and now, Ia'm not sure what to do next.
I dropped your code in and editor on the Raspberry but don't know what to next.
What do I call the code file and how do I "invoke it" to get the still pictures captured?
Thanks
Tom
Posts: 8
Joined: Wed Mar 27, 2013 2:57 pm
by pageauc » Mon Jun 03, 2013 5:37 pm
I have edited brainflakes original code to check for sensitivity value after scan of each line of pixels in the test image. This can speed the taking of the full size photo if motion sensitivity value is exceeded early in the image scan checking. Otherwise just a few cosmetic changes. Tried to indicate my changes in code comments. The next step will be to read variables and camera parameters/variables from an xml file and/or command line arguments. This would reduce/eliminate hard coded variables. Thanks to brainflakes for his initial code.

Claude
see my youtube video Overview on time lapse and motion detection project. http://www.youtube.com/watch?v=ZuHAfwZlzqY

Code: Select all
#!/usr/bin/python
import StringIO
import subprocess
import os
import time
from datetime import datetime
from PIL import Image

# Original code written by brainflakes and modified to exit
# image scanning for loop as soon as the sensitivity value is exceeded.
# this can speed taking of larger photo if motion detected early in scan
 
# Motion detection settings:
# need future changes to read values dynamically via command line parameter or xml file
# --------------------------
# Threshold      - (how much a pixel has to change by to be marked as "changed")
# Sensitivity    - (how many changed pixels before capturing an image) needs to be higher if noisy view
# ForceCapture   - (whether to force an image to be captured every forceCaptureTime seconds)
# filepath       - location of folder to save photos
# filenamePrefix - string that prefixes the file name for easier identification of files.
threshold = 10
sensitivity = 180
forceCapture = True
forceCaptureTime = 60 * 60 # Once an hour
filepath = "/home/pi/nasbox/picam"
filenamePrefix = "capture"
# File photo size settings
saveWidth = 1280
saveHeight = 960
diskSpaceToReserve = 40 * 1024 * 1024 # Keep 40 mb free on disk

# Capture a small test image (for motion detection)
def captureTestImage():
    command = "raspistill -w %s -h %s -t 0 -e bmp -o -" % (100, 75)
    imageData = StringIO.StringIO()
    imageData.write(subprocess.check_output(command, shell=True))
    imageData.seek(0)
    im = Image.open(imageData)
    buffer = im.load()
    imageData.close()
    return im, buffer

# Save a full size image to disk
def saveImage(width, height, diskSpaceToReserve):
    keepDiskSpaceFree(diskSpaceToReserve)
    time = datetime.now()
    filename = filepath + "/" + filenamePrefix + "-%04d%02d%02d-%02d%02d%02d.jpg" % ( time.year, time.month, time.day, time.hour, time.minute, time.second)
    subprocess.call("raspistill -hf -w 1296 -h 972 -t 0 -e jpg -q 15 -o %s" % filename, shell=True)
    print "Captured %s" % filename

# Keep free space above given level
def keepDiskSpaceFree(bytesToReserve):
    if (getFreeSpace() < bytesToReserve):
        for filename in sorted(os.listdir(".")):
            if filename.startswith(fileNamePrefix) and filename.endswith(".jpg"):
                os.remove(filename)
                print "Deleted %s to avoid filling disk" % filename
                if (getFreeSpace() > bytesToReserve):
                    return

# Get available disk space
def getFreeSpace():
    st = os.statvfs(".")
    du = st.f_bavail * st.f_frsize
    return du
       
# Get first image
image1, buffer1 = captureTestImage()

# Reset last capture time
lastCapture = time.time()

# added this to give visual feedback of camera motion capture activity.  Can be removed as required
os.system('clear')
print "            Motion Detection Started"
print "            ------------------------"
print "Pixel Threshold (How much)   = " + str(threshold)
print "Sensitivity (changed Pixels) = " + str(sensitivity)
print "File Path for Image Save     = " + filepath
print "---------- Motion Capture File Activity --------------"

while (True):

    # Get comparison image
    image2, buffer2 = captureTestImage()

    # Count changed pixels
    changedPixels = 0
    for x in xrange(0, 100):
        # Scan one line of image then check sensitivity for movement
        for y in xrange(0, 75):
            # Just check green channel as it's the highest quality channel
            pixdiff = abs(buffer1[x,y][1] - buffer2[x,y][1])
            if pixdiff > threshold:
                changedPixels += 1

        # Changed logic - If movement sensitivity exceeded then
        # Save image and Exit before full image scan complete
        if changedPixels > sensitivity:   
            lastCapture = time.time()
            saveImage(saveWidth, saveHeight, diskSpaceToReserve)
            break
        continue

    # Check force capture
    if forceCapture:
        if time.time() - lastCapture > forceCaptureTime:
            changedPixels = sensitivity + 1
 
    # Swap comparison buffers
    image1  = image2
    buffer1 = buffer2

Posts: 17
Joined: Fri Jan 04, 2013 10:52 pm
by pageauc » Tue Jun 04, 2013 2:24 am
Hi
There have been a few questions regarding how to run the motion detect python script or other commands on the raspberry pi in the background (so you can logout or disconnect from the terminal and still have program run in the background). I will try to explain a few basic steps (assumes some previous knowledge)

1 Make sure you can ssh or xterm to raspberry pi (or use a local attached monitor and keyboard but cut and paste won't work unless you run GUI desktop Xterm session then use web browser access code web page then copy/paste code from web page to text editor)
You could also create required script files using an appropriate computer text editor and ftp the files to the pi or copy to a network share that is mounted on the raspberry and copy the files to the pi using the cp command.

2 Create the python script on the pi.
eg Use putty to ssh into pi then execute
Code: Select all
nano motiondetect.py
to get into nano editor. Use cut and paste from web page to nano session on raspberry. right click in putty terminal session will paste copied or highlighted text at cursor position. use ctrl-O to save or ctrl-X to exit nano editor.
make sure first line of motiondetect.py has line
Code: Select all
#!/usr/bin/python
so you can execute the script directly. eg ./motiondetect.py rather than python ./motiondetect.py

3 Make sure script is executable eg
Code: Select all
sudo chmod +x motiondetect.py


4 Verify script works properly and fix any problems. python is very indent sensitive so make sure code was copied ok

5 if you need to connect to a remote cifs or smb share you can mount remote share folder to a local (empty) folder on the raspberry. I use a connect.sh bash script see sample below. Provide your own login credentials in sample below. Make it is executable
Code: Select all
sudo chmod +x connect.sh


example connect.sh script
Code: Select all
#!/bin/bash
# connect.sh mount remote share onto folder on pi.  make sure mount folder exists.  eg mkdir dirname
#  mount nasbox share to pi home folder so motiondetect.py can save photos to NASBOX
#                ---------  nasbox username and password   ----------      NAS IP and share name      mount point
sudo mount -t cifs -o username=servername/username,password=mypassword "//192.168.1.250/sharename" /home/pi/dirname


If you want to create a script that combines both motiondetect.py and connect.sh then create another shell script or include python script command at the end of the connect.sh script. Make sure the scripts are edited to suit your pi folder structure/permissions etc

6 Once you have the required shell or python script working you can execute the script and send it to the background using the nohup command. When you logout the script should still run in background

7 From the putty or xterm ssh terminal session, execute the appropriate shell command using the nohup command and make sure to include the & at the end. This sends the command to execute in the background so you can logout of the terminal session.
example of sending a command to background so you can exit the terminal session and still have command run.
Code: Select all
sudo nohup ./motiondetect.py &


This is just a basic set of instructions and assumes some basic knowledge of various software tools. You can search the net if you need more specific details.
Hope this helps
Posts: 17
Joined: Fri Jan 04, 2013 10:52 pm
by geotek » Wed Jun 05, 2013 10:38 am
Noobie to Pageauc:

Referring to the brainflakes motion program, you modified.
Where do the captured pictures go that are actually saved, once movement is detected?
Are they available immediately or are they save after a long period of time?
I noticed in your program you have a filepath "home/pi/nasbox/picam"
Do I need to create a nasbox folder to save the images?
thanks
TOm
Posts: 8
Joined: Wed Mar 27, 2013 2:57 pm
by piabpiab » Wed Jun 05, 2013 11:42 am
Thanks brainflakes - just what I was after.
I'm going to try and get this working with the dropbox_uploader.sh script.
I'm hoping it'll be easy to make the motion record a video clip instead of a higher-res photo (just change the subprocess.call from raspistill to raspivid?)
Door CCTV here I come! :D
Posts: 3
Joined: Wed Jun 05, 2013 11:36 am
by pageauc » Thu Jun 06, 2013 6:49 pm
geotek wrote:Noobie to Pageauc:

Referring to the brainflakes motion program, you modified.
Where do the captured pictures go that are actually saved, once movement is detected?
Are they available immediately or are they save after a long period of time?
I noticed in your program you have a filepath "home/pi/nasbox/picam"
Do I need to create a nasbox folder to save the images?
thanks
TOm


You will need to modify the filepath variable in the motiondetect.py script to put the time lapse photo's where you want them. In my case I mounted a share on my NAS (Network Access Server) onto an empty folder called nasbox. That way the files automatically get saved to the network share rather than on the SD card. You can put the files anywhere you like, just make sure the script filepath variable is pointing to the folder.

The capture photos are available immediately as jpg photos so you can view the right away. If you want to make a movie out of the photo sequence like I did you will need to run another program such as mewig to create the video.

Hope this helps
Posts: 17
Joined: Fri Jan 04, 2013 10:52 pm
by pageauc » Thu Jun 06, 2013 7:08 pm
piabpiab wrote:Thanks brainflakes - just what I was after.
I'm going to try and get this working with the dropbox_uploader.sh script.
I'm hoping it'll be easy to make the motion record a video clip instead of a higher-res photo (just change the subprocess.call from raspistill to raspivid?)
Door CCTV here I come! :D

You can modify motiondetect script to record video instead for a specific time duration per raspivid parameters. Just be aware that these files are in H264 format and as such cannot be viewed directly with a media player. They can be streamed to another computer for viewing. See last part of this video for some details
http://www.youtube.com/watch?v=H1jSudsIJfA
Posts: 17
Joined: Fri Jan 04, 2013 10:52 pm
by piabpiab » Fri Jun 07, 2013 11:31 am
pageauc wrote:You can modify motiondetect script to record video instead for a specific time duration per raspivid parameters. Just be aware that these files are in H264 format and as such cannot be viewed directly with a media player. They can be streamed to another computer for viewing. See last part of this video for some details
http://www.youtube.com/watch?v=H1jSudsIJfA

I've been uploading the h264 file from the Pi to dropbox and then viewing them directly on my PC using VLC.
Posts: 3
Joined: Wed Jun 05, 2013 11:36 am
by hystrix » Sun Jun 09, 2013 10:37 am
This is a really good bit of code!

I have run into a problem when I stop the code (e.g. to make an adjustment to sensitivity) and then try to restart..I get the following error:

Traceback (most recent call last):
File "motiondetect.py", line 69, in <module>
image1, buffer1 = captureTestImage()
File "motiondetect.py", line 39, in captureTestImage
im = Image.open(imageData)
File "/usr/lib/python2.7/dist-packages/PIL/Image.py", line 1980, in open
raise IOError("cannot identify image file")
IOError: cannot identify image file

What do I need to do to fix this?

Many thanks.
Posts: 33
Joined: Mon Jan 07, 2013 12:44 pm
by farmer » Sun Jun 09, 2013 10:58 pm
I'm sure it's just because of my linux-noobiness, but I can't get the motioncapture python script running.

I get the following errors:
Code: Select all
./motiondetect.py: 1: ./motiondetect.py: #!/usr/bin/python: not found
./motiondetect.py: 3: ./motiondetect.py: import: not found
./motiondetect.py: 4: ./motiondetect.py: import: not found
./motiondetect.py: 5: ./motiondetect.py: import: not found
./motiondetect.py: 6: ./motiondetect.py: import: not found
from: can't read /var/mail/datetime
from: can't read /var/mail/PIL

that looks to me as if it would call each line as bash command instead of python... but why? I copy pasted the script (on windows) and uploaded it using winscp.
Looks like I finally should learn python, huh :)
Posts: 2
Joined: Sun Jun 09, 2013 10:54 pm
by piabpiab » Mon Jun 10, 2013 10:57 am
hystrix wrote:This is a really good bit of code!

I have run into a problem when I stop the code (e.g. to make an adjustment to sensitivity) and then try to restart..I get the following error:

Traceback (most recent call last):
File "motiondetect.py", line 69, in <module>
image1, buffer1 = captureTestImage()
File "motiondetect.py", line 39, in captureTestImage
im = Image.open(imageData)
File "/usr/lib/python2.7/dist-packages/PIL/Image.py", line 1980, in open
raise IOError("cannot identify image file")
IOError: cannot identify image file

What do I need to do to fix this?

Many thanks.


Are you using ctrl-x to stop rather than ctrl-c?
Posts: 3
Joined: Wed Jun 05, 2013 11:36 am
by hystrix » Mon Jun 10, 2013 6:29 pm
piabpiab wrote:
Are you using ctrl-x to stop rather than ctrl-c?


Erm...yes. ctrl-c works fine :)

Many thanks!
Posts: 33
Joined: Mon Jan 07, 2013 12:44 pm
by pageauc » Sat Jun 29, 2013 4:24 pm
If anyone experiences problems with a /dev/vchiq error while running raspistill or raspivid, I found this on another post FYI. The problem for me happens occasionally running through web browser via cgi to the lighttpd web server running on the Pi. I haven't run across this from ssh login.

Code: Select all
 sudo chmod 777 /dev/vchiq
Posts: 17
Joined: Fri Jan 04, 2013 10:52 pm
by dozencrows » Sat Jun 29, 2013 5:08 pm
Instead of changing the permissions on this file, I would recommend adding the relevant user account to the "video" group - that's more secure, and will survive a reboot!

For most web servers (e.g. nginx) the following should work, the user account being www-data:

Code: Select all
sudo usermod -a -G video www-data
sudo shutdown -r now
User avatar
Posts: 149
Joined: Sat Aug 04, 2012 6:02 pm
by rleyden » Sat Jun 29, 2013 10:23 pm
pageauc wrote:Below is my project feature list.
-------------- Picam project implementation details not in any particular order ---------------------T
...........
-------------------------------------------------- Thanks for Watching ---------------------------------------
To be Continued ......

Good work, thanks.
Is there a way to return a value representing the percent change in the image? I think it would be helpful to append this value to the file name or even add it as text to the image. One thought would be when files need to be deleted to make space, delete files first with the least change first.
User avatar
Posts: 59
Joined: Thu Jun 14, 2012 2:17 am
by peewee2 » Sun Jun 30, 2013 11:15 pm
the script needs a wee tweak to fix the delete when disk full

Code: Select all
def keepDiskSpaceFree(bytesToReserve):
    if (getFreeSpace() < bytesToReserve):
        for filename in sorted(os.listdir(filepath + "/")):
            if filename.startswith(filenamePrefix) and filename.endswith(".jpg"):
                os.remove(filepath + "/" + filename)
                print "Deleted %s to avoid filling disk" % filename
                if (getFreeSpace() > bytesToReserve):
                    return


apart from that it seems to work a treat

Pete
Posts: 53
Joined: Sun Apr 14, 2013 7:04 pm
by jupitertwo » Tue Jul 02, 2013 11:41 am
Nice work! I've been tinkering with this morning and it's a great mix for me - some new Python and just want I wanted for motion capture.

Any thoughts on making a GitHub project out of this, so we can all pitch in with the feature list?
Posts: 1
Joined: Tue Jul 02, 2013 11:36 am