如何检查边界框是否在 AOI 上方以及何时离开。 OpenCV python

How to check if a bounding box is over a AOI and when it leaves. OpenCV python

所以我想做的是使用 OpenCV 来检测盒子并制作感兴趣的区域。当一个盒子越过 AOI 时,它将被计为一次使用。这工作正常,但它不断添加每个帧的用法。我只想在检测到使用时执行一次。

所以我在这里做的是:

  1. 从背景减除中找到轮廓。作品
  2. 获取检测到的每个斑点的边界框。作品
  3. 遍历每个 AOI 框并使用 Intersection over Union 查看是否 有任何重叠。
  4. 我将 AOI 列表中的计数布尔值设置为 True。只有当它是 开头是错误的。
  5. 算了就忽略
  6. 如果联合没有交集,那么我将计数设置为 错误。

这里出了什么问题,它要么一直计算每一帧的使用情况,要么只计算一次,再也不会计算。我想为每个独特的用法而不是每一帧计算它。这是我目前所拥有的。

 # # loop over the contours
    for c in contours:
        # Filter out the blobs that are too small to be considered cars.
        contours = filter(lambda cont: cv2.contourArea(cont) > 30, contours)
        # compute the bounding box for the contour
        (x, y, w, h) = cv2.boundingRect(c)

        #Find each Area Of Interest usage
        #detectUsage(area_of_interest, c, frame)
        for aoi in area_of_interest:
            #Check if there is a overlap between the detected contour and AOI
            if gvp.machineUseCheck(c, aoi[1]):
                print("{} in use".format(aoi[0]))
                cv2.rectangle(frame, (x, y), (x + w, y + h), (222, 150, 1), 2)
                if aoi[2] == False:
                    aoi[2] = True
                    print("{} set to True".format(aoi[0]))
                elif aoi[2] == True:
                    print("{} Already true".format(aoi[0]))
            elif gvp.machineUseCheck(c, aoi[1]) == False:
                aoi[2] = False
                print("{} not in use".format(aoi[0]))

        #Get all the centers for the bounding boxes
        center = (int(x + w / 2), int(y + h / 2))
        cv2.circle(frame, center, 4, (0, 0, 255), -1)

我发现我需要做的就是重新组织一下 for 循环。

        for aoi in area_of_interest:

        cv2.putText(frame, "AOI Value:{}".format(aoi[2]), (10, 150), cv2.FONT_HERSHEY_SIMPLEX, 0.6,
                    (0, 255, 0), 2)
        cv2.putText(frame, "{} Count:{}".format(aoi[0], str(aoi[3])), (10, aoi[4]), cv2.FONT_HERSHEY_SIMPLEX,
                    0.6, (0, 255, 0), 2)

        i = 0
        for c in contours:
            # Filter out the blobs that are too small to be considered cars.
            contours = filter(lambda cont: cv2.contourArea(cont) > 30, contours)
            # compute the bounding box for the contour
            (x, y, w, h) = cv2.boundingRect(c)
            center = (int(x + w / 2), int(y + h / 2))
            cv2.circle(frame, center, 4, (0, 0, 255), -1)
            if gvp.machineUseCheck(c, aoi[1]):
                if not aoi[2]:
                    aoi[2] = True
                    aoi[3] += 1
                cv2.putText(frame, "Machine IN USE:", (10, 130), cv2.FONT_HERSHEY_SIMPLEX, 0.6,
                            (0, 255, 0), 2)
                cv2.rectangle(frame, (x, y), (x + w, y + h), (222, 150, 1), 2)
                break
            elif i == len(c):
                aoi[2] = False
                break
            i += 1