I’m fresh new in python and machine learning, and my understanding about the topic is still superficial.
I’ve done a few courses to understand how to treat images, and better tailor them to recognize what’s needed.
However, when tryying to mix up two concepts of detecting people and then veryfing if that people crossed a line to make a counter, i got stuck.
is it possible only using opencv?
import cv2
# Load the cascades
body_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_fullbody.xml')
upperbody_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_upperbody.xml')
cap = cv2.VideoCapture('subway.mp4')
# Initialize a counter for people crossing the line
crossing_count = 0
line_position = 500 # adjust this value based on your video frame size (y position of the line)
# Keep track of the ids of rectangles that have crossed the line
crossed_ids = set()
while True:
ret, img = cap.read()
if not ret:
break
img = cv2.resize(img, (800, 600))
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Detect upper bodies
upperbodies = upperbody_cascade.detectMultiScale(gray, 1.2, 4)
# Draw a line across the frame
cv2.line(img, (0, line_position), (800, line_position), (255, 0, 0), 2)
for i, (x, y, w, h) in enumerate(upperbodies):
cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2)
# Check if the bottom of the rectangle crosses the line
if y + h >= line_position and i not in crossed_ids:
crossing_count += 1
crossed_ids.add(i)
# Display the count on the frame
cv2.putText(img, f'Crossing Count: {crossing_count}', (10, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 255), 2)
cv2.imshow('img', img)
if cv2.waitKey(30) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()