import gradio as gr import torch from PIL import Image import numpy as np model = torch.hub.load('ultralytics/yolov5', 'custom', path='best.pt') import json def yolo(im, size=1024): g = (size / max(im.size)) # gain im = im.resize((int(x * g) for x in im.size), Image.LANCZOS) # resize results = model(im) # inference if results.imgs: writable_img = np.copy(results.imgs[0]) # Create a writable copy of the image data results.imgs[0] = writable_img results.render() # updates results.imgs with boxes and labels df = results.pandas().xyxy[0].to_json(orient="records") res = json.loads(df) return [ Image.fromarray(results.imgs[0]), res ] inputs = gr.Image(type='pil', label="Original Image") outputs = [ gr.Image(type="pil", label="Output Image"), gr.JSON() ] title = "YOLOv5 NDL-DocL Datasets" description = "YOLOv5 NDL-DocL Datasets Gradio demo for object detection. Upload an image or click an example image to use." article = "

YOLOv5 NDL-DocL Datasets is an object detection model trained on the NDL-DocL Datasets.

" # examples = [ ['『源氏物語』(東京大学総合図書館所蔵).jpg'], ['『源氏物語』(京都大学所蔵).jpg'], ['『平家物語』(国文学研究資料館提供).jpg'] ] demo = gr.Interface(yolo, inputs, outputs, title=title, description=description, article=article, examples=examples) demo.launch(share=False)