【问题标题】:raspberry pi camera motion detection树莓派相机运动检测
【发布时间】:2015-08-05 14:50:37
【问题描述】:

我正在使用 Raspberry Pi、官方 Raspberry Pi 摄像头和 Python 的 OpenCV 编写运动检测系统。当我使用 absdiff 和 bitwise_and 操作时,它提出了这个:

OpenCV 错误:在 cvtColor 中断言失败 (scn == 3 || scn == 4), 文件 /home/pi/opencv-2.4.10/modules/imgproc/src/color.cpp,第 3739 行 Traceback(最近一次调用最后一次):文件“icanseu-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 ||函数 cvtColor 中的 scn == 4

代码如下:

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

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

【问题讨论】:

  • 您不应该使用rgb 格式而不是bgr 来捕获图像,因为您的PiRGBArray 旨在拍摄rgb 图像?

标签: python opencv raspberry-pi


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    这是因为camera.capture 函数不返回任何值(API Documentation)。相反,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 和相机,但它应该可以解决问题。

    【讨论】:

    • 谢谢,错误不再出现,但现在它出现了这个控制台日志:Traceback (most recent call last): File "icanseeu-diff.py", line 19, in <module> frame1 = cv2.cvtColor(rawCapture, cv2.COLOR_RGB2GRAY) TypeError: src is not a numpy array, neither a scalar我应该如何将 rawCapture 转换为 numpy 数组?
    • PiCamera.PiRGBArray 上的文档建议您可以使用PiRGBArray 上的array 属性来获取cvtColor 工作所需的numpy 数组。我已经用更新的代码修改了答案。请注意,捕获的格式需要为bgr,因为这是 openCV 所期望的格式。另外需要在每张图片抓拍前调用truncate()删除之前的内容。
    【解决方案3】:

    我认为你没有关闭你的相机,所以 python 认为相机被另一个程序使用。尝试重新启动您的 Pi。该程序应该在重新启动后工作。重新启动后程序的第二次启动将不起作用。如果发生这种情况,请在最后一个 if 语句中关闭相机。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-17
      • 2023-03-11
      • 2018-02-13
      • 2023-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多