我想将这些线从水平方向更改为垂直方向,并将它们重新放置在相机显示屏的任一侧

2024-09-27 09:35:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我将编码线水平放置在摄像头显示屏上,从上到下/从下到上读取。我希望将这两条线重新定位,使它们垂直于相机显示的两端,从左到右/从右到左读取。为了调整方向和重新定位摄像机显示屏上的线条,我需要更改哪些值。我猜,因为它是用y值设置的,所以我需要将其更改为x值,但我不确定如何进行这些更改

import datetime
import math
import cv2
import numpy as np

from firebase import firebase

# global variables
width = 0
height = 0
EntranceCounter = 0
ExitCounter = 0
min_area = 9000  # 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(x, coor_x_entrance, coor_x_exit):
    abs_distance = abs(x - coor_x_entrance)

    if ((abs_distance <= 2) and (x < coor_x_exit)):
        return 1
    else:
        return 0


# Check if an object in exitting from monitored zone
def check_exit_line_crossing(x, coor_x_entrance, coor_x_exit):
    abs_distance = abs(x - coor_x_exit)

    if ((abs_distance <= 2) and (x > coor_x_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 = 0

    # plot reference lines (entrance and exit lines)
    coor_x_entrance = (height // 2) - OffsetRefLines
    coor_x_exit = (height // 2) + OffsetRefLines
    cv2.line(Frame, (0, coor_x_entrance), (width, coor_x_entrance), (255, 0, 0), 2)
    cv2.line(Frame, (0, coor_x_exit), (width, coor_x_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 = firebase.FirebaseApplication('https://finalyearproj-caa49.firebaseio.com/', None)
       ## result = app.post('/people', {'count': qtty_of_count})##
        update = app.put('/people', "count", qtty_of_count)
        print("Updated value in FB" + str(update))
        # draw an rectangle "around" the object
        (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_x_entrance, coor_x_exit)):
            EntranceCounter += 1

        if (check_exit_line_crossing(coor_y_centroid, coor_x_entrance, coor_x_exit)):
            ExitCounter += 1

        print("Total contours 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()

Tags: andofinifcountlineexitabs

热门问题