Spaces:
Runtime error
Runtime error
added code for gesture recognition
Browse files- .gitattributes +1 -0
- app.py +40 -11
- models/gesture_recognizer.task +3 -0
.gitattributes
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
*.task filter=lfs diff=lfs merge=lfs -text
|
app.py
CHANGED
@@ -4,37 +4,66 @@ import gradio as gr
|
|
4 |
import mediapipe as mp
|
5 |
import numpy as np
|
6 |
|
7 |
-
mp_drawing = mp.solutions.drawing_utils
|
8 |
-
mp_selfie_segmentation = mp.solutions.selfie_segmentation
|
9 |
|
10 |
-
seg =
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
|
15 |
def close_segmentation_model():
|
16 |
-
print("Closing
|
17 |
seg.close()
|
|
|
18 |
|
19 |
|
20 |
-
def
|
21 |
image = cv2.cvtColor(video, cv2.COLOR_RGB2BGR)
|
22 |
results = seg.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
|
23 |
condition = np.stack((results.segmentation_mask,) * 3, axis=-1) > 0.5
|
24 |
|
25 |
bg_image = np.zeros(image.shape, dtype=np.uint8)
|
26 |
-
bg_image[:] =
|
27 |
output_image = np.where(condition, video, bg_image)
|
28 |
|
29 |
return output_image
|
30 |
|
31 |
|
32 |
-
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
inputs=gr.Image(sources=["webcam"], streaming=True),
|
35 |
outputs="image",
|
36 |
live=True,
|
37 |
)
|
38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
if __name__ == "__main__":
|
40 |
-
|
|
|
4 |
import mediapipe as mp
|
5 |
import numpy as np
|
6 |
|
|
|
|
|
7 |
|
8 |
+
seg = mp.solutions.selfie_segmentation.SelfieSegmentation()
|
9 |
+
options = mp.tasks.vision.GestureRecognizerOptions(
|
10 |
+
base_options=mp.tasks.BaseOptions(
|
11 |
+
model_asset_path="models/gesture_recognizer.task"
|
12 |
+
),
|
13 |
+
running_mode=mp.tasks.vision.RunningMode.IMAGE,
|
14 |
+
)
|
15 |
+
recognizer = mp.tasks.vision.GestureRecognizer.create_from_options(options)
|
16 |
|
17 |
|
18 |
def close_segmentation_model():
|
19 |
+
print("Closing Models")
|
20 |
seg.close()
|
21 |
+
recognizer.close()
|
22 |
|
23 |
|
24 |
+
def process_selfie(video):
|
25 |
image = cv2.cvtColor(video, cv2.COLOR_RGB2BGR)
|
26 |
results = seg.process(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
|
27 |
condition = np.stack((results.segmentation_mask,) * 3, axis=-1) > 0.5
|
28 |
|
29 |
bg_image = np.zeros(image.shape, dtype=np.uint8)
|
30 |
+
bg_image[:] = (0, 0, 0)
|
31 |
output_image = np.where(condition, video, bg_image)
|
32 |
|
33 |
return output_image
|
34 |
|
35 |
|
36 |
+
def process_gesture(video):
|
37 |
+
mp_image = mp.Image(
|
38 |
+
image_format=mp.ImageFormat.SRGB, data=cv2.cvtColor(video, cv2.COLOR_BGR2RGB)
|
39 |
+
)
|
40 |
+
result = recognizer.recognize(mp_image)
|
41 |
+
print(result.gestures)
|
42 |
+
if not result.gestures:
|
43 |
+
gesture_labels = {"None": 1.0}
|
44 |
+
else:
|
45 |
+
gesture_labels = {i.category_name: i.score for i in result.gestures[0]}
|
46 |
+
|
47 |
+
return gesture_labels
|
48 |
+
|
49 |
+
|
50 |
+
selfie_interface = gr.Interface(
|
51 |
+
fn=process_selfie,
|
52 |
inputs=gr.Image(sources=["webcam"], streaming=True),
|
53 |
outputs="image",
|
54 |
live=True,
|
55 |
)
|
56 |
|
57 |
+
gesture_interface = gr.Interface(
|
58 |
+
fn=process_gesture,
|
59 |
+
inputs=gr.Image(sources=["webcam"], streaming=True),
|
60 |
+
outputs="label",
|
61 |
+
live=True,
|
62 |
+
)
|
63 |
+
|
64 |
+
interface = gr.TabbedInterface(
|
65 |
+
[selfie_interface, gesture_interface], ["Selfie", "Gesture"]
|
66 |
+
)
|
67 |
+
|
68 |
if __name__ == "__main__":
|
69 |
+
interface.launch()
|
models/gesture_recognizer.task
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:97952348cf6a6a4915c2ea1496b4b37ebabc50cbbf80571435643c455f2b0482
|
3 |
+
size 8373440
|