Detecting People Crossing a Line with OpenCV in Python
The task of detecting people crossing a line has various applications in security, surveillance, and traffic management. OpenCV, a powerful image processing library, can be effectively utilized to achieve this. In this article, we will explore how to set up and implement such a system using Python and OpenCV. Specifically, we will walk through the steps to detect a line and track when a person crosses it.
Understanding the Line Detection
To create a crossing line, it can be done in two ways: either manually or by detecting a line automatically. This line, typically seen as a red line in most YouTube videos, acts as a trigger for detecting when a person crosses it. The line can be a straight horizontal or vertical line, or even a polygonal shape depending on the application.
Manual Line Drawing
One of the simplest ways to create a crossing line is by drawing it manually. You can use tools such as AspectIO’s Image Line Tool or all drawing tools available in any image editing software. Drawing the line in the correct position based on your frame size and resolution is crucial. Once the line is drawn, it can be converted into a binary image and then inspected for any crossing activities.
Automated Line Detection
For a more dynamic use case, automated line detection using OpenCV can be employed. This can be achieved through line detection algorithms like Hough Line Transform or edge detection methods. Here’s a step-by-step guide to implementing automated line detection:
Step 1: Load the Image and Convert to Grayscale
Before any processing can be done, the image needs to be loaded and converted to grayscale. Grayscale images are easier for edge detection and line detection algorithms.
import cv2import numpy as np# Load the imageimage ('input_')gray_image (image, _BGR2GRAY)
Step 2: Apply Edge Detection
Use the Canny edge detector to find the edges in the image. Edges are useful in identifying the line that the person is likely to cross.
canny_edges (gray_image, 50, 150)
Step 3: Apply Line Detection
Use the Hough Line Transform to detect lines in the image. The Hough Line Transform works well for detecting straight lines.
lines cv2.HoughLinesP(canny_edges, 1, np.pi / 180, 100, minLineLength100, maxLineGap10)
Step 4: Find the Line Closest to the Desired Position
Since the line might not be perfectly oriented, you can use additional pre-processing steps to identify the line that best corresponds to the crossing line.
Tracking Person Crossing the Line
Once the line is created and detected, the next step is to track people crossing this line. This can be done using object detection and tracking techniques. The YOLO (You Only Look Once) object detection framework, along with tracking algorithms like the Sort algorithm, can be used for accurate person detection and tracking.
Step 5: Tracking with YOLO
YOLO is a popular choice for object detection and can efficiently detect people in real-time videos. Here’s how you can utilize YOLO for person detection:
import cv2import numpy as npdef load_yolo(): net ("yolov4.weights", "") with open("", "r") as f: classes [() for line in ()] layer_names () output_layers [layer_names[i[0] - 1] for i in ()] return net, classes, output_layersdef detect_people(frame): net, classes, output_layers load_yolo() height, width, _ blob (frame, 1 / 255, (416, 416), swapRBTrue, cropFalse) (blob) outputs (output_layers) boxes [] confidences [] for output in outputs: for detection in output: scores detection[5:] class_id (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) ([x, y, w, h]) (float(confidence)) indices cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4) people [] for i in indices: i i[0] box boxes[i] (box) return peoplepeople detect_people(image)
Step 6: Tracking with Sort Algorithm
The Sort algorithm can then be used to track the people as they cross the line.
from sort import *tracker Sort()def track_people(frame, boxes): trackers tracker.update(boxes) for track in trackers: x, y, w, h, track_id track (frame, (x, y), (x w, y h), (0, 255, 0), 2) cv2.putText(frame, str(track_id), (x, y - 10), _HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2) return frametracked_image track_people(image, people)
Conclusion
Detecting people crossing a line using OpenCV in Python is a versatile task that can be achieved through various methods. Whether it is manual drawing or automated detection, the key is to use appropriate image processing techniques to identify the crossing line and then track people accurately. OpenCV, combined with object detection and tracking algorithms, provides a powerful solution for this use case. By implementing these techniques, you can build a robust system for pedestrian detection and tracking in a variety of applications.
Related Keywords
OpenCV, Python, Pedestrian Detection