luisarizmendi
commited on
Commit
·
f0236bb
1
Parent(s):
6467553
v1
Browse files- app.py +12 -14
- requirements.txt +1 -0
app.py
CHANGED
@@ -3,23 +3,21 @@ 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(
|
16 |
"""
|
17 |
Processes uploaded images for object detection.
|
18 |
"""
|
19 |
if not files:
|
20 |
return "No files uploaded.", []
|
21 |
|
22 |
-
|
|
|
|
|
23 |
|
24 |
results_images = []
|
25 |
for file in files:
|
@@ -36,14 +34,14 @@ def detect_objects_in_files(model_input, files):
|
|
36 |
except Exception as e:
|
37 |
return f"Error processing file: {file}. Exception: {str(e)}", []
|
38 |
|
|
|
|
|
|
|
39 |
return "Processing completed.", results_images
|
40 |
|
41 |
-
|
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")
|
@@ -53,5 +51,5 @@ iface = gr.Interface(
|
|
53 |
)
|
54 |
|
55 |
if __name__ == "__main__":
|
56 |
-
|
57 |
|
|
|
3 |
from PIL import Image
|
4 |
import os
|
5 |
import cv2
|
6 |
+
import torch
|
7 |
+
|
8 |
|
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
+
def detect_objects_in_files(files):
|
12 |
"""
|
13 |
Processes uploaded images for object detection.
|
14 |
"""
|
15 |
if not files:
|
16 |
return "No files uploaded.", []
|
17 |
|
18 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
19 |
+
model = YOLO("https://github.com/luisarizmendi/ai-apps/raw/refs/heads/main/models/luisarizmendi/safety-hat/safety-hat-v1.pt")
|
20 |
+
model.to(device)
|
21 |
|
22 |
results_images = []
|
23 |
for file in files:
|
|
|
34 |
except Exception as e:
|
35 |
return f"Error processing file: {file}. Exception: {str(e)}", []
|
36 |
|
37 |
+
del model
|
38 |
+
torch.cuda.empty_cache()
|
39 |
+
|
40 |
return "Processing completed.", results_images
|
41 |
|
42 |
+
interface = gr.Interface(
|
43 |
fn=detect_objects_in_files,
|
44 |
+
inputs=gr.Files(file_types=["image"], label="Select Images"),
|
|
|
|
|
|
|
45 |
outputs=[
|
46 |
gr.Textbox(label="Status"),
|
47 |
gr.Gallery(label="Results")
|
|
|
51 |
)
|
52 |
|
53 |
if __name__ == "__main__":
|
54 |
+
interface.launch()
|
55 |
|
requirements.txt
CHANGED
@@ -2,3 +2,4 @@ gradio
|
|
2 |
ultralytics
|
3 |
Pillow
|
4 |
opencv-python
|
|
|
|
2 |
ultralytics
|
3 |
Pillow
|
4 |
opencv-python
|
5 |
+
torch
|