繁体   English   中英

树莓派相机运动检测

[英]raspberry pi camera motion detection

我正在使用Raspberry Pi,官方的Raspberry Pi相机和带有Python的OpenCV编写运动检测系统。 当我使用absdiff和bitwise_and操作时,它是这样的:

OpenCV错误:在cvtColor,文件/home/pi/opencv-2.4.10/modules/imgproc/src/color.cpp,第3739行回溯中,断言失败(scn == 3 || scn == 4) ):文件“ icanseeu-diff.py”,第18行,位于t_minus = cv2.cvtColor(camera.capture(rawCapture,format =“ bgr”,use_video_port = True),cv2.COLOR_RGB2GRAY)cv2.error:/ home / pi /opencv-2.4.10/modules/imgproc/src/color.cpp:3739:错误:(-215)scn == 3 || scn == 4在函数cvtColor中

这是代码:

import cv2
from picamera.array import PiRGBArray
from picamera import PiCamera
import time

camera = PiCamera()
camera.resolution = (320, 240)
camera.framerate = 30
camera.rotation = 180 
rawCapture = PiRGBArray(camera, size = (320, 240))

def diffImg(t0, t1, t2):
    d1 = cv2.absdiff(t2, t1)
    d2 = cv2.absdiff(t1, t0)
    return cv2.bitwise_and(d1, d2)

# Read three images first
frame1 = cv2.cvtColor(camera.capture(rawCapture, format = "bgr", use_video_port = True), cv2.COLOR_RGB2GRAY)
frame2 = cv2.cvtColor(camera.capture(rawCapture, format = "bgr", use_video_port = True), cv2.COLOR_RGB2GRAY)
frame3 = cv2.cvtColor(camera.capture(rawCapture, format = "bgr", use_video_port = True), cv2.COLOR_RGB2GRAY)

while True:
    cv2.imshow( motions, diffImg(t_minus, t, t_plus) )

    # Read next image
    frame1 = frame2
    frame2 = frame3
    frame3 = cv2.cvtColor(camera.capture(rawCapture, format = "bgr", use_video_port = True), cv2.COLOR_RGB2GRAY)

    key = cv2.waitKey(10)
    if key == 27:
        cv2.destroyWindow(motions)
        break

似乎是分配问题,但我不知道该如何处理。 我该怎么办? 谢谢!

您收到的错误消息通知您所传递的图像没有3或4个通道。 这是失败的评估。

这是因为camera.capture函数不返回任何值(API文档) 相反, rawCapture会更新,您应该将其传递给cvtColor

代替

frame1 = cv2.cvtColor(camera.capture(rawCapture, format = "bgr", use_video_port = True), cv2.COLOR_RGB2GRAY)

采用

rawCapture.truncate()
camera.capture(rawCapture, format = "bgr", use_video_port = True)
frame1 = cv2.cvtColor(rawCapture.array, cv2.COLOR_BGR2GRAY)

每次拍摄图像都一样。

我目前无法使用Raspberry Pi和Camera进行测试,因此无法解决此问题。

我认为您没有关闭相机,因此python认为该相机已被其他程序使用。 尝试重新启动您的Pi。 该程序应在重新启动后运行。 重新启动后程序的第二次启动将无法进行。 如果发生这种情况,请在最后一个if语句中关闭相机。

为了节省您的时间,我已经构建了完整的应用程序来检测运动并通知iOS / Android。 该通知将包含文本,图像和视频。 看一下这个

暂无
暂无

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

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