Update run_model.py
Browse files- run_model.py +18 -18
run_model.py
CHANGED
|
@@ -2,51 +2,51 @@ import gradio as gr
|
|
| 2 |
from ultralytics import YOLO
|
| 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 |
-
model = YOLO(
|
| 19 |
-
|
| 20 |
if torch.cuda.is_available():
|
| 21 |
-
model.to('cuda')
|
| 22 |
print("Using GPU for inference")
|
| 23 |
else:
|
| 24 |
print("Using CPU for inference")
|
| 25 |
-
|
| 26 |
-
|
| 27 |
results_images = []
|
| 28 |
for file in files:
|
| 29 |
try:
|
| 30 |
image = Image.open(file).convert("RGB")
|
| 31 |
-
results = model(image)
|
| 32 |
result_img_bgr = results[0].plot()
|
| 33 |
result_img_rgb = cv2.cvtColor(result_img_bgr, cv2.COLOR_BGR2RGB)
|
| 34 |
-
results_images.append(result_img_rgb)
|
| 35 |
-
|
| 36 |
# If you want that images appear one by one (slower)
|
| 37 |
-
#yield "Processing image...", results_images
|
| 38 |
-
|
| 39 |
except Exception as e:
|
| 40 |
return f"Error processing file: {file}. Exception: {str(e)}", []
|
| 41 |
|
| 42 |
-
del model
|
| 43 |
torch.cuda.empty_cache()
|
| 44 |
-
|
| 45 |
return "Processing completed.", results_images
|
| 46 |
|
| 47 |
interface = gr.Interface(
|
| 48 |
fn=detect_objects_in_files,
|
| 49 |
-
inputs=
|
|
|
|
|
|
|
|
|
|
| 50 |
outputs=[
|
| 51 |
gr.Textbox(label="Status"),
|
| 52 |
gr.Gallery(label="Results")
|
|
|
|
| 2 |
from ultralytics import YOLO
|
| 3 |
from PIL import Image
|
| 4 |
import os
|
| 5 |
+
import cv2
|
| 6 |
+
import torch
|
| 7 |
|
| 8 |
+
DEFAULT_MODEL_URL = "https://github.com/luisarizmendi/ai-apps/raw/refs/heads/main/models/luisarizmendi/object-detector-hardhat-or-hat/object-detector-hardhat-or-hat.pt"
|
| 9 |
|
| 10 |
+
def detect_objects_in_files(model_input, files):
|
|
|
|
|
|
|
| 11 |
"""
|
| 12 |
Processes uploaded images for object detection.
|
| 13 |
"""
|
| 14 |
if not files:
|
| 15 |
return "No files uploaded.", []
|
| 16 |
|
| 17 |
+
model = YOLO(str(model_input))
|
|
|
|
| 18 |
if torch.cuda.is_available():
|
| 19 |
+
model.to('cuda')
|
| 20 |
print("Using GPU for inference")
|
| 21 |
else:
|
| 22 |
print("Using CPU for inference")
|
| 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 |
+
del model
|
| 40 |
torch.cuda.empty_cache()
|
| 41 |
+
|
| 42 |
return "Processing completed.", results_images
|
| 43 |
|
| 44 |
interface = gr.Interface(
|
| 45 |
fn=detect_objects_in_files,
|
| 46 |
+
inputs=[
|
| 47 |
+
gr.Textbox(value=DEFAULT_MODEL_URL, label="Model URL", placeholder="Enter the model URL"),
|
| 48 |
+
gr.Files(file_types=["image"], label="Select Images"),
|
| 49 |
+
],
|
| 50 |
outputs=[
|
| 51 |
gr.Textbox(label="Status"),
|
| 52 |
gr.Gallery(label="Results")
|