def main(fn): # 读取并预处理验证码 img = cv2.imread(fn, cv2.IMREAD_GRAYSCALE) text = pretreatment.get_text(img) imgs = pretreatment.get_imgs(img) text = text / 255.0 h, w = text.shape text.shape = (1, h, w, 1) # 识别文字 model = models.load_model('model.h5') label = model.predict(text) label = label.argmax() print(label) # 加载图片分类器 data = np.load('images.npz') images, labels = data['images'], data['labels'] labels = labels.argmax(axis=1) for pos, img in enumerate(imgs): try: img.dtype = np.uint64 img = img[0] idx = list(images).index(img) label = labels[idx] print(pos // 4, pos % 4, label) except: print('unknown')
def get_text(img, offset=0): text = pretreatment.get_text(img, offset) text = cv2.cvtColor(text, cv2.COLOR_BGR2GRAY) text = text / 255.0 h, w = text.shape text.shape = (1, h, w, 1) return text
def main(fn): # 读取并预处理验证码 img = cv2.imread(fn) text = pretreatment.get_text(img) text = cv2.cvtColor(text, cv2.COLOR_BGR2GRAY) imgs = np.array(list(pretreatment._get_imgs(img))) imgs = imgs / 255.0 text = text / 255.0 h, w = text.shape text.shape = (1, h, w, 1) _, h, w, _ = imgs.shape imgs.shape = (-1, h, w, 3) # 识别文字 model = models.load_model('model.h5') label = model.predict(text) label = label.argmax() print(label) # 加载图片分类器 model = models.load_model('12306.image.model.h5') labels = model.predict(imgs) labels = labels.argmax(axis=1) for pos, label in enumerate(labels): print(pos // 4, pos % 4, label)
def get_text(self, img): img_array = np.array(img) text = pretreatment.get_text(img_array) text = cv2.cvtColor(text, cv2.COLOR_BGR2GRAY) text = text / 255.0 h, w = text.shape text.shape = (1, h, w, 1) return text
def get_text(img, offset=0): """ 获取图片文本信息 :param img: 验证码图片句柄,cv2.imread返回值 :param offset: :return: 图片处理后的文本信息 """ text = pretreatment.get_text(img, offset) text = cv2.cvtColor(text, cv2.COLOR_BGR2GRAY) text = text / 255.0 h, w = text.shape text.shape = (1, h, w, 1) return text
def verify(self, i): verify_titles = [ '打字机', '调色板', '跑步机', '毛线', '老虎', '安全帽', '沙包', '盘子', '本子', '药片', '双面胶', '龙舟', '红酒', '拖把', '卷尺', '海苔', '红豆', '黑板', '热水袋', '烛台', '钟表', '路灯', '沙拉', '海报', '公交卡', '樱桃', '创可贴', '牌坊', '苍蝇拍', '高压锅', '电线', '网球拍', '海鸥', '风铃', '订书机', '冰箱', '话梅', '排风机', '锅铲', '绿豆', '航母', '电子秤', '红枣', '金字塔', '鞭炮', '菠萝', '开瓶器', '电饭煲', '仪表盘', '棉棒', '篮球', '狮子', '蚂蚁', '蜡烛', '茶盅', '印章', '茶几', '啤酒', '档案袋', '挂钟', '刺绣', '铃铛', '护腕', '手掌印', '锦旗', '文具盒', '辣椒酱', '耳塞', '中国结', '蜥蜴', '剪纸', '漏斗', '锣', '蒸笼', '珊瑚', '雨靴', '薯条', '蜜蜂', '日历', '口哨' ] img = Image.open("../pic/verify{}.png".format(i)) text = self.get_text(img) imgs = np.array(list(pretreatment._get_imgs(img))) imgs = preprocess_input(imgs) text_list = [] self.LoadTextModel() label = self.textModel.predict(text) label = label.argmax() text = verify_titles[label] text_list.append(text) print("题目是{}".format(text)) # 获取下一个词 # 根据第一个词的长度来定位第二个词的位置 if len(text) == 1: offset = 27 elif len(text) == 2: offset = 47 else: offset = 60 text = pretreatment.get_text(img, offset=offset) if text.mean() < 0.95: label = self.textModel.predict(text) label = label.argmax() text = verify_titles[label] text_list.append(text) print("题目是{}".format(text_list)) # 加载图片分类器 self.LoadImgModel() labels = self.imgModel.predict(imgs) labels = labels.argmax(axis=1) results = [] for pos, label in enumerate(labels): l = verify_titles[label] print(pos + 1, l) if l in text_list: results.append(str(pos + 1)) print(results) return results
import os import cv2 import numpy as np import pretreatment path = 'imgs' texts = [] images = [] i = 0 for fn in os.listdir(path): i += 1 fn = os.path.join(path, fn) im = cv2.imread(fn) text = pretreatment.get_text(im) texts.append(text) images.append(list(pretreatment._get_imgs(im))) if i >= 2000: break np.savez_compressed('dataset.npz', texts=texts, images=images)
# coding: utf-8 import sys import cv2 import numpy as np from keras import models import pretreatment from mlearn_for_image import preprocess_input def get_text(img, offset=0): text = pretreatment.get_text(img, offset) text = cv2.cvtColor(text, cv2.COLOR_BGR2GRAY) text = text / 255.0 h, w = text.shape text.shape = (1, h, w, 1) return text def main(fn): # 读取并预处理验证码 img = cv2.imread(fn) text = get_text(img) imgs = np.array(list(pretreatment._get_imgs(img))) imgs = preprocess_input(imgs) # 识别文字 model = models.load_model('model.v2.0.h5') label = model.predict(text) label = label.argmax()