def get_coordinate(img_str): # 储存最终坐标结果 result = '' orc_dir = '%spy12306/helpers/ocr/' % Config.PROJECT_DIR try: # 读取并预处理验证码 img = cv2.imdecode(np.fromstring(img_str, np.uint8), cv2.IMREAD_COLOR) text = get_text(img) imgs = np.array(list(_get_imgs(img))) imgs = preprocess_input(imgs) # 识别文字 model = models.load_model('%smodel.v2.0.h5' % orc_dir, compile=False) label = model.predict(text) label = label.argmax() fp = open('%stexts.txt' % orc_dir, encoding='utf-8') texts = [text.rstrip('\n') for text in fp] text = texts[label] # list放文字 titles = [text] position = [] # 获取下一个词 # 根据第一个词的长度来定位第二个词的位置 if len(text) == 1: offset = 27 elif len(text) == 2: offset = 47 else: offset = 60 text2 = get_text(img, offset=offset) if text2.mean() < 0.95: label = model.predict(text2) label = label.argmax() text2 = texts[label] titles.append(text2) # 加载图片分类器 model = models.load_model('%s12306.image.model.h5' % orc_dir, compile=False) labels = model.predict(imgs) labels = labels.argmax(axis=1) for pos, label in enumerate(labels): if texts[label] in titles: position.append(pos + 1) # 没有识别到结果 if len(position) == 0: return result result = position except: CommonLog.print_auto_code_fail( CommonLog.MESSAGE_GET_RESPONSE_FROM_FREE_AUTO_CODE) return result
def get_img_position_by_ruokuai(self, img): ruokuai_account = Config().AUTO_CODE_ACCOUNT soft_id = '119671' soft_key = '6839cbaca1f942f58d2760baba5ed987' rc = RKClient(ruokuai_account.get('user'), ruokuai_account.get('pwd'), soft_id, soft_key) result = rc.rk_create(img, 6113) if "Result" in result: return self.get_image_position_by_offset(list(result['Result'])) CommonLog.print_auto_code_fail(result.get("Error", CommonLog.MESSAGE_RESPONSE_EMPTY_ERROR)) return None
def get_image_by_free_site(self, img): data = {'img': img} response = self.session.post(API_FREE_CODE_QCR_API, data=data) result = response.json() if result.get('msg') == 'success': pos = result.get('result') return self.get_image_position_by_offset(pos) CommonLog.print_auto_code_fail( CommonLog.MESSAGE_GET_RESPONSE_FROM_FREE_AUTO_CODE) return None
def get_img_position_by_ruokuai(self, img_path): ruokuai_account = config.AUTO_CODE_ACCOUNT soft_id = '119671' soft_key = '6839cbaca1f942f58d2760baba5ed987' rc = RKClient(ruokuai_account.get('user'), ruokuai_account.get('pwd'), soft_id, soft_key) im = open(img_path, 'rb').read() result = rc.rk_create(im, 6113) if "Result" in result: return self.get_image_position_by_offset(list(result['Result'])) CommonLog.print_auto_code_fail(result.get("Error", '-')) return None
def get_img_position_by_ruokuai(self, img): ruokuai_account = Config().AUTO_CODE_ACCOUNT soft_id = '121426' soft_key = '1033017e34594d74b354da89f18717db' rc = RKClient(ruokuai_account.get('user'), ruokuai_account.get('pwd'), soft_id, soft_key) result = rc.rk_create(img, 6113) if "Result" in result: return self.get_image_position_by_offset(list(result['Result'])) CommonLog.print_auto_code_fail( result.get("Error", CommonLog.MESSAGE_RESPONSE_EMPTY_ERROR)) return None
def get_image_by_free_site(self, img): data = { 'img': img } if Config().AUTO_CODE_PLATFORM == 'free': response = self.session.post(API_FREE_CODE_QCR_API, data=data, timeout=30) else: response = self.session.post(Config().API_USER_CODE_QCR_API, data=data, timeout=30) result = response.json() if result.get('msg') == 'success': pos = result.get('result') return self.get_image_position_by_offset(pos) CommonLog.print_auto_code_fail(CommonLog.MESSAGE_GET_RESPONSE_FROM_FREE_AUTO_CODE) return None
def get_image_by_free_site(self, img): data = {'base64': img} response = self.session.post(API_FREE_CODE_QCR_API, json=data) result = response.json() if result.get('success') and result.get('check'): check_data = { 'check': result.get('check'), 'img_buf': img, 'logon': 1, 'type': 'D' } check_response = self.session.post(API_FREE_CODE_QCR_API_CHECK, json=check_data) check_result = check_response.json() if check_result.get('res'): position = check_result.get('res') return position.replace('(', '').replace(')', '').split(',') CommonLog.print_auto_code_fail(result.get("Error", '-')) return None