Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""import gradio as gr
|
2 |
+
import torch
|
3 |
+
from PIL import Image
|
4 |
+
from pathlib import Path
|
5 |
+
import numpy as np
|
6 |
+
|
7 |
+
# Load YOLO Model
|
8 |
+
model = torch.hub.load('ultralytics/yolov5', 'custom', path='https://huggingface.co/ayoubsa/yolo_model/blob/main/best.pt') # Replace 'model.pt' with your uploaded model's path
|
9 |
+
|
10 |
+
# Function to make predictions
|
11 |
+
def predict(image):
|
12 |
+
# Convert input to PIL image if it's not
|
13 |
+
if isinstance(image, np.ndarray):
|
14 |
+
image = Image.fromarray(image)
|
15 |
+
|
16 |
+
# Run inference
|
17 |
+
results = model(image)
|
18 |
+
|
19 |
+
# Extract predictions (bounding boxes, labels, confidence scores)
|
20 |
+
predictions = results.pandas().xyxy[0] # Pandas dataframe
|
21 |
+
annotated_image = np.array(results.render()[0]) # Annotated image as NumPy array
|
22 |
+
|
23 |
+
return annotated_image, predictions[['name', 'confidence']].to_dict(orient="records")
|
24 |
+
|
25 |
+
# Create Gradio Interface
|
26 |
+
image_input = gr.inputs.Image(type="numpy", label="Input Image")
|
27 |
+
output_image = gr.outputs.Image(type="numpy", label="Annotated Image")
|
28 |
+
output_text = gr.outputs.JSON(label="Predictions (Labels & Confidence)")
|
29 |
+
|
30 |
+
interface = gr.Interface(
|
31 |
+
fn=predict,
|
32 |
+
inputs=image_input,
|
33 |
+
outputs=[output_image, output_text],
|
34 |
+
title="YOLO Object Detection",
|
35 |
+
description="Upload an image to detect objects using YOLO.",
|
36 |
+
examples=["example1.jpg", "example2.jpg", "example3.jpg"] # Provide paths to example images
|
37 |
+
)
|
38 |
+
|
39 |
+
interface.launch()"""
|
40 |
+
|
41 |
+
|
42 |
+
from datasets import load_dataset
|
43 |
+
import gradio as gr
|
44 |
+
import random
|
45 |
+
import numpy as np
|
46 |
+
from PIL import Image
|
47 |
+
import torch
|
48 |
+
|
49 |
+
# Load the YOLO model
|
50 |
+
model = torch.hub.load('ultralytics/yolov5', 'custom', path='model.pt') # Replace with your uploaded model's path
|
51 |
+
|
52 |
+
# Load your dataset from Hugging Face
|
53 |
+
dataset = load_dataset("username/your_dataset_name") # Replace with your dataset's repository name on Hugging Face
|
54 |
+
|
55 |
+
# Function to get random examples from the dataset
|
56 |
+
def get_random_examples(dataset, num_examples=3):
|
57 |
+
images = dataset['train'][:]['image'] # Assuming the images are in the 'train' split and column 'image'
|
58 |
+
random_examples = random.sample(images, num_examples)
|
59 |
+
return random_examples
|
60 |
+
|
61 |
+
# Define the prediction function
|
62 |
+
def predict(image):
|
63 |
+
results = model(image) # Perform object detection using YOLO
|
64 |
+
results.render() # Render bounding boxes on the image
|
65 |
+
output_image = Image.fromarray(results.imgs[0]) # Convert to PIL image
|
66 |
+
return output_image
|
67 |
+
|
68 |
+
# Get examples for Gradio app
|
69 |
+
example_images = get_random_examples(dataset, num_examples=3)
|
70 |
+
|
71 |
+
# Create the Gradio interface
|
72 |
+
iface = gr.Interface(
|
73 |
+
fn=predict,
|
74 |
+
inputs=gr.inputs.Image(type="pil"), # PIL Image for compatibility with YOLO
|
75 |
+
outputs=gr.outputs.Image(type="pil"),
|
76 |
+
examples=example_images # Linking examples directly from Hugging Face dataset
|
77 |
+
)
|
78 |
+
|
79 |
+
# Launch the Gradio app
|
80 |
+
iface.launch()
|
81 |
+
|