-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdistance_to_person.py
62 lines (49 loc) · 2.94 KB
/
distance_to_person.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# Let's consolidate the explanation into a single Python script that demonstrates how to detect a person
# and estimate the distance from the camera using OpenCV. This script will rely on a simplistic approach
# for distance estimation based on the size of the detected person in the frame. This method is highly
# dependent on the camera's field of view and requires prior calibration to achieve reasonable accuracy.
# Note: This script does not incorporate depth sensors or stereo vision for actual distance measurement.
# Import necessary libraries
import cv2
import numpy as np
# Load a pre-trained model for person detection. For simplicity, we use a Haar Cascade for face detection,
# although for full-body detection in a more complex scenario, you would use models like YOLO or SSD with OpenCV's DNN module.
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
# Function to estimate distance from the camera to the person based on the size of the detected face
# This function needs calibration with known distances and object sizes to be accurate
def estimate_distance(frame_width, detected_width):
# Placeholder function for distance estimation based on the width of the detected face
# This requires calibration and conversion based on the camera's field of view and the actual size of a human face
# Here, we simply use a placeholder conversion factor (which you need to calibrate)
conversion_factor = 0.05 # This is a made-up value for demonstration; you must calibrate this value
distance = (frame_width / detected_width) * conversion_factor
return distance
# Initialize video capture
cap = cv2.VideoCapture(0)
while True:
# Capture frame-by-frame
ret, frame = cap.read()
if not ret:
break
# Convert frame to grayscale for face detection
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Detect faces in the frame
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
# Loop over all detected faces
for (x, y, w, h) in faces:
# Draw a rectangle around the face
cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
# Estimate distance and display it
distance = estimate_distance(frame.shape[1], w)
cv2.putText(frame, f"Distance: {distance:.2f} units", (x, y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)
# Display the resulting frame
cv2.imshow('Frame', frame)
# Break the loop on 'q' press
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything is done, release the capture
cap.release()
cv2.destroyAllWindows()
# Remember, this code needs actual calibration to provide meaningful distance measurements.
# The distance estimation function provided is highly simplified and serves as a placeholder for a more complex calibration process.
# For more accurate distance measurements, consider using depth sensors or stereo vision techniques, which require different hardware and software setups.