ozyman commited on
Commit
27cb655
·
1 Parent(s): fc6a3c7

video function

Browse files
Files changed (1) hide show
  1. app.py +47 -8
app.py CHANGED
@@ -200,13 +200,52 @@ def inference(img, dsdg_thresh):
200
  x2 = x + w
201
  y2 = y + h
202
  bbox = (x, y, x2, y2)
203
- img_deepix, confidences_deepix, cls_deepix = deepix_model_inference(img, bbox)
204
  img_dsdg, confidences_dsdg, cls_dsdg = dsdg_model_inference(img, bbox, dsdg_thresh)
205
- return img_deepix, confidences_deepix, cls_deepix, img_dsdg, confidences_dsdg, cls_dsdg
206
  else:
207
  return img, {}, None, img, {}, None
208
 
209
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
210
  def upload_to_s3(image_array, app_version, *labels):
211
  folder = 'demo'
212
  bucket_name = 'livenessng'
@@ -244,15 +283,15 @@ demo = gr.Blocks()
244
  with demo:
245
  with gr.Row():
246
  with gr.Column():
247
- input_img = gr.Image(source='webcam', shape=None, type='numpy', streaming=False)
248
  dsdg_thresh = gr.Slider(value=DSDG_THRESHOLD, label='DSDG threshold')
249
  btn_run = gr.Button(value="Run")
250
  with gr.Column():
251
  outputs=[
252
- gr.Image(label='DeePixBiS', type='numpy'),
253
  gr.Label(num_top_classes=2, label='DeePixBiS'),
254
  gr.Number(visible=False, value=-1),
255
- gr.Image(label='DSDG', type='numpy'),
256
  gr.Label(num_top_classes=2, label='DSDG'),
257
  gr.Number(visible=False, value=-1)]
258
  with gr.Column():
@@ -260,13 +299,13 @@ with demo:
260
  ["Spoof", "Real", "None"], label="True label", type='index')
261
  flag = gr.Button(value="Flag")
262
  status = gr.Textbox()
263
- example_block = gr.Examples(examples, [input_img], outputs)
264
 
265
- btn_run.click(inference, [input_img, dsdg_thresh], outputs)
266
  app_version_block = gr.Textbox(value=app_version, visible=False)
267
  flag.click(
268
  upload_to_s3,
269
- [input_img, app_version_block, radio]+[outputs[2], outputs[5]],
270
  [status], show_progress=True)
271
 
272
 
 
200
  x2 = x + w
201
  y2 = y + h
202
  bbox = (x, y, x2, y2)
203
+ # img_deepix, confidences_deepix, cls_deepix = deepix_model_inference(img, bbox)
204
  img_dsdg, confidences_dsdg, cls_dsdg = dsdg_model_inference(img, bbox, dsdg_thresh)
205
+ return img, {}, 2, img_dsdg, confidences_dsdg, cls_dsdg
206
  else:
207
  return img, {}, None, img, {}, None
208
 
209
 
210
+ def process_video(vid_path, dsdg_thresh):
211
+ cap = cv.VideoCapture(vid_path)
212
+
213
+ input_width = int(cap.get(cv.CAP_PROP_FRAME_WIDTH))
214
+ input_height = int(cap.get(cv.CAP_PROP_FRAME_HEIGHT))
215
+
216
+ # Set video codec and create VideoWriter object to save the output video
217
+ fourcc = cv.VideoWriter_fourcc(*'XVID')
218
+ output_vid_path = 'output_dsdg.mp4'
219
+ out_dsdg = cv.VideoWriter(output_vid_path, fourcc, 6.0, (input_width, input_height))
220
+
221
+ frame_counter = 0
222
+ confidences_arr = []
223
+ while cap.isOpened():
224
+ ret, frame = cap.read()
225
+ if not ret:
226
+ break
227
+
228
+ # Process only every 5th frame
229
+ if frame_counter % 5 == 0:
230
+ # Run inference on the current frame
231
+ _, _, _, img_dsdg, confidences_dsdg, _ = inference(frame, dsdg_thresh)
232
+ confidences_arr.append(confidences_dsdg['Real confidence'])
233
+
234
+ # Resize the DSDG frame to match the input video dimensions
235
+ img_dsdg = cv.resize(img_dsdg, (input_width, input_height))
236
+
237
+ # Write the DSDG frame to the output video
238
+ out_dsdg.write(img_dsdg)
239
+
240
+ frame_counter += 1
241
+ avg_conf = sum(confidences_arr) / len(confidences_arr)
242
+ confidences_dsdg = {'Average real confidence': avg_conf}
243
+ # Release resources
244
+ cap.release()
245
+ out_dsdg.release()
246
+ return vid_path, {'Not supported right now': 0}, 2, output_vid_path, avg_conf, avg_conf
247
+
248
+
249
  def upload_to_s3(image_array, app_version, *labels):
250
  folder = 'demo'
251
  bucket_name = 'livenessng'
 
283
  with demo:
284
  with gr.Row():
285
  with gr.Column():
286
+ input_vid = gr.Video(format='mp4', source='webcam')
287
  dsdg_thresh = gr.Slider(value=DSDG_THRESHOLD, label='DSDG threshold')
288
  btn_run = gr.Button(value="Run")
289
  with gr.Column():
290
  outputs=[
291
+ gr.Video(label='DeePixBiS', format='mp4'),
292
  gr.Label(num_top_classes=2, label='DeePixBiS'),
293
  gr.Number(visible=False, value=-1),
294
+ gr.Video(label='DSDG', format='mp4'),
295
  gr.Label(num_top_classes=2, label='DSDG'),
296
  gr.Number(visible=False, value=-1)]
297
  with gr.Column():
 
299
  ["Spoof", "Real", "None"], label="True label", type='index')
300
  flag = gr.Button(value="Flag")
301
  status = gr.Textbox()
302
+ # example_block = gr.Examples(examples, [input_vid], outputs)
303
 
304
+ btn_run.click(process_video, [input_vid, dsdg_thresh], outputs)
305
  app_version_block = gr.Textbox(value=app_version, visible=False)
306
  flag.click(
307
  upload_to_s3,
308
+ [input_vid, app_version_block, radio]+[outputs[2], outputs[5]],
309
  [status], show_progress=True)
310
 
311