def _imgETL(self, imgBase64): # Convert the base64 to PIL image __img = readImage(imgBase64, outFormat='PIL') __img = __img.convert("RGB") # Resize the image to the given width (x, y) = __img.size if x < y: x_s = 600 y_s = int(y * x_s / x) else: y_s = 600 x_s = int(x * y_s / y) __img0 = __img.resize((x_s, y_s), Image.ANTIALIAS) # Image Skew handling angle = self._deskew.determine_skew(__img0) logging.info('Image angle = ' + str(angle)) __img1 = self._deskew.deskew(__img0, angle) __imgGrey = __img1.convert('L') # Capture Face image __faceImg = captureFace(__img0) # Get Doc Type by running predict model docType, confidence = self._getDocType(__imgGrey) return __imgGrey, __faceImg, docType, confidence
def _docTypePredict(self, imgBase64): # Convert the base64 to PIL image __img = readImage(imgBase64, outFormat='PIL') __imgGrey = __img.convert('L') # Get Doc Type by running predict model docType, confidence = _docClass.predict(__imgGrey) return docType, confidence
def _getDocType(self, imgBase64): params = { "api_id": "demo_id", "timestamp": get_current_timestamp(), "image": readImage(imgBase64, outFormat='Base64'), "sign": "123" } params['sign'] = self._sign._sign(params) url = 'http://127.0.0.1:8089/v1/docType' headers = {'content-type': "application/json"} response = requests.post(url, data=json.dumps(params), headers=headers) __rslt = json.loads(response.content.decode()) return __rslt['data']['docType'], __rslt['data']['confidence']
def _getOcrResult(self, vendor, img): ocrBoxes = [] if vendor == 'Baidu': tmpImg = readImage(img, outFormat='Bytes') __ai = baidu_ai() __rslt = __ai.ocr_general(tmpImg) if 'words_result_num' in __rslt.keys(): if __rslt['words_result_num'] > 0: for box in __rslt['words_result']: if box['words']: ocrBoxes.append([ int(box['location']['left']), int(box['location']['top']), int(box['location']['width']), int(box['location']['height']), box['words'] ]) elif vendor == 'Tencent': tmpImg = readImage(img, outFormat='Base64') __ai = tencent_ai() __ret, __rslt = __ai.ocr_generalocr(tmpImg) if __ret == 0: for box in __rslt['data']['item_list']: if box['itemstring']: ocrBoxes.append([ int(box['itemcoord'][0]['x']), int(box['itemcoord'][0]['y']), int(box['itemcoord'][0]['width']), int(box['itemcoord'][0]['height']), box['itemstring'] ]) elif vendor == 'JD': tmpImg = readImage(img, outFormat='Base64') __ai = jd_ai() __ret, __rslt = __ai.ocr_generalocr_v2(tmpImg) if 'result' in __rslt.keys(): if __rslt['result']['code'] == 0: for box in __rslt['result']['resultData']: if box['text']: ocrBoxes.append([ int(box['location']['x']), int(box['location']['y']), int(box['location']['width']), int(box['location']['height']), box['text'] ]) elif vendor == 'Face++': tmpImg = readImage(img, outFormat='Base64') __ai = faceplusplus_ai() __rslt = __ai.recognizeText(imgBase64=tmpImg) if __rslt['status'] == 'success': for box in __rslt['result']: _tmp_X1 = box['child-objects'][0]['position'][0]['x'] _tmp_Y1 = box['child-objects'][0]['position'][0]['y'] _tmp_X2 = box['child-objects'][-1]['position'][2]['x'] _tmp_Y2 = box['child-objects'][-1]['position'][2]['y'] ocrBoxes.append([ int(_tmp_X1), int(_tmp_Y1), int(_tmp_X2 - _tmp_X1), int(_tmp_Y2 - _tmp_Y1), box['value'] ]) elif vendor == 'Face++_Template': if str(self._docType) in FACEPLUSPLUS_TEMPLATE.keys(): tmpImg = readImage(img, outFormat='Base64') __ai = faceplusplus_ai() __rslt = __ai.OCRTemplate(templateID=FACEPLUSPLUS_TEMPLATE[str( self._docType)], imgBase64=tmpImg) if __rslt['status'] == 'success': for box in __rslt['result']: ocrBoxes.append( [box['key'], ''.join(box['value']['text'])]) elif vendor == 'Netease': tmpImg = readImage(img, outFormat='Base64') __ai = netease_ai() __rslt = __ai.ocr_generalocr(tmpImg) if __rslt['status'] == '000000': for box in __rslt['detail']['ocr']: _position = box['position'].split(",") _tmp_X1 = int(float(_position[0])) _tmp_Y1 = int(float(_position[1])) _tmp_X2 = int(float(_position[2])) _tmp_Y2 = int(float(_position[3])) ocrBoxes.append([ _tmp_X1, _tmp_Y1, _tmp_X2 - _tmp_X1, _tmp_Y2 - _tmp_Y1, box['recognition'] ]) ocrBoxes = sorted(ocrBoxes, key=itemgetter(1, 0)) logging.info(vendor + ' OCR Boxes = ' + str(ocrBoxes)) return ocrBoxes
self._setParams(__params, 'appId', self._app_id) self._setParams(__params, 'timestamp', timestamps) self._setParams(__params, 'nonce', timestamps) self._setParams(__params, 'image', imageBase64) self._setParams(__params, 'type', 'base64') headers = { 'Content-Type': 'application/x-www-form-urlencoded', 'cache-control': 'no-cache', 'appId': self._app_id, 'nonce': timestamps, 'timestamp': timestamps, 'sign': self._genSignString(__params) } __params.pop('appId') __params.pop('timestamp') __params.pop('nonce') __rslt = self._invoke(url=__url, data=__params, headers=headers) return __rslt if __name__ == '__main__': ai = netease_ai() image_data = readImage(r"..\..\templates\Samples\14 - AU Driver - NSW.jpg", outFormat='Base64') print(ai.ocr_generalocr(image_data))
__rslt = self._request(self.__recognizeText, data) return __rslt @logging_elapsed_time('Face++ OCR - OCRTemplate') def OCRTemplate(self, templateID, imgPath=None, imgBase64=None): """ 调用者提供图片文件或者图片URL,进行图片分析,找出图片中出现的文字信息 """ data = {} data['template_id'] = templateID if imgBase64: data['image_base64'] = imgBase64 else: data['image_base64'] = self._get_img_content(imgPath) return self._request(self.__ocrTemplate, data) if __name__ == "__main__": ai = faceplusplus_ai() _img = readImage(r"..\..\templates\Samples\04 - MACAU_ID_0021.jpg", outFormat='Base64') # rslt = ai.recognizeText(imgBase64=_img) rslt = ai.OCRTemplate(templateID=1573524413, imgBase64=_img) print(rslt)
''' ====================================== 银行卡OCR识别 - 识别银行卡上面的字段信息 ====================================== ''' @logging_elapsed_time('Baidu OCR - Bank Card') def ocr_creditcardocr(self, image): # 定义参数变量 options = {} # 调用人脸属性检测接口 result = self.ocr.bankcard(image, options) json_rslt = self.load_json(result) return json_rslt if __name__ == '__main__': imgFile = r'..\..\templates\Samples\001.jpg' img = Image.open(imgFile) img = img.convert('RGB') imgBase64 = readImage(img, outFormat='Base64') __ai = baidu_ai() __rslt = __ai.ocr_general(base64.b64decode(imgBase64)) print(__rslt)