Spaces:
Sleeping
Sleeping
Add application file
Browse files
app.py
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from ultralytics import YOLO
|
2 |
+
from PIL import Image
|
3 |
+
import gradio as gr
|
4 |
+
from huggingface_hub import snapshot_download
|
5 |
+
import os
|
6 |
+
|
7 |
+
# Define the Hugging Face repository ID and the model path
|
8 |
+
REPO_ID = "aleong888/snoopy" # Replace with your Hugging Face repository ID
|
9 |
+
|
10 |
+
# Function to load the model from the repository
|
11 |
+
def load_model(repo_id):
|
12 |
+
download_dir = snapshot_download(repo_id)
|
13 |
+
print("Downloaded model to:", download_dir)
|
14 |
+
model_path = os.path.join(download_dir, "best_int8_openvino_model")
|
15 |
+
print("Model path:", model_path)
|
16 |
+
detection_model = YOLO(model_path, task='detect')
|
17 |
+
return detection_model
|
18 |
+
|
19 |
+
# Function to make predictions on input images
|
20 |
+
def predict(pilimg):
|
21 |
+
result = detection_model.predict(pilimg, conf=0.5, iou=0.4)
|
22 |
+
img_bgr = result[0].plot()
|
23 |
+
out_pilimg = Image.fromarray(img_bgr[..., ::-1]) # Convert BGR to RGB
|
24 |
+
return out_pilimg
|
25 |
+
|
26 |
+
# Load the model
|
27 |
+
detection_model = load_model(REPO_ID)
|
28 |
+
|
29 |
+
# Create the Gradio interface
|
30 |
+
gr.Interface(fn=predict,
|
31 |
+
inputs=gr.Image(type="pil", label="Upload an image"),
|
32 |
+
outputs=gr.Image(type="pil", label="Detection Result"),
|
33 |
+
title="Snoopy Detection Model",
|
34 |
+
description="Upload an image to detect Snoopy and Woodstock characters. This application uses a YOLOv8 model trained with OpenVINO optimization."
|
35 |
+
).launch(share=True)
|