def recognition_font_by_baiduocr(img_path): """ 识别图片中的文字内容 """ from aip import AipOcr cfg_file = "../config/common.ini" APP_ID = get_ini_value(cfg_file, 'BAIDUOCR', 'APP_ID') API_KEY = get_ini_value(cfg_file, 'BAIDUOCR', 'API_KEY') SECRET_KEY = get_ini_value(cfg_file, 'BAIDUOCR', 'SECRET_KEY') client = AipOcr(APP_ID, API_KEY, SECRET_KEY) with open(img_path, 'rb') as rfile: image_message = rfile.read() message = client.accurate(image_message) return message
def Img2Txt(fname): if(fname[-8:]=="_res.jpg"): return client = AipOcr(APP_ID, API_KEY, SECRET_KEY) image = get_file_content(fname) results = client.accurate(image,options={'recognize_granularity':'small'}) if ("error_code" in results )and results["error_code"]==17: print("\n超过每天的免费额度\n") return else: results = results["words_result"] #有其他字段 img = cv2.imread(fname)#底图 for result in results: flag='' data='' time='' start='' end='' money='' for char in result["chars"]: location = char["location"] if location["left"]<440:#小于440的(日期前)丢弃 flag='\n' continue elif location["left"]<570:#日期前加空格 data = data+char["char"] continue elif location["left"]<685:#时间前加空格 time = time+char["char"] continue elif location["left"]<920:#市区信息,丢弃 continue elif location["left"]<1500:#起点 start = start+char["char"] continue elif location["left"]<2230:#终点 end = end+char["char"] continue elif location["left"]<2400:#里程 continue else: money = money+char["char"] continue data=data[:2]+"-"+data[-2:] if ("-" not in data) and (data) else data time=time[:2]+":"+time[-2:] if (":" not in time) and (time) else time zhishi='---' if start else '' print(f"{flag} {data} {time} {start} {zhishi} {end} {money}",end=' ') cv2.rectangle(img, (result["location"]["left"],result["location"]["top"]), (result["location"]["left"]+result["location"]["width"],result["location"]["top"]+result["location"]["height"]), (0,255,0), 2) cv2.imwrite(fname[:-4]+"_res.jpg", img)
class OCRReg: def __init__(self, logger): self.__client = AipOcr(BAIDU_OCR_APP_ID, BAIDU_OCR_API_KEY, BAIDU_OCR_SECRET_KEY) self.__logger = logger def _GetFileContent(self, filePath): with open(filePath, 'rb') as fp: return fp.read() def GetResult(self, imgFile): img = self._GetFileContent(imgFile) """ 如果有可选参数 """ options = {} options["language_type"] = "CHN_ENG" # options["detect_direction"] = "true" # 图像朝向 options["detect_language"] = "true" options["probability"] = "true" """ 带参数调用通用文字识别, 图片参数为本地图片 """ resultLow = self.__client.basicGeneral(img, options) self.__logger.info('Process basic img: {0}'.format(imgFile)) #resultHigh = self.__client.basicAccurate(img) resultHigh = None resultHighPos = self.__client.accurate(img) self.__logger.info('Process accurate img: {0}'.format(imgFile)) #resultHighPos = None return resultLow, resultHigh, resultHighPos def GetTextOnly(self, imgFile): img = self._GetFileContent(imgFile) """ 如果有可选参数 """ options = {} options["language_type"] = "CHN_ENG" options["detect_language"] = "true" options["probability"] = "true" # options["detect_direction"] = "true" # 图像朝向 """ 带参数调用通用文字识别, 图片参数为本地图片 """ # resultLow = self.__client.basicGeneral(img, options) # self.__logger.info('Process basic img: {0}'.format(imgFile)) resultHigh = self.__client.basicAccurate(img) # resultHigh = None return resultHigh
def predict(image): cv2.imshow("Capture", image) def get_file_content(filePath): with open(filePath, 'rb') as fp: return fp.read() path = "temp.jpg" cv2.imwrite(path, image) data = get_file_content(path) try: xx = AipOcr.accurate(data, options) xxx = (xx['words_result']) xxxx = (xxx[0]) mdz = (xxxx['words']) except: return None else: return mdz
def imgReco(self): ''' 调用“百度智能云 通用文字识别(含位置)” 50次/每天 https://ai.baidu.com/tech/imagecensoring ''' # # 打开图片img2 # with open('img2.jpeg', 'rb') as f: # gf = f.read() #ID KEY SECRETKEY WAPPID = 'xxx' WAPPKEY = 'xxx' WSECRETKEY = 'xxx' op = {'language_type': 'CHN_ENG', 'recognize_granularity': 'small'} #使用百度云智能 WCLIENT = AipOcr(WAPPID, WAPPKEY, WSECRETKEY) ## 返回word为字典。['words_result']为识别结果 word = WCLIENT.accurate(gf, options=op) imgSrt = word['words_result'] return imgSrt
""" 使用百度API识别图片文字并保存 """ import base64 from aip import AipOcr # 获取access_token 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() filePath = "E:\\jpg\\GDP1-100.jpg" image = get_file_content(filePath) result = client.accurate(image) print(result)
class unlockScrapy(object): # super().__init__()的作用也就显而易见了,就是执行父类的构造函数,使得我们能够调用父类的属性。 def __init__(self, driver): super(unlockScrapy, self).__init__() # selenium驱动 self.driver = driver # self.WAPPID = '百度文字识别appid' # self.WAPPKEY = '百度文字识别appkey' # self.WSECRETKEY = '百度文字识别secretkey' # 百度文字识别sdk客户端 # self.WCLIENT = AipOcr(self.WAPPID, self.WAPPKEY, self.WSECRETKEY) self.WAPPID = '17062614' self.WAPPKEY = 'E15mYUgfBRVV3ohVVZZVcCCc' self.WSECRETKEY = 'ClxgLmf2U0DwgX9mSvZG7v4zInrrCT92' # 百度文字识别sdk客户端 self.WCLIENT = AipOcr(self.WAPPID, self.WAPPKEY, self.WSECRETKEY) print("5" * 10) ## 切换二维码登录,在切换回来,就会滑动出现 ## 滑动出现后,输错一次密码,再登录,就会出现文字顺序验证码 # 破解滑动 ## cpt - img - double - right - outer def unlockScroll(self): try: # 滑块element print("1" * 10) scrollElement = self.driver.find_elements_by_class_name( 'cpt-img-double-right-outer')[0] print("2" * 10) ActionChains(self.driver).click_and_hold( on_element=scrollElement).perform() ActionChains(self.driver).move_to_element_with_offset( to_element=scrollElement, xoffset=30, yoffset=10).perform() ActionChains(self.driver).move_to_element_with_offset( to_element=scrollElement, xoffset=100, yoffset=20).perform() ActionChains(self.driver).move_to_element_with_offset( to_element=scrollElement, xoffset=200, yoffset=50).perform() print("滑块破解成功") except: print("无滑块") # 下载上面的小图和下面的大图 def downloadImg(self): # 小图的src """//*[@id="sliderddnormal-choose"]/div[2]/div[1]/img""" # "/html/body/div[3]/div[1]/img" time.sleep(1) codeSrc = self.driver.find_element_by_xpath( "//*[@id='sliderddnormal-choose']/div[2]/div[1]/img").get_attribute("src") print(codeSrc) print("6" * 10) # 大图的src # "/html/body/div[3]/div[3]/img" checkSrc = self.driver.find_element_by_xpath( "//*[@id='sliderddnormal-choose']/div[2]/div[3]/img").get_attribute("src") print("7" * 10) print(codeSrc.split(',')) """ https://www.cnblogs.com/wswang/p/7717997.html Python解码base64遇到Incorrect padding错误 """ # 保存下载 # 由于其src是base64编码的,因此需要以base64编码形式写入, # 由于标准的Base64编码后可能出现字符+和/,在URL中就不能直接作为参数,所以又有一种"url safe"的base64编码 # base64.urlsafe_b64decode(base64_url) # fh.write(base64.b64decode(codeSrc.split(',')[1])) fh = open("code.jpeg", "wb") fh.write(base64.urlsafe_b64decode(codeSrc.split(',')[1])) fh.close() fh = open("checkCode.jpeg", "wb") fh.write(base64.urlsafe_b64decode(checkSrc.split(',')[1])) fh.close() """ https://www.cnblogs.com/kongzhagen/p/6295925.html 7. 点操作: im.point(function) #,这个function接受一个参数,且对图片中的每一个点执行这个函数 比如:out=im.point(lambdai:i*1.5)#对每个点进行50%的加强 """ # 图片二值化,便于识别其中的文字 def chageImgLight(self): im = Image.open("code.jpeg") im1 = im.point(lambda p: p * 4) im1.save("code.jpeg") im = Image.open("checkCode.jpeg") im1 = im.point(lambda p: p * 4) im1.save("checkCode.jpeg") # 读取图片文件 def getFile(self, filePath): with open(filePath, 'rb') as fp: print("8读取图片" * 2) return fp.read() """ # 请求参数 language_type : 识别语言类型,默认为CHN_ENG中英文混合;。可选值包括: detect_direction :是否检测图像朝向,默认不检测, ture 是检测 # 返回参数 words_result """ # 识别上面小图中的文字 def iTow(self): try: print("开始识别小图...") op = {'language_type': 'CHN_ENG', 'detect_direction': 'true'} res = self.WCLIENT.basicAccurate( self.getFile('code.jpeg'), options=op) # options 可选参数 words = '' print("9" * 10) # http://ai.baidu.com/docs#/OCR-Python-SDK/80d64770 print(res['words_result']) # api已经定好的 array 定位和识别结果数组 print("10" * 10) for item in res['words_result']: if item['words'].endswith('。'): words = words + item['words'] + '\r\n' else: words = words + item['words'] print('小图中的文字: ' + words) print("小图文字识别完成") return words except: return 'error' """ # 请求参数 recognize_granularity:是否定位单字符位置,big:不定位单字符位置,默认值;small:定位单字符位置 item['chars'] :+chars array 单字符结果,recognize_granularity=small时存在 """ # 识别下面大图中的文字及坐标 def getPos(self, words): try: print("开始识别大图...") op = {'language_type': 'CHN_ENG', 'recognize_granularity': 'small'} res = self.WCLIENT.accurate( self.getFile('checkCode.jpeg'), options=op) # 所有文字的位置信息 allPosInfo = [] # 需要的文字的位置信息 needPosInfo = [] print("#1" * 10) # 每日50000次,超时报错{'error_code': 17, 'error_msg': 'Open api daily request limit reached'} print(res) print(res['words_result']) print("#2" * 10) print("11" * 10) for item in res['words_result']: allPosInfo.extend(item['chars']) print(item['chars']) # 文字及位置信息,见百度api print("12" * 10) # 筛选出需要的文字的位置信息 for word in words: for item in allPosInfo: if word == item['char']: needPosInfo.append(item) time.sleep(1) print('大图中的文字: ' + item['char']) # 返回出现文字的位置信息 print(needPosInfo) print("13" * 10) print("大图识别完成...") return needPosInfo except Exception as e: print(e) """ https://blog.csdn.net/huilan_same/article/details/52305176 ActionChains: 模拟鼠标操作比如单击、双击、点击鼠标右键、拖拽等等 selenium之 玩转鼠标键盘操作(ActionChains) https://blog.csdn.net/ccggaag/article/details/75717186 web自动化测试第6步:模拟鼠标操作(ActionChains) """ # 点击大图上的文字 def clickWords(self, wordsPosInfo): # 获取到大图的element # /html/body/div[3]/div[3]/img imgElement = self.driver.find_element_by_xpath( '//*[@id="sliderddnormal-choose"]/div[2]/div[3]/img') # 根据上图文字在下图中的顺序依次点击下图中的文字 for info in wordsPosInfo: # move_to_element_with_offset(to_element, xoffset, yoffset) ——移动到距某个元素(左上角坐标)多少距离的位置 ActionChains(self.driver).move_to_element_with_offset( to_element=imgElement, xoffset=info['location']['left'] + 20, yoffset=info['location']['top'] + 20).click().perform() time.sleep(1) # 若出现点击图片,则破解 def pic_main(self): try: ## 先下载图片 time.sleep(1) self.downloadImg() print("14-0" * 10) ## 图片二值化,方便识别 self.chageImgLight() ## 读取图片(调用百度ocr),识别小图文字 text = self.iTow() ## 读取图片(调用百度ocr)识别大图文字及位置信息 posInfo = self.getPos(text) ## 点击提交按钮 ,在点击之前确认一下,大图与小图数字是否完全相等,若不相等,则重新识别 print(type(text)) print(type(posInfo)) print(len(text)) print(len(posInfo)) print("14" * 10) ### 提交之前先判断一下,大小图字数是否一致,若不等,重新生成图片,重新识别 while len(text) != len(posInfo) or posInfo is None: ## 刷新图片 # /html/body/div[3]/div[4]/div/a self.driver.find_elements_by_xpath( '//*[@id="sliderddnormal-choose"]/div[2]/div[4]/div/a')[0].click() time.sleep(2) ## 下载图片 self.downloadImg() print("14-1" * 10) ## 图片二值化,方便识别 self.chageImgLight() ## 识别小图文字 text = self.iTow() ## 识别大图文字及位置信息 posInfo = self.getPos(text) print('匹配成功,开始点击') ## 按顺序模拟点击 self.clickWords(posInfo) ## 点选文字后提交 self.driver.find_elements_by_xpath( '//*[@id="sliderddnormal-choose"]/div[2]/div[4]/a')[0].click() print("模拟点击完成,已提交...点选图片破解成功...") except: print("无点选文字点击图片")
class unlockScrapy(object): def __init__(self, driver): super(unlockScrapy, self).__init__() # selenium驱动 self.driver = driver self.WAPPID = '百度文字识别appid' self.WAPPKEY = '百度文字识别appkey' self.WSECRETKEY = '百度文字识别secretkey' # 百度文字识别sdk客户端 self.WCLIENT = AipOcr(self.WAPPID, self.WAPPKEY, self.WSECRETKEY) # 按顺序点击图片中的文字 def clickWords(self, wordsPosInfo): # 获取到大图的element imgElement = self.driver.find_element_by_xpath( "/html/body/div[3]/div[3]/img") # 根据上图文字在下图中的顺序依次点击下图中的文字 for info in wordsPosInfo: ActionChains(self.driver).move_to_element_with_offset( to_element=imgElement, xoffset=info['location']['left'] + 20, yoffset=info['location']['top'] + 20).click().perform() time.sleep(1) # 下载上面的小图和下面的大图 def downloadImg(self): # 小图的src codeSrc = self.driver.find_element_by_xpath( "/html/body/div[3]/div[1]/img").get_attribute("src") # 大图的src checkSrc = self.driver.find_element_by_xpath( "/html/body/div[3]/div[3]/img").get_attribute("src") # 保存下载 fh = open("code.jpeg", "wb") # 由于其src是base64编码的,因此需要以base64编码形式写入 fh.write(base64.b64decode(codeSrc.split(',')[1])) fh.close() fh = open("checkCode.jpeg", "wb") fh.write(base64.b64decode(checkSrc.split(',')[1])) fh.close() # 图片二值化,便于识别其中的文字 def chageImgLight(self): im = Image.open("code.jpeg") im1 = im.point(lambda p: p * 4) im1.save("code.jpeg") im = Image.open("checkCode.jpeg") im1 = im.point(lambda p: p * 4) im1.save("checkCode.jpeg") # 破解滑动 def unlockScroll(self): # 滑块element scrollElement = self.driver.find_elements_by_class_name( 'cpt-img-double-right-outer')[0] ActionChains( self.driver).click_and_hold(on_element=scrollElement).perform() ActionChains(self.driver).move_to_element_with_offset( to_element=scrollElement, xoffset=30, yoffset=10).perform() ActionChains(self.driver).move_to_element_with_offset( to_element=scrollElement, xoffset=100, yoffset=20).perform() ActionChains(self.driver).move_to_element_with_offset( to_element=scrollElement, xoffset=200, yoffset=50).perform() # 读取图片文件 def getFile(self, filePath): with open(filePath, 'rb') as fp: return fp.read() # 识别上面小图中的文字 def iTow(self): try: op = {'language_type': 'CHN_ENG', 'detect_direction': 'true'} res = self.WCLIENT.basicAccurate(self.getFile('code.jpeg'), options=op) words = '' for item in res['words_result']: if item['words'].endswith('。'): words = words + item['words'] + '\r\n' else: words = words + item['words'] return words except: return 'error' # 识别下面大图中文字的坐标 def getPos(self, words): try: op = {'language_type': 'CHN_ENG', 'recognize_granularity': 'small'} res = self.WCLIENT.accurate(self.getFile('checkCode.jpeg'), options=op) # 所有文字的位置信息 allPosInfo = [] # 需要的文字的位置信息 needPosInfo = [] for item in res['words_result']: allPosInfo.extend(item['chars']) # 筛选出需要的文字的位置信息 for word in words: for item in allPosInfo: if word == item['char']: needPosInfo.append(item) return needPosInfo except Exception as e: print(e) def main(self): # 破解滑块 self.unlockScroll() time.sleep(2) # 下载图片 self.downloadImg() time.sleep(2) # 图像二值化,方便识别 self.chageImgLight() # 识别小图文字 text = self.iTow() # 获取大图的文字位置信息 posInfo = self.getPos(list(text)) # 由于小图或大图文字识别可能不准确,因此这里设置识别出的文字少于4个则重新识别 while len(posInfo) != 4 or len(text) != 4: # 点击重新获取图片,再次识别 self.driver.find_elements_by_xpath( '/html/body/div[3]/div[4]/div/a')[0].click() time.sleep(2) self.downloadImg() time.sleep(2) text = self.iTow() posInfo = self.getPos(list(text)) time.sleep(3) print('匹配成功,开始点击') # 点击下面大图中的文字 self.clickWords(posInfo) # 点击提交按钮 self.driver.find_elements_by_xpath( '/html/body/div[3]/div[4]/a')[0].click() time.sleep(2) # 如果破解成功,html的title会变 if self.driver.title != '携程在手,说走就走': print('破解成功') else: # 再次尝试 print('破解失败,再次破解') self.main()
class Ocr(object): def __init__(self, APP_ID, API_KEY, SECRET_KEY, is_accurate=False): try: self.aip = AipOcr(APP_ID, API_KEY, SECRET_KEY) self.is_accurate = is_accurate except Exception as e: raise OCRError( f'Error occurred when initializing the ocr client\n{e}') def scan(self, file_path, **options): """ Scan picture and return its content :param file_path: Path of picture :return: (str) Content of the picture(with index and other info) """ try: with open(file_path, 'rb') as pic: if self.is_accurate: return self.parse_wholepage_result( self.aip.accurate(pic.read(), options)) else: return self.parse_wholepage_result( self.aip.general(pic.read(), options)) except Exception as e: raise OCRError(e) @staticmethod def cut_image(path, x1, x2, y1, y2): # TODO try: img = Image.open(path) img = img.crop((x1, x2, y1, y2)) img.save(path) except Exception as e: raise OCRError @staticmethod def parse_wholepage_result(aip_ret): if 'error_code' in aip_ret or 'error_msg' in aip_ret: raise OCRError( f"{aip_ret['error_code']}:" f"(read https://cloud.baidu.com/doc/OCR/s/zjwvxzmhh for more information)\n" f"{aip_ret['error_msg']}") ret = { 'flag': { 'is_homepage': False, 'is_mission_page': False, 'is_preparation_page': False }, 'location': dict(), 'strength': '', 'log_id': aip_ret['log_id'], 'words_result': aip_ret['words_result'] } for word in aip_ret['words_result']: ret['flag']['is_homepage'] |= '采购中心' in word['words'] ret['flag']['is_mission_page'] |= '开始行动' in word['words'] ret['flag']['is_preparation_page'] |= '本次行动配置不可更改' in word['words'] if ret['flag']['is_mission_page']: ret['strength'] = aip_ret['words_result'][2]['words'].split('/')[0] ret['location']['strength'] = aip_ret['words_result'][2][ 'location'] ret['location']['start_mission'] = [ word['location'] for word in aip_ret['words_result'] if '开始行动' in word['words'] ][0] elif ret['flag']['is_homepage']: ret['location']['strength'] = [ word['location'] for word in aip_ret['words_result'] if '作战' in word['words'] ][0] elif ret['flag']['is_preparation_page']: ret['location']['prepare_start'] = [ word['location'] for word in aip_ret['words_result'] if '开始' in word['words'] ][0] # Change flags into string ret['flag']['is_homepage'] = Ocr.upper_str(ret['flag']['is_homepage']) ret['flag']['is_mission_page'] = Ocr.upper_str( ret['flag']['is_mission_page']) ret['flag']['is_preparation_page'] = Ocr.upper_str( ret['flag']['is_preparation_page']) return ret @staticmethod def upper_str(in_bool): return str(in_bool).upper()
# image = get_file_content('***.jpg') """ 调用通用文字识别(高精度版) """ # client.basicAccurate(image) """ 如果有可选参数 """ options = {} options["detect_direction"] = "true" options["probability"] = "false" options["language_type"] = "ENG" """ 带参数调用通用文字识别(高精度版) """; # result = client.basicAccurate(image, options) image = get_file_content('F:\\NEW-1-1.jpg') result = client.accurate(image, options) for j in result['words_result']: print(j['words']) # 一眼就能看出来,百度的识别成功率高 ### 使用百度云识别整个文件夹中的图片并写到word文档中 path3 = 'F:\\file_2018_05_17\\' # 读取文件夹中的所有文件,生成文件名list file = os.listdir(path3) ok_list = [] # 用来存储有正确返回的 err_list = [] # 用来存储报错的,可用来多次上传百度云再识别
client = AipOcr(APP_ID, API_KEY, SECRET_KEY) #res=client.accurate(resp) #print(res) """ 如果有可选参数 """ options = {} options["detect_direction"] = "true" options["probability"] = "true" """ 带参数调用通用文字识别(高精度版) """ res=client.accurate(resp,options) print(res) #谷歌的文字识别 #谷歌ai安装 #sudo apt-get install tesseract-ocr #pip3 install pytesseract from PIL import Image from pytesseract import * imag=Image_to_string(image) print(image)
class MyOcr(object): """ 文字识别 @app_id @api_key @secret_key 为百度ai平台上申请的值 @typeid 精度的选择 1--调用通用文字识别 2--含位置信息的通用文字识别 3--高精度的文字识别 4--含位置信息的高精度文字识别 """ def __init__(self, typeid, app_id = APP_ID, api_key = API_KEY, secret_key = SECRET_KEY): self.client = AipOcr(app_id, api_key, secret_key) #self.client = AipOcr(appid[1], apikey[1], secretkey[1]) self.typeid = typeid self.codepath = os.path.dirname(__file__) self.datapath = self.codepath + '\data' os.makedirs(self.datapath, exist_ok=True) self.log = LogMgr() def _get_file_content(self, filePath): """读取图片""" with open(filePath, 'rb') as fp: return fp.read() def _write_json_file(self, filepath, data): """写入json文件""" with open(filepath, 'w', encoding = 'utf-8') as fw: fw.write(json.dumps(data, ensure_ascii=False)) def _list_custom(self, path): root = os.listdir(path) return os.listdir(path + '\\' + root[0]), path + '\\' + root[0] def ocr_deploy(self, rec_dict): files = rec_dict['files'] #ocr所需的参数 options = {} options["detect_direction"] = "true" options["detect_language"] = "true" options["probability"] = "true" #dirlist = os.listdir(imgpath) #dirlist, root = self._list_custom(imgpath) for file in files: if re.search(r'进口注册证|GMP|说明书|药品再注册批件|营业执照|生产许可证|进口药品许可证|进口药品注册证', file['type']): for img in file['imgs']: print('Current img: {}'.format(img['imgpath'])) try: data = self.client.accurate(base64.b64decode(bytes(img['base64'], encoding='utf-8')), options) except Exception as e: print('Error: ', e) self.log.error(img['imgpath'] + "Error! : " + str(e)) continue img.update({"imgjson" : data}) return rec_dict def _ocr(self, imgpath): """ 识别img文件下的图片 @输出json数据,保存到data文件夹下 """ #imgpath = self.codepath + '\IMG'+'\国控天星' #FIXME:电脑环境不同,路径也不一样,切换环境的话要修改路径 #imgpath = 'F:\IMG' #imgpath = r'D:\IMG' options = {} options["detect_direction"] = "true" options["detect_language"] = "true" options["probability"] = "true" #FIXME:图片路径需改 dirlist = os.listdir(imgpath) root = imgpath #dirlist, root = self._list_custom(imgpath) for file in os.walk(imgpath): for file_name in file[2]: if re.search(r'进口注册证|GMP|说明书|药品再注册批件|营业执照|生产许可证|进口药品许可证', file_name): if '备案' in file_name: continue if os.path.isdir(file[0] + '\\' + file_name): continue if not re.match(r'[jJ][pP][gG]', file_name[-3:]): continue datafilepath = self.datapath + file[0].split('IMG')[1] if not os.path.exists(datafilepath): os.makedirs(datafilepath) img = self._get_file_content(file[0] + '\\' + file_name) if file_name[:-4].find('.'): file_name = file_name[:-4].replace('.', '') + file_name[-4:] try: prefix,suffix = file_name.split('.') except Exception as e: print('split error: {}\ncurrent file: {}'.format(e, file[0] + '\\' + file_name)) self.log.error(file[0] + '\\' + file_name + " Error!! : " + str(e)) continue #判断文件是否存在 if os.path.isfile((datafilepath +'\{}.json').format(prefix + '_' + suffix)): continue print('Current img: {}'.format(file[0] + '\\' + file_name)) #FIXME: testdict = dict() testdict['base64'] = str(base64.b64encode(img), 'utf-8') #img_test = str.encode(testdict['base64']) #self._write_json_file('F:\\IMG\\11A0015\\test.json', str(img)) try: if self.typeid == 1: data = self.client.basicGeneral(img, options) elif self.typeid == 2: data = self.client.general(img, options) elif self.typeid == 3: data = self.client.basicAccurate(base64.b64decode(bytes(testdict['base64'], encoding='utf-8')), options) elif self.typeid == 4: data = self.client.accurate(img, options) except Exception as e: print('Error: ', e) self.log.error(file[0] + '\\' + file_name + " Error!! : " + str(e)) continue self._write_json_file((datafilepath +'\{}.json').format(prefix + '_' + suffix), data) def _write_dict(self): files = os.listdir(self.datapath) for file in files: format_data = introduction.introduction(self.datapath + '\\' + file) print(format_data) def pdf2img(self): """pdf转jpg""" file_dir = self.codepath + '/PDF/说明书/' save_dir = self.codepath + '/IMG/图片/' for files in os.walk(file_dir): for file_name in files[2]: file_path = file_dir [file_name_prefix, file_name_suffix] = file_name.split('.') file = file_dir + file_name with(Image(filename=file, resolution=300)) as img: images = img.sequence pages = len(images) for i in range(pages): images[i].type = 'truecolor' save_name = save_dir + file_name_prefix + str(i) + '.jpg' Image(images[i]).save(filename=save_name) def run(self, imgpath): """入口函数""" print('********Start Identify********') self._ocr(imgpath) print('********End********')
import json import requests from PIL import Image # 定义常量 APP_ID = '16456650' API_KEY = 'b8oSXq9N8lmx5XfyyCEczUcL' SECRET_KEY = '0cwgwmSEEAQHOHxy6SK0B1T6yNAPQTVY' # 初始化AipFace对象 client = AipOcr(APP_ID, API_KEY, SECRET_KEY) check_code_src = r'http://jw2.ahu.cn/CheckCode.aspx' check_code = requests.get(check_code_src) with open('CheckCode.jpg', 'wb') as file: file.write(check_code.content) check_code_jpg = Image.open('CheckCode.jpg') check_code_jpg = check_code_jpg.convert('L') check_code_jpg.save('tmp.jpg') with open('tmp.jpg', 'rb') as file: check_code_jpg = file.read() ans = client.accurate(check_code_jpg) print(ans)
class Base(): def __init__(self, driver): self.d = driver # 根据app包名操作app def useApp(self, packagename, action): ''' :param packagename: 应用包名 :param action: 动作 :return: ''' if action == 'start': self.d.app_start(packagename) elif action == 'stop': self.d.app_stop(packagename) elif action == 'clear': self.d.app_clear(packagename) logging.info("{}: {}".format(action, packagename)) # 对手机硬件进行home、back操作 def usePhone(self, action): ''' :param action: 操作动作 :return: ''' if action == 'home': self.d.press('home') elif action == 'back': self.d.press('back') logging.info("对设备进行操作,操作动作为: {}".format(action)) # 根据当前屏幕显示结果确认手机是否需要先解锁 def unlock(self): ''' :param action: 操作动作 :return: ''' if not self.d.info.get('screenOn'): self.d.unlock() sleep(1) logging.info("解锁手机") # 根据元素名称进行点击操作 def clickByElement(self, element, logtext): ''' :param element: 元素名称,可根据resource、xpath、坐标及Text进行判断并点击 :param logtext: 打印log的文案 :return: ''' if str(element).startswith("com"): self.d(resourceId=element).click() elif re.findall("//", str(element)): self.d.xpath(element).click() elif type(element) == tuple: self.d.click(element[0], element[1]) elif str(element).startswith("android"): self.d(resourceId=element).click() else: self.d(text=element).click() logging.info("点击元素: {}".format(logtext)) # 根据元素id及text组合进行点击操作 —— LJX def clickByElementIdAndText(self, id, text, logtext): ''' :param id: 元素id :param text: 元素text :param logtext:打印log的文案 :return: ''' self.d(resourceId=id, text=text).click() logging.info("点击元素: {}".format(logtext)) # 根据元素className及text组合进行点击操作 def clickByElementClassNameAndText(self, className, text, logtext, instance=1): ''' :param className: 传入className元素 :param text: 传入text元素 :param logtext: 打印log的文案 :param instance: 第几位 :return: ''' self.d(className=className, text=text, instance=instance).click() logging.info("点击元素: {}".format(logtext)) # 向下滑动页面 def scroll(self, num=1): ''' :param num: 滑动次数,默认为1次 :return: ''' for i in range(num): self.d(scrollable=True).scroll(steps=100) sleep(1) logging.info("滑动页面: {}次".format(num)) # 滑动页面到指定元素位置 def scrollToElement(self, element): ''' :param element: 元素名称,仅使用id或text识别 :return: 是否找到指定元素,返回True or False ''' if str(element).startswith("com"): result = self.d(scrollable=True).scroll.to(resourceId=element) else: result = self.d(scrollable=True).scroll.to(text=element) logging.info('滑动查找元素: {}, 是否找到指定元素:{}'.format(element, result)) return result # 查找元素,判断元素存在 def elementIsExit(self, element, timeout=5): ''' :param element: 元素名称 :param timeout: 超时时间 :return: 返回查找结果True or False ''' isExit = False while timeout > 0: # 获取到当前页面的hierarchy page_xml = self.d.dump_hierarchy() # 判断元素是否存在于hierarchy中 if re.findall(element, page_xml): isExit = True logging.info('查询到元素: {}'.format(element)) break else: timeout -= 1 sleep(1) if isExit == False: logging.info('未找到元素,元素名称为: {}'.format(element)) return isExit # 当元素未找到时,直接断言失败,跳过该用例 def assertFalse(self, element): ''' :param element: 元素名称 :return: ''' assert False, '未找到元素,断言失败,元素名称为: {}'.format(element) # 断言元素是否存在 def assertTrue(self, element, mark=True, timeout=5): ''' :param element: 元素名称 :param timeout: 超时时间 :param mark: 判断元素是否存在,默认为True,如判断元素不存在,则必须传False :return: ''' sleep(2) if mark: assert self.elementIsExit( element, timeout) == True, "断言元素存在失败,元素名称为: {}".format(element) logging.info("已找到元素,断言成功,元素名称为: {}".format(element)) else: assert self.elementIsExit( element, timeout) == False, "断言元素不存在失败,元素名称为: {}".format(element) logging.info("元素已不存在,断言成功,元素名称为: {}".format(element)) # 断言元素是否相等————LCM def assertEqual(self, element, element1, mark=True, timeout=5): ''' :param element: 元素名称 :param element1: 元素名称1 :param mark: 判断两个元素是否相等,如果为True(默认),则需要判断元素相等、包含的情况;若传False,则判断两个元素不相等 :param timeout: 超时时间 :return: ''' if mark: if element == element1: assert True logging.info("元素相等,断言成功,元素1名称:{} && 元素2名称:{}".format( element, element1)) elif element in element1: assert True logging.info("元素包含,断言成功,元素1名称:{} && 元素2名称:{}".format( element, element1)) elif process.extract(element, element1, limit=50): # 从字符串中找出前50与element最相似的句子 assert fuzz.ratio( element, element1), "断言元素不匹配,断言失败,元素名称为: {} {}".format( element, element1) logging.info("元素匹配,断言成功,元素名称为: {} {}".format( element, element1)) else: assert False, "元素1名称与元素2名称不相等且不包含,断言失败,元素1名称:{} && 元素2名称:{}".format( element, element1) logging.info("元素包含,断言成功,元素1名称:{} && 元素2名称:{}".format( element, element1)) else: assert element != element1, "元素1名称与元素2名称相等,断言失败,元素1名称:{} && 元素2名称:{}".format( element, element1) logging.info("元素不相等,断言成功,元素1名称:{} && 元素2名称:{}".format( element, element1)) # 提取元素文本 ---wmw def elementText(self, element, logtext, instance=0): ''' :param element: 元素名称,可根据resource、xpath进行判断并提取元素文本 :param logtext: 打印log的文案 :return: ''' if str(element).startswith("com"): text = self.d(resourceId=element, instance=instance).get_text() logging.info("提取第{}位的{}元素文本".format(instance, logtext)) return text elif re.findall("//", str(element)): text = self.d.xpath(element, instance=instance).get_text() logging.info("提取第{}位的{}元素文本".format(instance, logtext)) return text # 输入文本——LYX def elementSetText(self, element, text, logtext): ''' :param element: 元素名称,可根据resource、xpath进行判断并设置文本 :param text:所输入的文本 :param logtext: 打印log的文案 :return: ''' if str(element).startswith("com"): text = self.d(resourceId=element).set_text(text) elif re.findall("//", str(element)): text = self.d.xpath(element).set_text(text) logging.info("输入文本: {}".format(logtext)) # 根据元素名称进行长按操作——LYX def long_clickByElement(self, element, logtext, duration=5): ''' :param element: 元素名称,可根据resource、坐标及Text进行判断并长按 :param logtext: 打印log的文案 :param duration:长按的时长,单位秒 :return: ''' if str(element).startswith("com"): self.d(resourceId=element).long_click(duration) elif type(element) == tuple: self.d.long_click(element[0], element[1], duration) else: self.d(text=element).long_click(duration) logging.info("长按元素: {}".format(logtext)) # 根据元素id及text组合进行长按操作——LYX def long_clickByElementIdAndText(self, id, text, logtext): ''' :param id: 元素id :param text: 元素text :param logtext: 打印log的文案 :return: ''' self.d(resourceId=id, text=text).long_click() logging.info("长按元素: {}".format(logtext)) # 根据元素名称拖动控件——LYX def swipeByElement(self, element, logtext, direction="up", steps=20): ''' :param element: 元素名称,可根据resource、坐标、元组(坐标)及Text进行判断并拖动 :param direction: 拖动的方向 :param logtext: 打印log的文案 :param steps:1 steps大概 5ms :return: ''' if str(element).startswith("com"): self.d(resourceId=element).swipe(direction, steps) elif re.findall("//", str(element)): self.d.xpath(element).swipe(direction, steps) elif type(element) == tuple: self.d.swipe(element[0], element[1], element[2], element[3]) else: self.d(text=element).swipe(direction, steps) logging.info("拖动元素: {}".format(logtext)) # 将元素从一个位置滑动至另一个位置————LCM def dragByElement(self, element, element1, num=1): ''' :param element: 元素名称,可根据resource进行判断并拖动 :param element1:元素坐标位置 :param num:拖动的次数 :return: ''' for i in range(num): if str(element).startswith('com'): self.d(resourceId=element).drag_to(element1[0], element1[1], duration=0.05) else: self.d(text=element).drag_to(element1[0], element1[1], duration=4) logging.info("选择一个位置拖拽到另一个位置: {}次".format(num)) # 把指定元素拖拽到指定元素 —— LJX def dragElementToElement(self, elementFrom, elementTo): ''' :param elementFrom: 元素名称,仅可根据text进行拖拽 :param elementTo: 元素名称,仅可根据text进行拖拽 :return: ''' self.d(text=elementFrom).drag_to(text=elementTo, duration=3) logging.info("把'{}'元素拖拽到 '{}' 元素".format(elementFrom, elementTo)) # 根据元素id位于第几个进行点击操作——wmw def clickByElementIdAndInstance(self, id, logtext, instance=0): ''' :param id: 元素ID :param logtext: 打印log的文案 :param instance: 位于第几个 :return: ''' self.d(resourceId=id, instance=instance).click() logging.info("点击元素: {}".format(logtext)) # 根据元素id位于第几个进行长按操作 —— LJX def long_clickByElementIdAndInstance(self, element, logtext, instance=0, duration=1): ''' :param element: 元素id :param logtext: 打印log的文案 :param instance:第几位 :param duration:长按的时长,单位秒,默认1秒 :return: ''' self.d(resourceId=element, instance=instance).long_click(duration) logging.info("长按元素: {}".format(logtext)) # 增加公共监听 def browserWatcher(self): self.d.watchers.run() self.d.watcher("始终允许").when(text='始终允许').click() self.d.watcher("允许").when(text='允许').click() self.d.watcher("确定").when(text='确定').click() self.d.watcher('我知道了').when(text='我知道了').click() # 二进制读取图片 def readImage(self, imageFile): with open(imageFile, 'rb') as fp: return fp.read() # 图片识别返回文字 def baiduOcr(self): text = [] # 增加图片名称和地址 pic = str(now_time) + "图片识别.jpg" pic_name = os.path.join(dir_screenshot, pic) # 拼接接口参数,接口api参数详解(https://cloud.baidu.com/doc/OCR/OCR-API/26.5C.E8.AF.B7.E6.B1.82.E8.AF.B4.E6.98.8E-3.html#.E8.AF.B7.E6.B1.82.E8.AF.B4.E6.98.8E) options = {} options["recognize_granularity"] = "big" options["detect_direction"] = "false" options["vertexes_location"] = "false" options["probability"] = "false" # 传对应的参数进行实例化 self.client = AipOcr('17235264', 'ejTp8Xd1ZTUGjnO1WkRoWFWE', 'MPapr5pwGlYBu5GHZXjYxnGmOekDd2QB') # 实时截图,截图存放在screenshot目录下 screenshot = self.d.screenshot("{}".format(pic_name)) logging.info('截图后进行图片识别:{}'.format(pic_name)) image = self.readImage(screenshot) # 获取图片文字识别后的返回结果 result = self.client.accurate(image, options) for word in result['words_result']: text.append(word['words']) return text # 获取元素坐标信息 —— LJX def getInfoBottom(self, elemnt, logtext): ''' :param elemnt: 元素text :param logtext:打印log的文案 :return:返回元素信息的坐标 ''' bottom = self.d(text=elemnt).info['bounds'] logging.info("获取元素坐标: {}".format(logtext)) return bottom # 根据焦点位置,输入文本 ---wmw def elementInputFocalPositionText(self, text, logtext, clear=False): ''' :param text: 输入文本 :param logtext: 打印log的文案 :param clear: 是否先清除文本内容 :return: ''' text = self.d.send_keys(text, clear=clear) logging.info("根据焦点位置,输入文本: {}".format(logtext)) # 点击元素某个方向上的元素 —— LJX def clickByElementRight(self, elementText, element, direction): ''' :param elementText: 元素文本 :param element: 元素名称,仅可根据id、text进行点击 :param direction: 方向 :return: ''' if direction == 'up': if str(element).startswith("com"): self.d(text=elementText).up(resourceId=element).click() else: self.d(text=elementText).up(text=element).click() elif direction == 'down': if str(element).startswith("com"): self.d(text=elementText).down(resourceId=element).click() else: self.d(text=elementText).down(text=element).click() elif direction == 'right': if str(element).startswith("com"): self.d(text=elementText).right(resourceId=element).click() else: self.d(text=elementText).right(text=element).click() else: if str(element).startswith("com"): self.d(text=elementText).left(resourceId=element).click() else: self.d(text=elementText).left(text=element).click() logging.info("点击{}元素{}方向的{}元素".format(elementText, direction, element)) # 提取元素右边的元素文本 ---wmw def ObtianRightelementText(self, elementText, element, direction): ''' :param elementText: 元素名称 :param element: 元素ID :param direction: 方向 :return: ''' if direction == 'right': if str(element).startswith("com"): text = self.d(text=elementText).right( resourceId=element).get_text() logging.info("提取{}元素{}方向的{}元素文本".format( elementText, direction, element)) return text
""" 你的 APPID AK SK """ APP_ID = '10723946' API_KEY = 'AVKXrbaEYwStf7tGLdm7EbWE' SECRET_KEY = 'hp8kP1Krqa19t7fMIQfQ4oDK5ScHGeK6' 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('G:\IMG\国控盐城\保健药品\补气口服液J000000841\药品GMP证书_1.jpg') """ 如果有可选参数 """ options = {} options["language_type"] = "CHN_ENG" options["detect_direction"] = "true" options["detect_language"] = "true" options["probability"] = "true" """ 带参数调用通用文字识别, 图片参数为本地图片 """ datas = client.accurate(image, options) print(datas['words_result']) write_json_file('F:\data\Test\GMP证书.json',datas)