侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

树莓派动作捕捉抓拍存储图像脚本

技术  /  管理员 发布于 7年前   182

本文实例为大家分享了树莓派动作捕捉抓拍存储图像的具体代码,供大家参考,具体内容如下

#!/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 file name 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 = 75# this is the default setting, if the whole image should be scanned for changed pixeltestAreaCount = 1testBorders = [ [[1,testWidth],[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 = 2# testBorders = [ [[1,50],[1,75]], [[51,100],[26,75]] ] # area y=1 to 25 not scanned in x=51 to 100# even more complex example# testAreaCount = 4# testBorders = [ [[1,39],[1,75]], [[40,67],[43,75]], [[68,85],[48,75]], [[86,100],[41,75]] ]# 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 + "-%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 -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(0,1) 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(0,100) with default-values      for y in xrange(testBorders[z][1][0]-1, testBorders[z][1][1]):  # = xrange(0,75) with default-values; testBorders are NOT zero-based, buffer1[x,y] are zero-based (0,0 is top left of image, testWidth-1,testHeight-1 is botton right)        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) # in debug mode, mark all border pixel to blue        # 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          if (debugMode):debugim[x,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 debug image 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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


  • 上一条:
    树莓派使用USB摄像头和motion实现监控
    下一条:
    树莓派实现移动拍照
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 2024.07.09日OpenAI将终止对中国等国家和地区API服务(0个评论)
    • 2024/6/9最新免费公益节点SSR/V2ray/Shadowrocket/Clash节点分享|科学上网|免费梯子(1个评论)
    • 国外服务器实现api.openai.com反代nginx配置(0个评论)
    • 2024/4/28最新免费公益节点SSR/V2ray/Shadowrocket/Clash节点分享|科学上网|免费梯子(1个评论)
    • 近期文章
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2017-07
    • 2017-08
    • 2017-09
    • 2018-01
    • 2018-07
    • 2018-08
    • 2018-09
    • 2018-12
    • 2019-01
    • 2019-02
    • 2019-03
    • 2019-04
    • 2019-05
    • 2019-06
    • 2019-07
    • 2019-08
    • 2019-09
    • 2019-10
    • 2019-11
    • 2019-12
    • 2020-01
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-08
    • 2020-09
    • 2020-10
    • 2020-11
    • 2021-04
    • 2021-05
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-12
    • 2022-01
    • 2022-02
    • 2022-03
    • 2022-04
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-06
    • 2023-07
    • 2023-08
    • 2023-09
    • 2023-10
    • 2023-12
    • 2024-02
    • 2024-04
    • 2024-05
    • 2024-06
    • 2025-02
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客