DocLayoutYOLO.py 886 B

123456789101112131415161718192021
  1. from doclayout_yolo import YOLOv10
  2. class DocLayoutYOLOModel(object):
  3. def __init__(self, weight, device):
  4. self.model = YOLOv10(weight)
  5. self.device = device
  6. def predict(self, image):
  7. layout_res = []
  8. doclayout_yolo_res = self.model.predict(image, imgsz=1024, conf=0.25, iou=0.45, verbose=True, device=self.device)[0]
  9. for xyxy, conf, cla in zip(doclayout_yolo_res.boxes.xyxy.cpu(), doclayout_yolo_res.boxes.conf.cpu(),
  10. doclayout_yolo_res.boxes.cls.cpu()):
  11. xmin, ymin, xmax, ymax = [int(p.item()) for p in xyxy]
  12. new_item = {
  13. 'category_id': int(cla.item()),
  14. 'poly': [xmin, ymin, xmax, ymin, xmax, ymax, xmin, ymax],
  15. 'score': round(float(conf.item()), 3),
  16. }
  17. layout_res.append(new_item)
  18. return layout_res