Hướng của tài liệu chụp bằng điện thoại di động bị sai. Xử lý EXIF trước rồi gửi đến OCR.

Ảnh hợp đồng trên điện thoại của tôi nhìn dọc nhưng sau khi tải lên thì nó lại xoay ngang. Nguyên nhân có thể là do bản thân các pixel không được xoay, người xem chỉ cần điều chỉnh hiển thị dựa trên các điểm đánh dấu định hướng EXIF. Nếu đầu vào OCR bỏ qua dấu này, nó sẽ đọc trang theo hướng khác.

Ảnh hợp đồng chụp bằng điện thoại di động nằm dọc trong album nhưng sẽ nằm ngang sau khi tải lên. Nhiều người sẽ cho rằng ảnh bị lệch hoặc file sai. Bài viết này viết cho những người cần gửi ảnh từ điện thoại di động vào OCR hoặc quá trình dịch thuật để giúp bạn phân biệt giữa "hướng hiển thị" và "hướng pixel".

Sau khi đọc xong, bạn sẽ biết cờ định hướng EXIF là gì, Pillow sử dụng chức năng nào để xử lý nó và những vấn đề mà nó không giải quyết được. Các ví dụ sử dụng mẫu tổng hợp và mã đã được kiểm tra trong môi trường biệt lập, nhưng điều này không thể hiện sự đảm bảo về tất cả các tệp thực hoặc hiệu suất sản phẩm. Trước khi xử lý các văn bản quan trọng, vui lòng giữ lại bản chính và xem xét theo ranh giới và các mục chấp nhận trong văn bản.

Tôi phải làm gì nếu tài liệu được chụp bằng điện thoại di động của tôi bị sai hướng? Một câu kết luận

Trước tiên, hãy sử dụng ImageOps.exif_transpose để áp dụng dấu hướng EXIF, sau đó gửi hình ảnh đã điều chỉnh tới OCR; các vấn đề về độ nghiêng và phối cảnh cần được giải quyết riêng.

hoàn cảnh của bạnCác phương pháp được đề xuấtlý do
Trình xem hiển thị bình thường nhưng kết quả phân tích cú pháp bị lệchĐầu tiên sử dụng ImageOps.exif_transpose để áp dụng hướng EXIFPixel không được xoay, chỉ có lớp hiển thị được điều chỉnh
Toàn bộ bức ảnh hơi nghiêngĐánh giá hiệu chỉnh độ nghiêng riêng lẻexif_transpose không xử lý được góc chụp
Tờ giấy có hình thangPhát hiện ranh giới giấy và thực hiện chỉnh sửa phối cảnhBiến dạng phối cảnh không phải là vấn đề định hướng EXIF
Cần lưu lại hộp OCR trả vềGhi lại kích thước đầu vào, chuyển đổi hướng và kích thước đầu ra cùng một lúcTọa độ ảnh được điều chỉnh khác với ảnh gốc

Hướng hiển thị và hướng pixel phải được xem riêng.

Các pixel của ảnh trên thiết bị di động có thể không tự xoay được và thẻ định hướng EXIF trong tệp sẽ cho người xem biết cách hiển thị ảnh đó. Người xem sẽ điều chỉnh hình ảnh cho phù hợp và nếu công cụ OCR bỏ qua dấu này, nó sẽ đọc trang theo hướng khác.

ImageOps.exif_transpose của Pillow điều chỉnh các pixel dựa trên thông tin định hướng và loại bỏ các điểm đánh dấu định hướng được áp dụng để tránh các lần xoay tiếp theo. Nó xử lý hướng EXIF chứ không phải độ nghiêng ảnh tùy ý hoặc biến dạng phối cảnh. Việc làm thẳng tờ giấy bị cong bằng điện thoại di động vẫn cần một bộ xử lý hình học khác.

Kiểm tra giới hạn xoay bằng pixel tổng hợp

Đoạn mã sau sử dụng hình ảnh tổng hợp để kiểm tra xem các điểm đánh dấu định hướng có được áp dụng chính xác hay không:

from PIL import Image, ImageOps

source = Image.new("RGB", (40, 20), "white")
source.putpixel((0, 0), (255, 0, 0))
source.getexif()[274] = 6
fixed = ImageOps.exif_transpose(source)
assert fixed.size == (20, 40)
assert fixed.getpixel((19, 0)) == (255, 0, 0)
assert fixed.getexif().get(274) is None
assert source.size == (40, 20)
again = ImageOps.exif_transpose(fixed)
assert again.size == fixed.size
assert again.tobytes() == fixed.tobytes()

Các pixel màu đỏ ở các góc được sử dụng ở đây để giúp kiểm tra các mối quan hệ hình học mà không cần gọi công cụ OCR hoặc mô phỏng văn bản thực. Do đó, bài kiểm tra chỉ có thể hỗ trợ kết luận rằng "các điểm đánh dấu hướng được áp dụng chính xác", chứ không thể hỗ trợ "tỷ lệ nhận dạng văn bản đã được cải thiện đến mức nào". Các ví dụ phụ thuộc vào Gối và nên chạy ở phiên bản bị khóa.

Chuyển hướng bất thường về hướng

When you encounter a direction problem, first look at the phenomenon, and then judge the cause. The following table can help you decide what to do next:

ObservePossible reasonsNext step
Viewer normal, parser tappedEXIF not appliedCheck orientation labels and pixels
The whole page tilts slightlyShooting angleIndependent evaluation of tilt correction
The page is trapezoidalPerspective deformationCorrection after detecting paper boundary
The text is upside down but unmarkedMissing metadataJudge according to the content of the page, do not guess blindly

# coordinate recording starts after rotation

If OCR returns a text box on the adjusted image, the coordinates of the box correspond to the adjusted image, not the original one. Therefore, three pieces of information need to be retained: input size, direction transformation and output size. When the display is highlighted, the inverse transformation is applied to the coordinates to return to the original position.

Save only one set of boxes without recording which picture it belongs to, and the highlighted position deviates from the text.

# correct the image before translation

When dealing with the scanning material through the OCR and translation entrance of OmniDoc (Product Import), you can first confirm the direction against the original. Orientation, perspective and clarity are three different check items, which need to be confirmed separately.

After the input conditions are sorted out, the bilingual results are used to judge the translation quality. If a missing word is attributed to the translation model, the real input problem will often be left out.

Tham khảo: Pillow ImageOps.

# Core comparison

Working with objectTreatment modeWhether it is completed byexif_transpose
EXIF direction markAdjust pixels by markerYes
The whole page tilts slightlyShooting angle correctionNo, independent evaluation is required.
The page is trapezoidalPerspective correctionNo, paper boundaries need to be detected

# which method is suitable for you?

if you.

- the viewer is normal, and the OCR result is horizontal : apply the EXIF direction first, and then re-enter the OCR. - overall skew of the photo : take tilt correction as an independent step and do not expect direction processing to solve the problem. - the edge of the paper is trapezoidal : check the edge of the paper first, and then make perspective correction. - need to highlight the location of the original image : save the input size and transformation information, and then reverse transform when displaying.

# frequently asked questions (FAQ)

1. Can exif_transpose correct the crooked paper?

No, I can't. It only adjusts pixels according to EXIF orientation markers and does not deal with shooting angles or perspective deformations, which require additional geometric processing.

2. Why does the viewer display normally, but the OCR result is horizontal?

The viewer applies EXIF direction markers, and the pixels themselves may not be rotated. If OCR ignores the tag, it reads to the page in the other direction.

3. Can the sample code show how much the recognition rate has been improved?

No, I can't. The example uses composite pixels to check if the orientation markers are applied correctly, without invoking the OCR engine or simulating real text.

4. Why does the box returned by OCR not match the location of the original image?

If OCR is running on the adjusted diagram, the coordinates of the box correspond to the adjusted diagram. It is necessary to record the input size, transformation and output size, and then do the inverse transformation during the display.

# Summary

The direction of mobile phone photos, first look at the display direction or pixel direction, and then deal with the direction, tilt and perspective respectively. Please keep the original and complete the review before dealing with important documents.

Experience now: upload the document and start translation, keeping the original typesetting

Cập nhật lúc 2026-11-11