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
| def load_image(path): img = Image.open(path).convert("L") img = img.resize((32, 32), Image.ANTIALIAS) img = np.array(img).reshape(1, 1, 32, 32)\ .astype('float32') img = img / 255.0 * 2.0 - 1.0 img = paddle.to_tensor(img) return img
def divide_picture(path): license = cv2.imread(path) gray_img = cv2.cvtColor(license, cv2.COLOR_RGB2GRAY) retval, bin_img = cv2.threshold( gray_img, 100, 255, cv2.THRESH_BINARY )
result = [] for col in range(bin_img.shape[1]): result.append(0) for row in range(bin_img.shape[0]): result[col] += bin_img[row][col] / 255.0
place_dict, num, i = {}, 0, 0 while i < len(result): if result[i] == 0: i += 1 else: index = i + 1 while result[index] != 0: index += 1 place_dict[num] = [i, index-1] num += 1 i = index
characters = [] if not os.path.exists(TEMP_PATH): os.mkdir(TEMP_PATH) for i in range(8): if i == 2: continue padding = (170 - (place_dict[i][1] - place_dict[i][0])) / 2 ndarray = np.pad( bin_img[:, place_dict[i][0]: place_dict[i][1]], ((0, 0), (int(padding), int(padding))), "constant", constant_values=(0, 0) ) ndarray = cv2.resize(ndarray, (32, 32)) cv2.imwrite(TEMP_PATH + "/%d.png" % i, ndarray) characters.append(ndarray)
def match_labels(label_dict): temp = {'yun': '云', 'cuan': '川', 'hei': '黑', 'zhe': '浙', 'ning': '宁', 'yu': '豫', 'ji': '冀', 'hu': '沪', 'jl': '吉', 'sx': '晋', 'lu': '鲁', 'qing': '青', 'zang': '藏', 'e1': '鄂', 'meng': '蒙', 'gan1': '甘', 'qiong': '琼', 'shan': '陕', 'min': '闽', 'su': '苏', 'xin': '新', 'wan': '皖', 'jing': '京', 'xiang': '湘', 'gui': '贵', 'yu1': '渝', 'jin': '津', 'gan': '赣', 'yue': '粤', 'gui1': '桂', 'liao': '辽'} name_dict = {} for key, val in label_dict.items(): name_dict[key] = temp.get(val, val) return name_dict
|