Spaces:
Runtime error
Runtime error
Update app.py
Browse fileschanged helped funcs and imported copy
app.py
CHANGED
@@ -5,7 +5,7 @@ from flax.training.common_utils import shard
|
|
5 |
from PIL import Image
|
6 |
from argparse import Namespace
|
7 |
import gradio as gr
|
8 |
-
|
9 |
import numpy as np
|
10 |
import mediapipe as mp
|
11 |
from mediapipe import solutions
|
@@ -18,39 +18,54 @@ from diffusers import (
|
|
18 |
FlaxControlNetModel,
|
19 |
FlaxStableDiffusionControlNetPipeline,
|
20 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
|
|
22 |
|
23 |
-
|
24 |
-
MARGIN = 10 # pixels
|
25 |
-
FONT_SIZE = 1
|
26 |
-
FONT_THICKNESS = 1
|
27 |
-
HANDEDNESS_TEXT_COLOR = (88, 205, 54) # vibrant green
|
28 |
-
|
29 |
-
def draw_landmarks_on_image(rgb_image, detection_result):
|
30 |
-
hand_landmarks_list = detection_result.hand_landmarks
|
31 |
-
handedness_list = detection_result.handedness
|
32 |
-
annotated_image = np.zeros_like(rgb_image)
|
33 |
-
|
34 |
-
# Loop through the detected hands to visualize.
|
35 |
-
for idx in range(len(hand_landmarks_list)):
|
36 |
-
hand_landmarks = hand_landmarks_list[idx]
|
37 |
-
handedness = handedness_list[idx]
|
38 |
-
|
39 |
-
# Draw the hand landmarks.
|
40 |
-
hand_landmarks_proto = landmark_pb2.NormalizedLandmarkList()
|
41 |
-
hand_landmarks_proto.landmark.extend([
|
42 |
-
landmark_pb2.NormalizedLandmark(x=landmark.x, y=landmark.y, z=landmark.z) for landmark in hand_landmarks
|
43 |
-
])
|
44 |
-
solutions.drawing_utils.draw_landmarks(
|
45 |
-
annotated_image,
|
46 |
-
hand_landmarks_proto,
|
47 |
-
solutions.hands.HAND_CONNECTIONS,
|
48 |
-
solutions.drawing_styles.get_default_hand_landmarks_style(),
|
49 |
-
solutions.drawing_styles.get_default_hand_connections_style())
|
50 |
-
|
51 |
-
return annotated_image
|
52 |
-
|
53 |
-
def generate_annotation(img):
|
54 |
"""img(input): numpy array
|
55 |
annotated_image(output): numpy array
|
56 |
"""
|
@@ -68,7 +83,7 @@ def generate_annotation(img):
|
|
68 |
detection_result = detector.detect(image)
|
69 |
|
70 |
# STEP 5: Process the classification result. In this case, visualize it.
|
71 |
-
annotated_image = draw_landmarks_on_image(image.numpy_view(), detection_result)
|
72 |
return annotated_image
|
73 |
|
74 |
args = Namespace(
|
|
|
5 |
from PIL import Image
|
6 |
from argparse import Namespace
|
7 |
import gradio as gr
|
8 |
+
import copy # added
|
9 |
import numpy as np
|
10 |
import mediapipe as mp
|
11 |
from mediapipe import solutions
|
|
|
18 |
FlaxControlNetModel,
|
19 |
FlaxStableDiffusionControlNetPipeline,
|
20 |
)
|
21 |
+
right_style_lm = copy.deepcopy(solutions.drawing_styles.get_default_hand_landmarks_style())
|
22 |
+
left_style_lm = copy.deepcopy(solutions.drawing_styles.get_default_hand_landmarks_style())
|
23 |
+
right_style_lm[0].color=(251, 206, 177)
|
24 |
+
left_style_lm[0].color=(255, 255, 225)
|
25 |
+
|
26 |
+
def draw_landmarks_on_image(rgb_image, detection_result, overlap=False, hand_encoding=False):
|
27 |
+
hand_landmarks_list = detection_result.hand_landmarks
|
28 |
+
handedness_list = detection_result.handedness
|
29 |
+
if overlap:
|
30 |
+
annotated_image = np.copy(rgb_image)
|
31 |
+
else:
|
32 |
+
annotated_image = np.zeros_like(rgb_image)
|
33 |
+
|
34 |
+
# Loop through the detected hands to visualize.
|
35 |
+
for idx in range(len(hand_landmarks_list)):
|
36 |
+
hand_landmarks = hand_landmarks_list[idx]
|
37 |
+
handedness = handedness_list[idx]
|
38 |
+
# Draw the hand landmarks.
|
39 |
+
hand_landmarks_proto = landmark_pb2.NormalizedLandmarkList()
|
40 |
+
hand_landmarks_proto.landmark.extend([
|
41 |
+
landmark_pb2.NormalizedLandmark(x=landmark.x, y=landmark.y, z=landmark.z) for landmark in hand_landmarks
|
42 |
+
])
|
43 |
+
if hand_encoding:
|
44 |
+
if handedness[0].category_name == "Left":
|
45 |
+
solutions.drawing_utils.draw_landmarks(
|
46 |
+
annotated_image,
|
47 |
+
hand_landmarks_proto,
|
48 |
+
solutions.hands.HAND_CONNECTIONS,
|
49 |
+
left_style_lm,
|
50 |
+
solutions.drawing_styles.get_default_hand_connections_style())
|
51 |
+
if handedness[0].category_name == "Right":
|
52 |
+
solutions.drawing_utils.draw_landmarks(
|
53 |
+
annotated_image,
|
54 |
+
hand_landmarks_proto,
|
55 |
+
solutions.hands.HAND_CONNECTIONS,
|
56 |
+
right_style_lm,
|
57 |
+
solutions.drawing_styles.get_default_hand_connections_style())
|
58 |
+
else:
|
59 |
+
solutions.drawing_utils.draw_landmarks(
|
60 |
+
annotated_image,
|
61 |
+
hand_landmarks_proto,
|
62 |
+
solutions.hands.HAND_CONNECTIONS,
|
63 |
+
solutions.drawing_styles.get_default_hand_landmarks_style(),
|
64 |
+
solutions.drawing_styles.get_default_hand_connections_style())
|
65 |
|
66 |
+
return annotated_image
|
67 |
|
68 |
+
def generate_annotation(img, overlap=False, hand_encoding=False):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
"""img(input): numpy array
|
70 |
annotated_image(output): numpy array
|
71 |
"""
|
|
|
83 |
detection_result = detector.detect(image)
|
84 |
|
85 |
# STEP 5: Process the classification result. In this case, visualize it.
|
86 |
+
annotated_image = draw_landmarks_on_image(image.numpy_view(), detection_result, overlap=overlap, hand_encoding=hand_encoding)
|
87 |
return annotated_image
|
88 |
|
89 |
args = Namespace(
|