-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpose_detection.py
More file actions
60 lines (48 loc) · 2.19 KB
/
Copy pathpose_detection.py
File metadata and controls
60 lines (48 loc) · 2.19 KB
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
import cv2
import mediapipe as mp
from mediapipe.tasks.python import vision
from landmarker_result import LandmarkerResult
import time
# Initialize live camera feed with openCV
capture = cv2.VideoCapture(0)#TODO : change the index if you have multiple cameras
if not capture.isOpened():
print("Error: Could not open camera.")
exit()
#initializing task for model
BaseOptions = mp.tasks.BaseOptions
PoseLandmarker = mp.tasks.vision.PoseLandmarker
PoseLandmarkerOptions = mp.tasks.vision.PoseLandmarkerOptions
PoseLandmarkerResult = mp.tasks.vision.PoseLandmarkerResult
VisionRunningMode = mp.tasks.vision.RunningMode
landmarkerResult = LandmarkerResult(True)
options = PoseLandmarkerOptions(
base_options=BaseOptions(model_asset_path="pose_landmarker_lite.task"),
running_mode=VisionRunningMode.LIVE_STREAM,
result_callback=landmarkerResult.callbackResult)
# Create a pose landmarker instance with the live stream mode:
with PoseLandmarker.create_from_options(options) as landmarker:
#Start the camera feed loop
while capture.isOpened():
# Use time.time() for the timestamp as capture.get() can sometimes return 0 on webcams
timestamp = int(time.time() * 1000)
# Capture frame-by-frame
ret, cameraFeed = capture.read()
cameraFeed = cv2.resize(cameraFeed, (860,640))
h,w, _ = cameraFeed.shape
#coloring conversion for mediapipe
feedForDetection = cv2.cvtColor(cameraFeed, cv2.COLOR_BGR2RGB)
#Transform the OpenCV image to mediapipe image format
mp_image = mp.Image(image_format=mp.ImageFormat.SRGB, data=feedForDetection)
# Use the landmarker to detect poses in the input camera feed.
landmarker.detect_async(mp_image, timestamp)
print('is pose detected:', landmarkerResult.isPoseDetected())
print(f'Detected Pose: {landmarkerResult.detectedPose}\n')
# Display the resulting frame
landmarkerResult.drawResult(cameraFeed, h, w)
cv2.imshow('Camera Feed', cameraFeed)
#exit on 'q' key press
if cv2.waitKey(1) == ord('q'):
break
# When everything done, release the capture
capture.release()
cv2.destroyAllWindows()