Spaces:
Runtime error
Runtime error
jiawenchim
commited on
Commit
•
e441fe1
1
Parent(s):
8b33909
Update app.py
Browse files
app.py
CHANGED
@@ -70,6 +70,43 @@ def predict2(image_np):
|
|
70 |
return result_pil_img
|
71 |
|
72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
# Specify paths to example images
|
74 |
sample_images = [["00000031.jpg"], ["00000053.jpg"],
|
75 |
["00000057.jpg"], ["00000078.jpg"],
|
@@ -85,9 +122,22 @@ detection_model = load_model()
|
|
85 |
# predicted_img = predict(image_arr)
|
86 |
# predicted_img.save('predicted.jpg')
|
87 |
|
88 |
-
gr.Interface(fn=predict,
|
89 |
inputs=gr.Image(type="pil"),
|
90 |
outputs=gr.Image(type="pil"),
|
91 |
examples=sample_images,
|
92 |
title="Object Detection (Battery and Dice)"
|
93 |
).launch(share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
return result_pil_img
|
71 |
|
72 |
|
73 |
+
def predict_on_video(video_in_filepath, video_out_filepath, detection_model, category_index):
|
74 |
+
video_reader = cv2.VideoCapture(video_in_filepath)
|
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(
|
80 |
+
video_out_filepath,
|
81 |
+
cv2.VideoWriter_fourcc(*'mp4v'),
|
82 |
+
fps,
|
83 |
+
(frame_w, frame_h)
|
84 |
+
)
|
85 |
+
while True:
|
86 |
+
ret, frame = video_reader.read()
|
87 |
+
if not ret:
|
88 |
+
break # Break the loop if the video is finished
|
89 |
+
|
90 |
+
processed_frame = predict(frame)
|
91 |
+
processed_frame_np = np.array(processed_frame)
|
92 |
+
video_writer.write(processed_frame_np)
|
93 |
+
|
94 |
+
# Release camera and close windows
|
95 |
+
video_reader.release()
|
96 |
+
video_writer.release()
|
97 |
+
cv2.destroyAllWindows()
|
98 |
+
cv2.waitKey(1)
|
99 |
+
video_reader.release()
|
100 |
+
video_writer.release()
|
101 |
+
cv2.destroyAllWindows()
|
102 |
+
cv2.waitKey(1)
|
103 |
+
|
104 |
+
# Function to process a video
|
105 |
+
def process_video(video_path):
|
106 |
+
output_path = "output_video.mp4" # Output path for the processed video
|
107 |
+
predict_on_video(video_path, output_path, detection_model, category_index)
|
108 |
+
return output_path
|
109 |
+
|
110 |
# Specify paths to example images
|
111 |
sample_images = [["00000031.jpg"], ["00000053.jpg"],
|
112 |
["00000057.jpg"], ["00000078.jpg"],
|
|
|
122 |
# predicted_img = predict(image_arr)
|
123 |
# predicted_img.save('predicted.jpg')
|
124 |
|
125 |
+
tab1 = gr.Interface(fn=predict,
|
126 |
inputs=gr.Image(type="pil"),
|
127 |
outputs=gr.Image(type="pil"),
|
128 |
examples=sample_images,
|
129 |
title="Object Detection (Battery and Dice)"
|
130 |
).launch(share=True)
|
131 |
+
|
132 |
+
tab2 = gr.Interface(
|
133 |
+
fn=process_video,
|
134 |
+
inputs=gr.File(label="Upload a video"),
|
135 |
+
outputs=gr.File(label="output"),
|
136 |
+
title='Video Processing',
|
137 |
+
examples=[["Three Dice Trick.mp4"],["Look at the fork and battery-in power.mp4"]
|
138 |
+
)
|
139 |
+
|
140 |
+
|
141 |
+
iface = gr.TabbedInterface([tab1, tab2], title='Battery and Dice detection')
|
142 |
+
|
143 |
+
iface.launch(share=True)
|