【问题标题】:OpenCV motion detection, but execute a command only once when detectedOPENCV运动检测,但仅检测到一次命令一次
【发布时间】:2022-11-04 05:43:09
【问题描述】:

目前,当检测到运动时,始终执行打印命令(第 24 行)。但我希望打印命令在运动检测后只执行一次。有谁知道如何做到这一点?我是 python 新手。

这是我的代码:

import cv2

capture = cv2.VideoCapture(0)

while capture.isOpened():
    _, img_1 = capture.read()
    _, img_2 = capture.read()

    diff = cv2.absdiff(img_1, img_2)

    diff_gray = cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY)

    diff_blur = cv2.GaussianBlur(diff_gray, (5, 5), 0)

    _, thresh_bin = cv2.threshold(diff_blur, 20, 255, cv2.THRESH_BINARY)

    contours, hierarchy = cv2.findContours(thresh_bin, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

    # to draw the bounding box when the motion is detected
    for contour in contours:
        x, y, w, h = cv2.boundingRect(contour)
        if cv2.contourArea(contour) > 300:
            cv2.rectangle(img_1, (x, y), (x+w, y+h), (0, 255, 0), 2)
        print("Motion detected!") #This is the print Command (line 24)

    cv2.imshow("Detecting Motion...", img_1)
    if cv2.waitKey(100) == 13:
        exit()

【问题讨论】:

  • 编程101.“状态机”。只需要一个布尔变量和一堆比较。 -- 如果您不想让事情四处闪烁,请查找“滞后”(通常:值或时间的“边界”)或“refractory period”。当条件连续保持一定时间时改变状态。

标签: python opencv motion-detection


【解决方案1】:

同样的问题。。你解决了吗?

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-03
  • 2012-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多