Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
+
# coding: utf-8
|
3 |
+
|
4 |
+
# In[4]:
|
5 |
+
|
6 |
+
|
7 |
+
from ultralytics import YOLO
|
8 |
+
from PIL import Image
|
9 |
+
import gradio as gr
|
10 |
+
from huggingface_hub import snapshot_download
|
11 |
+
import os
|
12 |
+
|
13 |
+
model_path = "best_int8_openvino_model"
|
14 |
+
|
15 |
+
def load_model(repo_id):
|
16 |
+
download_dir = snapshot_download(repo_id)
|
17 |
+
print(download_dir)
|
18 |
+
path = os.path.join(download_dir, "best_int8_openvino_model")
|
19 |
+
print(path)
|
20 |
+
detection_model = YOLO(path, task='detect')
|
21 |
+
return detection_model
|
22 |
+
|
23 |
+
|
24 |
+
def predict(pilimg):
|
25 |
+
|
26 |
+
source = pilimg
|
27 |
+
# x = np.asarray(pilimg)
|
28 |
+
# print(x.shape)
|
29 |
+
result = detection_model.predict(source, conf=0.4, iou=0.6)
|
30 |
+
img_bgr = result[0].plot()
|
31 |
+
out_pilimg = Image.fromarray(img_bgr[..., ::-1]) # RGB-order PIL image
|
32 |
+
|
33 |
+
return out_pilimg
|
34 |
+
|
35 |
+
|
36 |
+
REPO_ID = "ITI107-2024S2/8035531F"
|
37 |
+
detection_model = load_model(REPO_ID)
|
38 |
+
|
39 |
+
title = "Detect Durian and Mangosteen (King and Queen of Fruits) In The Image"
|
40 |
+
interface = gr.Interface(
|
41 |
+
fn=predict,
|
42 |
+
inputs=gr.Image(type="pil", label="Input Image"),
|
43 |
+
outputs=gr.Image(type="pil", label="Object Detected Image"),
|
44 |
+
title=title,
|
45 |
+
)
|
46 |
+
|
47 |
+
# Launch the interface
|
48 |
+
interface.launch(share=True)
|
49 |
+
|
50 |
+
|
51 |
+
# In[ ]:
|
52 |
+
|
53 |
+
|
54 |
+
|
55 |
+
|