Overview

The character region detection app is published at the following link.

https://huggingface.co/spaces/nakamura196/yolov5-char

The above app had stopped working, so I fixed it following the same procedure as in the following article.

The model used in this app was built using the “Japanese Classical Character Dataset” (held by NIJL and others / processed by CODH) doi:10.20676/00000340.

I also made some minor improvements during this fix, which I will introduce here.

Setting the Height of gr.JSON

When the returned JSON data becomes large, the results could be difficult to view.

By configuring demo.css as follows:

...

demo = gr.Interface(yolo, inputs, outputs, title=title, description=description, article=article, examples=examples)

demo.css = """
.json-holder {
    height: 300px;
    overflow: auto;
}
"""

demo.launch()

The results can now be displayed with a scrollbar as shown below.

Returning Only Bounding Boxes

When there are many characters, the “Output Image” could be hard to see. To address this, I added the output “Output Image with Boxes.”

This is achieved with the following processing:

def yolo(im):

    results = model(im)  # inference

    df = results.pandas().xyxy[0].to_json(orient="records")
    res = json.loads(df)

    im_with_boxes = results.render()[0]  # results.render() returns a list of images

    # Convert the numpy array back to an image
    output_image = Image.fromarray(im_with_boxes)

    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 [
        output_image,
        res,
        im,
    ]

Summary

I hope this serves as a helpful reference.