Merge in RR/fb_detr_prediction_container from non_max_supprs to master
Squashed commit of the following:
commit 9cc31b70e39412b3613a117228554608d947dbb5
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Fri Feb 4 17:41:00 2022 +0100
refactoring, renaming
commit ebc37299df598b71f7569d8e8473bdb66bbbbd1a
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Fri Feb 4 17:34:26 2022 +0100
renaming
commit d694866e1e98e6129f37eaf4c1950b962fed437f
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Fri Feb 4 17:33:07 2022 +0100
applied black
commit 381fe2dbf5d88f008d87bd807b84174376c5bcfe
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Fri Feb 4 17:32:22 2022 +0100
duplicate detection removal completed
commit ef2bab300322da3b12326d470f1c41263779e4a0
Author: Julius Unverfehrt <Julius.Unverfehrt@iqser.com>
Date: Fri Feb 4 09:58:49 2022 +0100
box merging algo WIP
commit d770e56a7f31a28dea635816cae3b7b75fed0e24
Author: Julius Unverfehrt <Julius.Unverfehrt@iqser.com>
Date: Fri Feb 4 09:37:17 2022 +0100
refactor & box dropping working but algo is faulty & drops too much WIP
commit 289848871caadb4438f889b8a030f30cfb64201a
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Thu Feb 3 23:56:04 2022 +0100
non max supprs WIP
commit 2f1ec100b2d33409e9178af8d53218b57d9bb0e2
Author: Matthias Bisping <matthias.bisping@iqser.com>
Date: Thu Feb 3 13:32:22 2022 +0100
changed Flask to not listen on public IP
59 lines
1.5 KiB
Python
59 lines
1.5 KiB
Python
import argparse
|
|
import json
|
|
from operator import itemgetter
|
|
|
|
import pdf2image
|
|
import requests
|
|
from PIL import ImageDraw, ImageFont
|
|
|
|
|
|
def draw_coco_box(draw: ImageDraw.Draw, bbox, klass, proba):
|
|
x1, y1, x2, y2 = itemgetter("x1", "y1", "x2", "y2")(bbox)
|
|
draw.rectangle(((x1, y1), (x2, y2)), outline="red")
|
|
|
|
fnt = ImageFont.truetype("Pillow/Tests/fonts/FreeMono.ttf", 30)
|
|
|
|
draw.text((x1, y2), text=f"{klass}: {proba:.2f}", fill=(0, 0, 0, 100), font=fnt)
|
|
|
|
|
|
def draw_coco_boxes(image, bboxes, classes, probas):
|
|
|
|
draw = ImageDraw.Draw(image)
|
|
for bbox, klass, proba in zip(bboxes, classes, probas):
|
|
draw_coco_box(draw, bbox, klass, proba)
|
|
|
|
return image
|
|
|
|
|
|
def annotate(pdf_path, predictions):
|
|
pages = pdf2image.convert_from_path(pdf_path)
|
|
|
|
for prd in predictions:
|
|
page_idx, boxes, classes, probas = itemgetter("page_idx", "bboxes", "classes", "probas")(prd)
|
|
page = pages[page_idx]
|
|
image = draw_coco_boxes(page, boxes, classes, probas)
|
|
image.save(f"/tmp/serv_out/{page_idx}.png")
|
|
|
|
|
|
def parse_args():
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("--pdf_path", required=True)
|
|
args = parser.parse_args()
|
|
|
|
return args
|
|
|
|
|
|
def main(args):
|
|
|
|
response = requests.post("http://127.0.0.1:5000", data=open(args.pdf_path, "rb"))
|
|
response.raise_for_status()
|
|
predictions = response.json()
|
|
|
|
print(json.dumps(predictions, indent=2))
|
|
annotate(args.pdf_path, predictions)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
args = parse_args()
|
|
main(args)
|