|
import numpy as np |
|
from PIL import Image |
|
import gradio as gr |
|
from ultralytics import YOLO |
|
from ultralytics.yolo.utils.ops import scale_image |
|
import cv2 |
|
|
|
|
|
m_raw_model = YOLO("M-Raw.pt") |
|
n_raw_model = YOLO("N-Raw.pt") |
|
s_raw_model = YOLO("S-Raw.pt") |
|
|
|
def snap(image, model, conf, iou): |
|
|
|
image = np.array(image) |
|
|
|
|
|
results = None |
|
if model == "M-Raw": |
|
results = m_raw_model(image, conf=conf, iou=iou) |
|
elif model == "N-Raw": |
|
results = n_raw_model(image, conf=conf, iou=iou) |
|
elif model == "S-Raw": |
|
results = s_raw_model(image, conf=conf, iou=iou) |
|
|
|
|
|
for result in results: |
|
classes = result.boxes.cls.cpu().numpy() |
|
probs = result.boxes.conf.cpu().numpy() |
|
boxes = result.boxes.xyxy[0].cpu().numpy() |
|
|
|
for i in range(len(boxes)): |
|
x1, y1, x2, y2 = boxes[i] |
|
x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2) |
|
cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2) |
|
cv2.putText(image, f"{classes[i]} {probs[i]:.2f}", (x1, y1), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2) |
|
|
|
|
|
resulting_image = Image.fromarray(image) |
|
|
|
|
|
labels = results.pandas().xyxy[0]["name"].values |
|
|
|
|
|
|
|
|
|
return [resulting_image] |
|
|
|
|
|
demo = gr.Interface( |
|
snap, |
|
[gr.Image(source="webcam", tool=None, streaming=True), gr.inputs.Radio(["M-Raw", "S-Raw", "N-Raw"], value="M-Raw"), gr.Slider(0, 1, value=0.6, label="Classifier Confidence Threshold"), gr.Slider(0, 1, value=0.7, label="IoU Threshold")], |
|
["image"], |
|
title="Baybayin Instance Detection" |
|
) |
|
|
|
if __name__ == "__main__": |
|
demo.launch() |