Created
May 17, 2024 20:39
-
-
Save raunakdoesdev/e8389aa6651dab5d9df51bc488911d41 to your computer and use it in GitHub Desktop.
Code snippet for visualizing bounding boxes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import os.path | |
from pdf2image.pdf2image import convert_from_path | |
from PIL import ImageDraw | |
pdf_path = os.path.expanduser("~/Downloads/research-highlight.pdf") | |
upload_form = requests.post("https://v1.api.reducto.ai/upload").json() | |
requests.put(upload_form["presigned_url"], data=open(pdf_path, "rb")) | |
response = requests.post( | |
"https://v1.api.reducto.ai/parse", | |
json={"document_url": upload_form["file_id"]}, | |
headers={"Authorization": f"Bearer {os.environ['REDUCTO_API_KEY']}"}, | |
) | |
blocks = [ | |
block for chunk in response.json()["result"]["chunks"] for block in chunk["blocks"] | |
] | |
images = convert_from_path(pdf_path) | |
for page_num, image in enumerate(images, start=1): | |
draw = ImageDraw.Draw(image) | |
for block in blocks: | |
if block["bbox"]["page"] == page_num: | |
bbox = block["bbox"] | |
draw.rectangle( | |
( | |
bbox["left"] * image.width, | |
bbox["top"] * image.height, | |
(bbox["left"] + bbox["width"]) * image.width, | |
(bbox["top"] + bbox["height"]) * image.height, | |
), | |
outline="red", | |
width=10, | |
) | |
image.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment