def post(self, request): from aip import AipOcr pic = request.FILES.get("pic") filePath = os.path.join( BASE_DIR, "frontend/dist/static/media/ic_card" + '/' + pic.name) if pic: with open( os.path.join(BASE_DIR, "frontend/dist/static/media/ic_card/%s") % pic.name, 'wb') as f: for chunk in pic.chunks(): f.write(chunk) """ 你的 APPID AK SK """ client = AipOcr(APP_ID_BAIDU, API_KEY_BAIDU, SECRET_KEY_BAIDU) """ 读取图片 """ def get_file_content(filePath): with open(filePath, 'rb') as fp: return fp.read() image = get_file_content(filePath) try: resp = client.bankcard(image) number = resp['result']['bank_card_number'] bankname = resp['result']['bank_name'] # print('识别到的卡号是:%s' % resp['result']['bank_card_number']) # print('识别卡所属银行是:%s' % resp['result']['bank_name']) data = {"识别卡号": number, "所属银行": bankname} return JsonResponse(data=data, json_dumps_params={'ensure_ascii': False}) except Exception as e: return HttpResponse('请提交银行卡照片')
class BaiduOcr(object): def __init__(self, APP_ID, API_KEY, SECRET_KEY): self.APP_ID = APP_ID self.API_KEY = API_KEY self.SECRET_KEY = SECRET_KEY self.client = AipOcr(self.APP_ID, self.API_KEY, self.SECRET_KEY) def get_file_content(self, filePath): """ :param filePath: 文件路径 :return: 文件的二进制表示 """ with open(filePath, 'rb') as fp: return fp.read() def ocr(self, filepath=None, binary_content=None): """ 输入的是图片路径或者是二进制表示,输出的是该图片上所识别出来的文字,是一个字符串 :param filepath:图片路径 :return:该图片上所识别出来的文字,是一个字符串 """ try: if filepath is not None: image = self.get_file_content(filepath) text = self.client.basicGeneral(image) else: text = self.client.basicGeneral(binary_content) words_result = text['words_result'] except: words_result = [] texts = [i['words'] for i in words_result] texts = "".join(itertools.chain(*texts)) # 将一维列表变成一个字符串 return texts def get_card_text(self, filepath): """ 输入的是一张图片的路径,输出的是该图片上所识别出来的银行类别文字是一个字符串 :param filepath:图片的路径 :return:该图片上所识别出来的银行类别文字是一个字符串 """ image = self.get_file_content(filepath) text = self.client.bankcard(image) # 识别银行卡 try: words_result = text['result'] texts = words_result['bank_name'] texts = "".join(itertools.chain(*texts)) except: texts = [] return texts
from aip import AipOcr import os # 初始化AipFace对象 APP_ID = '10908207' API_KEY = 'x4kTCeUjDOM0SNnB106WkGGv' SECRET_KEY = 'DHSAwwDrj5VuukhHbT1znjxPfGI8jef6' client = AipOcr(APP_ID, API_KEY, SECRET_KEY) def get_file_content(filePath): with open(filePath, 'rb') as fp: return fp.read() image = get_file_content(os.getcwd() + '/Images/RankCard.jpg') # 将左侧括号内3.jpg替换为待识别的图片路径 # 调用银行卡识别 result_bank = client.bankcard(image) print("银行卡号:", result_bank["result"]["bank_card_number"]) print("发卡银行:", result_bank["result"]["bank_name"])
from aip import AipOcr """ 你的 APPID AK SK """ APP_ID = '16131914' API_KEY = '0UgpVnwEPHjoyTATytsjO86N' SECRET_KEY = '6VMhro31R5tRG7zGwW0yGh9ZWrudK371' client = AipOcr(APP_ID, API_KEY, SECRET_KEY) """ 读取图片 """ def get_file_content(filePath): with open(filePath, 'rb') as fp: return fp.read() image = get_file_content('/home/wy/Desktop/iccard.jpg') resp = client.bankcard(image) print('识别到的卡号是:%s'%resp['result']['bank_card_number']) print('识别卡所属银行是:%s'%resp['result']['bank_name'])
class baidu_ai(object): def __init__(self): self.ocr = AipOcr(settings.BAIDU_APP_ID, settings.BAIDU_API_KEY, settings.BAIDU_SECRET_KEY) def load_json(self, rslt): json_dump = json.dumps(rslt, sort_keys=True, indent=4, separators=(',', ': '), ensure_ascii=True) json_rslt = json.loads(json_dump) return json_rslt ''' ====================================== 通用OCR识别 - 识别上传图像上面的字段信息 ====================================== 根据用户上传的图像,返回识别出的字段信息。 ''' @logging_elapsed_time('Baidu OCR - Basic General') def ocr_basicGeneral(self, image): # 定义参数变量 options = { 'detect_direction': "true", 'probability': "true", } # 调用人脸属性检测接口 result = self.ocr.basicGeneral(image, options) json_rslt = self.load_json(result) return json_rslt ''' ====================================== 通用文字识别(高精度版)- 用户向服务请求识别某张图中的所有文字,相对于通用文字识别该产品精度更高,但是识别耗时会稍长。 ====================================== 根据用户上传的图像,返回识别出的字段信息。 ''' @logging_elapsed_time('Baidu OCR - Basic Accurate') def ocr_basicAccurate(self, image): # 定义参数变量 options = { 'detect_direction': "true", 'probability': "true", } # 调用人脸属性检测接口 result = self.ocr.basicAccurate(image, options) json_rslt = self.load_json(result) return json_rslt ''' ====================================== 通用文字识别(含位置信息版)- 用户向服务请求识别某张图中的所有文字,并返回文字在图中的位置信息。 ====================================== 根据用户上传的图像,返回识别出的字段信息。 ''' @logging_elapsed_time('Baidu OCR - General') def ocr_general(self, image): # 定义参数变量 options = { 'detect_direction': "true", 'probability': "true", 'detect_language': 'true', 'recognize_granularity': 'small', 'language_type': 'CHN_ENG' } result = self.ocr.general(image, options) json_rslt = self.load_json(result) return json_rslt ''' ====================================== 身份证识别 - 用户向服务请求识别身份证,身份证识别包括正面和背面。 ====================================== ''' @logging_elapsed_time('Baidu OCR - ID Card') def ocr_idcardocr(self, image, id_card_side='front'): # 定义参数变量 options = { 'detect_direction': "true", 'detect_risk': "true", } # 调用人脸属性检测接口 result = self.ocr.idcard(image, id_card_side, options) json_rslt = self.load_json(result) return json_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