JeVoisBase  1.21
JeVois Smart Embedded Machine Vision Toolkit Base Modules
Share this page:
Loading...
Searching...
No Matches
PyObjectron.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## 3D object detection using MediaPipe
9#
10# Detect objects and draw estimated 3D bounding boxes, using MediaPipe in Python
11#
12# Only works for a few pre-trained objects: 'Shoe', 'Chair', 'Cup', 'Camera', with Shoe selected by default. So
13# point the camera to your shoes and see what happens...
14#
15# This code is derived from sample_objectron.py at https://github.com/Kazuhito00/mediapipe-python-sample
16#
17# @author Laurent Itti
18#
19# @videomapping JVUI 0 0 30.0 CropScale=RGB24@512x288:YUYV 1920 1080 30.0 JeVois PyObjectron
20# @email itti\@usc.edu
21# @address University of Southern California, HNB-07A, 3641 Watt Way, Los Angeles, CA 90089-2520, USA
22# @copyright Copyright (C) 2022 by Laurent Itti, iLab and the University of Southern California
23# @mainurl http://jevois.org
24# @supporturl http://jevois.org/doc
25# @otherurl http://iLab.usc.edu
26# @license GPL v3
27# @distribution Unrestricted
28# @restrictions None
29# @ingroup modules
31 # ###################################################################################################
32 ## Constructor
33 def __init__(self):
34 # Parameters:
35 max_num_objects = 5
36 min_detection_confidence = 0.5
37 min_tracking_confidence = 0.99
38 self.model_name = 'Shoe' # 'Shoe', 'Chair', 'Cup', 'Camera'
39
40 # Instantiate a JeVois Timer to measure our processing framerate:
41 self.timer = jevois.Timer("objectron", 30, jevois.LOG_DEBUG)
42
43 # Instantiate mediapipe model:
44 self.mp_objectron = mp.solutions.objectron
45 self.objectron = self.mp_objectron.Objectron(
46 static_image_mode = False,
47 max_num_objects = max_num_objects,
48 min_detection_confidence = min_detection_confidence,
49 min_tracking_confidence = min_tracking_confidence,
50 model_name = self.model_name)
51
52 # ###################################################################################################
53 ## Process function with GUI output
54 def processGUI(self, inframe, helper):
55 # Start a new display frame, gets its size and also whether mouse/keyboard are idle:
56 idle, winw, winh = helper.startFrame()
57
58 # Draw full-resolution input frame from camera:
59 x, y, w, h = helper.drawInputFrame("c", inframe, False, False)
60 helper.itext('JeVois-Pro 3D Object Detection', 0, -1)
61 helper.itext('Detecting: ' + self.model_name + ' - edit source code to change')
62
63 # Get the next camera image at processing resolution (may block until it is captured):
64 image = inframe.getCvRGBp()
65 iw, ih = image.shape[1], image.shape[0]
66
67 # Start measuring image processing time:
68 self.timer.start()
69
70 # Run graph:
71 results = self.objectron.process(image)
72
73 # Draw results:
74 if results.detected_objects is not None:
75 for detected_object in results.detected_objects:
76 self.draw_landmarks(helper, iw, ih, detected_object.landmarks_2d)
77 self.draw_axis(helper, iw, ih, detected_object.rotation, detected_object.translation, 0.1)
78
79 # Write frames/s info from our timer:
80 fps = self.timer.stop()
81 helper.iinfo(inframe, fps, winw, winh);
82
83 # End of frame:
84 helper.endFrame()
85
86
87 # ###################################################################################################
88 def draw_landmarks(self, helper, iw, ih, landmarks):
89 col = 0xff00ff00
90 idx_to_coordinates = {}
91
92 for index, landmark in enumerate(landmarks.landmark):
93 lm = helper.i2d(landmark.x * iw, landmark.y * ih, "c")
94 helper.drawCircle(lm.x, lm.y, 5, col, True)
95 idx_to_coordinates[index] = lm
96
97 # See https://github.com/google/mediapipe/blob/master/mediapipe/modules/objectron/calculators/box.h
98 # for the 8 vertex locations. Code here derived from mediapipe/python/solutions/drawing_utils.py
99 connections = self.mp_objectron.BOX_CONNECTIONS
100 if connections:
101 num_landmarks = len(landmarks.landmark)
102 for connection in connections:
103 start_idx = connection[0]
104 end_idx = connection[1]
105 if not (0 <= start_idx < num_landmarks and 0 <= end_idx < num_landmarks):
106 raise ValueError(f'Landmark index is out of range. Invalid connection '
107 f'from landmark #{start_idx} to landmark #{end_idx}.')
108 if start_idx in idx_to_coordinates and end_idx in idx_to_coordinates:
109 helper.drawLine(idx_to_coordinates[start_idx].x, idx_to_coordinates[start_idx].y,
110 idx_to_coordinates[end_idx].x, idx_to_coordinates[end_idx].y, col)
111
112 # ###################################################################################################
113 def draw_axis(self, helper, iw, ih, rotation, translation, axis_length):
114 focal_length = (1.0, 1.0)
115 principal_point = (0.0, 0.0)
116 axis_world = np.float32([[0, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 1]])
117 axis_cam = np.matmul(rotation, axis_length * axis_world.T).T + translation
118 x = axis_cam[..., 0]
119 y = axis_cam[..., 1]
120 z = axis_cam[..., 2]
121
122 # Project 3D points to NDC space.
123 fx, fy = focal_length
124 px, py = principal_point
125 x_ndc = np.clip(-fx * x / (z + 1e-5) + px, -1., 1.)
126 y_ndc = np.clip(-fy * y / (z + 1e-5) + py, -1., 1.)
127
128 # Convert from NDC space to image space.
129 x_im = (1 + x_ndc) * 0.5 * iw
130 y_im = (1 - y_ndc) * 0.5 * ih
131
132 # Draw, converting coords from low-res processing frame to full-res display frame:
133 orig = helper.i2d(x_im[0], y_im[0], "c")
134 xa = helper.i2d(x_im[1], y_im[1], "c")
135 ya = helper.i2d(x_im[2], y_im[2], "c")
136 za = helper.i2d(x_im[3], y_im[3], "c")
137 helper.drawLine(orig.x, orig.y, xa.x, xa.y, 0xff0000ff)
138 helper.drawLine(orig.x, orig.y, ya.x, ya.y, 0xff00ff00)
139 helper.drawLine(orig.x, orig.y, za.x, za.y, 0xffff0000)
140
3D object detection using MediaPipe
__init__(self)
Constructor.
processGUI(self, inframe, helper)
Process function with GUI output.
draw_axis(self, helper, iw, ih, rotation, translation, axis_length)
draw_landmarks(self, helper, iw, ih, landmarks)