Update app.py
Browse files
app.py
CHANGED
@@ -4,19 +4,21 @@ import cv2
|
|
4 |
import numpy as np
|
5 |
import os
|
6 |
import tempfile
|
|
|
|
|
7 |
from PIL import Image
|
8 |
|
9 |
-
def perform(image):
|
10 |
# Predict with the model
|
11 |
results = model(image, save=True) # predict on an image
|
12 |
|
13 |
# get the saved prediction
|
14 |
prediction_image = Image.open(os.path.join(results[0].save_dir, os.listdir(results[0].save_dir)[0]))
|
15 |
|
16 |
-
#
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
|
21 |
# initialize the output list to save the bbox
|
22 |
outputs = []
|
@@ -28,8 +30,11 @@ def perform(image):
|
|
28 |
confs = float(result.boxes.conf) # confidence score of each box
|
29 |
outputs.append({"xywh": xywh, "conf": confs, "name": name})
|
30 |
|
31 |
-
#
|
32 |
-
|
|
|
|
|
|
|
33 |
|
34 |
def load_model(model_name: str = "yolo11n.pt"):
|
35 |
"""Model loading"""
|
@@ -47,7 +52,7 @@ model = load_model()
|
|
47 |
demo = gr.Interface(
|
48 |
fn=perform,
|
49 |
inputs=[gr.Image(type="pil")], # PIL Image format
|
50 |
-
outputs=[gr.Image(type="
|
51 |
title="Object Detection using YOLO",
|
52 |
description="Upload an image and get image with bbox in it",
|
53 |
examples=None # You can add example images here if needed
|
@@ -55,4 +60,4 @@ demo = gr.Interface(
|
|
55 |
|
56 |
# Launch the app
|
57 |
if __name__ == "__main__":
|
58 |
-
demo.launch()
|
|
|
4 |
import numpy as np
|
5 |
import os
|
6 |
import tempfile
|
7 |
+
import base64
|
8 |
+
from io import BytesIO
|
9 |
from PIL import Image
|
10 |
|
11 |
+
def perform(image, task: str="detect"):
|
12 |
# Predict with the model
|
13 |
results = model(image, save=True) # predict on an image
|
14 |
|
15 |
# get the saved prediction
|
16 |
prediction_image = Image.open(os.path.join(results[0].save_dir, os.listdir(results[0].save_dir)[0]))
|
17 |
|
18 |
+
# Convert PIL image to base64
|
19 |
+
buffered = BytesIO()
|
20 |
+
prediction_image.save(buffered, format="JPEG")
|
21 |
+
img_base64 = base64.b64encode(buffered.getvalue()).decode('utf-8')
|
22 |
|
23 |
# initialize the output list to save the bbox
|
24 |
outputs = []
|
|
|
30 |
confs = float(result.boxes.conf) # confidence score of each box
|
31 |
outputs.append({"xywh": xywh, "conf": confs, "name": name})
|
32 |
|
33 |
+
# Add base64 image to outputs
|
34 |
+
outputs.append({"image_base64": f"data:image/jpeg;base64,{img_base64}"})
|
35 |
+
|
36 |
+
# return the PIL image (for display) and the outputs with base64
|
37 |
+
return prediction_image, outputs
|
38 |
|
39 |
def load_model(model_name: str = "yolo11n.pt"):
|
40 |
"""Model loading"""
|
|
|
52 |
demo = gr.Interface(
|
53 |
fn=perform,
|
54 |
inputs=[gr.Image(type="pil")], # PIL Image format
|
55 |
+
outputs=[gr.Image(type="pil"), gr.JSON()], # Back to PIL - Gradio handles public URL automatically
|
56 |
title="Object Detection using YOLO",
|
57 |
description="Upload an image and get image with bbox in it",
|
58 |
examples=None # You can add example images here if needed
|
|
|
60 |
|
61 |
# Launch the app
|
62 |
if __name__ == "__main__":
|
63 |
+
demo.launch()
|