JeVoisBase  1.18
JeVois Smart Embedded Machine Vision Toolkit Base Modules
Share this page:
PyPoseDetector.py
Go to the documentation of this file.
1 import pyjevois
2 if pyjevois.pro: import libjevoispro as jevois
3 else: import libjevois as jevois
4 import cv2
5 import numpy as np
6 import 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 
49  # Get the next camera image at processing resolution (may block until it is captured):
50  image = inframe.getCvRGBp()
51  iw, ih = image.shape[1], image.shape[0]
52 
53  # Start measuring image processing time:
54  self.timer.start()
55 
56  # Detect pose:
57  results = self.pose.process(image)
58 
59  # Draw results:
60  if results.pose_landmarks is not None:
61  draw_landmarks(helper, iw, ih, results.pose_landmarks, visibility_th = 0.5)
62  if self.use_brect:
63  brect = calc_bounding_rect(iw, ih, results.pose_landmarks)
64  helper.drawRect(brect[0], brect[1], brect[2], brect[3], 0x6040ffff, True)
65 
66  # Write frames/s info from our timer:
67  fps = self.timer.stop()
68  helper.iinfo(inframe, fps, winw, winh);
69 
70  # End of frame:
71  helper.endFrame()
72 
73 # ###################################################################################################
74 def calc_bounding_rect(iw, ih, landmarks):
75  landmark_array = np.empty((0, 2), int)
76 
77  for _, landmark in enumerate(landmarks.landmark):
78  landmark_x = min(int(landmark.x * iw), iw - 1)
79  landmark_y = min(int(landmark.y * ih), ih - 1)
80  landmark_point = [np.array((landmark_x, landmark_y))]
81  landmark_array = np.append(landmark_array, landmark_point, axis=0)
82  x, y, w, h = cv2.boundingRect(landmark_array)
83  return [x, y, x + w, y + h]
84 
85 # ###################################################################################################
86 def draw_line(helper, lp1, lp2, visibility_th, col):
87  if lp1[0] > visibility_th and lp2[0] > visibility_th:
88  helper.drawLine(lp1[1][0], lp1[1][1], lp2[1][0], lp2[1][1], col)
89 
90 # ###################################################################################################
91 def draw_landmarks(helper, iw, ih, landmarks, visibility_th):
92  lp = []
93  col = 0xff00ff00
94 
95  for index, landmark in enumerate(landmarks.landmark):
96  landmark_x = min(int(landmark.x * iw), iw - 1)
97  landmark_y = min(int(landmark.y * ih), ih - 1)
98  landmark_z = landmark.z
99  lp.append([landmark.visibility, (landmark_x, landmark_y)])
100 
101  if landmark.visibility >= visibility_th:
102  helper.drawCircle(landmark_x, landmark_y, 5, col, True)
103  helper.drawText(landmark_x - 7, landmark_y - 7, "z:" + str(round(landmark_z, 3)), 0xffff8080)
104 
105  if len(lp) > 0:
106  draw_line(helper, lp[1], lp[2], visibility_th, col)
107  draw_line(helper, lp[2], lp[3], visibility_th, col)
108  draw_line(helper, lp[4], lp[5], visibility_th, col)
109  draw_line(helper, lp[5], lp[6], visibility_th, col)
110  draw_line(helper, lp[9], lp[10], visibility_th, col)
111  draw_line(helper, lp[11], lp[12], visibility_th, col)
112  draw_line(helper, lp[11], lp[13], visibility_th, col)
113  draw_line(helper, lp[13], lp[15], visibility_th, col)
114  draw_line(helper, lp[12], lp[14], visibility_th, col)
115  draw_line(helper, lp[14], lp[16], visibility_th, col)
116  draw_line(helper, lp[15], lp[17], visibility_th, col)
117  draw_line(helper, lp[17], lp[19], visibility_th, col)
118  draw_line(helper, lp[19], lp[21], visibility_th, col)
119  draw_line(helper, lp[21], lp[15], visibility_th, col)
120  draw_line(helper, lp[16], lp[18], visibility_th, col)
121  draw_line(helper, lp[18], lp[20], visibility_th, col)
122  draw_line(helper, lp[20], lp[22], visibility_th, col)
123  draw_line(helper, lp[22], lp[16], visibility_th, col)
124  draw_line(helper, lp[11], lp[23], visibility_th, col)
125  draw_line(helper, lp[12], lp[24], visibility_th, col)
126  draw_line(helper, lp[23], lp[24], visibility_th, col)
127 
128  if len(lp) > 25:
129  draw_line(helper, lp[23], lp[25], visibility_th, col)
130  draw_line(helper, lp[25], lp[27], visibility_th, col)
131  draw_line(helper, lp[27], lp[29], visibility_th, col)
132  draw_line(helper, lp[29], lp[31], visibility_th, col)
133  draw_line(helper, lp[24], lp[26], visibility_th, col)
134  draw_line(helper, lp[26], lp[28], visibility_th, col)
135  draw_line(helper, lp[28], lp[30], visibility_th, col)
136  draw_line(helper, lp[30], lp[32], visibility_th, col)
demo.str
str
Definition: demo.py:35
demo.int
int
Definition: demo.py:37
PyPoseDetector.calc_bounding_rect
def calc_bounding_rect(iw, ih, landmarks)
Definition: PyPoseDetector.py:74
PyPoseDetector.draw_landmarks
def draw_landmarks(helper, iw, ih, landmarks, visibility_th)
Definition: PyPoseDetector.py:91
PyPoseDetector.PyPoseDetector.use_brect
use_brect
Definition: PyPoseDetector.py:38
PyPoseDetector.PyPoseDetector.__init__
def __init__(self)
Constructor.
Definition: PyPoseDetector.py:30
PyPoseDetector.draw_line
def draw_line(helper, lp1, lp2, visibility_th, col)
Definition: PyPoseDetector.py:86
PyPoseDetector.PyPoseDetector
Human body pose detection using MediaPipe.
Definition: PyPoseDetector.py:27
PyPoseDetector.PyPoseDetector.processGUI
def processGUI(self, inframe, helper)
Process function with GUI output.
Definition: PyPoseDetector.py:42
PyPoseDetector.PyPoseDetector.mp_pose
mp_pose
Definition: PyPoseDetector.py:35
PyPoseDetector.PyPoseDetector.pose
pose
Definition: PyPoseDetector.py:36
PyPoseDetector.PyPoseDetector.timer
timer
Definition: PyPoseDetector.py:32
jevois::Timer