Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -157,46 +157,6 @@ def predict_image(model_name, image, confidence, models):
|
|
157 |
except Exception as e:
|
158 |
return f"β Error during prediction: {str(e)}", None, None
|
159 |
|
160 |
-
def predict_video(model_name, video, confidence, models):
|
161 |
-
"""
|
162 |
-
Perform prediction on an uploaded video using the selected YOLO model.
|
163 |
-
|
164 |
-
Args:
|
165 |
-
model_name (str): The name of the selected model.
|
166 |
-
video (str): Path to the uploaded video file.
|
167 |
-
confidence (float): The confidence threshold for detections.
|
168 |
-
models (dict): The dictionary containing models and their info.
|
169 |
-
|
170 |
-
Returns:
|
171 |
-
tuple: A status message, the processed video, and the path to the output video.
|
172 |
-
"""
|
173 |
-
model_entry = models.get(model_name, {})
|
174 |
-
model = model_entry.get('model', None)
|
175 |
-
if not model:
|
176 |
-
return "Error: Model not found.", None, None
|
177 |
-
try:
|
178 |
-
|
179 |
-
os.makedirs(TEMP_DIR, exist_ok=True)
|
180 |
-
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
181 |
-
|
182 |
-
input_video_path = os.path.join(TEMP_DIR, f"{model_name}_input_video.mp4")
|
183 |
-
shutil.copy(video, input_video_path)
|
184 |
-
|
185 |
-
results = model(input_video_path, save=True, save_txt=False, conf=confidence)
|
186 |
-
|
187 |
-
latest_run = sorted(Path("runs/detect").glob("predict*"), key=os.path.getmtime)[-1]
|
188 |
-
output_video_path = os.path.join(latest_run, Path(input_video_path).name)
|
189 |
-
if not os.path.isfile(output_video_path):
|
190 |
-
|
191 |
-
output_video_path = results[0].save()[0]
|
192 |
-
|
193 |
-
final_output_path = os.path.join(OUTPUT_DIR, f"{model_name}_output_video.mp4")
|
194 |
-
shutil.copy(output_video_path, final_output_path)
|
195 |
-
|
196 |
-
return "β
Prediction completed successfully.", final_output_path, final_output_path
|
197 |
-
except Exception as e:
|
198 |
-
return f"β Error during prediction: {str(e)}", None, None
|
199 |
-
|
200 |
def main():
|
201 |
|
202 |
models = load_models()
|
@@ -208,7 +168,7 @@ def main():
|
|
208 |
gr.Markdown("# π§ͺ YOLOv11 Model Tester")
|
209 |
gr.Markdown(
|
210 |
"""
|
211 |
-
Upload images
|
212 |
"""
|
213 |
)
|
214 |
|
@@ -247,53 +207,29 @@ def main():
|
|
247 |
info="Adjust the minimum confidence required for detections to be displayed."
|
248 |
)
|
249 |
|
250 |
-
with gr.
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
type='pil',
|
256 |
-
label="Upload Image for Prediction"
|
257 |
-
|
258 |
-
)
|
259 |
-
image_predict_btn = gr.Button("π Predict on Image")
|
260 |
-
image_status = gr.Markdown("**Status will appear here.**")
|
261 |
-
image_output = gr.Image(label="Predicted Image")
|
262 |
-
image_download_btn = gr.File(label="β¬οΈ Download Predicted Image")
|
263 |
-
|
264 |
-
def process_image(selected_display_name, image, confidence):
|
265 |
-
if not selected_display_name:
|
266 |
-
return "β Please select a model.", None, None
|
267 |
-
model_name = display_to_name.get(selected_display_name)
|
268 |
-
return predict_image(model_name, image, confidence, models)
|
269 |
-
|
270 |
-
image_predict_btn.click(
|
271 |
-
fn=process_image,
|
272 |
-
inputs=[model_dropdown, image_input, confidence_slider],
|
273 |
-
outputs=[image_status, image_output, image_download_btn]
|
274 |
-
)
|
275 |
|
276 |
-
with gr.Tab("π₯ Video"):
|
277 |
-
with gr.Column():
|
278 |
-
video_input = gr.Video(
|
279 |
-
label="Upload Video for Prediction"
|
280 |
-
)
|
281 |
-
video_predict_btn = gr.Button("π Predict on Video")
|
282 |
-
video_status = gr.Markdown("**Status will appear here.**")
|
283 |
-
video_output = gr.Video(label="Predicted Video")
|
284 |
-
video_download_btn = gr.File(label="β¬οΈ Download Predicted Video")
|
285 |
-
|
286 |
-
def process_video(selected_display_name, video, confidence):
|
287 |
-
if not selected_display_name:
|
288 |
-
return "β Please select a model.", None, None
|
289 |
-
model_name = display_to_name.get(selected_display_name)
|
290 |
-
return predict_video(model_name, video, confidence, models)
|
291 |
-
|
292 |
-
video_predict_btn.click(
|
293 |
-
fn=process_video,
|
294 |
-
inputs=[model_dropdown, video_input, confidence_slider],
|
295 |
-
outputs=[video_status, video_output, video_download_btn]
|
296 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
297 |
|
298 |
gr.Markdown(
|
299 |
"""
|
@@ -305,4 +241,4 @@ def main():
|
|
305 |
demo.launch()
|
306 |
|
307 |
if __name__ == "__main__":
|
308 |
-
main()
|
|
|
157 |
except Exception as e:
|
158 |
return f"β Error during prediction: {str(e)}", None, None
|
159 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
160 |
def main():
|
161 |
|
162 |
models = load_models()
|
|
|
168 |
gr.Markdown("# π§ͺ YOLOv11 Model Tester")
|
169 |
gr.Markdown(
|
170 |
"""
|
171 |
+
Upload images to test different YOLOv11 models. Select a model from the dropdown to see its details.
|
172 |
"""
|
173 |
)
|
174 |
|
|
|
207 |
info="Adjust the minimum confidence required for detections to be displayed."
|
208 |
)
|
209 |
|
210 |
+
with gr.Tab("πΌοΈ Image"):
|
211 |
+
with gr.Column():
|
212 |
+
image_input = gr.Image(
|
213 |
+
type='pil',
|
214 |
+
label="Upload Image for Prediction"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
215 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
216 |
)
|
217 |
+
image_predict_btn = gr.Button("π Predict on Image")
|
218 |
+
image_status = gr.Markdown("**Status will appear here.**")
|
219 |
+
image_output = gr.Image(label="Predicted Image")
|
220 |
+
image_download_btn = gr.File(label="β¬οΈ Download Predicted Image")
|
221 |
+
|
222 |
+
def process_image(selected_display_name, image, confidence):
|
223 |
+
if not selected_display_name:
|
224 |
+
return "β Please select a model.", None, None
|
225 |
+
model_name = display_to_name.get(selected_display_name)
|
226 |
+
return predict_image(model_name, image, confidence, models)
|
227 |
+
|
228 |
+
image_predict_btn.click(
|
229 |
+
fn=process_image,
|
230 |
+
inputs=[model_dropdown, image_input, confidence_slider],
|
231 |
+
outputs=[image_status, image_output, image_download_btn]
|
232 |
+
)
|
233 |
|
234 |
gr.Markdown(
|
235 |
"""
|
|
|
241 |
demo.launch()
|
242 |
|
243 |
if __name__ == "__main__":
|
244 |
+
main()
|