To automate the cleaning of user-uploaded photos of AWS Builder Cards, a serverless image processing pipeline utilizes an AWS Lambda container running a vision segmentation model. The core task involves removing backgrounds, straightening, and cropping tilted images. To overcome the lack of GPU support in Lambda and strict memory constraints, the author implemented the BiRefNet-General-Lite model via the rembg library using the ONNX runtime. While the standard BiRefNet model consumed nearly 10 GB of memory—hitting 97% of the maximum 10 GB Lambda capacity—the "Lite" version (utilizing a Swin-v1-Tiny backbone) reduced median peak memory usage to approximately 7.5 GB. To further optimize performance, images with a long side exceeding 2048px are downscaled. The processing workflow combines AI with deterministic geometry: the BiRefNet model generates a binary mask to distinguish the card from the background, and OpenCV is then used to perform perspective rectification. This involves fitting lines to the card's edges while ignoring the outer 18% of the contour to account for rounded corners, allowing for precise corner detection and warping. The final output is a straightened PNG, which is then sent to Amazon Bedrock for text extraction via a multimodal model before being stored in S3 and DynamoDB.
Read original
dev.to