def BaiduOCR(picture, APP_ID, API_KEY, SECRET_KEY): """利用百度api识别文本,并保存提取的文字 picfile: 图片文件名 outfile: 输出文件 """ client = AipOcr(APP_ID, API_KEY, SECRET_KEY) if isinstance(picture, str): with open(picture, 'rb') as file: img = file.read() message = client.basicGeneral(img) # 通用文字识别,每天 50 000 次免费 # message = client.basicAccurate(img) # 通用文字高精度识别,每天 800 次免费 print("baidu ocr message: "+message) if message.get('words_result')[0] is not None: return [word['words'].replace(' ','') for word in message.get('words_result')] else: raise Exception("Failed to recognize") pass elif isinstance(picture, PIL.Image.Image): buf = BytesIO() picture.save(buf, format='PNG') img = buf.getvalue() #转换为字节流 message = client.basicGeneral(img) # 通用文字识别,每天 50 000 次免费 # message = client.basicAccurate(img) # 通用文字高精度识别,每天 800 次免费 print("baidu ocr message: " + message) if message.get('words_result')[0] is not None: return [word['words'].replace(' ','') for word in message.get('words_result')] else: raise Exception("Failed to recognize") else: raise Exception("Wrong picture data type")
def python_baidu(): try: picfile = 'D:/YZM/1.jpg' filename = path.basename(picfile) APP_ID = '10710735' # 刚才获取的 ID,下同 API_KEY = '42XxnGaFENV4rcWk1dFiulDZ' SECRECT_KEY = 'vNN9W604gIFTu4tDZ6vhxVZrzo0aPWOO' client = AipOcr(APP_ID, API_KEY, SECRECT_KEY) i = open(picfile, 'rb') img = i.read() print("正在识别图片:\t" + filename) message = client.basicGeneral(img) # 通用文字识别,每天 50 000 次免费 # message = client.basicAccurate(img) # 通用文字高精度识别,每天 800 次免费 for line in message["words_result"]: print(line["words"]) return line["words"] except Exception as e: print("第二个图片识别") picfile = 'D:/YZM/1.jpg' filename = path.basename(picfile) APP_ID = '11651945' # 刚才获取的 ID,下同 API_KEY = 'euBrpM5GVVHHaeOVyPWY2z9F' SECRECT_KEY = 'zhMOjAa58oyYAMeZGpF1qBu0CvtrSDBY' client = AipOcr(APP_ID, API_KEY, SECRECT_KEY) i = open(picfile, 'rb') img = i.read() print("正在识别图片:\t" + filename) message = client.basicGeneral(img) # 通用文字识别,每天 50 000 次免费 # message = client.basicAccurate(img) # 通用文字高精度识别,每天 800 次免费 for line in message["words_result"]: print(line["words"]) return line["words"]
def vcode2str(img_url): """ 你的 APPID AK SK """ APP_ID = '' API_KEY = '' SECRET_KEY = '' 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('YZM.jpg') image = get_file_content(img_url) """ 调用通用文字识别, 图片参数为本地图片 """ client.basicGeneral(image) """ 如果有可选参数 """ options = {} options["language_type"] = "CHN_ENG" options["detect_direction"] = "false" options["detect_language"] = "false" options["probability"] = "false" """ 带参数调用通用文字识别, 图片参数为本地图片 """ a = client.basicGeneral(image, options) length = len(a['words_result']) b = "" for i in range(length): b = b + a['words_result'][i]["words"] print("图片文本: " + b) with open("original.txt", 'w+') as fp: fp.write(b) print("文字识别部分已保存到本地") fp.close() return b
def scan_png(png_path, scan_type1, api_key_path='api.json'): """ 此函数用于识别png编号 :param png_path:png路径 :param scan_type1:识别类别 :param api_key_path: api相关信息所在路径 :return: """ f2 = open(api_key_path, 'rt', encoding='utf-8') api_dict3 = json.load(f2) f2.close() """ 你的 APPID AK SK """ app_id = api_dict3['app_id'] api_key = api_dict3['api_key'] secret_key = api_dict3['secret_key'] client = AipOcr(app_id, api_key, secret_key) with open(png_path, 'rb') as fp: image = fp.read() """ 调用通用文字识别, 图片参数为本地图片 """ client.basicGeneral(image) if scan_type1 == '识别票据': response = client.receipt(image) # 识别票据 elif scan_type1 == '识别数字': response = client.numbers(image) # 识别数字 elif scan_type1 == '普通识别(高精度)': response = client.basicAccurate(image) # 普通高精度识别 else: response = client.basicGeneral(image) # 普通识别 print(response) result1 = response['words_result'][0]['words'] # 获取结果 return result1
def ocr(input_file): """ 调用百度ocr :param input_file: :param outpath: :return: """ # client = AipOcr(config.APP_ID, config.API_KEY, config.SECRET_KEY) xi = input_file.split("/")[-1].split(".")[0] zzz = config.get_appid(int(xi)) client = AipOcr(zzz[0], zzz[1], zzz[2]) """ 调用通用文字识别(高精度版) """ image = read_image(input_file) client.basicGeneral(image) # client.basicAccurate(image); """ 如果有可选参数 """ options = {} options["detect_direction"] = "true" options["probability"] = "true" """ 带参数调用通用文字识别(高精度版) """ result = client.basicAccurate(image, options) print(input_file, 'result: ', result) # if result.get('error_code', 0) != 0: # return [] words_result = result.get("words_result", []) words = [ '' if re.match(u"[\u4e00-\u9fa5]+", words_result[i]["words"]) is None else words_result[i]["words"] for i in range(len(words_result)) ] return ''.join(words)
def main(): print('程序启动,版本 V1.0.1') os.system("adb shell /system/bin/screencap -p /sdcard/screenshot.png") os.system("adb pull /sdcard/screenshot.png ./screenshot.png") im = Image.open(r"screenshot.png") im.save("screen/screenshot%s.png" % time.time()) # 图片备份 # img_size = im.size #屏幕分辨路 w = im.size[0] # h = im.size[1] # print("xx:{}".format(img_size)) region = im.crop((70, 200, w - 70, 600)) # 裁剪的区域 # region = im.crop((70, 200, w - 70, 700)) # 裁剪的区域 region.save("crop_test1.png") answer1 = im.crop((70, 600, w - 70, 800)) answer1.save("answer1.png") answer2 = im.crop((70, 800, w - 70, 1000)) answer2.save("answer2.png") answer3 = im.crop((70, 1000, w - 70, 1200)) answer3.save("answer3.png") client = AipOcr(BaiDuApi.APP_ID, BaiDuApi.API_KEY, BaiDuApi.SECRET_KEY) image = get_file_content('crop_test1.png') """ 调用通用文字识别, 图片参数为本地图片 """ client.basicGeneral(image) """ 带参数调用通用文字识别, 图片参数为本地图片 """ reqDataDict = client.basicGeneral(image, BaiDuApi.options) try: words_result = reqDataDict['words_result'] except: pass else: question = words_result[0]['words'].split('.') getAnswer(question[1])
def post(self, request): from aip import AipOcr pic = request.FILES.get("pic") # 将前端提交来的图片保存本地 # fs = FileSystemStorage() # fs.save(pic.name, pic) # print(os.path.join(BASE_DIR, "frontend/dist/static/media/test")) if pic: with open( os.path.join(BASE_DIR, "frontend/dist/static/media/char/%s") % pic.name, 'wb') as f: for chunk in pic.chunks(): f.write(chunk) # return HttpResponse('保存成功') """ 你的 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( os.path.join( BASE_DIR, "frontend/dist/static/media/char" + '/' + pic.name)) """ 调用通用文字识别, 图片参数为本地图片 """ client.basicGeneral(image) """ 如果有可选参数 """ options = {} options["language_type"] = "CHN_ENG" options["detect_direction"] = "true" options["detect_language"] = "true" options["probability"] = "true" """ 带参数调用通用文字识别, 图片参数为本地图片 """ try: resp = client.basicGeneral(image, options) list = [] for i in resp['words_result']: word = i['words'] list.append(word) with open('./test.txt', 'w') as f: for i in list: f.writelines(i + "\n") with open('./test.txt', 'r') as f: # print('识别到的内容是:%s' % f.read()) content = f.read() new_content = content.replace('\n', '') # print(new_content) data = {"content": new_content} return JsonResponse(data=data, json_dumps_params={'ensure_ascii': False}) except Exception as e: print(e) return HttpResponse('请提交带文字的图片') else: return HttpResponse('请先提交文字图片')
def img_ocr(self): client = AipOcr(APP_ID, API_KEY, SECRET_KEY) image = self.get_file_content(self.img_path) """ 调用通用文字识别, 图片参数为本地图片 """ client.basicGeneral(image) """ 如果有可选参数 """ options = {} options["language_type"] = "CHN_ENG" #中英文混合 options["detect_direction"] = "true" #检测图像朝向 options["detect_language"] = "true" #检测语言 options["probability"] = "true" #返回置信度 """ 带参数调用通用文字识别, 图片参数为本地图片 """ bendi = client.basicGeneral(image, options) return bendi
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
class BaiDuOcr: def __init__(self): self.api_id = THEKEY2BD.THEKE2BD_api_id self.api_key = THEKEY2BD.THEKE2BD_api_key self.secret_key = THEKEY2BD.THEKE2BD_secret_key self.client = AipOcr(self.api_id, self.api_key, self.secret_key) def invoke(self,path): self.image = self.get_file_content(path) """ 调用通用文字识别, 图片参数为本地图片 """ self.client.basicGeneral(self.image); """ 如果有可选参数 """ options = {} options["language_type"] = "CHN_ENG" options["detect_direction"] = "true" options["detect_language"] = "true" options["probability"] = "true" """ 带参数调用通用文字识别, 图片参数为本地图片 """ data = self.client.basicGeneral(self.image, options) # print(data['words_result']) d=data['words_result'] return d def invokeHigh(self,path): self.image = self.get_file_content(path) """ 调用通用文字识别(高精度版) """ self.client.basicAccurate(self.image); """ 如果有可选参数 """ options = {} options["language_type"] = "CHN_ENG" options["detect_direction"] = "true" options["detect_language"] = "true" options["probability"] = "true" """ 带参数调用通用文字识别(高精度版) """ data=self.client.basicAccurate(self.image, options) d = data['words_result'] return d # print(data['words_result']) def get_file_content(self,filePath): with open(filePath, 'rb') as fp: return fp.read()
def get_text_from_image(image_data, app_id, app_key, app_secret, api_version=0, timeout=3): """ Get image text use baidu ocr :param image_data: :param app_id: :param app_key: :param app_secret: :param api_version: :param timeout: :return: """ client = AipOcr(appId=app_id, apiKey=app_key, secretKey=app_secret) client.setConnectionTimeoutInMillis(timeout * 1000) options = {} options["language_type"] = "CHN_ENG" if api_version == 1: result = client.basicAccurate(image_data, options) else: result = client.basicGeneral(image_data, options) if "error_code" in result: print("baidu api error: ", result["error_msg"]) return "" return "".join([words["words"] for words in result["words_result"]])
class ShowService(object): def __init__(self): # 读取工单配置信息--登录百度云》产品》人工智能》文字识别》立即使用 target = configparser.ConfigParser() target.read('PWconfig.ini', encoding='utf-8') AppID = target.get('MyPW', 'AppID') APIKey = target.get('MyPW', 'APIKey') SecretKey = target.get('MyPW', 'SecretKey') # 类内均可调用 self.client = AipOcr(AppID, APIKey, SecretKey) # 读取图片 @staticmethod #静态方法 def getPicture(filepath): with open(filepath, 'rb') as file: return file.read() # 识别图片 def showPicText(self, filepath): # 读取图片 image = self.getPicture(filepath) # 识别图片 text = self.client.basicGeneral(image) #print(text) content = '' for item in text['words_result']: #print(item['words']) content = content + ''.join(item['words']) return content
def get_captcha_code(session): captcha_link = "http://ceodelhi.gov.in/OnlineErms/CapchaControlImage.aspx" headers = {'User-Agent': UserAgent().random} response = session.get(url=captcha_link, headers=headers) with open("./captchacode.png", "wb") as fn: fn.write(response.content) img = Image.open("./captchacode.png") imgry = img.convert('L') # 转化为灰度图 imgry.save('./captchacode.png') #使用的百度AIP APP_ID = '15179847' API_KEY = 'CvVnnyicDjZHBoBn9itQD9sG' SECEER_KEY = 'nv0hjAgETYSEG1NOykEcRSqdCkf2o7wI' client = AipOcr(APP_ID, API_KEY, SECEER_KEY) def get_file_content(filePath): with open(filePath, 'rb') as fp: return fp.read() # 定义参数变量 options = { 'detect_direction': 'true', 'language_type': 'CHN_ENG', } filePath = './captchacode.png' # 调用通用文字识别接口 result = client.basicGeneral(get_file_content(filePath), options) captcha = result['words_result'][0]['words'] # print(captcha) # captcha = input("请输入验证码:") # print(captcha) return captcha
def ocr_pic(file): """ 你的 APPID AK SK,在百度云获取,https://cloud.baidu.com/""" APP_ID = '10665005' API_KEY = '2ly8CzT3FhvU6nIlbemyxR58' SECRET_KEY = 'Aetgo4i2O35HNH1M8Kn5ROFv6BdgRrPC' client = AipOcr(APP_ID, API_KEY, SECRET_KEY) # 定义参数变量 options = { 'detect_direction': 'true', 'language_type': 'CHN_ENG', } # 调用通用文字识别接口 result = client.basicGeneral(get_file_content(file), options)['words_result'] if 'error_code' in result: print('错误代码:', result['error_code'], '\n错误原因:', result['error_msg']) exit() words = '' for each in result: words += each['words'] return words
def baiduOCR(picfile, outfile): """利用百度api识别文本,并保存提取的文字 picfile: 图片文件名 outfile: 输出文件 """ filename = path.basename(picfile) APP_ID = '19790590' # 刚才获取的 ID,下同 API_KEY = 'Qbm4Aha9xyGXss4iDaD1i82E' SECRECT_KEY = 'aEgDovQFCkkNx5EYosBEPsoFfes3hzUy' client = AipOcr(APP_ID, API_KEY, SECRECT_KEY) i = open(picfile, 'rb') img = i.read() print("正在识别图片:\t" + filename) message = client.basicGeneral(img) # 通用文字识别,每天 50 000 次免费 # message = client.basicAccurate(img) # 通用文字高精度识别,每天 800 次免费 print("识别成功!") i.close(); with open(outfile, 'a+') as fo: fo.writelines("+" * 60 + '\n') fo.writelines("识别图片:\t" + filename + "\n" * 2) fo.writelines("文本内容:\n") # 输出文本内容 for text in message.get('words_result'): fo.writelines(text.get('words') + '\n') fo.writelines('\n' * 2) print("文本导出成功!") print()
def run(self): # """ 带参数调用通用文字识别, 图片参数为本地图片 """ AipOcr.setConnectionTimeoutInMillis(self, ms=5000) #设置连接超时时间,一般没必要设置 AipOcr.setSocketTimeoutInMillis(self, ms=6000) #设置传送文件超时时间,一般么没必要设置 client = AipOcr(APP_ID, API_KEY, SECRET_KEY) result = {} try: result = client.basicGeneral(image, options) if result.get("error_msg"): print(result['error_msg']) contents.SetValue(result['error_msg']) else: resultword = result['words_result'] num = result['words_result_num'] OCRtext = [] for i in range(0, num): print(resultword[i]['words']) #由于返回的信息 OCRtext.append(resultword[i]['words']) OutPutOCRtext = '\n'.join(OCRtext) wx.CallAfter(pub.sendMessage, 'update', re_msg="以下为识别内容") wx.CallAfter(pub.sendMessage, 'update', re_msg=OutPutOCRtext) except: print('发生错误') wx.CallAfter(pub.sendMessage, 'update', re_msg="发生错误\n请重试")
def baiduOCR(picfile, outfile): """利用百度api识别文本,并保存提取的文字 picfile: 图片文件名 outfile: 输出文件 """ filename = path.basename(picfile) APP_ID = '11538352' # 刚才获取的 ID,下同 API_KEY = 'xghp7zQ6MfTxhwft11gQWNuZ' SECRECT_KEY = 'scH7OdvTYz80gURqcAOipIAR9xzZKVRp' client = AipOcr(APP_ID, API_KEY, SECRECT_KEY) i = open(picfile, 'rb') img = i.read() print("正在识别图片:\t" + filename) message = client.basicGeneral(img) # 通用文字识别,每天 50 000 次免费 #message = client.basicAccurate(img) # 通用文字高精度识别,每天 800 次免费 print("识别成功!") i.close() with open(outfile, 'a+') as fo: fo.writelines("+" * 60 + '\n') fo.writelines("识别图片:\t" + filename + "\n" * 2) fo.writelines("文本内容:\n") # 输出文本内容 for text in message.get('words_result'): fo.writelines(text.get('words') + '\n') fo.writelines('\n' * 2) print("文本导出成功!") print()
def ocr_img_job_baidu(image, config): # 百度OCR API ,在 https://cloud.baidu.com/product/ocr 上注册新建应用即可 """ 你的 APPID AK SK """ APP_ID = config.get('baidu_api', 'APP_ID') API_KEY = config.get('baidu_api', 'API_KEY') SECRET_KEY = config.get('baidu_api', 'SECRET_KEY') client = AipOcr(APP_ID, API_KEY, SECRET_KEY) global rest_region # 切割题目+选项区域,左上角坐标和右下角坐标,自行测试分辨率 job_region = config.get("region", "job_region").replace(' ', '').split(',') job_region = list(map(int, job_region)) region_im = image.crop( (job_region[0], job_region[1], job_region[2], job_region[3])) print(region_im) # 转化为灰度图 #region_im = region_im.convert('L') # 把图片变成二值图像 #region_im = binarizing(region_im, 190) #region_im.show() img_byte_arr = io.BytesIO() region_im.save(img_byte_arr, format='PNG') image_data = img_byte_arr.getvalue() # base64_data = base64.b64encode(image_data) response = client.basicGeneral(image_data) print(response) words_result = response['words_result'] texts = [x['words'] for x in words_result] return texts
class BaiDuAPI(object): # """ 你的 APPID AK SK """ APP_ID = '14238273' API_KEY = 'Wie6KuTOYulYblTEITKdeQAv' SECRET_KEY = 'XPqD1IuqnQ5QOUFdMLDkqyU0rySfwdet' def __init__(self): self.client_AipOcr = AipOcr(self.APP_ID, self.API_KEY, self.SECRET_KEY) # 语音识别 #self.client_AipSpeech = AipSpeech(self.APP_ID, self.API_KEY, self.SECRET_KEY) def pic2txt(self, filename): '''根据图像识别文字''' image = self.getPictuer(filename) texts = self.client_AipOcr.basicGeneral(image) return texts @staticmethod def getPictuer(filename): with open(filename, 'rb') as fp: return fp.read() # 解析体检编号 def get_tjbh(self, filename): texts = self.pic2txt(filename) comtent = reduce(lambda x, y: x + y, [words['words'] for words in texts['words_result']]) re_tjbh = re.compile(r"D\|?(\d{9})", re.DOTALL) tjbhs = re_tjbh.findall(comtent) if tjbhs: return tjbhs[0] else: return ''
def baiduOCR(picfile, outfile): """利用百度api识别文本,并保存提取的文字 picfile: 图片文件名 outfile: 输出文件 """ filename = path.basename(picfile) APP_ID = '19311862' # 刚才获取的 ID,下同 API_KEY = '4B0sdcxAUuasMRxeuhecBTrb' SECRECT_KEY = 'rOntQ0PfIpVN3h5FlMx3UtRhnhTb5vjU' client = AipOcr(APP_ID, API_KEY, SECRECT_KEY) i = open(picfile, 'rb') img = i.read() print("正在识别图片:\t" + filename) message = client.basicGeneral(img) # 通用文字识别,每天 50 000 次免费 #message = client.basicAccurate(img) # 通用文字高精度识别,每天 800 次免费 print("识别成功!") i.close() with open(outfile, 'a+') as fo: fo.writelines("+" * 60 + '\n') fo.writelines("识别图片:\t" + filename + "\n" * 2) fo.writelines("文本内容:\n") # 输出文本内容 for text in message.get('words_result'): fo.writelines(text.get('words') + '\n') fo.writelines('\n' * 2) print("文本导出成功!") print()
class RecGt(object): def __init__(self): """ 你的 APPID AK SK """ self.APP_ID = '19387667' self.API_KEY = 'XIC5SNSR6npc95LOIanGSdHL' self.SECRET_KEY = 'LjrXDtObeVAlsgmSk3KT6tS60GRCFmf1' self.client = AipOcr(self.APP_ID, self.API_KEY, self.SECRET_KEY) self.filepath = r'e:\python_xls\文字识别\test.png' # r在这里是防止转义 def screenshot(self): if not keyboard.wait(hotkey='f1'): # 如果按下了F1,并且 再按下了enter,ctrl+c有点问题 if not keyboard.wait(hotkey='enter'): time.sleep(0.01) # 延时0.01 image = ImageGrab.grabclipboard() # 放入剪切板 image.save('test.png') def reg(self): f = open(self.filepath, 'rb') image = f.read() f.close() """ 调用通用文字识别, 图片参数为本地图片 """ req = self.client.basicGeneral(image) print(req) all_text = '' for i in req['words_result']: all_text += i['words'] + '\n' print(all_text)
def main(enter, app): time_start = time.time() # 处理设备截图 adb_get_screen(enter, app) # 获取 OCR 结果 OCR = AipOcr(APP_ID, API_KEY, SECRET_KEY) try: respon = OCR.basicGeneral( get_crop_data(r"./screenshots/screenshot_crop.png")) words_result = respon["words_result"] except: print("error: baidu ocr error") sys.exit() # 处理获取结果 question = "" if app in (1, 2, 3): answer = ["", "", ""] else: answer = ["", "", "", ""] i = 0 for words in words_result: i += 1 if i <= len(words_result) - len(answer): question += words["words"] else: answer[len(words_result) - i] = words["words"] # 开始统计搜索 AI(question, answer[::-1]).ai_search(app) # 统计程序用时 time_end = time.time() print("use {0} seconds".format(round(time_end - time_start, 2)))
def baidu(filePath): # 定义常量 APP_ID = '17711416' API_KEY = 'EYb372x8lvKK9c5dBUXhwzFV' SECRET_KEY = 'M9X5TSh3lBvsXphfOaEdqYqnToB1VoAo' # 初始化AipFace对象 aipOcr = AipOcr(APP_ID, API_KEY, SECRET_KEY) # # 读取图片 # filePath = "test.png" def get_file_content(filePath): with open(filePath, 'rb') as fp: return fp.read() # 定义参数变量 options = { 'detect_direction': 'true', 'language_type': 'CHN_ENG', } # 调用通用文字识别接口 result = aipOcr.basicGeneral(get_file_content(filePath), options) # print(type(result)) print(result) if result['words_result']: return result['words_result'][0]['words'] else: return ''
def screenShot(): info = [] for _ in range(sys.maxsize): APP_ID = '11394681' API_KEY = 'UV8a1KOWokF4kYx4AiD4V9NX' SECRET_KEY = 'uNwhkOv56NaIlz9DDVZHn8fAV8oxN9Tg' client = AipOcr(APP_ID, API_KEY, SECRET_KEY) def get_file_content(filePath): with open(filePath, 'rb') as fp: return fp.read() # 截图开始 if keyboard.wait(hotkey='ctrl+alt+0') == None: # print(keyboard.wait(hotkey='ctrl+alt+0') ) # 截图条件 if keyboard.wait(hotkey='enter') == None: # 防止获取上一张截图 # 复制剪切板图片 time.sleep(0.01) im = ImageGrab.grabclipboard() im.save('Picture.png') image = get_file_content('Picture.png') sum = client.basicGeneral(image)['words_result'] for i in range(len(sum)): result = sum[i]['words'] info.append(result) print(''.join(info))
def get_word_by_image(img): app_id = '10731656' api_key = 'ZeGHQUlwVvMY9fFl81P12WD9' secret_key = 'Oy3FdgBajMlBf1utUcUMT7I6PyfKLRji' client = AipOcr(app_id, api_key, secret_key) res = client.basicGeneral(img) return res
def 识别区域文字(区域坐标): # 根据传入位置截图并保存 s = ImageGrab.grab(区域坐标) s.save(f"{区域坐标}.png") # s.show() # 创建图片识别客户端 client = AipOcr(APP_ID, API_KEY, SECRET_KEY) # 读取图片 with open(f"{区域坐标}.png", 'rb') as fp: img = fp.read() # 读完删除 os.remove(f"{区域坐标}.png") # 开始图片识别 while True: res = client.basicGeneral(img); # print(res) if "limit" in str(res) or "words_result" not in str(res):continue else: r = "" for i in res["words_result"]: r += i["words"]+"\n" return r
def screenshot(self): """ 通过百度的API读取图片中的文本信息 :return:编译出的文本信息 """ # 在配置文件中读取百度API的账号信息 APP_ID = ProfileDataProcessing("commonality", "APP_ID").config_File() API_KEY = ProfileDataProcessing("commonality", "API_KEY").config_File() SECRET_KEY = ProfileDataProcessing("commonality", "SECRET_KEY").config_File() # 定义参数变量 options = { 'detect_direction': 'true', 'language_type': 'CHN_ENG', } # 初始化AipFace对象 aipOcr = AipOcr(APP_ID, API_KEY, SECRET_KEY) # 调用通用文字识别接口 result = aipOcr.basicGeneral( pictureProcessing(self.path).get_file_content(), options) words_result = result['words_result'] for i in range(len(words_result)): self.list1.append(words_result[i]['words']) # 合并列表 str = ''.join(self.list1) return str
def get_text_from_image(image_data, app_id, app_key, app_secret, timeout=3): """ Get image text use baidu ocr :param image_data: :param app_id: :param app_key: :param app_secret: :param timeout: :return: """ client = AipOcr(appId=app_id, apiKey=app_key, secretKey=app_secret) client.setConnectionTimeoutInMillis(timeout * 1000) options = {} options["language_type"] = "CHN_ENG" options["detect_direction"] = "true" options["detect_language"] = "true" options["probability"] = "true" result = client.basicGeneral(image_data, options) if "error_code" in result: print("baidu api error: ", result["error_msg"]) return "" return "".join([words["words"] for words in result["words_result"]])
def 识别区域(位置): # 根据传入位置截图并保存 ImageGrab.grab(位置).save(f"{位置}.png") # 创建图片识别客户端 client = AipOcr(APP_ID, API_KEY, SECRET_KEY) # 读取图片 with open(f"{位置}.png", 'rb') as fp: img = fp.read() # 读完删除 os.remove(f"{位置}.png") # 开始图片识别 while True: res = client.basicGeneral(img) if "limit" in str(res): continue else: # # for i in res["words_result"]: # # print(i["words"]) # print(res) # break return str(res)
def baiduOCR(picfile, outfile): """利用百度api识别文本,并保存提取的文字 picfile: 图片文件名 outfile: 输出文件 """ filename = path.basename(picfile) APP_ID = input('appId:') API_KEY = input('api_key:') SECRECT_KEY = input('secret_key:') client = AipOcr(APP_ID, API_KEY, SECRECT_KEY) i = open(picfile, 'rb') img = i.read() print("正在识别图片:\t" + filename) message = client.basicGeneral(img) # 通用文字识别,每天 50 000 次免费 # message = client.basicAccurate(img) # 通用文字高精度识别,每天 800 次免费 print("识别成功!") i.close() with open(outfile, 'a+') as fo: fo.writelines("+" * 60 + '\n') fo.writelines("识别图片:\t" + filename + "\n" * 2) fo.writelines("文本内容:\n") # 输出文本内容 for text in message.get('words_result'): fo.writelines(text.get('words') + '\n') fo.writelines('\n' * 2) print("文本导出成功!") print()
def baiduOrc(): # 百度ORC APPID,AK,SK APP_ID = '10665196' API_KEY = 'Vzts1FQORkGMydqNyWieFdX6' SECRET_KEY = '43k9OGgOcj5RZOQe0KXmdmKeeKTUXonT' clicent = AipOcr(APP_ID, API_KEY, SECRET_KEY) # 读取图片内容 question_image = getImgContent('question.png') # 调用通用文字识别, 图片参数为本地图片 qusetion_image_text = clicent.basicGeneral(question_image) # 提取问题 question = '' for i in qusetion_image_text['words_result']: question += i['words'] # 过滤字符 # 查找第一个字符‘.’,返回键值 dot_num = question.find('.') if dot_num > -1: question = question[dot_num + 1:] else: if question[:2].isdigit(): question = question[2:] elif question[:1].isdigit(): question = question[1:] print(u'问题:{}'.format(question)) return question
def imageRecognition(image): APP_ID = '1' API_KEY = '1' SECRET_KEY = '1' client = AipOcr(APP_ID, API_KEY, SECRET_KEY) text = client.basicGeneral(image) print 'read image...' kw_list = [] for xy in text['words_result']: kw_list.append(xy['words']) kw = ''.join(kw_list)#问题列表转字符串 return kw
def get_distinguish_img_str(name): s = '' try: client = AipOcr(AppID,API_Key,Secret_Key) img = open(name,'rb').read() msg = client.basicGeneral(img) for m in msg.get('words_result'): s += m.get('words') + '\n' except Exception as ex: s = str(ex) if not s: s = 'No Img Data !' return s
def ocr_img_baidu(image, config): # 百度OCR API ,在 https://cloud.baidu.com/product/ocr 上注册新建应用即可 """ 你的 APPID AK SK """ APP_ID = config.get('baidu_api','APP_ID') API_KEY = config.get('baidu_api','API_KEY') SECRET_KEY = config.get('baidu_api','SECRET_KEY') client = AipOcr(APP_ID, API_KEY, SECRET_KEY) global combine_region # 切割题目+选项区域,左上角坐标和右下角坐标,自行测试分辨率 combine_region = config.get("region", "combine_region").replace(' ','').split(',') combine_region = list(map(int, combine_region)) region_im = image.crop((combine_region[0], combine_region[1], combine_region[2], combine_region[3])) # 转化为灰度图 #region_im = region_im.convert('L') # 把图片变成二值图像 #region_im = binarizing(region_im, 190) #region_im.show() img_byte_arr = io.BytesIO() region_im.save(img_byte_arr, format='PNG') image_data = img_byte_arr.getvalue() # base64_data = base64.b64encode(image_data) response = client.basicGeneral(image_data) #print(response) words_result = response['words_result'] texts = [x['words'] for x in words_result] # print(texts) if len(texts) > 2: question = texts[0] choices = texts[1:] choices = [x.replace(' ', '') for x in choices] else: print(Fore.RED + '截图区域设置错误,请重新设置' + Fore.RESET) exit(0) # 处理出现问题为两行或三行 if choices[0].endswith('?'): question += choices[0] choices.pop(0) elif choices[1].endswith('?'): question += choices[0] question += choices[1] choices.pop(0) choices.pop(0) return question, choices
from aip import AipOcr import json APP_ID = '10706210' API_KEY = 'kh6kczBGNeE6zFDDFS6U6zC4' SECRET_KEY = 'L8D6xMO5BkVlISKGaG2TP90vhM0yd1eV' aipOcr = AipOcr(APP_ID, API_KEY, SECRET_KEY) options = { 'detect_direction': 'true', 'language_type': 'CHN_ENG', } def get_file_content(c_filePath): with open(c_filePath, 'rb') as fp: return fp.read() if __name__ == '__main__': q_filePath = "test.jpg" result = aipOcr.basicGeneral(get_file_content(q_filePath), options) c_Result_s = '' for word_s in result['words_result']: c_Result_s = c_Result_s + word_s['words'] print(c_Result_s)
class sxufreesite: index_url = "" class_url = "" score_url = "" usrname = "" password = "" header = "" table = "" cookie = "" handler = "" values = {} tr_list = [] td_list = [] class_list = [] js_list = [] jxl_list_101 = [] jxl_list_102 = [] final_list = [] APP_ID = '11519354' API_KEY = 'tLlZhgC4kwx8ArqEhBXzCvRw' SECRET_KEY = 'GnpZ0XXBFgZXz8v0aYTGIMhHRMmlRKSd' def __init__(self): self.index_url = "http://bkjw.sxu.edu.cn/" self.class_url = "http://bkjw.sxu.edu.cn/ZNPK/KBFB_RoomSel.aspx" self.score_url = "http://bkjw.sxu.edu.cn/_data/login.aspx" self.cookie = cookiejar.CookieJar() self.handler = rq.HTTPCookieProcessor(self.cookie) self.opener = rq.build_opener(self.handler) self.header = { "Host":"bkjw.sxu.edu.cn", "Origin":"http://bkjw.sxu.edu.cn", "Content-Type":"application/x-www-form-urlencoded", "Referer":"http://bkjw.sxu.edu.cn/_data/login.aspx", "Upgrade-Insecure-Requests":"1", "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36" } self.header2 = { "Host":"bkjw.sxu.edu.cn", "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36", "Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8", "Accept-Language":"zh-CN,zh;q=0.8", "Accept-Encoding":"gzip, deflate", "Referer":"http://bkjw.sxu.edu.cn/xscj/Stu_MyScore.aspx", "Content-Type":"application/x-www-form-urlencoded", "Content-Length":"76", "Cookie":"", "Connection":"keep-alive", "Upgrade-Insecure-Requests":"1" } self.jxl_list = ["101","105"] self.js_list_101 = ["1010101","1010102","1010103","1010104","1010105","1010106","1010107","1010108","1010109","1010110","1010111","1010112","1010113","1010114","1010115","1010201","1010202","1010203","1010204","1010205","1010206","1010207","1010208","1010301","1010302","1010303","1010304","1010305","1010306","1010307","1010308","1010401","1010402","1010501","1010502","1010503","1010504","1010505","1010506","1010507","1010508","1010509","1010510","1010511"] self.jxl_list_105 = ['1050101', '1050102', '1050103', '1050104', '1050105', '1050106', '1050107', '1050108', '1050109', '1050110', '1050111', '1050112', '1050113', '1050114', '1050115', '1050116','1050201', '1050202', '1050203', '1050204', '1050205', '1050206', '1050207', '1050208', '1050209','1050211', '1050212', '1050213', '1050214', '1050215', '1050216', '1050217', '1050218','1050301', '1050302', '1050303', '1050304', '1050305', '1050306', '1050307', '1050308', '1050309','1050310','1050311', '1050312', '1050313', '1050314', '1050315', '1050316', '1050317','1050401', '1050402', '1050403', '1050404', '1050405', '1050406', '1050407', '1050408', '1050409','1050501','1050502','1050503','1050504','1050505'] self.client = AipOcr(self.APP_ID, self.API_KEY, self.SECRET_KEY) def get_img_code(self): req = rq.Request("http://bkjw.sxu.edu.cn/sys/ValidateCode.aspx",headers=self.header) with self.opener.open(req) as gec: # print(cookie) name = "imgCode.jpg" img_res = gec.read() with open(name,"wb") as ic: ic.write(img_res) print(self.cookie) # def get_score(self): # username = "******" # password = "******" # cookies = {} # for item in self.cookies: # cookies["name"] = item.name # cookies["value"] = item.value # cookies["domain"] = ".bkjw.sxu.edu.cn" # cookies["path"] = "/" # cookies["expires"] = None # browser2 = webdriver.PhantomJS() # # browser2.get("http://bkjw.sxu.edu.cn/sys/ValidateCode.aspx") # # ck2 = browser2.get_cookies() # # print(ck2) # browser = webdriver.PhantomJS() # browser.get("http://bkjw.sxu.edu.cn") # browser.delete_all_cookies() # browser.add_cookie(cookies) # browser.refresh() # browser.switch_to.frame(0) # browser.find_element_by_id("txt_asmcdefsddsd").send_keys(username) # browser.find_element_by_id("txt_pewerwedsdfsdff").send_keys(password) # browser.find_element_by_id("txt_sdertfgsadscxcadsads").click() # a = browser.get_screenshot_as_file("1.jpg") # im = Image.open("1.jpg") # box = (145,278,224,298) # region = im.crop(box) # region2 = region.convert("RGB") # region2.save("imgCode.jpg") # browser.add_cookie(cookies) # for j in range(20): # imgcodeidentify.deal_img("imgCode.jpg") # imgcodeidentify.interference_line(imgcodeidentify.deal_img("imgCode.jpg"),"imgCode.jpg") # imgcodeidentify.interference_point(imgcodeidentify.interference_line(imgcodeidentify.deal_img("imgCode.jpg"),"imgCode.jpg"),"imgCode.jpg") # code = self.client.basicGeneral(self.get_file_content("imgCode.jpg"))["words_result"][0]["words"] # browser.find_element_by_id("txt_sdertfgsadscxcadsads").send_keys(code) # ck = browser.get_cookies() # print(ck) # time.sleep(5) # browser.get_screenshot_as_file("1.jpg") def get_score(self,opener,username,password,yzm): h1 = hashlib.md5() h1.update(password.encode(encoding='utf-8')) hex_password = h1.hexdigest() temp_pwd = username+hex_password[:30].upper()+"10108" h2 = hashlib.md5() h2.update(temp_pwd.encode(encoding='utf-8')) hex_temp = h2.hexdigest() dsdsdsdsdxcxdfgfg = hex_temp[:30].upper() #密码 txt_asmcdefsddsd = username #用户名 h3 = hashlib.md5() h3.update(yzm.upper().encode(encoding='utf-8')) hex_temp_yzm = h3.hexdigest()[:30].upper()+'10108' h4 = hashlib.md5() h4.update(hex_temp_yzm.encode(encoding='utf-8')) fgfggfdgtyuuyyuuckjg = h4.hexdigest()[:30].upper() #验证码 __VIEWSTATE = "dDwyMTIyOTQxMzM0Ozs+AI2AQlMGeOYvPjA1fJfST57PPCk=" pcInfo = "Mozilla/5.0+(Windows+NT+10.0;+Win64;+x64;+rv:61.0)+Gecko/20100101+Firefox/61.0Windows+NT+10.0;+Win64;+x645.0+(Windows)+SN:NULL" Sel_Type = "STU" typeName = "学生" values = {} values["__VIEWSTATE"] = __VIEWSTATE values["dsdsdsdsdxcxdfgfg"] = dsdsdsdsdxcxdfgfg values["fgfggfdgtyuuyyuuckjg"] = fgfggfdgtyuuyyuuckjg values["pcInfo"] = pcInfo values["Sel_Type"] = Sel_Type values["txt_asmcdefsddsd"] = txt_asmcdefsddsd values["txt_pewerwedsdfsdff"] = "" values["txt_sdertfgsadscxcadsads"] = "" values["typeName"] = typeName data = urllib.parse.urlencode(values).encode('gb2312') #GB18030 req = rq.Request(self.score_url,data,headers=self.header) html = self.opener.open(req).read().decode('gb2312') print(data) print(html) # http://bkjw.sxu.edu.cn/xscj/Stu_MyScore_rpt.aspx # http://bkjw.sxu.edu.cn/xscj/Stu_MyScore_Drawimg.aspx?x=1&h=2&w=782&xnxq=20171&xn=2017&xq=1&rpt=1&rad=2&zfx=0&xh=201700004159 def post_score(self,opener): # sel_xn=2017&sel_xq=1&SJ=1&btn_search=%BC%EC%CB%F7&SelXNXQ=2&zfx_flag=0&zxf=0 data = "sel_xn=2017&sel_xq=1&SJ=1&btn_search=%BC%EC%CB%F7&SelXNXQ=2&zfx_flag=0&zxf=0".encode('GB18030') for item in self.cookie: self.header2["Cookie"] = item.name+'='+item.value print(self.header2) head2 = urllib.parse.urlencode(self.header2).encode('utf-8') request = rq.Request("http://bkjw.sxu.edu.cn/xscj/Stu_MyScore_Drawimg.aspx?x=1&h=2&w=782&xnxq=20171&xn=2017&xq=1&rpt=1&rad=2&zfx=0&xh=201700004159",head2)#,data self.header2 html = self.opener.open(request).read() with open("score.jpg","wb") as jpg: jpg.write(html) print(html) def get_file_content(self,filePath): with open(filePath, 'rb') as fp: result = fp.read() return result def post_data(self,opener,Sel_XNXQ,rad_gs,imgcode,Sel_XQ,Sel_JXL,Sel_ROOM): self.values["Sel_XNXQ"] = Sel_XNXQ self.values["rad_gs"] = rad_gs self.values["txt_yzm"] = imgcode self.values["Sel_XQ"] = Sel_XQ self.values["Sel_JXL"] = Sel_JXL self.values["Sel_ROOM"] = Sel_ROOM data = urllib.parse.urlencode(self.values).encode('GB18030') request = rq.Request("http://bkjw.sxu.edu.cn/ZNPK/KBFB_RoomSel_rpt.aspx", data, self.header) html = self.opener.open(request).read().decode('GB18030') reg = re.compile("<tr.*>.*</tr>") self.table = reg.findall(html)[0] return html def recommend_class(self): EmptyClassList = [] for i in range(5): for j in range(7): if self.tr_list[i][j] == "": t = (j+1,i+1) EmptyClassList.append(t) print("¸Ã½ÌÊÒÐÇÆÚ"+str(j+1)+"µÚ"+str(i+1)+"½Ú¿ÎΪ¿Õ½ÌÊÒ") return EmptyClassList def deal_table(self,html): soup = BeautifulSoup(html,"html5lib") td_list = soup.findAll(valign = "top") tr_list1 = [] tr_list2 = [] tr_list3 = [] tr_list4 = [] tr_list5 = [] count = 1 for i in td_list: if count <= 7: tr_list1.append(i.text) elif count <=14 and count >=8: tr_list2.append(i.text) elif count <=21 and count >=15: tr_list3.append(i.text) elif count <=28 and count >=22: tr_list4.append(i.text) elif count <=35 and count >=29: tr_list5.append(i.text) else: pass count = count + 1 self.tr_list.append(tr_list1) self.tr_list.append(tr_list2) self.tr_list.append(tr_list3) self.tr_list.append(tr_list4) self.tr_list.append(tr_list5) def main(self):#,xq,time count = 0 for i in self.js_list_101: while True: if count%10 == 0: opener = self.get_img_code() for j in range(30): imgcodeidentify.deal_img("imgCode.jpg") imgcodeidentify.interference_line(imgcodeidentify.deal_img("imgCode.jpg"),"imgCode.jpg") imgcodeidentify.interference_point(imgcodeidentify.interference_line(imgcodeidentify.deal_img("imgCode.jpg"),"imgCode.jpg"),"imgCode.jpg") try: code = self.client.basicGeneral(self.get_file_content("imgCode.jpg"))["words_result"][0]["words"] except IndexError: continue #self.client.basicGeneral(self.get_file_content("imgCode.jpg"))["words_result"][0]["words"] code = code.replace(" ","") print(code) # input("ÇëÊäÈëÑéÖ¤Âë\n") try: html = self.post_data(opener,"20171","1",code,"1","101",i) except IndexError: continue else: self.deal_table(html) temp_list = self.recommend_class() temp_dict = {} temp_dict[str(i)] = temp_list self.final_list.append(temp_dict) # print("Àí¿ÆÂ¥"+str(i)+"½ÌÊÒ²éѯÍê±Ï") count = count + 1 break
# 引入文字识别OCR SDK from aip import AipOcr # 定义常量 APP_ID = '9838807' API_KEY = 'ZyNwfGnvQQnYPIuGt25iTWhw' SECRET_KEY = 'r8RZWXQPMBnS4TyUorzdO6fpFO4h1Ggs' # 读取图片 def get_file_content(filePath): with open(filePath, 'rb') as fp: return fp.read() # 初始化ApiOcr对象 aipOcr = AipOcr(APP_ID, API_KEY, SECRET_KEY) # 定义参数变量 options = { 'detect_direction': 'true', 'language_type': 'CHN_ENG', } # 调用通用文字识别接口 result = aipOcr.basicGeneral(get_file_content('general.png'), options) print(result)
APP_ID = '***' API_KEY = '***' SECRET_KEY = '***' 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('example.jpg') text = client.basicGeneral(image) print text['log_id'] print type(text['words_result']) # with open('result.txt',r'a+') as my: # my.write(str(text['words_result'])) for xy in text['words_result']: print xy['words'] ''' """ 读取图片 """ def get_file_content(filePath): with open(filePath, 'rb') as fp:
class AipClient(object): ''' 百度识别api ''' def __init__(self, appid, api_key, secrrt_key, redis_url): self.appid = appid self.api_key = api_key self.secrrt_key = secrrt_key self.client = AipOcr(appid, api_key, secrrt_key) self.redis = RedisClient(redis_url) def __new__(cls, *args, **kw): ''' api 单例模式 ''' if not hasattr(cls, '_instance'): cls._instance = super().__new__(cls) return cls._instance @property def options(self): return {"language_type":"CHN_ENG", "detect_direction":"false", "detect_language":"false", "probability":"false"} def General(self, image,**kwargs): print('调取General_api 识别') return self.client.basicGeneral(image, self.options) def Accurate(self, image): print('调取Accurate_api 识别') return self.client.basicAccurate(image, self.options) def orc(self, image, font_key, word, **kwargs): hash_value = MD5.md5(image) results = self.General(image, **kwargs) if results.get('words_result'): if results.get('words_result') != '*': result = results['words_result'][0]['words'] self.redis.add(hash_value, result) self.redis.hadd(font_key, word, result) return result results = self.Accurate(image) if results.get('words_result'): if results.get('words_result') != '*': result = results['words_result'][0]['words'] self.redis.add(hash_value, result) self.redis.hadd(font_key, word, result) return result # Image.open(BytesIO(image)).show() # print(hash_value) return '*' def run(self, image, font_key,word, **kwargs): hash_value = MD5.md5(image) if self.redis.exists(hash_value): result = self.redis.get(hash_value) self.redis.hadd(font_key, word, result) return result else: return self.orc(image, font_key, word, **kwargs)
# encoding=utf-8 # Date: 2018-05-28 # Author: pangjian from aip import AipOcr APP_ID = '11312548' API_KEY = 'F8VHIMmovwN8oaINsLHTYNXk' SECRET_KEY = 'DVvrhcOjTAhRLphpgXjV9AuGRGinp1HQ' client = AipOcr(APP_ID, API_KEY, SECRET_KEY) def get_file_content(filePath): try: file = open(filePath, 'r') ret = file.read() return ret except: print 'read file error' finally: file.close() """ 如果有可选参数 """ options = {} options["language_type"] = "ENG" options["detect_direction"] = "true" options["detect_language"] = "true" options["probability"] = "true" image = get_file_content('image2.jpg') client.basicGeneral(image, options)