luisarizmendi commited on
Commit
8e18127
·
1 Parent(s): 754d539

Add application

Browse files
Files changed (1) hide show
  1. app.py +57 -0
app.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from ultralytics import YOLO
3
+ from PIL import Image
4
+ import os
5
+ import cv2
6
+
7
+
8
+ def load_model(model_input):
9
+ """
10
+ Load the model either from a file or from Hugging Face.
11
+ """
12
+ model = YOLO(model_input)
13
+ return model
14
+
15
+ def detect_objects_in_files(model_input, files):
16
+ """
17
+ Processes uploaded images for object detection.
18
+ """
19
+ if not files:
20
+ return "No files uploaded.", []
21
+
22
+ model = load_model(model_input)
23
+
24
+ results_images = []
25
+ for file in files:
26
+ try:
27
+ image = Image.open(file).convert("RGB")
28
+ results = model(image)
29
+ result_img_bgr = results[0].plot()
30
+ result_img_rgb = cv2.cvtColor(result_img_bgr, cv2.COLOR_BGR2RGB)
31
+ results_images.append(result_img_rgb)
32
+
33
+ # If you want that images appear one by one (slower)
34
+ #yield "Processing image...", results_images
35
+
36
+ except Exception as e:
37
+ return f"Error processing file: {file}. Exception: {str(e)}", []
38
+
39
+ return "Processing completed.", results_images
40
+
41
+ interface = gr.Interface(
42
+ fn=detect_objects_in_files,
43
+ inputs=[
44
+ gr.File(label="Upload Model file"),
45
+ gr.Files(file_types=["image"], label="Select Images"),
46
+ ],
47
+ outputs=[
48
+ gr.Textbox(label="Status"),
49
+ gr.Gallery(label="Results")
50
+ ],
51
+ title="Object Detection on Images",
52
+ description="Upload images to perform object detection. The model will process each image and display the results."
53
+ )
54
+
55
+ if __name__ == "__main__":
56
+ interface.launch(server_name="0.0.0.0", server_port=8800)
57
+