How to Fix Crooked Documents Before OCR Runs
Source: Dev.to

Overview
Have you ever tried extracting text from a photo of a receipt or a scanned contract? If so, you likely know the results can be hit or miss. Perhaps the paper is tilted, the lighting is uneven, or the picture is grainy. Because of this, your OCR engine cannot read the words correctly.
Document Detection fixes these problems. It finds the document in your image, straightens it out, and cleans it up. This makes it much easier for the computer to read the text.
Why OCR Fails on Real‑World Images
OCR tools usually expect perfect pictures. They are trained on flat, bright, and straight text. Real photos from users are rarely perfect—users often take photos of bills at odd angles, capture ID cards on messy desks, or upload scans where the paper moved. Sending these raw images to an OCR tool will likely produce mixed‑up, missing, or out‑of‑order text.
Filestack Document Detection handles these messy inputs automatically. It finds the edges of the paper, fixes the angle, and cleans up the image, delivering a clean picture ready for text extraction.
How It Works
Document Detection uses two complementary methods:
- Edge detection – looks for lines and edges in the picture.
- Machine‑learning model – a neural network trained on thousands of document photos.
The process occurs in four steps:
- Create a map of the document using the model.
- Locate the four corners of the paper.
- Transform the image so the document fills the whole frame.
- (Optional) Clean up noise, darken shadows, and brighten highlights.
Three Detection Modes
Document Detection offers three modes depending on your needs.
Coordinates Mode
Returns the coordinates of the document corners.
API Call
doc_detection=coords:true
Response (JSON)
{
"coords": {
"x": 106,
"y": 464,
"width": 580,
"height": 231
}
}
Use this mode when you need to draw boxes, crop the image yourself, or inform another system of the document’s location.
Warped Mode
Straightens the image without altering colors or brightness.

API Call
doc_detection=preprocess:false
The result is a new image where the document fills the whole frame, preserving the original visual characteristics. Choose this mode when you want a straight image but plan to apply your own post‑processing.
Preprocessed Mode
Performs both warping and visual enhancement (noise reduction, contrast boost).

API Call
doc_detection=preprocess:true
This is the default setting. The cleaning step reduces noise and sharpens text, making it ideal for OCR tasks.
Full API Examples
Document Detection is applied by modifying the URL in the Processing API. All requests require a security policy and signature.
-
Get coordinates
https://cdn.filestackcontent.com/security=p:POLICY,s:SIGNATURE/doc_detection=coords:true/HANDLE -
Get warped image
https://cdn.filestackcontent.com/security=p:POLICY,s:SIGNATURE/doc_detection=preprocess:false/HANDLE -
Get preprocessed image
https://cdn.filestackcontent.com/security=p:POLICY,s:SIGNATURE/doc_detection=preprocess:true/HANDLE
Chaining with Resize
Images must be ≤ 2000 × 2000 px. For larger files, add a resize step:
https://cdn.filestackcontent.com/security=p:POLICY,s:SIGNATURE/resize=height:1500/doc_detection=preprocess:true/HANDLE
Chaining with OCR
You can send the cleaned image directly to Filestack OCR:
https://cdn.filestackcontent.com/security=p:POLICY,s:SIGNATURE/doc_detection=preprocess:true/ocr/HANDLE
The OCR response (excerpt) for the example receipt:
{
"text": "Manila Automated Fare\nCollection System\nRECEIPT\nTax ID:\nReceipt No:\nDate and Time:\nStation Name:\nPOS ID:\nOperator ID:\nCard ID:\nCard Name:\n004-1-231125-1134-53\n23 NOV 2025 11:34\nGil Puyat\nPOI, POS.A.02.00.03\n100108195\n6378050082232813\nStandard SVC\nAdd value\nAdd Value:\nold Remaining value:\n42.00\n100.00\nNew Remaining value:\n142.00\nAmount Payable:\n100.00\nAmount Received:\n100.00"
}