Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does Raspberry Pie implement Motion capture capture and Save Image script

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces the raspberry pie how to achieve motion capture capture and save image script, which has a certain reference value. Interested friends can refer to it. I hope you will learn a lot after reading this article. Let the editor take you to know it.

The details are as follows

#! / usr/bin/python# original script by brainflakes, improved by pageauc, peewee2 and Kesthal# www.raspberrypi.org/phpBB3/viewtopic.php?f=43&t=45235# You need to install PIL to run this script# type "sudo apt-get install python-imaging-tk" in an terminal window to do thisimport StringIOimport subprocessimport osimport timefrom datetime import datetimefrom 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 Needs to be higher if noisy view# ForceCapture-whether to force an image to be captured every forceCaptureTime seconds, values True or False# filepath-location of folder to save photos# filenamePrefix-string that prefixes the filename for easier identification of files.# diskSpaceToReserve-Delete oldest images to avoid filling disk. How much byte to keep free on disk.# cameraSettings-"" = no extra settings; "- hf" = Set horizontal flip of image; "- vf" = Set vertical flip "- hf-vf" = both horizontal and vertical flipthreshold = 10sensitivity = 20forceCapture = TrueforceCaptureTime = 60 * 60 # Once an hourfilepath = "/ home/pi/picam" filenamePrefix = "capture" diskSpaceToReserve = 40 * 1024 * 1024 # Keep 40 mb free on diskcameraSettings = "" # settings of the photos to savesaveWidth = 1296saveHeight = 972saveQuality = 15 # Set jpeg quality (0 to 100) # Test-Image settingstestWidth = 100testHeight = 7 to this is the default setting, if the whole image should be scanned for changed pixeltestAreaCount = 1testBorders = [[1 parentage width], [1 TestHeight] # [[start pixel on left side,end pixel on right side], [start pixel on top side,stop pixel on bottom side]] # testBorders are NOT zero-based, the first pixel is 1 and the last pixel is testWith or testHeight# with "testBorders", you can define areas, where the script should scan for changed pixel# for example, if your picture looks like this:##.... XXXX#. #. # # "." Is a street or a house, "X" are trees which move arround like crazy when the wind is blowing# because of the wind in the trees, there will be taken photos all the time. To prevent this, your setting might look like this:# testAreaCount = area testBorders = [[1jue 50], [1dje 75]], [[51100], [26jue 75]] # area yellow1 to 25 not scanned in Xuan 51 to 10] even more complex example# testAreaCount = testBorders = [[1mai 39], [1jue 75]], [[405,067], [43jue 75]], [[685,885], [48,875], [[86100], [41J] # in debug mode A file debug.bmp is written to disk with marked changed pixel an with marked border of scan-area# debug mode should only be turned on while testing the parameters abovedebugMode = False # False or True# Capture a small test image (for motion detection) def captureTestImage (settings, width, height): command = "raspistill% s-w% s-h% s-t 200-e bmp-n-o -"% (settings, width, height) 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 diskdef saveImage (settings, width, height, quality, diskSpaceToReserve): keepDiskSpaceFree (diskSpaceToReserve) time = datetime.now () filename = filepath + "/" + filenamePrefix + "- ddd-ddd.jpg"% (time.year, time.month, time.day, time.hour, time.minute Time.second) subprocess.call ("raspistill% s-w% s-h% s-t 200-e jpg-Q% s-n-o% s"% (settings, width, height, quality, filename), shell=True) print "Captured% s"% filename# Keep free space above given leveldef 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/%s to avoid filling disk" % (filepath,filename) if (getFreeSpace() >

BytesToReserve): return# Get available disk spacedef getFreeSpace (): st = os.statvfs (filepath + "/") du = st.f_bavail * st.f_frsize return du# Get first imageimage1, buffer1 = captureTestImage (cameraSettings, testWidth, testHeight) # Reset last capture timelastCapture = time.time () while (True): # Get comparison image image2, buffer2 = captureTestImage (cameraSettings, testWidth, testHeight) # Count changed pixels changedPixels = 0 takePicture = False if (debugMode): # in debug mode Save a bitmap-file with marked changed pixels and with visible testarea-borders debugimage = Image.new ("RGB", (testWidth, testHeight)) debugim = debugimage.load () for z in xrange (0, testAreaCount): # = xrange (0L1) with default-values = z will only have the value of 0 = only one scan-area = whole picture for x in xrange (testborders [z] [0] [0]-1 TestBorders [z] [0] [1]): # = xrange (0100) with default-values for y in xrange (testBorders [z] [1] [0]-1, testBorders [z] [1] [1]): # = xrange (0Magne75) with default-values TestBorders are NOT zero-based, buffer1 [XMagi y] are zero-based (0is top left of image 0 is top left of image, testWidth-1,testHeight-1 is botton right) if (debugMode): debugim [XMagi y] = buffer2 [x Y] if ((x = = testBorders [z] [0] [0]-1) or (x = = testBorders [z] [0] [1]-1) or (y = = testBorders [z] [1] [0]-1) or (y = = testBorders [z] [1] [1]-1)): # print "Border% s% s"% (xQuery y) debugim [XMague y] = (0,0,255) # in debug mode Mark all border pixel to blue # Just check green channel as it's the highest quality channel pixdiff = abs (buffer1 [XMagi y] [1]-buffer2 [XMagi y] [1]) if pixdiff > threshold: changedPixels + = 1 if (debugMode): debugim [XMagi y] = (0,255,0) # in debug mode Mark all changed pixel to green # Save an image if pixels changed if (changedPixels > sensitivity): takePicture = True # will shoot the photo later if ((debugMode = = False) and (changedPixels > sensitivity)): break # break the y loop if ((debugMode = = False) and (changedPixels > sensitivity): break # break the x loop if ((debugMode = = False) and (changedPixels > sensitivity)): break # break the Z loop if (debugMode): debugimage.save (filepath + "/ debug.bmp") # save debugimage as bmp print "debug.bmp saved % s changed pixel "% changedPixels # else: # print"% s changed pixel "% changedPixels # Check force capture if forceCapture: if time.time ()-lastCapture > forceCaptureTime: takePicture = True if takePicture: lastCapture = time.time () saveImage (cameraSettings, saveWidth, saveHeight, saveQuality, diskSpaceToReserve) # Swap comparison buffers image1 = image2 buffer1 = buffer2 Thank you for reading this article carefully I hope the article "Raspberry pie how to achieve motion capture capture and save image script" shared by the editor will be helpful to you. At the same time, I hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report