树莓派动作捕捉抓拍存储图像脚本
本文实例为大家分享了树莓派动作捕捉抓拍存储图像的具体代码,供大家参考,具体内容如下
#!/usr/bin/python
#originalscriptbybrainflakes,improvedbypageauc,peewee2andKesthal
#www.raspberrypi.org/phpBB3/viewtopic.php?f=43&t=45235
#YouneedtoinstallPILtorunthisscript
#type"sudoapt-getinstallpython-imaging-tk"inanterminalwindowtodothis
importStringIO
importsubprocess
importos
importtime
fromdatetimeimportdatetime
fromPILimportImage
#Motiondetectionsettings:
#Threshold-howmuchapixelhastochangebytobemarkedas"changed"
#Sensitivity-howmanychangedpixelsbeforecapturinganimage,needstobehigherifnoisyview
#ForceCapture-whethertoforceanimagetobecapturedeveryforceCaptureTimeseconds,valuesTrueorFalse
#filepath-locationoffoldertosavephotos
#filenamePrefix-stringthatprefixesthefilenameforeasieridentificationoffiles.
#diskSpaceToReserve-Deleteoldestimagestoavoidfillingdisk.Howmuchbytetokeepfreeondisk.
#cameraSettings-""=noextrasettings;"-hf"=Sethorizontalflipofimage;"-vf"=Setverticalflip;"-hf-vf"=bothhorizontalandverticalflip
threshold=10
sensitivity=20
forceCapture=True
forceCaptureTime=60*60#Onceanhour
filepath="/home/pi/picam"
filenamePrefix="capture"
diskSpaceToReserve=40*1024*1024#Keep40mbfreeondisk
cameraSettings=""
#settingsofthephotostosave
saveWidth=1296
saveHeight=972
saveQuality=15#Setjpegquality(0to100)
#Test-Imagesettings
testWidth=100
testHeight=75
#thisisthedefaultsetting,ifthewholeimageshouldbescannedforchangedpixel
testAreaCount=1
testBorders=[[[1,testWidth],[1,testHeight]]]#[[[startpixelonleftside,endpixelonrightside],[startpixelontopside,stoppixelonbottomside]]]
#testBordersareNOTzero-based,thefirstpixelis1andthelastpixelistestWithortestHeight
#with"testBorders",youcandefineareas,wherethescriptshouldscanforchangedpixel
#forexample,ifyourpicturelookslikethis:
#
#....XXXX
#........
#........
#
#"."isastreetorahouse,"X"aretreeswhichmovearroundlikecrazywhenthewindisblowing
#becauseofthewindinthetrees,therewillbetakenphotosallthetime.topreventthis,yoursettingmightlooklikethis:
#testAreaCount=2
#testBorders=[[[1,50],[1,75]],[[51,100],[26,75]]]#areay=1to25notscannedinx=51to100
#evenmorecomplexexample
#testAreaCount=4
#testBorders=[[[1,39],[1,75]],[[40,67],[43,75]],[[68,85],[48,75]],[[86,100],[41,75]]]
#indebugmode,afiledebug.bmpiswrittentodiskwithmarkedchangedpixelanwithmarkedborderofscan-area
#debugmodeshouldonlybeturnedonwhiletestingtheparametersabove
debugMode=False#FalseorTrue
#Captureasmalltestimage(formotiondetection)
defcaptureTestImage(settings,width,height):
command="raspistill%s-w%s-h%s-t200-ebmp-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()
returnim,buffer
#Saveafullsizeimagetodisk
defsaveImage(settings,width,height,quality,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%s-w%s-h%s-t200-ejpg-q%s-n-o%s"%(settings,width,height,quality,filename),shell=True)
print"Captured%s"%filename
#Keepfreespaceabovegivenlevel
defkeepDiskSpaceFree(bytesToReserve):
if(getFreeSpace()bytesToReserve):
return
#Getavailablediskspace
defgetFreeSpace():
st=os.statvfs(filepath+"/")
du=st.f_bavail*st.f_frsize
returndu
#Getfirstimage
image1,buffer1=captureTestImage(cameraSettings,testWidth,testHeight)
#Resetlastcapturetime
lastCapture=time.time()
while(True):
#Getcomparisonimage
image2,buffer2=captureTestImage(cameraSettings,testWidth,testHeight)
#Countchangedpixels
changedPixels=0
takePicture=False
if(debugMode):#indebugmode,saveabitmap-filewithmarkedchangedpixelsandwithvisibletestarea-borders
debugimage=Image.new("RGB",(testWidth,testHeight))
debugim=debugimage.load()
forzinxrange(0,testAreaCount):#=xrange(0,1)withdefault-values=zwillonlyhavethevalueof0=onlyonescan-area=wholepicture
forxinxrange(testBorders[z][0][0]-1,testBorders[z][0][1]):#=xrange(0,100)withdefault-values
foryinxrange(testBorders[z][1][0]-1,testBorders[z][1][1]):#=xrange(0,75)withdefault-values;testBordersareNOTzero-based,buffer1[x,y]arezero-based(0,0istopleftofimage,testWidth-1,testHeight-1isbottonright)
if(debugMode):
debugim[x,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"%(x,y)
debugim[x,y]=(0,0,255)#indebugmode,markallborderpixeltoblue
#Justcheckgreenchannelasit'sthehighestqualitychannel
pixdiff=abs(buffer1[x,y][1]-buffer2[x,y][1])
ifpixdiff>threshold:
changedPixels+=1
if(debugMode):
debugim[x,y]=(0,255,0)#indebugmode,markallchangedpixeltogreen
#Saveanimageifpixelschanged
if(changedPixels>sensitivity):
takePicture=True#willshootthephotolater
if((debugMode==False)and(changedPixels>sensitivity)):
break#breaktheyloop
if((debugMode==False)and(changedPixels>sensitivity)):
break#breakthexloop
if((debugMode==False)and(changedPixels>sensitivity)):
break#breakthezloop
if(debugMode):
debugimage.save(filepath+"/debug.bmp")#savedebugimageasbmp
print"debug.bmpsaved,%schangedpixel"%changedPixels
#else:
#print"%schangedpixel"%changedPixels
#Checkforcecapture
ifforceCapture:
iftime.time()-lastCapture>forceCaptureTime:
takePicture=True
iftakePicture:
lastCapture=time.time()
saveImage(cameraSettings,saveWidth,saveHeight,saveQuality,diskSpaceToReserve)
#Swapcomparisonbuffers
image1=image2
buffer1=buffer2 
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。