File size: 1,597 Bytes
b2141e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
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
import gradio as gr
import cv2
import requests
import os

from ultralytics import YOLO

file_urls = [
    "https://www.dropbox.com/scl/fi/2mlc191y9lzbe8nwu45ss/duck76.jpeg?rlkey=qwnr78mtdy7sjg71ldrui6vf0&dl=0",
    "https://www.dropbox.com/scl/fi/2y4cdkwwn3drlh4ob86ic/duck85.jpeg?rlkey=lcl3n0jav7ougsj4tamm1hh93&dl=0",
    "https://www.dropbox.com/scl/fi/8gojxnm8wwhs2isj6k4zv/duck23.jpeg?rlkey=2zioinhr0wfpv22qq963tnv8a&dl=0",
]

def download_file(url, save_name):
    if not os.path.exists(save_name):
        file = requests.get(url)
        open(save_name, "wb").write(file.content)

for i, url in enumerate(file_urls):
    if "mp4" in url:
        download_file(url, "video.mp4")
    else:
        download_file(url, f"image_{i}.jpg")

model = YOLO("best.pt")

def show_preds_image(image):
    outputs = model.predict(source=image)
    image_np = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
    results = outputs.xyxy[0].cpu().numpy()

    for det in results:
        cv2.rectangle(
            image_np,
            (int(det[0]), int(det[1])),
            (int(det[2]), int(det[3])),
            color=(0, 0, 255),
            thickness=2,
            lineType=cv2.LINE_AA,
        )
    return cv2.cvtColor(image_np, cv2.COLOR_BGR2RGB)

inputs_image = gr.inputs.Image(type="file", label="Input Image")
outputs_image = gr.outputs.Image(type="numpy", label="Output Image")
interface_image = gr.Interface(
    fn=show_preds_image,
    inputs=inputs_image,
    outputs=outputs_image,
    title="Duck Image Segmentation",
    examples=file_urls,
    allow_flagging=False,
)

interface_image.launch()