cham_diem_vsk / app.py
linhcuem's picture
Update app.py
3b14e8b
raw
history blame
2.58 kB
import json
import gradio as gr
import yolov5
from PIL import Image
from huggingface_hub import hf_hub_download
import os
app_title = "Detect san pham VSK"
models_ids = ['linhcuem/gold_yolov5m','linhcuem/yolov5m_chamdiem_raw13','linhcuem/yolov5m_cham_diemraw15', 'linhcuem/yolov5m_aHieu_ver15','linhcuem/yolov5m6_raw17_yaml', 'linhcuem/yolov5m_chamdiem_ver1',
'linhcuem/cham_diemraw16', 'linhcuem/yolov5m_chamdiem_ver2', 'linhcuem/yolov5m6_cham_diemraw17','linhcuem/yolov5m_chamdiem_ver7', 'linhcuem/yolov5m_chamdiem_ver8', 'linhcuem/yolov5m_chamdiem_ver10',
'linhcuem/yolov5_chamdiem_ver9']
current_model_id = models_ids[-1]
model = yolov5.load(current_model_id)
examples = [['test_images/yen thien viet_4.jpg', 0.25, 'linhcuem/gold_yolov5m'], ['test_images/yen thien viet_6.jpg', 0.25, 'linhcuem/gold_yolov5m'], ['test_images/yen thien viet_7.jpg', 0.25, 'linhcuem/gold_yolov5m'], ['test_images/yen thien viet_7.jpg', 0.25, 'linhcuem/gold_yolov5m'],
['test_images/yen thien viet_8.jpg', 0.25, 'linhcuem/gold_yolov5m'], ['test_images/yen thien viet_9.jpg', 0.25, 'linhcuem/gold_yolov5m'], ['test_images/yen thien viet_94.jpg', 0.25, 'linhcuem/gold_yolov5m'], ['test_images/yen thien viet_13.jpg', 0.25, 'linhcuem/gold_yolov5m'],
['test_images/yen thien viet_16.jpg', 0.25, 'linhcuem/gold_yolov5m'], ['test_images/yen thien viet_19.jpg', 0.25, 'linhcuem/gold_yolov5m'], ['test_images/yen thien viet_13.jpg', 0.25, 'linhcuem/gold_yolov5m']]
def predict(image, threshold=0.25, model_id=None):
#update model if required
global current_model_id
global model
if model_id != current_model_id:
model = yolov5.load(model_id)
# model_yolov8 = YOLO(DEFAULT_DET_MODEL_ID_yolov8)
current_model_id = model_id
# get model input size
config_path = hf_hub_download(repo_id=model_id, filename="config.json")
with open(config_path, "r") as f:
config = json.load(f)
input_size = config["input_size"]
#perform inference
model.conf = threshold
results = model(image, size=input_size)
numpy_image = results.render()[0]
output_image = Image.fromarray(numpy_image)
return output_image
gr.Interface(
title=app_title,
description="DO ANH DAT",
fn=predict,
inputs=[
gr.Image(type="pil"),
gr.Slider(maximum=1, step=0.01, value=0.25),
gr.Dropdown(models_ids, value=models_ids[-1]),
],
outputs=gr.Image(type="pil"),
examples=examples,
cache_examples=True if examples else Fale,
).launch(enable_queue=True)