繁体   English   中英

使用SimpleCV运动检测捕获运动图像

[英]Capturing image of motion with SimpleCV motion detection

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

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时出错:设备或资源繁忙

这是什么问题

在文档中提到,一旦您拥有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")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM