-
-
Save tigrouind/2f0645d891f0594f84011eb45966d014 to your computer and use it in GitHub Desktop.
Detect image orientation using OCR
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import pathlib | |
| import pytesseract | |
| import cv2 | |
| import sys | |
| import exif | |
| import multiprocessing | |
| import enchant | |
| def ocr(img): | |
| return pytesseract.image_to_string(img, lang='fra', config="--psm 6").replace('\n', ' ') #psm: disable orientation detection | |
| def getscore(text): | |
| dictionary = enchant.Dict("fr_FR") | |
| words = [word for word in text.split() if len(word) >= 4] | |
| return sum(1 for word in words if dictionary.check(word)) | |
| def rotate_exif(filename, rot): | |
| with open(filename, 'rb') as sf : | |
| img = exif.Image(sf) | |
| if not(img.has_exif): img.orientation = 1 | |
| neworientation = [1,6,3,8][({1:0, 6:1, 3:2, 8:3}[img.orientation] + rot)%4] | |
| if img.orientation != neworientation: | |
| img.orientation = neworientation | |
| output = img.get_file() | |
| with open(filename, 'wb') as df: | |
| df.write(output) | |
| def detect(path): | |
| filename = str(path) | |
| img = cv2.imread(filename) | |
| img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #grayscale | |
| img = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1] #high contrast | |
| bestscore = bestangle = -1 | |
| for angle in range(4): | |
| if angle == 0: | |
| score = getscore(ocr(img)) | |
| #elif angle == 1: | |
| # score = getscore(ocr(cv2.rotate(img, cv2.ROTATE_90_CLOCKWISE))) | |
| elif angle == 2: | |
| score = getscore(ocr(cv2.rotate(img, cv2.ROTATE_180))) | |
| #elif angle == 3: | |
| # score = getscore(ocr(cv2.rotate(img, cv2.ROTATE_90_COUNTERCLOCKWISE))) | |
| if score>bestscore and score>5: #at least five correct words | |
| bestscore = score | |
| bestangle = angle | |
| if bestangle>-1: | |
| rotate_exif(filename, bestangle) | |
| print(f'{filename} {bestangle}') | |
| if __name__ == '__main__': | |
| if len(sys.argv) == 1: | |
| raise SystemExit(f"Usage: {sys.argv[0]} <directory_with_jpg_files>") | |
| pool = multiprocessing.Pool(multiprocessing.cpu_count()) | |
| pool.map(detect, pathlib.Path(sys.argv[1]).glob('*.jpg')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment