Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding:utf-8 -*-
- import keras
- from keras.applications.vgg19 import VGG19
- from keras.applications.densenet import decode_predictions
- from PIL import Image
- import numpy as np
- import os.path
- from DenseNet.DenseNet_ccc import densenet
- from keras.models import Model
- from keras.layers import Conv2D, Dense, Input, add, Activation, AveragePooling2D, GlobalAveragePooling2D, Lambda, \
- concatenate
- from keras import optimizers, regularizers
- from matplotlib import pyplot as plt
- import cv2
- import pickle
- import copy
- def count_paint(path): # 用于提示汉字的剪切
- img = cv2.imread(path) # 读取图片,装换为可运算的数组
- img = img[344:384, 0:344]
- GrayImage = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 将BGR图转为灰度图
- ret, thresh1 = cv2.threshold(GrayImage, 160, 255, cv2.THRESH_BINARY) # 将图片进行二值化(130,255)之间的点均变为255(背景)
- (h, w) = thresh1.shape # 返回高和宽
- a = [0 for z in range(0, w)]
- # # 记录每一列的波峰
- for j in range(0, w): # 遍历一列
- for i in range(0, h): # 遍历一行
- if thresh1[i, j] == 0: # 如果改点为黑点
- a[j] += 1 # 该列的计数器加一计数
- b = []
- for j in range(0, w):
- if a[j] == 0:
- b.append(j)
- c = []
- for i in range(len(b)-1):
- if b[i+1] - b[i] >= 3:
- c.append(b[i])
- c.append(b[i+1])
- d = []
- for i in range(1, len(c) - 1, 2):
- if c[i + 1] - c[i] < 2:
- d.append(i)
- if len(d) != 0:
- d.reverse()
- for i in range(len(d)):
- if c[d[i] + 2] - c[d[i] - 1] <= 40:
- c.pop(d[i] + 1)
- c.pop(d[i])
- e = []
- for i in range(0, len(c), 2):
- if c[i + 1] - c[i] < 10:
- left = c[i + 1] - c[i - 2] if i - 2 >= 0 else 40
- right = c[i + 3] - c[i] if i + 3 <= len(c) else 40
- if left < right:
- e.append(i - 1)
- e.append(i)
- else:
- e.append(i + 1)
- e.append(i + 2)
- if len(e) != 0:
- e.reverse()
- for i in range(len(e)):
- c.pop(e[i])
- imgs = []
- for i in range(0,len(c),2):
- sss = thresh1[0:40, c[i]:c[i+1]]
- size = sss.shape
- if size[1] < 40:
- padding = 40-size[1]
- left = padding//2
- sss = cv2.copyMakeBorder(sss, 0, 0, left, padding-left, cv2.BORDER_CONSTANT, value=[255,255,255])
- imgs.append(sss)
- else:
- print(size,'++++++++')
- plt.imshow(sss, cmap=plt.gray())
- plt.show()
- imgs.append(sss)
- return imgs, thresh1
- def get_label_dict():
- f=open('./chinese_labels','rb')
- label_dict = pickle.load(f)
- f.close()
- return label_dict
- img_rows, img_cols = 40, 40
- img_channels = 1
- num_classes = 3755
- img_input = Input(shape=(img_rows, img_cols, img_channels))
- output = densenet(img_input, num_classes)
- model = Model(img_input, output)
- model.load_weights('model-ep008-loss0.615-val_loss0.633.h5')
- sgd = optimizers.SGD(lr=.1, momentum=0.9, nesterov=True)
- model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
- label_dict = get_label_dict()
- path = '/Users/haodexian/PycharmProjects/scrapy-async-selenium-develop/reviews/img/'
- img_generator = (os.path.join(path, file) for file in os.listdir(path))
- while True:
- text = input('input "n" ( input Q to exit ): ')
- if text == 'Q' :
- break
- if text == 'n' :
- img_path = next(img_generator)
- print(img_path)
- if not os.path.exists(img_path):
- print("file not exist!")
- continue
- try:
- images, ori = count_paint(img_path)
- plt.imshow(ori[:,0:150], cmap=plt.gray())
- plt.show()
- for i in range(len(images)):
- img = cv2.resize(images[i], (40, 40))
- img = (img.reshape(1, 40, 40, 1)).astype('int32')/255 #例子的个数,图片的 通道数,图片的长与宽
- results = model.predict(img)[0]
- # predict = np.argmax(results, axis=1)
- # print(label_dict[predict[0]])
- top_indices = results.argsort()[-20:][::-1]
- result = []
- for i in top_indices:
- if results[i] * 100 >= 1:
- result.append((label_dict[i],int(results[i] * 100)))
- print(result)
- except Exception as e:
- raise e
Add Comment
Please, Sign In to add comment