TW_plate_number / app.py
stphtan94117's picture
Update app.py
f4cdc36
import gradio as gr
import torch
from PIL import Image
import json
from ultralytics import YOLO
# Images
torch.hub.download_url_to_file(
'https://i.imgur.com/4GmZXID.jpg', '1.jpg')
torch.hub.download_url_to_file(
'https://i.imgur.com/ktIGRvs.jpg', '2.jpg')
torch.hub.download_url_to_file(
'https://i.imgur.com/fSEsXoE.jpg', '3.jpg')
torch.hub.download_url_to_file(
'https://i.imgur.com/lsVJRzd.jpg', '4.jpg')
torch.hub.download_url_to_file(
'https://i.imgur.com/1OFmJd1.jpg', '5.jpg')
torch.hub.download_url_to_file(
'https://i.imgur.com/GhfAWMJ.jpg', '6.jpg')
# Model
# model = torch.hub.load('ultralytics/yolov5', 'yolov5s') # force_reload=True to update
model = torch.hub.load('ultralytics/yolov5', 'custom', path='plate.pt', source="local")
def yolo(im):
model.conf = 0.6 # NMS confidence threshold
# g = (size / max(im.size)) # gain
# im = im.resize((int(x * g) for x in im.size), Image.ANTIALIAS) # resize
results = model(im, size=1280) # inference
results.render() # updates results.imgs with boxes and labels
df = results.pandas().xyxy[0].sort_values('xmin')[['name']].to_json(orient="records") # 可以把[['name']]刪除即可顯示全部
res = json.loads(df)
return [Image.fromarray(results.ims[0]), res]
inputs = gr.inputs.Image(type='pil', label="Original Image")
outputs = [gr.outputs.Image(type="pil", label="Output Image"),
gr.outputs.JSON(label="Output JSON")]
title = "TW_plate_number"
description = "TW_plate_number"
# article = "<p style='text-align: center'>TW_plate_number <a href=\"http://codh.rois.ac.jp/char-shape/\">日本古典籍くずし字データセット</a>.</p>"
examples = [['1.jpg'], ['2.jpg'], ['3.jpg'], ['4.jpg'], ['5.jpg'], ['6.jpg']]
gr.Interface(yolo, inputs, outputs, title=title, description=description, examples=examples, theme="huggingface").launch(enable_queue=True) # cache_examples=True,