23b719w commited on
Commit
7eb83f3
1 Parent(s): b8a968b

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -16
app.py CHANGED
@@ -48,9 +48,49 @@ def load_model2():
48
  threshold = 0.50
49
 
50
  def predict(pilimg,video_in_filepath,threshold):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
 
52
- image_np = pil_image_as_numpy_array(pilimg)
53
- return predict2(image_np,threshold),None
54
 
55
  def predict2(image_np,threshold):
56
 
@@ -130,20 +170,20 @@ detection_model = load_model()
130
  # predicted_img = predict(image_arr)
131
  # predicted_img.save('predicted.jpg')
132
 
133
- # gr.Interface(fn=predict,
134
- # inputs=[gr.Image(type="pil",label="Input Image"),gr.Video(label="Input Video"),gr.Textbox(placeholder="0.50",label="Set the confidence threshold (0.00-1.00)")],
135
- # outputs=[gr.Image(type="pil",label="Output Image"),gr.Video(label="Output Video")],
136
- # title="Facemask & Glasses",
137
- # description="Model: ssd_mobilenet_v2_320x320",
138
- # theme=gr.themes.Soft(primary_hue="blue", secondary_hue="sky"),
139
- # examples=[["test_samples/image489.png","",0.55], ["test_samples/image825.png","",0.55], ["test_samples/image833.png","",0.55], ["test_samples/image846.png","",0.55]]
140
- # ).launch(share=True)
141
-
142
- gr.Interface(fn=video_fn,
143
- inputs=gr.Video(label="Input Video"),
144
- outputs=gr.Video(label="Output Video"),
145
  title="Facemask & Glasses",
146
  description="Model: ssd_mobilenet_v2_320x320",
147
  theme=gr.themes.Soft(primary_hue="blue", secondary_hue="sky"),
148
- #examples="test_samples/test_video.mp4"
149
- ).launch(share=True)
 
 
 
 
 
 
 
 
 
 
48
  threshold = 0.50
49
 
50
  def predict(pilimg,video_in_filepath,threshold):
51
+ if pilimg != "":
52
+ image_np = pil_image_as_numpy_array(pilimg)
53
+ return predict2(image_np,threshold),None
54
+ else:
55
+ video_reader = cv2.VideoCapture(video_in_filepath)
56
+
57
+ nb_frames = int(video_reader.get(cv2.CAP_PROP_FRAME_COUNT))
58
+ frame_h = int(video_reader.get(cv2.CAP_PROP_FRAME_HEIGHT))
59
+ frame_w = int(video_reader.get(cv2.CAP_PROP_FRAME_WIDTH))
60
+ fps = video_reader.get(cv2.CAP_PROP_FPS)
61
+
62
+
63
+ video_out_filepath = 'detected.mp4'
64
+ video_writer = cv2.VideoWriter(video_out_filepath,
65
+ cv2.VideoWriter_fourcc(*'mp4v'),
66
+ fps,
67
+ (frame_w, frame_h))
68
+
69
+ for i in tqdm(range(nb_frames)):
70
+ ret, image_np = video_reader.read()
71
+ input_tensor = tf.convert_to_tensor(np.expand_dims(image_np, 0), dtype=tf.uint8)
72
+ results = detection_model(input_tensor)
73
+ viz_utils.visualize_boxes_and_labels_on_image_array(
74
+ image_np,
75
+ results['detection_boxes'][0].numpy(),
76
+ (results['detection_classes'][0].numpy()+ label_id_offset).astype(int),
77
+ results['detection_scores'][0].numpy(),
78
+ category_index,
79
+ use_normalized_coordinates=True,
80
+ max_boxes_to_draw=200,
81
+ min_score_thresh=.50,
82
+ agnostic_mode=False,
83
+ line_thickness=2)
84
+
85
+ video_writer.write(np.uint8(image_np))
86
+
87
+ # Release camera and close windows
88
+ video_reader.release()
89
+ video_writer.release()
90
+ cv2.destroyAllWindows()
91
+ cv2.waitKey(1)
92
+ return None,video_out_filepath
93
 
 
 
94
 
95
  def predict2(image_np,threshold):
96
 
 
170
  # predicted_img = predict(image_arr)
171
  # predicted_img.save('predicted.jpg')
172
 
173
+ gr.Interface(fn=predict,
174
+ inputs=[gr.Image(type="pil",label="Input Image"),gr.Video(label="Input Video"),gr.Textbox(placeholder="0.50",label="Set the confidence threshold (0.00-1.00)")],
175
+ outputs=[gr.Image(type="pil",label="Output Image"),gr.Video(label="Output Video")],
 
 
 
 
 
 
 
 
 
176
  title="Facemask & Glasses",
177
  description="Model: ssd_mobilenet_v2_320x320",
178
  theme=gr.themes.Soft(primary_hue="blue", secondary_hue="sky"),
179
+ examples=[["test_samples/image489.png","",0.55], ["test_samples/image825.png","",0.55], ["test_samples/image833.png","",0.55], ["test_samples/image846.png","",0.55]]
180
+ ).launch(share=True)
181
+
182
+ # gr.Interface(fn=video_fn,
183
+ # inputs=gr.Video(label="Input Video"),
184
+ # outputs=gr.Video(label="Output Video"),
185
+ # title="Facemask & Glasses",
186
+ # description="Model: ssd_mobilenet_v2_320x320",
187
+ # theme=gr.themes.Soft(primary_hue="blue", secondary_hue="sky"),
188
+ # #examples="test_samples/test_video.mp4"
189
+ # ).launch(share=True)