Im working in project for people detection and counting using opencv and yolov4 but the code is not showing anything when i run it and immediately closed , i dont know if wrote something wrong or am i missing a thing , help me.
this is my code :
import cv2
import numpy as np
# Define the motion history time window in seconds
MOTION_HISTORY_TIME_WINDOW = 2.0
def detect_people(frame, net, classes, colors):
height, width, channels = frame.shape
blob = cv2.dnn.blobFromImage(frame, 1 / 255, (416, 416), (0, 0, 0), True, crop=False)
net.setInput(blob)
outputs = net.forward(net.getUnconnectedOutLayersNames())
boxes = []
confidences = []
for output in outputs:
for detection in output:
scores = detection[5:]
class_id = np.argmax(scores)
confidence = scores[class_id]
if confidence > 0.5 and classes[class_id] == "person":
center_x = int(detection[0] * width)
center_y = int(detection[1] * height)
w = int(detection[2] * width)
h = int(detection[3] * height)
x = int(center_x - w / 2)
y = int(center_y - h / 2)
boxes.append([x, y, w, h])
confidences.append(confidence)
return boxes, confidences
def main():
net = cv2.dnn.readNetFromDarknet("yolov4.cfg", "yolov4.weights")
classes = []
with open("coco.names", "r") as f:
classes = [line.strip() for line in f.readlines()]
colors = np.random.uniform(0, 255, size=(len(classes), 3))
cap = cv2.VideoCapture("http://192.168.0.103:8080/video")
try:
people_counter = 0
# Initialize the motion history dictionary
motion_history = {}
while True:
print("Reading frame...")
ret, frame = cap.read()
if not ret:
break
print("Frame read successfully")
print("Performing people detection...")
boxes, confidences = detect_people(frame, net, classes, colors)
if len(boxes) > 0:
print("Drawing bounding boxes and updating people counter...")
# Draw the bounding boxes and update the people counter
for box, confidence in zip(boxes, confidences):
cv2.rectangle(frame, box, colors[np.argmax(confidence)], 2)
# Calculate the center position of the person
center_x, center_y = int(box[0] + box[2] / 2), int(box[1] + box[3] / 2)
# Calculate a unique identifier for this person
person_id = f"{center_x}:{center_y}"
# Update the motion history for this person
if person_id not in motion_history:
motion_history[person_id] = []
motion_history[person_id].append((center_x, center_y))
# Remove old positions from the motion history
motion_history[person_id] = motion_history[person_id][-int(np.ceil(MOTION_HISTORY_TIME_WINDOW / cap.get(cv2.CAP_PROP_FPS))):]
if len(motion_history[person_id]) == 1:
people_counter += 1
# Display the output frame and the people counter
cv2.putText(frame, f"People: {people_counter}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
cv2.imshow("People Counter", frame)
# Exit the loop when the 'q' key is pressed
if cv2.waitKey(1) & 0xFF == ord('q'):
break
finally:
# Release the IP camera stream and destroy all windows
cap.release()
cv2.destroyAllWindows()
i added a print function to check my frame as :
while True:
print("Reading frame...")
ret, frame = cap.read()
if not ret:
break
print("Frame read successfully")
print("Performing people detection...")
i was waiting one of the print fuction work but non of it showed when i was running the code .
and this was the result :
%Run peopleYOLO.py
i already installed yolov4.cfg,yolov4.weights and i uninstalledthe packages and reinstalled it again and nothing appeard , i change also cap = cv2.VideoCapture("http://192.168.0.103:8080/video") to a video path not url and the same problem .