brogelio commited on
Commit
812ac03
·
1 Parent(s): 273a096

Edited Interface

Browse files
Files changed (1) hide show
  1. app.py +44 -23
app.py CHANGED
@@ -1,12 +1,13 @@
1
  import cv2
2
  import numpy as np
3
  from PIL import Image
 
4
  import mediapipe as mp
5
  import time
6
  import gradio as gr
7
  import glob
8
 
9
- width_, height_, = 144, 96
10
 
11
  drawing_flag = False
12
  sleepy_time = time.time()
@@ -14,6 +15,20 @@ sleepy_time = time.time()
14
  output_frames = []
15
 
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  def find_hands(brain, img):
18
  if img is not None:
19
  img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # opencv image is in BGR form but mp is trained with RGB
@@ -103,25 +118,26 @@ def show(video, dominant_hand): # main
103
  # print(idx_coords)
104
  cv2.circle(rgb_image_f, idx_coords, 5, color, cv2.FILLED)
105
 
106
- if idx_coords[1] < 72: # brute force but should be extremely marginally faster lol
107
- if idx_coords[0] < 71: # red
108
- color = (0, 0, 255)
109
- if 71 < idx_coords[0] < 142: # orange
110
- color = (0, 115, 255)
111
- if 142 < idx_coords[0] < 213: # yellow
112
- color = (0, 229, 255)
113
- if 213 < idx_coords[0] < 284: # green
114
- color = (0, 195, 88)
115
- if 284 < idx_coords[0] < 356: # blue
116
- color = (195, 85, 0)
117
- if 356 < idx_coords[0] < 427: # indigo
118
- color = (195, 0, 68)
119
- if 427 < idx_coords[0] < 498: # violet
120
- color = (195, 0, 143)
121
- if 498 < idx_coords[0] < 569: # black
122
- color = (0, 0, 0)
123
- if 569 < idx_coords[0]: # white / eraser
124
- color = (255, 255, 255)
 
125
 
126
  if len(past_holder) and drawing_flag: # start drawing
127
  cv2.line(paper, past_holder, idx_coords, color, 5)
@@ -130,6 +146,7 @@ def show(video, dominant_hand): # main
130
  # paper[idx_coords[0]][idx_coords[1]][3] = 255
131
  cv2.circle(rgb_image_f, idx_coords, 5, color, cv2.FILLED)
132
 
 
133
  # if save(lm_list1) and time.time() - sleepy_time > 3: # save / output
134
  # paper[0:height_, w - width_: w] = 255 # presenter eraser
135
  # paper = cv2.cvtColor(paper, cv2.COLOR_BGR2RGB)
@@ -158,7 +175,6 @@ def show(video, dominant_hand): # main
158
 
159
  finally:
160
  if True:
161
- rgb_image_f[0:48, ] = palette # 48 small
162
  presenter = cv2.resize(rgb_image_f, (width_, height_))
163
  h, w, _ = rgb_image_f.shape
164
  paper[0:height_, w - width_: w] = presenter
@@ -189,8 +205,13 @@ title = 'Air Draw'
189
  desc = 'A Mediapipe Hands Wrapper for Drawing in the Air'
190
  iface = gr.Interface(
191
  fn=show,
192
- inputs=[gr.inputs.Video(source="webcam", label="Record from Webcam"), gr.inputs.Radio(['Right', 'Left'], label="Dominant Hand")],
193
- outputs='video', title=title,
 
 
 
 
 
194
  description=desc)
195
 
196
  iface.launch(share=True, enable_queue=True)
 
1
  import cv2
2
  import numpy as np
3
  from PIL import Image
4
+ from PIL import ImageColor
5
  import mediapipe as mp
6
  import time
7
  import gradio as gr
8
  import glob
9
 
10
+ width_, height_ = 144, 96
11
 
12
  drawing_flag = False
13
  sleepy_time = time.time()
 
15
  output_frames = []
16
 
17
 
18
+ def is_hex(hexq):
19
+ if hexq[0] == '#' and len(hexq) == 7 and hexq[1:7].isalnum():
20
+ return True
21
+ else:
22
+ return False
23
+
24
+
25
+ def hex2rgb(hex):
26
+ if is_hex(hex):
27
+ return ImageColor.getcolor(hex, "RGB")
28
+ else:
29
+ return (255, 255, 255)
30
+
31
+
32
  def find_hands(brain, img):
33
  if img is not None:
34
  img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # opencv image is in BGR form but mp is trained with RGB
 
118
  # print(idx_coords)
119
  cv2.circle(rgb_image_f, idx_coords, 5, color, cv2.FILLED)
120
 
121
+ ### Discontinued function due to gradio limitations ###
122
+ # if idx_coords[1] < 72: # brute force but should be extremely marginally faster lol
123
+ # if idx_coords[0] < 71: # red
124
+ # color = (0, 0, 255)
125
+ # if 71 < idx_coords[0] < 142: # orange
126
+ # color = (0, 115, 255)
127
+ # if 142 < idx_coords[0] < 213: # yellow
128
+ # color = (0, 229, 255)
129
+ # if 213 < idx_coords[0] < 284: # green
130
+ # color = (0, 195, 88)
131
+ # if 284 < idx_coords[0] < 356: # blue
132
+ # color = (195, 85, 0)
133
+ # if 356 < idx_coords[0] < 427: # indigo
134
+ # color = (195, 0, 68)
135
+ # if 427 < idx_coords[0] < 498: # violet
136
+ # color = (195, 0, 143)
137
+ # if 498 < idx_coords[0] < 569: # black
138
+ # color = (0, 0, 0)
139
+ # if 569 < idx_coords[0]: # white / eraser
140
+ # color = (255, 255, 255)
141
 
142
  if len(past_holder) and drawing_flag: # start drawing
143
  cv2.line(paper, past_holder, idx_coords, color, 5)
 
146
  # paper[idx_coords[0]][idx_coords[1]][3] = 255
147
  cv2.circle(rgb_image_f, idx_coords, 5, color, cv2.FILLED)
148
 
149
+ ### Discontinued function due to gradio limitations ###
150
  # if save(lm_list1) and time.time() - sleepy_time > 3: # save / output
151
  # paper[0:height_, w - width_: w] = 255 # presenter eraser
152
  # paper = cv2.cvtColor(paper, cv2.COLOR_BGR2RGB)
 
175
 
176
  finally:
177
  if True:
 
178
  presenter = cv2.resize(rgb_image_f, (width_, height_))
179
  h, w, _ = rgb_image_f.shape
180
  paper[0:height_, w - width_: w] = presenter
 
205
  desc = 'A Mediapipe Hands Wrapper for Drawing in the Air'
206
  iface = gr.Interface(
207
  fn=show,
208
+ inputs=[
209
+ gr.inputs.Video(source="webcam", label="Record from Webcam"),
210
+ gr.inputs.Radio(['Right', 'Left'], label="Dominant Hand"),
211
+ gr.inputs.Textbox(placeholder="#355C7D", label="Hex Code")
212
+ ],
213
+ outputs='video',
214
+ title=title,
215
  description=desc)
216
 
217
  iface.launch(share=True, enable_queue=True)