import gradio as gr from PIL import Image, ImageDraw import yolov5 import json model = yolov5.load("./best.pt") def yolo(im): results = model(im) # inference df = results.pandas().xyxy[0].to_json(orient="records") res = json.loads(df) draw = ImageDraw.Draw(im) for bb in res: xmin = bb['xmin'] ymin = bb['ymin'] xmax = bb['xmax'] ymax = bb['ymax'] draw.rectangle([xmin, ymin, xmax, ymax], outline="red", width=3) return [ res, im, ] inputs = [ gr.Image(type='pil', label="Original Image") ] outputs = [ gr.JSON(label="Output JSON"), gr.Image(type='pil', label="Output Image with Boxes"), ] title = "YOLOv5 Face" description = """

YOLOv5 Face Gradio demo for object detection. Upload an image or click an example image to use.

""" article = """

YOLOv5 Face is an object detection model trained on the 顔コレデータセット.

""" examples = [ ["『文正草子』(日本古典籍データセット(国文研所蔵)CODH配信).jpg"], ['『源氏百人一首』(大阪公立大学中百舌鳥図書館 国文学研究資料館).jpg'], ['『源氏物語』(国文学研究資料館).jpg'], ['『百鬼夜行図』(東京大学).jpg'] ] demo = gr.Interface( fn=yolo, inputs=inputs, outputs=outputs, title=title, description=description, article=article, examples=examples ) demo.css = """ .json-holder { height: 300px; overflow: auto; } """ demo.launch(share=False)