import gradio as gr import torch from PIL import Image import json # Model model = torch.hub.load('ultralytics/yolov5', 'custom', path='best.pt', source="local") def yolo(im, size=1024): g = (size / max(im.size)) # gain im = im.resize((int(x * g) for x in im.size), resample=Image.Resampling.LANCZOS) # resize results = model(im) # inference results.render() 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(label="Output 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)