Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -69,46 +69,49 @@ def predict2(image_np):
|
|
69 |
return result_pil_img
|
70 |
|
71 |
def detect_video(video):
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
|
|
|
|
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 |
-
|
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__":
|