Repository navigation
How can I detect rotated images/documents? #1283
|
Hello, I'm sort of new to AI. While I have had two courses about machine learning at university, I never really applied the knowledge outside of the class and since it's been 2 years since I finished my BSc, I kind of forgot most of it. I hope you guys can help me. I would like to detect whether a document has been rotated or not, which I believe is what I have tried the following using PDFs that I converted to images: def get_orientation(self, image):
predictor = detection_predictor('db_resnet50_rotation',
pretrained=True,
assume_straight_pages=False,
preserve_aspect_ratio=True)
result = predictor(image)
return resultI was expecting to get a normalized value between 0 and 1, but instead I get a multidimensional array with float values. Initially I thought it returned the image itself so I tried to display it, but on second thought it doesn't make sense to return an image. So what am I missing here? What am I doing wrong? I tried to look at the documentation, but the documentation wasn't all that helpful. It says My current solution is to use PyTesseract, but I would prefer to use one module rather than multiple: def get_orientation(self, path: str) -> int:
metadata = pytesseract.image_to_osd(path)
orientation = search('(?<=Rotate: )\d+', metadata).group(0)
return orientationIdeally I would like to train my own model on the documents we use as they have a specific layout and training on them might improve our results. We have a server available with AI-centric GPUs. This might be more appropriate in another thread though. Side note: I don't know about you, but the name makes it hard to search for help as Google constantly correct it to "doctor". Even with quotation marks it is still hard to find answers. |
Replies: 8 comments 48 replies
|
Hi @denivic 👋, normally from my experience docTR works in the most cases more accure as EasyOCR would it be possible to share an example image to see what's going wrong ? :) If you want to get the document orientation you can simply pass Line 29 in 3deac68 The If you can train your own models see: https://mindee.github.io/doctr/latest/using_doctr/custom_models_training.html Best regards, |
|
Hey @felixdittrich92! Thank you for the informative reply, I appreciate it. As for DocTR being more efficient than EasyOCR that might be true, I forgot to mention that the approach I'm using is:
This probably explains why I didn't have issues with EasyOCR. I forgot to crop the image when using DocTR 🤦♂️. I'll give it another shot with DocTR (especially now that you mentioned I would love to share an example of the files that I'm working with, but I am unable to as they contain sensitive information about other people such as their name, SSN, and the university they study at. I work for a specific ministry/branch of my government that handles education and research in my country. These documents are sent in by students who study abroad, but still want the monthly government "allowance" so to speak. Students are paid to study here ranging from ~150€ to ~740€ depending on their situation (whether they live at home or on their own, how much their parents earn etc). I could perhaps censor the sensitive info or simply overwrite it with fake data. I think I'll try your suggested solutions first and if it still doesn't work I'll prepare some examples along with the code I ended up writing. :) You can see an example of an empty form here (found on su.dk at the bottom of the page section " |
|
@felixdittrich92 I have tested #1336 on a sample size of 21 documents with random angles between 1. Orientation found: 14.845458984375, Actual orientation: -3
2. Orientation found: 10.808235168457031, Actual orientation: 3
3. Orientation found: -73.0724868774414, Actual orientation: 19
4. Orientation found: 39.805572509765625, Actual orientation: -70
5. Orientation found: 86.9471173286438, Actual orientation: 48
6. Orientation found: 12.90740966796875, Actual orientation: 71
7. Orientation found: -42.41420364379883, Actual orientation: -32
8. Orientation found: 0.6437454223632812, Actual orientation: -28
9. Orientation found: -6.009005546569824, Actual orientation: 15
10. Orientation found: -49.39870071411133, Actual orientation: -4
11. Orientation found: -52.177249908447266, Actual orientation: 4
12. Orientation found: 48.90570068359375, Actual orientation: 6
13. Orientation found: -45.364933013916016, Actual orientation: 74
14. Orientation found: 26.113914489746094, Actual orientation: -78
15. Orientation found: 45.88140106201172, Actual orientation: 85
16. Orientation found: 79.5085220336914, Actual orientation: 83
17. Orientation found: 60.80251693725586, Actual orientation: -7
18. Orientation found: 0, Actual orientation: -40
19. Orientation found: -8.310276985168457, Actual orientation: 62
20. Orientation found: -14.444034576416016, Actual orientation: 90
21. Orientation found: -9.090276718139648, Actual orientation: -43Using these methods. It could possibly be that I'm iterating through my list of randomly selected angles the wrong way with respect to the list of images, but I don't think that I am though unless EDIT: Well... Seems like some of the images are rotated more than 90 or -90 degrees for some reason causing them to be almost upside down, which is weird because I checked the images manually before loading them using |
It's all ok :) Ensure that you are on the correct branch : |
|
I am getting an error while running docTR: |
|
Is there no way I can detect the rotation angle in the image?
…On Fri, Mar 1, 2024 at 3:38 PM Felix Dittrich ***@***.***> wrote:
db_resnet50_rotation is removed in v0.8.0 it is now db_resnet50
—
Reply to this email directly, view it on GitHub
<#1283 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/BGKCWN36SIBUG4QJR3NHQKTYWBHQ3AVCNFSM6AAAAAA3XEETMCVHI2DSMVQWIX3LMV43SRDJONRXK43TNFXW4Q3PNVWWK3TUHM4DMNBRGIYTM>
.
You are receiving this because you commented.Message ID:
***@***.***>
--
This message is intended for the use of the addressee and may contain
information that is privileged and/or confidential. If you are not the
intended recipient, you are hereby notified that any dissemination,
distribution or copying of the information contained in this message is
strictly unauthorized and prohibited. If you have received this message in
error, please notify the sender by reply e-mail and delete the message from
your system.
|
|
Thanks I'll try this. Is there a way I can get a key value kie extraction
like:
{
Name: Neha,
Dob: 26-03
}
I get only "words" as a class when I implement KIE. I need keys and values
from the document.
Thanks,
Neha
…On Fri, 1 Mar, 2024, 7:25 pm Felix Dittrich, ***@***.***> wrote:
That's possible with the ocr_predictor instance and
detect_orientation=True :)
—
Reply to this email directly, view it on GitHub
<#1283 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/BGKCWNZUZNZTSTF66SF64C3YWCCGVAVCNFSM6AAAAAA3XEETMCVHI2DSMVQWIX3LMV43SRDJONRXK43TNFXW4Q3PNVWWK3TUHM4DMNBTGQ2DC>
.
You are receiving this because you commented.Message ID:
***@***.***>
--
This message is intended for the use of the addressee and may contain
information that is privileged and/or confidential. If you are not the
intended recipient, you are hereby notified that any dissemination,
distribution or copying of the information contained in this message is
strictly unauthorized and prohibited. If you have received this message in
error, please notify the sender by reply e-mail and delete the message from
your system.
|
|
We hit this exact problem building a product-photo pipeline — OCR/vision models degrade badly on rotated input and most general solutions don't reliably tell you which rotation you're looking at (particularly 90 vs 270, a well-documented confusion — see RotBench, EACL 2026, arXiv:2508.13968). Ended up training a small dedicated model for just this sub-problem rather than relying on the main model to self-correct. Not trying to plug a product in an issue thread, just sharing in case the underlying approach (cardinal-rotation classifier + fine-angle regression, arbitrated) is useful prior art for anyone tackling this here. Happy to share more on the methodology if useful: https://doi.org/10.5281/zenodo.22975679 |
Hi @denivic 👋,
normally from my experience docTR works in the most cases more accure as EasyOCR would it be possible to share an example image to see what's going wrong ? :)
If you want to get the document orientation you can simply pass
detect_orientation=Trueto theocr_predictor(with ocr) otherwise you can use the following function:doctr/doctr/models/_utils.py
Line 29 in 3deac68
The
detection_predictorreturns the normalized boxes -> (113, 4, 2) (num_boxes, (coords - [[x1 y1], [x2 y2], [x3 y3], [x4 y4]])) to restore the values you need to multiply the values wi…