(page_number, fields_json_path, input_path, output_path)
| 9 | |
| 10 | |
| 11 | def create_validation_image(page_number, fields_json_path, input_path, output_path): |
| 12 | # Input file should be in the `fields.json` format described in forms.md. |
| 13 | with open(fields_json_path, 'r') as f: |
| 14 | data = json.load(f) |
| 15 | |
| 16 | img = Image.open(input_path) |
| 17 | draw = ImageDraw.Draw(img) |
| 18 | num_boxes = 0 |
| 19 | |
| 20 | for field in data["form_fields"]: |
| 21 | if field["page_number"] == page_number: |
| 22 | entry_box = field['entry_bounding_box'] |
| 23 | label_box = field['label_bounding_box'] |
| 24 | # Draw red rectangle over entry bounding box and blue rectangle over the label. |
| 25 | draw.rectangle(entry_box, outline='red', width=2) |
| 26 | draw.rectangle(label_box, outline='blue', width=2) |
| 27 | num_boxes += 2 |
| 28 | |
| 29 | img.save(output_path) |
| 30 | print(f"Created validation image at {output_path} with {num_boxes} bounding boxes") |
| 31 | |
| 32 | |
| 33 | if __name__ == "__main__": |
no test coverage detected