Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,43 +1,43 @@
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
from PIL import Image
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
#
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
)
|
41 |
|
42 |
# Launch the interface
|
43 |
-
|
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
from PIL import Image
|
4 |
+
import torchvision.transforms as T
|
5 |
+
|
6 |
+
# Load the trained model (YOLOv8n) with your weights
|
7 |
+
model = torch.hub.load('ultralytics/yolov8', 'yolov8n')
|
8 |
+
model.load_state_dict(torch.load("best_p6.pt"))
|
9 |
+
model.eval()
|
10 |
+
|
11 |
+
# Define the image transformation (if required, based on your dataset preprocessing)
|
12 |
+
transform = T.Compose([T.ToTensor()])
|
13 |
+
|
14 |
+
# Define the inference function
|
15 |
+
def process_image(image):
|
16 |
+
# Convert the image to tensor and make inference
|
17 |
+
image_tensor = transform(image).unsqueeze(0) # Add batch dimension
|
18 |
+
with torch.no_grad():
|
19 |
+
outputs = model(image_tensor)
|
20 |
+
|
21 |
+
# Get the output image with bounding boxes (you can adjust this part based on your model's output)
|
22 |
+
result_image = outputs.render()[0] # This will render bounding boxes on the image
|
23 |
+
|
24 |
+
# Convert to PIL image for easy download
|
25 |
+
result_pil_image = Image.fromarray(result_image)
|
26 |
+
|
27 |
+
# Save the output image for download
|
28 |
+
output_path = "/tmp/output_image.jpg"
|
29 |
+
result_pil_image.save(output_path)
|
30 |
+
|
31 |
+
return output_path
|
32 |
+
|
33 |
+
# Define Gradio interface
|
34 |
+
iface = gr.Interface(
|
35 |
+
fn=process_image,
|
36 |
+
inputs=gr.Image(type="pil"), # Image input from user
|
37 |
+
outputs=gr.File(label="Download Processed Image"), # Provide the file output for download
|
38 |
+
title="Waste Detection", # Interface title
|
39 |
+
description="Upload an image of floating waste, and the model will detect and label the objects in it."
|
40 |
)
|
41 |
|
42 |
# Launch the interface
|
43 |
+
iface.launch()
|