Upload 2 files
Browse files- app.py +56 -58
- requirements.txt +1 -1
app.py
CHANGED
@@ -1,58 +1,56 @@
|
|
1 |
-
from clearml import Model
|
2 |
-
from ultralytics import YOLO
|
3 |
-
from PIL import Image, ImageDraw
|
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 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
)
|
56 |
-
|
57 |
-
# Launch the Gradio app
|
58 |
-
interface.launch(share=True)
|
|
|
1 |
+
from clearml import Model
|
2 |
+
from ultralytics import YOLO
|
3 |
+
from PIL import Image, ImageDraw
|
4 |
+
from huggingface_hub import hf_hub_download
|
5 |
+
import gradio as gr
|
6 |
+
|
7 |
+
|
8 |
+
# fetching the model from the ClearML
|
9 |
+
def fetch_model_from_HG():
|
10 |
+
try:
|
11 |
+
hg_model = hf_hub_download(repo_id="manvi23/GroupG", filename="best.pt")
|
12 |
+
return hg_model
|
13 |
+
except Exception as e:
|
14 |
+
print(f"Failed to fetch model from HuggingFace: {e}")
|
15 |
+
raise
|
16 |
+
|
17 |
+
model_path = fetch_model_from_HG()
|
18 |
+
model = YOLO(model_path)
|
19 |
+
|
20 |
+
|
21 |
+
def predict_and_visualize(image, confidence_threshold=0.3):
|
22 |
+
results = model.predict(image, conf=confidence_threshold)
|
23 |
+
|
24 |
+
boxes = results[0].boxes.xyxy.cpu().numpy() # Bounding box coordinates
|
25 |
+
scores = results[0].boxes.conf.cpu().numpy() # Confidence scores
|
26 |
+
|
27 |
+
# Draw bounding boxes on the image
|
28 |
+
draw = ImageDraw.Draw(image)
|
29 |
+
for box, score in zip(boxes, scores):
|
30 |
+
x_min, y_min, x_max, y_max = box
|
31 |
+
draw.rectangle([x_min, y_min, x_max, y_max], outline="red", width=3)
|
32 |
+
draw.text((x_min, y_min), f"{score:.2f}", fill="red")
|
33 |
+
|
34 |
+
return image
|
35 |
+
|
36 |
+
|
37 |
+
# Gradio interface function
|
38 |
+
def gradio_app(image):
|
39 |
+
result_image = predict_and_visualize(image)
|
40 |
+
return result_image
|
41 |
+
|
42 |
+
|
43 |
+
# Create Gradio app
|
44 |
+
interface = gr.Interface(
|
45 |
+
fn=gradio_app,
|
46 |
+
inputs=[
|
47 |
+
gr.Image(type="pil"), # Input image
|
48 |
+
],
|
49 |
+
outputs=gr.Image(type="pil"), # Output image
|
50 |
+
title="Object Detection with model of Group G",
|
51 |
+
description="Upload an image to detect the objects.",
|
52 |
+
)
|
53 |
+
|
54 |
+
# Launch the Gradio app
|
55 |
+
interface.launch(share=True)
|
56 |
+
|
|
|
|
requirements.txt
CHANGED
@@ -4,4 +4,4 @@ clearml==1.16.4
|
|
4 |
ultralytics==8.3.49
|
5 |
numpy==1.26.4
|
6 |
pillow==11.0.0
|
7 |
-
|
|
|
4 |
ultralytics==8.3.49
|
5 |
numpy==1.26.4
|
6 |
pillow==11.0.0
|
7 |
+
huggingface_hub
|