forked from NoMeansNowastaken/KarutaSniper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathocr.py
85 lines (60 loc) · 2.38 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
82
83
84
85
# all of this code was taken from https://github.com/riccardolunardi/KarutaBotHack
try:
from PIL import Image
except ImportError:
import Image
from os import listdir
from os.path import isfile, join
import cv2
import pytesseract
import requests
def download(url):
filename = "main.png"
with open(filename, "wb") as file:
response = requests.get(url)
file.write(response.content)
return filename
def filelength(filepath):
im = cv2.imread(filepath)
return im.shape[1]
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]
gray = cv2.cvtColor(crop_img, cv2.COLOR_BGR2GRAY)
cv2.imwrite(output, gray)
def get_bottom(input, output):
img = cv2.imread(input)
crop_img = img[55 + 255:110 + 255, 45:235]
gray = cv2.cvtColor(crop_img, cv2.COLOR_BGR2GRAY)
cv2.imwrite(output, gray)
def get_print(input, output):
img = cv2.imread(input)
crop_img = img[372:385, 145:203]
gray = cv2.cvtColor(crop_img, cv2.COLOR_BGR2GRAY)
cv2.imwrite(output, gray)
# TESTS
if __name__ == "__main__":
fname = download("https://media.discordapp.net/attachments/776520559621570621/974137396184641546/card.webp")
path_to_ocr = "C:\\users\\user\\Desktop\\KarutaBotHack-main\\tests"
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').strip())