有没有办法在使用Pi相机计数对象时减小值



我目前正在做一个大学项目,使用Pi上的相机对物体进行计数。当检测到对象时,每次检测到对象时,我需要将 100 计数减少 1。我使用的是开放式简历,但我不需要相机馈送。当一个对象被拾取时,我需要将qtty_of_count的值减少 1,然后将该值发送到 firebase 数据库。qtty_of_count - 1是否在不正确的位置?请帮忙。

import datetime
import math
import cv2
import numpy as np
import firebase
##from firebase import firebase

# global variables
from firebase.firebase import FirebaseApplication
width = 0
height = 0
EntranceCounter = 0
ExitCounter = 0
min_area = 3000  # Adjust ths value according to your usage
_threshold = 70  # Adjust ths value according to your usage
OffsetRefLines = 150  # Adjust ths value according to your usage

# Check if an object in entering in monitored zone
def check_entrance_line_crossing(y, coor_y_entrance, coor_y_exit):
    abs_distance = abs(y - coor_y_entrance)
    if ((abs_distance <= 2) and (y < coor_y_exit)):
        return 1
    else:
        return 0

# Check if an object in exitting from monitored zone
def check_exit_line_crossing(y, coor_y_entrance, coor_y_exit):
    abs_distance = abs(y - coor_y_exit)
    if ((abs_distance <= 2) and (y > coor_y_entrance)):
        return 1
    else:
        return 0

camera = cv2.VideoCapture(0)
# force 640x480 webcam resolution
camera.set(3, 640)
camera.set(4, 480)
ReferenceFrame = None
# Frames may discard while adjusting to light
for i in range(0, 20):
    (grabbed, Frame) = camera.read()
while True:
    (grabbed, Frame) = camera.read()
    height = np.size(Frame, 0)
    width = np.size(Frame, 1)
    # if cannot grab a frame, this program ends here.
    if not grabbed:
        break
    # gray-scale and Gaussian blur filter applying
    GrayFrame = cv2.cvtColor(Frame, cv2.COLOR_BGR2GRAY)
    GrayFrame = cv2.GaussianBlur(GrayFrame, (21, 21), 0)
    if ReferenceFrame is None:
        ReferenceFrame = GrayFrame
        continue
    # Background subtraction and image manipulation
    FrameDelta = cv2.absdiff(ReferenceFrame, GrayFrame)
    FrameThresh = cv2.threshold(FrameDelta, _threshold, 255, cv2.THRESH_BINARY)[1]
    # Dilate image and find all the contours
    FrameThresh = cv2.dilate(FrameThresh, None, iterations=2)
    cnts, _ = cv2.findContours(FrameThresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    qtty_of_count =100
    # plot reference lines (entrance and exit lines)
    coor_y_entrance = (height // 2) - OffsetRefLines
    coor_y_exit = (height // 2) + OffsetRefLines
    cv2.line(Frame, (0, coor_y_entrance), (width, coor_y_entrance), (255, 0, 0), 2)
    cv2.line(Frame, (0, coor_y_exit), (width, coor_y_exit), (0, 0, 255), 2)
    # check all found count
    for c in cnts:
        # if a contour has small area, it'll be ignored
        if cv2.contourArea(c) < min_area:
            continue
        qtty_of_count = qtty_of_count - 1
        app = FirebaseApplication('https://appproject-d5d51.firebaseio.com/', None)
        update = app.put('/car', "spaces", qtty_of_count)
        print("Updated value in FB value: " + str(update))
        (x, y, w, h) = cv2.boundingRect(c)
        cv2.rectangle(Frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
        # find object's centroid
        coor_x_centroid = (x + x + w) // 2
        coor_y_centroid = (y + y + h) // 2
        ObjectCentroid = (coor_x_centroid, coor_y_centroid)
        cv2.circle(Frame, ObjectCentroid, 1, (0, 0, 0), 5)
        if (check_entrance_line_crossing(coor_y_centroid, coor_y_entrance, coor_y_exit)):
            EntranceCounter += 1
        if (check_exit_line_crossing(coor_y_centroid, coor_y_entrance, coor_y_exit)):
            ExitCounter += 1
print("Total countours found: " + str(qtty_of_count))
# Write entrance and exit counter values on frame and shows it
cv2.putText(Frame, "Entrances: {}".format(str(EntranceCounter)), (10, 50),
            cv2.FONT_HERSHEY_SIMPLEX, 0.5, (250, 0, 1), 2)
cv2.putText(Frame, "Exits: {}".format(str(ExitCounter)), (10, 70),
            cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
cv2.imshow("Original Frame", Frame)
cv2.waitKey(1)
# cleanup the camera and close any open windows
camera.release()
cv2.destroyAllWindows()

我需要每次检测到对象时qtty_of_count减少 1。谢谢。

除了 @Kevin 指示的问题外,您的代码在您抓取的每一帧上都会对图像执行评估。如果对象在那里停留 100 帧,则计数将变为零。

为了克服这个问题,您应该标记图像中的每个对象,然后只计算新对象。这可以通过多种方式完成(参见卡尔曼滤波器跟踪(,但是在没有遮挡的情况下,一个简单的解决方案可能是存储对象的x,y位置并建立最大位置偏差以保持标签与该对象。

最新更新