apailang commited on
Commit
c1d9208
β€’
1 Parent(s): 685deac

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -38
app.py CHANGED
@@ -69,46 +69,49 @@ def predict2(image_np):
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),
92
- results['detection_scores'][0].numpy(),
93
- category_index,
94
- use_normalized_coordinates=True,
95
- max_boxes_to_draw=200,
96
- min_score_thresh=.50,
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"
111
  detection_model = load_model()
 
112
  # pil_image = Image.open(image_path)
113
  # image_arr = pil_image_as_numpy_array(pil_image)
114
 
@@ -138,8 +141,6 @@ tts_demo = gr.Interface(
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
@@ -150,7 +151,7 @@ video_out_file = os.path.join(samples_folder,'detected' + '.mp4')
150
  stt_demo = gr.Interface(
151
  fn=detect_video,
152
  inputs=gr.Video(),
153
- outputs="data/detected.mp4",
154
  examples=[
155
  [a],
156
  [b],
@@ -158,6 +159,7 @@ stt_demo = gr.Interface(
158
  ],
159
  cache_examples=False
160
  )
 
161
  demo = gr.TabbedInterface([tts_demo, stt_demo], ["Image", "Video"])
162
 
163
  if __name__ == "__main__":
 
69
  return result_pil_img
70
 
71
  def detect_video(video):
72
+ # Create a video capture object
73
+ cap = cv2.VideoCapture(video)
74
+
75
+ # Process frames in a loop
76
+ while cap.isOpened():
77
+ ret, frame = cap.read()
78
+ if not ret:
79
+ break
80
+
81
+ # Expand dimensions since model expects images to have shape: [1, None, None, 3]
82
+ image_np_expanded = np.expand_dims(frame, axis=0)
83
+
84
+ # Run inference
85
+ output_dict = model(image_np_expanded)
86
+
87
+ # Extract detections
88
+ boxes = output_dict['detection_boxes'][0].numpy()
89
+ scores = output_dict['detection_scores'][0].numpy()
90
+ classes = output_dict['detection_classes'][0].numpy().astype(np.int64)
91
+
92
+ # Draw bounding boxes and labels
93
+ image_np_with_detections = viz_utils.visualize_boxes_and_labels_on_image_array(
94
+ frame,
95
+ boxes,
96
+ classes,
97
+ scores,
98
+ category_index,
99
+ use_normalized_coordinates=True,
100
+ max_boxes_to_draw=20,
101
+ min_score_thresh=.5,
102
+ agnostic_mode=False)
103
+
104
+ # Yield the processed frame
105
+ yield image_np_with_detections
106
+
107
+ # Release resources
108
+ cap.release()
109
 
110
 
111
  label_id_offset = 0
112
  REPO_ID = "apailang/mytfodmodel"
113
  detection_model = load_model()
114
+ samples_folder = 'data'
115
  # pil_image = Image.open(image_path)
116
  # image_arr = pil_image_as_numpy_array(pil_image)
117
 
 
141
  cache_examples=True
142
  )#.launch(share=True)
143
 
 
 
144
  a = os.path.join(os.path.dirname(__file__), "data/a.mp4") # Video
145
  b = os.path.join(os.path.dirname(__file__), "data/b.mp4") # Video
146
  c = os.path.join(os.path.dirname(__file__), "data/c.mp4") # Video
 
151
  stt_demo = gr.Interface(
152
  fn=detect_video,
153
  inputs=gr.Video(),
154
+ utputs=gr.Video(label="Detected Video"),,
155
  examples=[
156
  [a],
157
  [b],
 
159
  ],
160
  cache_examples=False
161
  )
162
+
163
  demo = gr.TabbedInterface([tts_demo, stt_demo], ["Image", "Video"])
164
 
165
  if __name__ == "__main__":