amosfang commited on
Commit
52418c1
1 Parent(s): 06f8627

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -5
app.py CHANGED
@@ -5,12 +5,14 @@ import tensorflow as tf
5
  from object_detection.utils import label_map_util
6
  from object_detection.utils import visualization_utils as viz_utils
7
  from object_detection.utils import ops as utils_op
8
- import tarfile
9
- import wget
10
  import gradio as gr
11
- from huggingface_hub import snapshot_download
12
  import os
13
  import cv2
 
 
14
 
15
  PATH_TO_LABELS = 'data/label_map.pbtxt'
16
  category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS, use_display_name=True)
@@ -57,18 +59,25 @@ def predict_on_video(video_in_filepath, video_out_filepath, detection_model, cat
57
  fps,
58
  (frame_w, frame_h)
59
  )
60
- label_id_offset = 0
61
  while True:
62
  ret, frame = video_reader.read()
63
  if not ret:
64
  break # Break the loop if the video is finished
 
 
65
  processed_frame = predict(frame)
 
 
66
  processed_frame_np = np.array(processed_frame)
67
  video_writer.write(processed_frame_np)
 
 
 
 
68
  video_reader.release()
69
  video_writer.release()
70
  cv2.destroyAllWindows()
71
- cv2.waitKey(1)
72
 
73
  # Function to process a video
74
  def process_video(video_path):
 
5
  from object_detection.utils import label_map_util
6
  from object_detection.utils import visualization_utils as viz_utils
7
  from object_detection.utils import ops as utils_op
8
+ # import tarfile
9
+ # import wget
10
  import gradio as gr
11
+ # from huggingface_hub import snapshot_download
12
  import os
13
  import cv2
14
+ import time
15
+
16
 
17
  PATH_TO_LABELS = 'data/label_map.pbtxt'
18
  category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS, use_display_name=True)
 
59
  fps,
60
  (frame_w, frame_h)
61
  )
 
62
  while True:
63
  ret, frame = video_reader.read()
64
  if not ret:
65
  break # Break the loop if the video is finished
66
+
67
+ start_time = time.time()
68
  processed_frame = predict(frame)
69
+ end_time = time.time()
70
+ processing_time = end_time - start_time
71
  processed_frame_np = np.array(processed_frame)
72
  video_writer.write(processed_frame_np)
73
+
74
+ # Adjust the delay based on processing time
75
+ delay_time = max(1, int((1 / fps - processing_time) * 1000))
76
+ cv2.waitKey(delay_time)
77
  video_reader.release()
78
  video_writer.release()
79
  cv2.destroyAllWindows()
80
+ cv2.waitKey(5)
81
 
82
  # Function to process a video
83
  def process_video(video_path):