-
Notifications
You must be signed in to change notification settings - Fork 5
/
ocr.py
81 lines (66 loc) · 2.7 KB
/
ocr.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
try:
from PIL import Image
except ImportError:
import Image
import pytesseract
import requests
import numpy as np
import matplotlib.pyplot as plt
import cv2
from os import listdir
from os.path import isfile, join
def download(url):
filename = "main.png"
with open(filename, "wb") as file:
response = requests.get(url)
file.write(response.content)
return filename
def change_contrast(img, level):
factor = (259 * (level + 255)) / (255 * (259 - level))
def contrast(c):
return 128 + factor * (c - 128)
return img.point(contrast)
def get_card(path, input, n_img):
img = cv2.imread(input)
crop_img = img[0:0+414, n_img*278:n_img*278+278]
crop_img = cv2.cvtColor(crop_img, cv2.COLOR_BGR2GRAY)
cv2.imwrite(path, crop_img)
def get_top(input, output):
img = cv2.imread(input)
crop_img = img[65:105, 45:230]
# Grayscale, Gaussian blur, Otsu's threshold
gray = cv2.cvtColor(crop_img, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (3,3), 0)
thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
# Morph open to remove noise and invert image
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3))
opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=1)
invert = 255 - opening
cv2.imwrite(output,invert)
def get_bottom(input, output):
img = cv2.imread(input)
crop_img = img[55+255:110+255, 45:230]
# Grayscale, Gaussian blur, Otsu's threshold
gray = cv2.cvtColor(crop_img, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (3,3), 0)
thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
# Morph open to remove noise and invert image
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3))
opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=1)
invert = 255 - opening
cv2.imwrite(output,invert)
#TESTS
fname = download("https://cdn.discordapp.com/attachments/300255115259150339/788363007948226590/card.jpg")
path_to_ocr = "src_to_ocr"
for i in range(0,3):
get_card(f"card{i+1}.png", fname, i)
get_card(f"card{i+1}.png", fname, i)
get_card(f"card{i+1}.png", fname, i)
for i in range(0,3):
get_top(f"card{i+1}.png",f"{path_to_ocr}/top{i+1}.png")
get_bottom(f"card{i+1}.png",f"{path_to_ocr}/bottom{i+1}.png")
onlyfiles = [f for f in listdir(path_to_ocr) if isfile(join(path_to_ocr, f))]
print("File trovati: ", onlyfiles)
custom_config = r"--psm 10 --oem 3 -c tessedit_char_whitelist=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz:-!\'\",.@�()"
for img in onlyfiles:
print(pytesseract.image_to_string(Image.open(path_to_ocr+"/"+img), lang='eng', config = '--psm 6'))