JeVoisBase  1.22
JeVois Smart Embedded Machine Vision Toolkit Base Modules
Share this page:
Loading...
Searching...
No Matches
PyPoseDetector.py
Go to the documentation of this file.
1import pyjevois
2if pyjevois.pro: import libjevoispro as jevois
3else: import libjevois as jevois
4import cv2
5import numpy as np
6import mediapipe as mp
7
8## Human body pose detection using MediaPipe
9#
10# Detect human body pose using MediaPipe in Python
11#
12# This code is derived from sample_pose.py at https://github.com/Kazuhito00/mediapipe-python-sample
13#
14# @author Laurent Itti
15#
16# @videomapping JVUI 0 0 30.0 CropScale=RGB24@512x288:YUYV 1920 1080 30.0 JeVois PyPoseDetector
17# @email itti\@usc.edu
18# @address University of Southern California, HNB-07A, 3641 Watt Way, Los Angeles, CA 90089-2520, USA
19# @copyright Copyright (C) 2021 by Laurent Itti, iLab and the University of Southern California
20# @mainurl http://jevois.org
21# @supporturl http://jevois.org/doc
22# @otherurl http://iLab.usc.edu
23# @license GPL v3
24# @distribution Unrestricted
25# @restrictions None
26# @ingroup modules
28 # ###################################################################################################
29 ## Constructor
30 def __init__(self):
31 # Instantiate a JeVois Timer to measure our processing framerate:
32 self.timer = jevois.Timer("pose", 100, jevois.LOG_INFO)
33
34 # Instantiate mediapipe pose detector. model_complexity should be 0, 1, or 2:
35 self.mp_pose = mp.solutions.pose
36 self.pose = self.mp_pose.Pose(model_complexity = 1, min_detection_confidence = 0.5,
37 min_tracking_confidence = 0.5)
38 self.use_brect = False; # true to show a bounding rectangle around each hand
39
40 # ###################################################################################################
41 ## Process function with GUI output
42 def processGUI(self, inframe, helper):
43 # Start a new display frame, gets its size and also whether mouse/keyboard are idle:
44 idle, winw, winh = helper.startFrame()
45
46 # Draw full-resolution input frame from camera:
47 x, y, w, h = helper.drawInputFrame("c", inframe, False, False)
48 helper.itext('JeVois-Pro Body Pose Skeleton Detection')
49
50 # Get the next camera image at processing resolution (may block until it is captured):
51 image = inframe.getCvRGBp()
52 iw, ih = image.shape[1], image.shape[0]
53
54 # Start measuring image processing time:
55 self.timer.start()
56
57 # Detect pose:
58 results = self.pose.process(image)
59
60 # Draw results:
61 if results.pose_landmarks is not None:
62 draw_landmarks(helper, iw, ih, results.pose_landmarks, visibility_th = 0.5)
63 if self.use_brect:
64 brect = calc_bounding_rect(iw, ih, results.pose_landmarks)
65 helper.drawRect(brect[0], brect[1], brect[2], brect[3], 0x6040ffff, True)
66
67 # Write frames/s info from our timer:
68 fps = self.timer.stop()
69 helper.iinfo(inframe, fps, winw, winh);
70
71 # End of frame:
72 helper.endFrame()
73
74# ###################################################################################################
75def calc_bounding_rect(iw, ih, landmarks):
76 landmark_array = np.empty((0, 2), int)
77
78 for _, landmark in enumerate(landmarks.landmark):
79 landmark_x = min(int(landmark.x * iw), iw - 1)
80 landmark_y = min(int(landmark.y * ih), ih - 1)
81 landmark_point = [np.array((landmark_x, landmark_y))]
82 landmark_array = np.append(landmark_array, landmark_point, axis=0)
83 x, y, w, h = cv2.boundingRect(landmark_array)
84 return [x, y, x + w, y + h]
85
86# ###################################################################################################
87def draw_line(helper, lp1, lp2, visibility_th, col):
88 if lp1[0] > visibility_th and lp2[0] > visibility_th:
89 helper.drawLine(lp1[1][0], lp1[1][1], lp2[1][0], lp2[1][1], col)
90
91# ###################################################################################################
92def draw_landmarks(helper, iw, ih, landmarks, visibility_th):
93 lp = []
94 col = 0xff00ff00
95
96 for index, landmark in enumerate(landmarks.landmark):
97 landmark_x = min(int(landmark.x * iw), iw - 1)
98 landmark_y = min(int(landmark.y * ih), ih - 1)
99 landmark_z = landmark.z
100 lp.append([landmark.visibility, (landmark_x, landmark_y)])
101
102 if landmark.visibility >= visibility_th:
103 helper.drawCircle(landmark_x, landmark_y, 5, col, True)
104 helper.drawText(landmark_x - 7, landmark_y - 7, "z:" + str(round(landmark_z, 3)), 0xffff8080)
105
106 if len(lp) > 0:
107 draw_line(helper, lp[1], lp[2], visibility_th, col)
108 draw_line(helper, lp[2], lp[3], visibility_th, col)
109 draw_line(helper, lp[4], lp[5], visibility_th, col)
110 draw_line(helper, lp[5], lp[6], visibility_th, col)
111 draw_line(helper, lp[9], lp[10], visibility_th, col)
112 draw_line(helper, lp[11], lp[12], visibility_th, col)
113 draw_line(helper, lp[11], lp[13], visibility_th, col)
114 draw_line(helper, lp[13], lp[15], visibility_th, col)
115 draw_line(helper, lp[12], lp[14], visibility_th, col)
116 draw_line(helper, lp[14], lp[16], visibility_th, col)
117 draw_line(helper, lp[15], lp[17], visibility_th, col)
118 draw_line(helper, lp[17], lp[19], visibility_th, col)
119 draw_line(helper, lp[19], lp[21], visibility_th, col)
120 draw_line(helper, lp[21], lp[15], visibility_th, col)
121 draw_line(helper, lp[16], lp[18], visibility_th, col)
122 draw_line(helper, lp[18], lp[20], visibility_th, col)
123 draw_line(helper, lp[20], lp[22], visibility_th, col)
124 draw_line(helper, lp[22], lp[16], visibility_th, col)
125 draw_line(helper, lp[11], lp[23], visibility_th, col)
126 draw_line(helper, lp[12], lp[24], visibility_th, col)
127 draw_line(helper, lp[23], lp[24], visibility_th, col)
128
129 if len(lp) > 25:
130 draw_line(helper, lp[23], lp[25], visibility_th, col)
131 draw_line(helper, lp[25], lp[27], visibility_th, col)
132 draw_line(helper, lp[27], lp[29], visibility_th, col)
133 draw_line(helper, lp[29], lp[31], visibility_th, col)
134 draw_line(helper, lp[24], lp[26], visibility_th, col)
135 draw_line(helper, lp[26], lp[28], visibility_th, col)
136 draw_line(helper, lp[28], lp[30], visibility_th, col)
137 draw_line(helper, lp[30], lp[32], visibility_th, col)
Human body pose detection using MediaPipe.
processGUI(self, inframe, helper)
Process function with GUI output.
draw_landmarks(helper, iw, ih, landmarks, visibility_th)
draw_line(helper, lp1, lp2, visibility_th, col)
calc_bounding_rect(iw, ih, landmarks)