brogelio commited on
Commit
cdd0122
·
1 Parent(s): 05eeddf

Updated Application File

Browse files
Files changed (1) hide show
  1. app.py +64 -48
app.py CHANGED
@@ -4,11 +4,11 @@ from PIL import Image
4
  import mediapipe as mp
5
  import time
6
  import gradio as gr
 
7
 
8
  DOMINANT_HAND = "Right"
9
 
10
- # width, height = 1280, 720
11
- width_, height_, = 256, 144
12
 
13
  drawing_flag = False
14
  sleepy_time = time.time()
@@ -17,25 +17,31 @@ output_frames = []
17
 
18
 
19
  def find_hands(brain, img):
20
- img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # opencv image is in BGR form but mp is trained with RGB
21
- results = brain.process(
22
- img_rgb) # process finds the hands and outputs classification and 21 landmarks for each hand
23
- all_hands = [] # initializing array to hold the dictionary for the hands
24
- h, w, _ = img.shape # get height and width of image for scaling
25
- if results.multi_hand_landmarks:
26
- for hand_type, hand_lms in zip(results.multi_handedness,
27
- results.multi_hand_landmarks): # elegant solution for mp list object traversal
28
- hand = {} # initializing dict for each hand
29
- lm_list = [] # landmarks array for all 21 point of the hand
30
- for lm in hand_lms.landmark:
31
- px, py, pz = int(lm.x * w), int(lm.y * h), int(
32
- lm.z * w) # scaling landmark points to image size for frame coordinates
33
- lm_list.append([px, py, pz])
34
-
35
- hand["lm_list"] = lm_list # add "lm_list" key for all landmark points of the hand
36
- hand["type"] = hand_type.classification[0].label # adds the label (left/right) for the hand
37
- all_hands.append(hand) # appends the dict
38
- return all_hands
 
 
 
 
 
 
39
 
40
 
41
  def is_drawing(index, thumb): # proximity function with arbitrary threshold
@@ -75,7 +81,7 @@ def show(video): # main
75
  paper.fill(255)
76
 
77
  past_holder = () # hold previous index coordinates
78
- palette = cv2.imread('palette.jpg')
79
 
80
  page_num = 0 # iterating for saving (not a viable function for gradio)
81
 
@@ -83,11 +89,10 @@ def show(video): # main
83
 
84
  global sleepy_time # get sleep time for multiple gestures
85
 
86
- # runny = 1
87
  while cam.isOpened():
88
  # runny -= 1
89
  x, rgb_image = cam.read()
90
- rgb_image_f = cv2.flip(np.asanyarray(rgb_image), 1) # mirrored video
91
 
92
  hands = find_hands(detector, rgb_image_f)
93
 
@@ -130,16 +135,15 @@ def show(video): # 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
135
- paper = cv2.cvtColor(paper, cv2.COLOR_BGR2RGB)
136
- im = Image.fromarray(paper)
137
- im.save("paper%s.png" % page_num)
138
- print("saved")
139
- sleepy_time = time.time()
140
- paper = cv2.cvtColor(paper, cv2.COLOR_RGB2BGR)
141
- page_num += 1
142
- return paper
143
 
144
  if clear(lm_list1) and time.time() - sleepy_time > 3: # reset paper
145
  paper = np.zeros((height, width, 3), dtype=np.uint8)
@@ -158,23 +162,35 @@ def show(video): # main
158
  pass
159
 
160
  finally:
161
- rgb_image_f[0:72, ] = palette
162
- presenter = cv2.resize(rgb_image_f, (width_, height_))
163
- h, w, _ = rgb_image_f.shape
164
- paper[0:height_, w - width_: w] = presenter
165
-
166
- # output_frames.append(paper)
167
-
168
- # cv2.imshow("Image", rgb_image_f)
169
- # cv2.imshow("paper", paper)
170
- # key = cv2.waitKey(1)
171
- # if key & 0xFF == ord('q') or key == 27: # Press esc or 'q' to close the image window
172
- # break
173
 
174
  else:
175
  break
176
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
177
 
178
- iface = gr.Interface(fn=show, inputs=gr.inputs.Video(source="webcam", type="mp4"), outputs='image')
179
 
180
- iface.launch(share=True)
 
4
  import mediapipe as mp
5
  import time
6
  import gradio as gr
7
+ import glob
8
 
9
  DOMINANT_HAND = "Right"
10
 
11
+ width_, height_, = 144, 96
 
12
 
13
  drawing_flag = False
14
  sleepy_time = time.time()
 
17
 
18
 
19
  def find_hands(brain, img):
20
+ if img is not None:
21
+ # print(type(img))
22
+ img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # opencv image is in BGR form but mp is trained with RGB
23
+ results = brain.process(
24
+ img_rgb) # process finds the hands and outputs classification and 21 landmarks for each hand
25
+ all_hands = [] # initializing array to hold the dictionary for the hands
26
+ h, w, _ = img.shape # get height and width of image for scaling
27
+ if results.multi_hand_landmarks:
28
+ for hand_type, hand_lms in zip(results.multi_handedness,
29
+ results.multi_hand_landmarks): # elegant solution for mp list object traversal
30
+ hand = {} # initializing dict for each hand
31
+ lm_list = [] # landmarks array for all 21 point of the hand
32
+ for lm in hand_lms.landmark:
33
+ px, py, pz = int(lm.x * w), int(lm.y * h), int(
34
+ lm.z * w) # scaling landmark points to image size for frame coordinates
35
+ lm_list.append([px, py, pz])
36
+
37
+ hand["lm_list"] = lm_list # add "lm_list" key for all landmark points of the hand
38
+ hand["type"] = hand_type.classification[0].label # adds the label (left/right) for the hand
39
+ all_hands.append(hand) # appends the dict
40
+ return all_hands
41
+
42
+ else:
43
+ return 0
44
+
45
 
46
 
47
  def is_drawing(index, thumb): # proximity function with arbitrary threshold
 
81
  paper.fill(255)
82
 
83
  past_holder = () # hold previous index coordinates
84
+ palette = cv2.imread('palette_small.jpg')
85
 
86
  page_num = 0 # iterating for saving (not a viable function for gradio)
87
 
 
89
 
90
  global sleepy_time # get sleep time for multiple gestures
91
 
 
92
  while cam.isOpened():
93
  # runny -= 1
94
  x, rgb_image = cam.read()
95
+ rgb_image_f = cv2.flip(rgb_image, 1) # mirrored video
96
 
97
  hands = find_hands(detector, rgb_image_f)
98
 
 
135
  # paper[idx_coords[0]][idx_coords[1]][3] = 255
136
  cv2.circle(rgb_image_f, idx_coords, 5, color, cv2.FILLED)
137
 
138
+ # if save(lm_list1) and time.time() - sleepy_time > 3: # save / output
139
+ # paper[0:height_, w - width_: w] = 255 # presenter eraser
140
+ # paper = cv2.cvtColor(paper, cv2.COLOR_BGR2RGB)
141
+ # im = Image.fromarray(paper)
142
+ # im.save("paper%s.png" % page_num)
143
+ # print("saved")
144
+ # sleepy_time = time.time()
145
+ # paper = cv2.cvtColor(paper, cv2.COLOR_RGB2BGR)
146
+ # page_num += 1
 
147
 
148
  if clear(lm_list1) and time.time() - sleepy_time > 3: # reset paper
149
  paper = np.zeros((height, width, 3), dtype=np.uint8)
 
162
  pass
163
 
164
  finally:
165
+ if True:
166
+ rgb_image_f[0:48, ] = palette # 48 small
167
+ presenter = cv2.resize(rgb_image_f, (width_, height_))
168
+ h, w, _ = rgb_image_f.shape
169
+ paper[0:height_, w - width_: w] = presenter
 
 
 
 
 
 
 
170
 
171
  else:
172
  break
173
 
174
+ paper = cv2.cvtColor(paper, cv2.COLOR_RGB2BGR)
175
+ im = Image.fromarray(paper)
176
+ output_frames.append(paper)
177
+ im.save("paper%s.png" % page_num)
178
+ page_num += 1
179
+
180
+ img_array = []
181
+ for filename in glob.glob('*.png'):
182
+ imggg = cv2.imread(filename)
183
+ img_array.append(imggg)
184
+
185
+ video_output = cv2.VideoWriter('any.webm', cv2.VideoWriter_fourcc(*'VP80'), 30, (640, 480))
186
+
187
+ for i in range(len(img_array)):
188
+ video_output.write(img_array[i])
189
+ video_output.release()
190
+
191
+ return 'any.webm'
192
+
193
 
194
+ iface = gr.Interface(fn=show, inputs=gr.inputs.Video(source="webcam"), outputs='video')
195
 
196
+ iface.launch(share=True, enable_queue=True)