· 6 years ago · Dec 02, 2019, 08:30 AM
1import numpy as np
2import cv2
3import math
4from PIL import Image, ImageFilter, ImageDraw, ImageChops
5import os
6import pandas as pd
7import tesserocr
8import csv
9from tqdm import tqdm_notebook
10import random
11import time
12
13PATH = 'D:\\desktop\\test_2'
14images_list = os.listdir(PATH)
15tocsv = []
16
17ditLetter2Number = {
18 'O': '0',
19 'Q': '9',
20 'P': '6',
21 'I': '1',
22 'B': '8',
23 'S': '5',
24 'E': '3',
25 'T': '7',
26 'Y': '9'
27}
28let = {
29 '1': 0, '2': 0, '3': 0, '4': 0, '5': 0, '6': 0, '7': 0, '8': 0, '9': 0, '0': 0, 'A': 0, 'B': 0, 'C': 0, 'E': 0,
30 'H': 0, 'M': 0, 'O': 0, 'P': 0, 'T': 0, 'X': 0, 'Y': 0
31}
32ditNumber2Letter = {}
33for key, value in ditLetter2Number.items():
34 ditNumber2Letter[value] = key
35ns = ""
36
37
38def normalString(nom):
39 upp = nom.upper().replace(" ", "").replace("\n", "").replace("|", "").replace("%", "").replace(".", "").replace(",",
40 "").replace(
41 "`", "").replace("'", "").replace("_", "").replace("!", "").replace("[", "").replace("]", "").replace("{",
42 "").replace(
43 "}", "").replace("/", "")
44 i = 0
45 res = ""
46 global ns
47 for s in upp:
48 if (i == 0 or i == 4 or i == 5):
49 try:
50 ns = ditNumber2Letter[s]
51 except:
52 ns = s
53 else:
54 try:
55 ns = ditLetter2Number[s]
56 except:
57 ns = s
58 try:
59 a = let[ns]
60 except:
61 ns = ""
62 i -= 1
63 res += ns
64 i += 1
65
66 return res
67
68
69def getNumber(stname):
70 img = cv2.imread(stname)
71 width, height = 1200, 900
72 img = cv2.resize(img, (width, 600))
73 h_min = np.array((0, 0, 120), np.uint8)
74 h_max = np.array((250, 255, 255), np.uint8)
75 hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
76 thresh = cv2.medianBlur(cv2.inRange(hsv, h_min, h_max), 5)
77 image = Image.new("LA", (width, height), color=(0, 255))
78 indices = np.where(thresh != [0])
79 coordinates = zip(indices[1], indices[0])
80 for coordinate in coordinates:
81 image.putpixel((coordinate[0], coordinate[1]), (255, 255))
82
83 def getCropImg(img, rect, box):
84 W = rect[1][0]
85 H = rect[1][1]
86 Xs = [i[0] for i in box]
87 Ys = [i[1] for i in box]
88 x1 = min(Xs)
89 x2 = max(Xs)
90 y1 = min(Ys)
91 y2 = max(Ys)
92 angle = rect[2]
93 if angle < -45:
94 angle += 90
95 center = ((x1 + x2) / 2, (y1 + y2) / 2)
96
97 size = (x2 - x1, y2 - y1)
98 M = cv2.getRotationMatrix2D((size[0] / 2, size[1] / 2), angle, 1.0)
99
100 cropped = cv2.getRectSubPix(img, size, center)
101 cropped = cv2.warpAffine(cropped, M, size)
102 croppedW = H if H > W else W
103 croppedH = H if H < W else W
104
105 return cv2.getRectSubPix(cropped, (int(croppedW), int(croppedH)), (size[0] / 2, size[1] / 2))
106
107 def main():
108 original = image
109 original.save("out.png")
110 imger = cv2.imread("out.png")
111 hsv = cv2.cvtColor(imger, cv2.COLOR_BGR2GRAY)
112 _, contours0, hierarchy = cv2.findContours(hsv.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_TC89_L1)
113 print("------")
114 for cnt in contours0:
115 rect = cv2.minAreaRect(cnt)
116 box = cv2.boxPoints(rect)
117 box = np.int0(box)
118 area = int(rect[1][0] * rect[1][1])
119 anglepm = 17
120 ratepm = 2
121 rate = 4 / 1
122 w, h = rect[1][0], rect[1][1]
123 if area > width * height * 0.0005 and (
124 (rect[2] > -anglepm and rect[2] < anglepm) or (
125 rect[2] > -anglepm + 90 and rect[2] < anglepm + 90) or (
126 rect[2] > -anglepm - 90 and rect[2] < anglepm - 90) or (
127 rect[2] > -anglepm + 180 and rect[2] < anglepm + 180)):
128 if (rect[2] < -80 and rect[2] > -100):
129 w, h = h, w
130 mw = max(w, h)
131 mh = min(w, h)
132 if (w > h and w < width / 2):
133 if mw / mh > rate - ratepm and mw / mh < rate + ratepm:
134 cropped = getCropImg(img, rect, box)
135 cropped = cv2.resize(cropped, (200, 50), interpolation=cv2.INTER_NEAREST)
136 h_min = np.array((0, 0, 140), np.uint8)
137 h_max = np.array((250, 200, 255), np.uint8)
138 hsv = cv2.cvtColor(cropped, cv2.COLOR_BGR2HSV)
139 range = cv2.inRange(hsv, h_min, h_max)
140 cv2.imwrite("tmp.png", cropped)
141 # cv2.imshow("xt"+str(random.random()),cropped)
142 with tesserocr.PyTessBaseAPI() as api:
143 api.SetImageFile("tmp.png")
144 print("====")
145 str3 = api.GetUTF8Text()
146 print(str3)
147
148 sdddd = normalString(str3)
149 lll = len(sdddd)
150 if (lll == 9 or lll == 8):
151 return sdddd
152 else:
153 if mw / mh > rate + ratepm:
154 cv2.drawContours(imger, [box], 0, (155, 0, 0), 1)
155 else:
156 cv2.drawContours(imger, [box], 0, (0, 0, 155), 1)
157
158 return main()
159
160
161with open("D:\\desktop\\res.csv", "w", newline="") as file:
162 columns = ["ImageId", "PredictionString"]
163 writer = csv.DictWriter(file, fieldnames=columns)
164 writer.writeheader()
165 totallen = len(tqdm_notebook(images_list))
166 i = 0
167 j = 0
168 for image_name in tqdm_notebook(images_list):
169 stime = time.time()
170 # image_name=images_list[342]
171 i += 1
172 image_path = os.path.join(PATH, image_name)
173 s = getNumber(image_path)
174 print(s)
175 print(i, "/", totallen)
176 if s == None:
177 s = ""
178 if s == "":
179 s = "A000AA00"
180 else:
181 j += 1
182 tocsv = {"ImageId": image_name, "PredictionString": s}
183 writer.writerow(tocsv)
184 # break
185 print("time diff: " + str(time.time() - stime) + " s.")
186 print("total: " + str(j))
187
188cv2.waitKey(0)
189cv2.destroyAllWindows()