Spaces:
Runtime error
Runtime error
Mohammed Innat
commited on
Commit
•
890c650
1
Parent(s):
d8b2189
Upload objectron3d.py
Browse files- objectron3d.py +38 -0
objectron3d.py
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import mediapipe as mp
|
2 |
+
from utils import read_n_resize
|
3 |
+
|
4 |
+
mp_objectron = mp.solutions.objectron
|
5 |
+
mp_drawing = mp.solutions.drawing_utils
|
6 |
+
|
7 |
+
def detect_in_3d(image, model_name='Chair'):
|
8 |
+
with mp_objectron.Objectron(
|
9 |
+
static_image_mode=True,
|
10 |
+
max_num_objects=5,
|
11 |
+
min_detection_confidence=0.35,
|
12 |
+
model_name=model_name
|
13 |
+
) as objectron:
|
14 |
+
results = objectron.process(read_n_resize(image, read=False))
|
15 |
+
return results
|
16 |
+
|
17 |
+
def draw_3d(results, image):
|
18 |
+
annotated_image = image.copy()
|
19 |
+
for detected_object in results.detected_objects:
|
20 |
+
mp_drawing.draw_landmarks(
|
21 |
+
annotated_image,
|
22 |
+
detected_object.landmarks_2d,
|
23 |
+
mp_objectron.BOX_CONNECTIONS
|
24 |
+
)
|
25 |
+
mp_drawing.draw_axis(
|
26 |
+
annotated_image,
|
27 |
+
detected_object.rotation,
|
28 |
+
detected_object.translation
|
29 |
+
)
|
30 |
+
return annotated_image
|
31 |
+
|
32 |
+
|
33 |
+
def mp_objectron_fn(image, min_detect_conf=0.5):
|
34 |
+
for model_name in ['Chair', 'Shoe', 'Cup', 'Camera']:
|
35 |
+
results = detect_in_3d(image, model_name=model_name)
|
36 |
+
if results.detected_objects:
|
37 |
+
annotated_image = draw_3d(results, image)
|
38 |
+
return annotated_image
|