-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVideo_Face_Body_Hand_Detection.py
76 lines (57 loc) · 2.51 KB
/
Video_Face_Body_Hand_Detection.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import cv2
import mediapipe as mp
# Initialize MediaPipe holistic model for face, body, hand, and pose detection
mp_holistic = mp.solutions.holistic
holistic = mp_holistic.Holistic(min_detection_confidence=0.2, min_tracking_confidence=0.2)
# Open video stream
cap = cv2.VideoCapture('Enter_the_location_of_the_video,mp4')
# Skeleton position
sklet_x, sklet_y = 50, 50
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
# Process face, body, hand, and pose detection using MediaPipe
results = holistic.process(frame)
# Add a black background
frame[:] = [0, 0, 0]
# Uncomment the following line to display the skeleton
# cv2.putText(frame, 'Skeleton', (sklet_x, sklet_y), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
# Draw detected faces
if results.face_landmarks:
for landmark in results.face_landmarks.landmark:
ih, iw, _ = frame.shape
x, y = int(landmark.x * iw), int(landmark.y * ih)
cv2.circle(frame, (x, y), 2, (0, 255, 0), -1)
# Draw detected body landmarks
if results.pose_landmarks:
for landmark in results.pose_landmarks.landmark:
ih, iw, _ = frame.shape
x, y = int(landmark.x * iw), int(landmark.y * ih)
cv2.circle(frame, (x, y), 2, (255, 0, 0), -1)
# Draw detected hand landmarks
if results.left_hand_landmarks:
for landmark in results.left_hand_landmarks.landmark:
ih, iw, _ = frame.shape
x, y = int(landmark.x * iw), int(landmark.y * ih)
cv2.circle(frame, (x, y), 2, (0, 0, 255), -1)
if results.right_hand_landmarks:
for landmark in results.right_hand_landmarks.landmark:
ih, iw, _ = frame.shape
x, y = int(landmark.x * iw), int(landmark.y * ih)
cv2.circle(frame, (x, y), 2, (0, 0, 255), -1)
# Draw body connections
if results.pose_landmarks:
# Connect the anatomical landmarks
mp_drawing = mp.solutions.drawing_utils
mp_drawing.draw_landmarks(frame, results.pose_landmarks, mp_holistic.POSE_CONNECTIONS)
# Draw connections for hands and body
mp_drawing.draw_landmarks(frame, results.right_hand_landmarks, mp_holistic.HAND_CONNECTIONS)
mp_drawing.draw_landmarks(frame, results.left_hand_landmarks, mp_holistic.HAND_CONNECTIONS)
# Display on the screen
cv2.imshow('Holistic Detection', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release resources
cap.release()
cv2.destroyAllWindows()