概要 以下で文字領域の検出アプリを公開しています。
https://huggingface.co/spaces/nakamura196/yolov5-char
上記アプリが動作しなくなっていたので、以下の記事と同じ手順で修正しました。
なお、本アプリで使用しているモデルの構築にあたっては、「『日本古典籍くずし字データセット』(国文研ほか所蔵/CODH加工) doi:10.20676/00000340」を使用しています。
この修正において、細かい改善も加えたので、紹介します。
gr.JSONの高さ設定 返却結果のJSONデータが大きくなると、結果が見づらいことがありました。
そこで、以下のように、demo.cssを設定することにより、
... demo = gr.Interface(yolo, inputs, outputs, title=title, description=description, article=article, examples=examples) demo.css = """ .json-holder { height: 300px; overflow: auto; } """ demo.launch() 以下のように、スクロールバーとともに結果を表示できるようになりました。
矩形のみの返却 文字数が多い場合、「Output Image」の画像が見にくいケースがありました。そこで、出力「Output Image with Boxes」を追加しました。
以下のような処理によって実現しています。
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, ] まとめ 参考になりましたら幸いです。
...