Skip to content

Commit

Permalink
feat: add originating bounding box to crop for use in downstream proc…
Browse files Browse the repository at this point in the history
…essing in EXIF comment field, e.g. UserComment: b'bbox:534,16,817,300'
  • Loading branch information
danellecline committed Nov 11, 2024
1 parent 3fd36b4 commit ad49140
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions aipipeline/prediction/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import cv2
import numpy as np
import piexif
from PIL import Image
import pandas as pd
import xml.etree.ElementTree as ET
Expand Down Expand Up @@ -157,6 +158,15 @@ def crop_square_image(row: pd.Series, square_dim: int):
img.save(row.crop_path)
img.close()

# Encode the cropped coordinates in the exif data
with open(row.crop_path, "rb") as f:
img = Image.open(f)
exif_dict = piexif.load(img.info.get("exif", b""))
bounding_box = f"{x1},{y1},{x2},{y2}"
exif_dict["Exif"][piexif.ExifIFD.UserComment] = f"bbox:{bounding_box}".encode("utf-8")
exif_bytes = piexif.dump(exif_dict)
img.save(row.crop_path, exif=exif_bytes)

except Exception as e:
logger.exception(f"Error cropping {row.image_path} {e}")
raise e
Expand Down

0 comments on commit ad49140

Please sign in to comment.