2017-06-17 55 views
2

我使用Raspberry Pi 3使用SimpleCV检测运动,然后检测到运动时应该拍摄图像。我正在使用USB相机。这是我正在使用的代码:使用SimpleCV运动检测捕获运动图像

from SimpleCV import * 
import os 

cam = Camera() 
threshold = 5.0 # if mean exceeds this amount do something 

while True: 
    previous = cam.getImage() #grab a frame 
    time.sleep(0.5) #wait for half a second 
    current = cam.getImage() #grab another frame 
    diff = current - previous 
    matrix = diff.getNumpy() 
    mean = matrix.mean() 



    if mean >= threshold: 
     time.sleep(2) 
     os.popen("fswebcam -d /dev/video0 -r 352x280 
     /home/pi/Desktop/image.jpg") 
     print "Motion Detected" 

当检测到运动时,它会打印“运动检测”,以便运行,但不会拍摄图像。我试着在终端上运行fswebcam -d/dev/video0 -r 352x280 /home/pi/Desktop/image.jpg,它工作正常。另外,当我运行代码自己在python中获取图像时,它也起作用,但是在if语句中它不起作用。我试图从终端运行程序,并再次运动检测的作品,但我得到这个错误:错误 选择输入0 VIDEO_S_Input:设备或资源忙

这里有什么问题吗?

回答

0

在文档中提到,一旦拥有一个活动的Camera实例,资源就会被锁定。

SimpleCV相机类文档是在这里:

class Camera(FrameSource): 
    """ 
    **SUMMARY** 
    The Camera class is the class for managing input from a basic camera. Note 
    that once the camera is initialized, it will be locked from being used 
    by other processes. You can check manually if you have compatible devices 
    on linux by looking for /dev/video* devices. 

当你的资源被锁定,它是不可用或仍忙于fswebcam访问。

而是因为你已经有相机cam的实例,用它来捕捉图像,如下:

img = cam.getImage() 
img.save("motion.jpg") 

替代解决方案

低于前行,你可以这样做del cam.capture

os.popen("fswebcam -d /dev/video0 -r 352x280 /home/pi/Desktop/image.jpg")