apailang commited on
Commit
64ebe74
β€’
1 Parent(s): 5da04d0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -33
app.py CHANGED
@@ -69,20 +69,23 @@ def predict2(image_np):
69
  return result_pil_img
70
 
71
  def detect_video(video):
72
- # Create a video capture object
73
- cap = cv2.VideoCapture(video)
74
-
75
- nb_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
76
- frame_h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
77
- frame_w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
78
- fps = cap.get(cv2.CAP_PROP_FPS)
79
 
80
- # Process frames in a loop
 
 
 
 
81
  for i in tqdm(range(nb_frames)):
82
- ret, image_np = cap.read()
83
  input_tensor = tf.convert_to_tensor(np.expand_dims(image_np, 0), dtype=tf.uint8)
84
  results = detection_model(input_tensor)
85
- image_np_with_detections = viz_utils.visualize_boxes_and_labels_on_image_array(
86
  image_np,
87
  results['detection_boxes'][0].numpy(),
88
  (results['detection_classes'][0].numpy()+ label_id_offset).astype(int),
@@ -94,21 +97,14 @@ def detect_video(video):
94
  agnostic_mode=False,
95
  line_thickness=2)
96
 
 
 
 
 
 
 
 
97
 
98
- # Yield the processed frame
99
- yield image_np_with_detections
100
-
101
- # Release resources
102
- cap.release()
103
-
104
-
105
- inputs_video = [
106
- gr.components.Video( label="Input Video"),
107
-
108
- ]
109
- outputs_video = [
110
- gr.components.Image( label="Output Image"),
111
- ]
112
 
113
  label_id_offset = 0
114
  REPO_ID = "apailang/mytfodmodel"
@@ -142,25 +138,19 @@ tts_demo = gr.Interface(
142
  cache_examples=True
143
  )#.launch(share=True)
144
 
 
145
 
146
  a = os.path.join(os.path.dirname(__file__), "data/a.mp4") # Video
147
  b = os.path.join(os.path.dirname(__file__), "data/b.mp4") # Video
148
  c = os.path.join(os.path.dirname(__file__), "data/c.mp4") # Video
149
 
150
 
151
- # interface_video = gr.Interface(
152
- # fn=show_preds_video,
153
- # inputs=inputs_video,
154
- # outputs=outputs_video,
155
- # title="Pothole detector",
156
- # examples=video_path,
157
- # cache_examples=False,
158
- # )
159
 
160
  stt_demo = gr.Interface(
161
  fn=detect_video,
162
  inputs=inputs_video,
163
- outputs=gr.Video(),
164
  examples=[
165
  [a],
166
  [b],
 
69
  return result_pil_img
70
 
71
  def detect_video(video):
72
+ video_reader = cv2.VideoCapture(video)
73
+
74
+ nb_frames = int(video_reader.get(cv2.CAP_PROP_FRAME_COUNT))
75
+ frame_h = int(video_reader.get(cv2.CAP_PROP_FRAME_HEIGHT))
76
+ frame_w = int(video_reader.get(cv2.CAP_PROP_FRAME_WIDTH))
77
+ fps = video_reader.get(cv2.CAP_PROP_FPS)
 
78
 
79
+ video_writer = cv2.VideoWriter(video_out_filepath,
80
+ cv2.VideoWriter_fourcc(*'mp4v'),
81
+ fps,
82
+ (frame_w, frame_h))
83
+
84
  for i in tqdm(range(nb_frames)):
85
+ ret, image_np = video_reader.read()
86
  input_tensor = tf.convert_to_tensor(np.expand_dims(image_np, 0), dtype=tf.uint8)
87
  results = detection_model(input_tensor)
88
+ viz_utils.visualize_boxes_and_labels_on_image_array(
89
  image_np,
90
  results['detection_boxes'][0].numpy(),
91
  (results['detection_classes'][0].numpy()+ label_id_offset).astype(int),
 
97
  agnostic_mode=False,
98
  line_thickness=2)
99
 
100
+ video_writer.write(np.uint8(image_np))
101
+
102
+ # Release camera and close windows
103
+ video_reader.release()
104
+ video_writer.release()
105
+ cv2.destroyAllWindows()
106
+ cv2.waitKey(1)
107
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
 
109
  label_id_offset = 0
110
  REPO_ID = "apailang/mytfodmodel"
 
138
  cache_examples=True
139
  )#.launch(share=True)
140
 
141
+ samples_folder = 'data'
142
 
143
  a = os.path.join(os.path.dirname(__file__), "data/a.mp4") # Video
144
  b = os.path.join(os.path.dirname(__file__), "data/b.mp4") # Video
145
  c = os.path.join(os.path.dirname(__file__), "data/c.mp4") # Video
146
 
147
 
148
+ video_out_file = os.path.join(samples_folder,'detected' + '.mp4')
 
 
 
 
 
 
 
149
 
150
  stt_demo = gr.Interface(
151
  fn=detect_video,
152
  inputs=inputs_video,
153
+ outputs="data/detected.mp4",
154
  examples=[
155
  [a],
156
  [b],