Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -78,7 +78,45 @@ def predict2(image_np):
|
|
| 78 |
result_pil_img = tf.keras.utils.array_to_img(image_np_with_detections[0])
|
| 79 |
|
| 80 |
return result_pil_img
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
REPO_ID = "YEHTUT/tfodmodel"
|
| 84 |
detection_model = load_model()
|
|
@@ -90,11 +128,11 @@ detection_model = load_model()
|
|
| 90 |
Image_tab = Interface(fn=predict,
|
| 91 |
inputs=gr.Image(type="pil"),
|
| 92 |
outputs=gr.Image(type="pil")
|
| 93 |
-
)
|
| 94 |
Video_tab = Interface(fn=predict,
|
| 95 |
inputs=gr.Video,
|
| 96 |
outputs=gr.Video
|
| 97 |
-
)
|
| 98 |
|
| 99 |
gr.TabbedInterface([Image_tab, Video_tab], ["Image", "Video"]).launch(share=True)
|
| 100 |
#gr.Interface(fn=predict,
|
|
|
|
| 78 |
result_pil_img = tf.keras.utils.array_to_img(image_np_with_detections[0])
|
| 79 |
|
| 80 |
return result_pil_img
|
| 81 |
+
def write_video(video_in_filepath, video_out_filepath, detection_model):
|
| 82 |
+
if not os.path.exists(video_in_filepath):
|
| 83 |
+
print('video filepath not valid')
|
| 84 |
+
|
| 85 |
+
video_reader = cv2.VideoCapture(video_in_filepath)
|
| 86 |
+
|
| 87 |
+
nb_frames = int(video_reader.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 88 |
+
frame_h = int(video_reader.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 89 |
+
frame_w = int(video_reader.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 90 |
+
fps = video_reader.get(cv2.CAP_PROP_FPS)
|
| 91 |
+
|
| 92 |
+
video_writer = cv2.VideoWriter(video_out_filepath,
|
| 93 |
+
cv2.VideoWriter_fourcc(*'mp4v'),
|
| 94 |
+
fps,
|
| 95 |
+
(frame_w, frame_h))
|
| 96 |
+
|
| 97 |
+
for i in tqdm(range(nb_frames)):
|
| 98 |
+
ret, image_np = video_reader.read()
|
| 99 |
+
input_tensor = tf.convert_to_tensor(np.expand_dims(image_np, 0), dtype=tf.uint8)
|
| 100 |
+
results = detection_model(input_tensor)
|
| 101 |
+
viz_utils.visualize_boxes_and_labels_on_image_array(
|
| 102 |
+
image_np,
|
| 103 |
+
results['detection_boxes'][0].numpy(),
|
| 104 |
+
(results['detection_classes'][0].numpy()+ label_id_offset).astype(int),
|
| 105 |
+
results['detection_scores'][0].numpy(),
|
| 106 |
+
category_index,
|
| 107 |
+
use_normalized_coordinates=True,
|
| 108 |
+
max_boxes_to_draw=200,
|
| 109 |
+
min_score_thresh=.50,
|
| 110 |
+
agnostic_mode=False,
|
| 111 |
+
line_thickness=2)
|
| 112 |
+
|
| 113 |
+
video_writer.write(np.uint8(image_np))
|
| 114 |
+
|
| 115 |
+
# Release camera and close windows
|
| 116 |
+
video_reader.release()
|
| 117 |
+
video_writer.release()
|
| 118 |
+
cv2.destroyAllWindows()
|
| 119 |
+
cv2.waitKey(1)
|
| 120 |
|
| 121 |
REPO_ID = "YEHTUT/tfodmodel"
|
| 122 |
detection_model = load_model()
|
|
|
|
| 128 |
Image_tab = Interface(fn=predict,
|
| 129 |
inputs=gr.Image(type="pil"),
|
| 130 |
outputs=gr.Image(type="pil")
|
| 131 |
+
)
|
| 132 |
Video_tab = Interface(fn=predict,
|
| 133 |
inputs=gr.Video,
|
| 134 |
outputs=gr.Video
|
| 135 |
+
)
|
| 136 |
|
| 137 |
gr.TabbedInterface([Image_tab, Video_tab], ["Image", "Video"]).launch(share=True)
|
| 138 |
#gr.Interface(fn=predict,
|