Example #1
0
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"]])
Example #2
0
from aip import AipOcr
""" 你的 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()


path = '../../Test/file/picture/'
image = get_file_content(path + '1567605252774.jpg')

# idCardSide = "front"
# """ 调用身份证识别 """
# res = client.idcard(image, idCardSide)
# print(res)
""" 如果有可选参数 """
options = dict()
options["detect_direction"] = "true"
options["probability"] = "true"
""" 带参数调用通用文字识别(高精度版) """
res = client.basicAccurate(image, options)
# print(res)
for r in res['words_result']:
    print(r['words'])
Example #3
0
class PicName(object):
    # 编辑图片
    def __init__(self):
        # """ 你的 APPID AK SK """
        APP_ID = '15584553'
        API_KEY = 'MGGfM6EGySBdKOM6605nbhDg'
        SECRET_KEY = 'zxpcnv2pd0VD5zlRwwySFx55BOU0sXhl'
        self.file_path = input('请输入图片文件夹: ')
        self.stylecode = int(input('请输入款号长度: '))
        # self.stylecode = 11
        self.client = AipOcr(APP_ID, API_KEY, SECRET_KEY)

    def get_file_path(self):
        result_file_path = []
        pic_format = ['jpg', 'png']
        try:
            all_file = [
                os.path.join(self.file_path, i)
                for i in os.listdir(self.file_path)
            ]
            for i in all_file:
                one_file = os.path.basename(i).lower()
                if '~$' in i:
                    pass
                elif one_file[-3:] in pic_format:
                    result_file_path.append(i)
        except FileNotFoundError as e:
            print('路径输入错误', e)
        return result_file_path

    def get_file_content(self, picture_file):
        img = Image.open(picture_file)
        width, height = img.size
        roi = img.crop((0, 0, width / 3 * 2, height / 4))
        # roi.show()
        img.close()
        roi.save('temp.jpg')
        # 读取图片
        with open('temp.jpg', 'rb') as fp:
            return fp.read()

    def get_picture_name(self, picture_file):
        image = self.get_file_content(picture_file)
        # 调用通用文字识别, 图片参数为本地图片
        relative_name = os.path.basename(picture_file)
        dir_name = os.path.dirname(picture_file)
        result_dict = self.client.basicAccurate(image)
        words_result = result_dict['words_result']
        stylecode = words_result[0]['words']
        if len(stylecode) == self.stylecode:
            print('\t{}名称识别成功'.format(relative_name))
        else:
            print('\t{}名称识别不成功'.format(relative_name))
        new_name = os.path.join(dir_name, stylecode + '.jpg')
        os.renames(picture_file, new_name)
        return '\t文件重命名成功'

    def main(self):
        for picture_file in self.get_file_path():
            print(picture_file)
            pic_rename = self.get_picture_name(picture_file)
            print(pic_rename)
Example #4
0
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()
Example #5
0
API_KEY = 'grTm0iuaEnyjNn0XZriXGkKU'
SECRET_KEY = 'vUVtHedaEKmG8ecjWarhxSjmxWTBeIu7'

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(r'C:\Users\Administrator\Desktop\WebCrawler\day16\baiduAi\realbaiduAi\test_pic\sina.jpg')

""" 调用通用文字识别, 图片参数为本地图片 """
# list_w=client.basicGeneral(image)['words_result']
list_w=client.basicAccurate(image)['words_result']
for i in list_w:
    for k in i.items():
        print(i['words'])

""" 如果有可选参数 """
options = {}
options["language_type"] = "CHN_ENG"
options["detect_direction"] = "true"
options["detect_language"] = "true"
options["probability"] = "true"


""" 带参数调用通用文字识别, 图片参数为本地图片 """
client.basicGeneral(image, options)
APP_ID = ''
API_KEY = ''
SECRET_KEY = ''

client = AipOcr(APP_ID, API_KEY, SECRET_KEY)

print('----------------------------------------')

with open("capture.png", 'rb') as f:        # 若图片已存在,请填写文件实际路径
                                            # 例如 with open(r"C:\Users\name\Desktop\jietu.png", 'rb') as f:
    file = open("temp.txt", 'w+', encoding='utf-8')
    image = f.read()
    # 调用百度API通用文字识别(高精度版),提取图片中的内容
    # text = client.basicGeneral(image)     # 粗略识别
    text = client.basicAccurate(image)      # 精确识别,每日免费500次
    result = text["words_result"]
    for i in result:
        #print(i["words"])
        #file.write(i["words"] + '\n')      # 换行
        file.write(i["words"])              # 不换行
    file.close()
    file = open("temp.txt", 'r', encoding='utf-8')
    contents = file.read()
    print(contents)                         # 输出结果,并保存在文件 temp.txt 中
    pyperclip.copy(contents)                # 同时将结果存放在剪贴板中
    file.close()
os.remove("capture.png")                    # 移除图片文件
os.remove("temp.txt")                       # 移除结果文件

print('----------------------------------------' + '\n')
Example #7
0
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()


#此处需要AI识别图片的名称和位置
image = get_file_content('clipboard.jpg')
""" 调用通用文字识别(高精度版) """
client.basicAccurate(image)
""" 如果有可选参数 """
options = {}
options["detect_direction"] = "false"  #是否检测斜方文字
options["probability"] = "true"
""" 带参数调用通用文字识别(高精度版) """
result = client.basicAccurate(image, options)

# 把获得的result转化为字符串,同时改成大写以后后续replace处理
str1 = json.dumps(result)
str1 = str1.upper()
str1 = str1.replace('G8', 'GB')
#str1=str1.replace('E','B') 这里替换后面就拿不到words_result键了……

#字符串转换为字典
dict1 = json.loads(str1)
Example #8
0
from aip import AipOcr
import json

APP_ID = "10562475";
API_KEY = "Oe2vzofwaTplkUn0fGaSy5nY";
SECRET_KEY = "M8Zd3vD0zgwyRD9VNainUYxo2msZoghR";
client = AipOcr(APP_ID, API_KEY, SECRET_KEY)


def get_file_content(filePath):
    with open(filePath, 'rb') as fp:
        return fp.read()


options = {
    'detect_direction': 'true',
    'language_type': 'CHN_ENG',
}

result = client.basicAccurate(get_file_content('../general.jpg'), options)
print json.dumps(result).decode("unicode-escape")
Example #9
0
def Transform_GT(accuracy_option, image=0):

    Recognize = accuracy_option
    """
    User AppID AK SK
    AK = API_KEY
    SK = Secret_KEY
    """
    APP_ID = '16289977'
    AK = '1UUME9IqLLRwlbrQFcjePBwe'
    SK = '0wNmCRtXgNGpvwGVNxFewiqqzrgKvwUv'

    client = AipOcr(APP_ID, AK, SK)

    root = tk.Tk()
    root.withdraw()
    ''' If there is no incoming image data, the picture needs to be opened. '''
    if image == 0:
        file_path = filedialog.askopenfilename(title='打开需要识别的图片',
                                               filetypes=[('JPG', '*.jpg'),
                                                          ('BMP', '*.bmp'),
                                                          ('PNG', '*.png')
                                                          ])  # 获取文件的打开路径

        print(file_path)
        file_path = str(file_path)
        '''Distinguish between have been caught image and no image'''

        image = get_file_content(file_path)
    """ Generasion object Calling """
    try:
        client.basicGeneral(image)
    except TypeError:
        return 0
    """ IF there are optional parameter  """
    options = {}
    options["language_type"] = "CHN_ENG"
    options["detect_direction"] = "true"
    options["detect_language"] = "true"
    options["probability"] = "true"
    """ 带参数调用通用文字识别,图片参数为本地图片 """
    if Recognize == 0:
        word = client.basicGeneral(image, options)
    elif Recognize == 1:
        word = client.basicAccurate(image, options)
    print("识别结果如下,共识别出 {[words_result_num]} 段文字".format(word))
    data_layer = "words_result"
    """创建一个空字符串来存储结果"""
    words_buffer = ""

    for layer in word.keys():
        if layer == data_layer:
            layer_obj = word[layer]
            for layer1 in layer_obj:
                if isinstance(layer1, dict):
                    try:
                        if IS_Linefeed("{[words]}".format(layer1)):
                            words_buffer += ("{[words]}".format(layer1) + "\n")
                        else:
                            words_buffer += (("{[words]}").format(layer1))
                    except KeyError:
                        pass

    #将文本黏贴至剪贴板
    pyperclip.copy(words_buffer)
    print(words_buffer)
    return words_buffer
Example #10
0
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********')
Example #11
0
    access_token = get_access_token()
    if access_token is None:
        return None
    print access_token
    params = {'access_token': access_token}
    headers = {'Content-type': 'application/x-www-form-urlencoded'}
    rq = {'image': img, 'templateSign': '75728a7d5fbaa201049f6198f651305f2019'}
    res = requests.request(
        'POST',
        'https://aip.baidubce.com/rest/2.0/solution/v1/iocr/recognise',
        params=params,
        headers=headers,
        data=rq)
    if res:
        print res.json()
    return None


image = get_file_content('test_aip.jpg')
data0 = client.basicAccurate(image)
print_result(data0['words_result'])
# get_img(image)
from jy_word.File import File
my_file = File()
data1 = client.custom(image, template_id)
# print data1
my_file.write('aip_test.json', data1)

if __name__ == "__main__":
    pass
Example #12
0
 def ocrChar(self, imgPath):
     # 图片数据
     image = self.readImgFile(imgPath)
     client = AipOcr(self.AppID, self.APIKey, self.SecretKey)
     return client.basicAccurate(image)
""" 你的 APPID AK SK """
APP_ID = '9588288'
API_KEY = 'lchwrQNmq2868qS6rNFXYtNG'
SECRET_KEY = 'EvlfpbVnUqWjq22EVpuR6Rjd56xV8sWg'


# 读取图片
def get_file_content(filePath):
    with open(filePath, 'rb') as fp:
        return fp.read()


# 调用通用文字识别接口

# 定义参数变量
options = {
    'detect_direction': 'true',
    'language_type': 'CHN_ENG',
}

aipOcr = AipOcr(APP_ID, API_KEY, SECRET_KEY)
#result = aipOcr.basicGeneral(get_file_content(r"C:\Users\Tsinghua-yincheng\Desktop\SZday16\ocrdata\libaby.jpg"),
#options)

result = aipOcr.basicAccurate(
    get_file_content(
        r"C:\Users\Tsinghua-yincheng\Desktop\SZday16\ocrdata\libaby.jpg"),
    options)

print(result)
Example #14
0
File: ocr.py Project: pczb/vimconf
from aip import AipOcr

""" 你的 APPID AK SK """
APP_ID = ''
API_KEY = 'Vfoayf6ZuupEesUXDEygLbPQ'
SECRET_KEY = '50kwhTnPZok7KPu2yF8HXVub6fzIqOXK'

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('/Users/tigerzhang/Downloads/huafei.JPG')

""" 调用通用文字识别(高精度版) """
client.basicAccurate(image);

""" 如果有可选参数 """
options = {}
options["detect_direction"] = "true"
options["probability"] = "true"


""" 带参数调用通用文字识别(高精度版) """
resp = client.basicAccurate(image, options)
for word in resp['words_result']:
    if word['words'].startswith('充值卡密码'):
        print(word['words'])
Example #15
0
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("无点选文字点击图片")
Example #16
0
APP_ID = '19534018'
API_KEY = 'dgHolPV3cciGmfZusoys5kyg'
SECRET_KEY = '74HQMDvmoAf6C0kjtU3oYm4TRcbNAjqL'

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('3.jpg')

basic_mode = client.basicAccurate(image)

sum_words = basic_mode['words_result_num']
result = ''
"""""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """"""
for i in range(0, sum_words):
    result = result + str(basic_mode['words_result'][i]['words']) + '\n'
print(result)
"""""" """""" """""" """""" """""" """""" """""" """""" """""" """""" """"""
with open('basic_mode.txt', 'w') as f:
    f.write(result)

#print(high_mode)
#print(basic_mode)
Example #17
0
from aip import AipOcr

APP_ID = '16657913'
API_KEY = 'SNbBvp0R4Lbu6DqssoTnUGc0'
SECRECT_KEY = 'hp4tO0XDGDSFZ5tLbSbV68qgDvn4fENL'
client = AipOcr(APP_ID, API_KEY, SECRECT_KEY)


def get_file_content(filePath):
    with open(filePath, 'rb') as fp:
        return fp.read()


options = {"detect_language": "true", 'language_type': "CHN_ENG"}
image = get_file_content('./img/xt.jpg')
message = client.basicAccurate(image)
print(message)
Example #18
0
# -*- coding: UTF-8 -*-
from aip import AipOcr
""" 百度ocr申请到的三个秘钥 """
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('/home/***/Software/OCR/Pictures/1.png')
""" 调用通用文字识别(高精度版)"""
restu1 = client.basicAccurate(image)  # 通用文字高精度识别,每天 500 次免费
#restu1 = client.basicGeneral(image)   # 通用文字识别,每天 50 000 次免费
""" 输出识别到的文字 """
lists = restu1['words_result']  #列表
for listss in lists:
    print(listss['words'])
Example #19
0
from aip import AipOcr
""" 你的 APPID AK SK """
APP_ID = '17219475'
API_KEY = 'wunGv6RtkjGbexAyh9yIKRjf'
SECRET_KEY = 'izG1oSuj1EBQ21A6YosKkLmv6aPcrXra'
client = AipOcr(APP_ID, API_KEY, SECRET_KEY)  #配置百度接口
""" 读取图片 """


def get_file_content(filePath):
    with open(filePath, 'rb') as fp:
        return fp.read()


while 1:
    print("等待中...")
    keyboard.wait(hotkey="ctrl+alt+a")

    while 1:
        image = ImageGrab.grabclipboard()
        if image:
            break
    image.save("cutimg.jpg")
    image = get_file_content('cutimg.jpg')
    print(image)
    """ 调用通用文字识别, 图片参数为本地图片 """
    res = client.basicAccurate(image)
    linenum = res["words_result_num"]
    for i in res["words_result"]:
        print(i["words"])
Example #20
0
import time
from aip import AipOcr

app_id = ''
api_key = ''
secret_key = ''

client = AipOcr(app_id, api_key, secret_key)

while True:

    keyboard.wait(hotkey='alt+a')
    keyboard.wait(hotkey='ctrl+s')
    time.sleep(0.1)

    image = ImageGrab.grabclipboard()
    image.save('image_001.jpg')

    with open('image_001.jpg', 'rb') as file:
        image = file.read()
        result = client.basicAccurate(image)
        result = result['words_result']
        for i in result:
            print(i['words'])
            with open('word.txt', 'a+', encoding='UTF-8') as text:
                text.writelines('%s\n' % i['words'])

    hotkey = keyboard.read_hotkey()
    if hotkey == 'q':
        break
    def sign_in_by_code(self, task, entry, config):
        app_id = config.get('aipocr_app_id')
        api_key = config.get('aipocr_api_key')
        secret_key = config.get('aipocr_secret_key')

        if not (AipOcr and Image):
            entry['result'] = 'baidu-aip or pillow not existed'
            entry.fail(entry['result'])
            return
        if not (app_id and api_key and secret_key):
            entry['result'] = 'Api not set'
            entry.fail(entry['result'])
            return

        client = AipOcr(app_id, api_key, secret_key)

        response = self._request(task,
                                 entry,
                                 'get',
                                 entry['base_url'],
                                 headers=entry['headers'])
        state = self.check_state(entry, response, entry['base_url'])
        if state != SignState.NO_SIGN_IN:
            return

        response = self._request(task,
                                 entry,
                                 'get',
                                 entry['url'],
                                 headers=entry['headers'])
        content = self._decode(response)
        image_hash_re = re.search('(?<=imagehash=).*?(?=")', content)
        img_src_re = re.search('(?<=img src=").*?(?=")', content)

        if image_hash_re and img_src_re:
            image_hash = image_hash_re.group()
            img_src = img_src_re.group()
            img_response = self._request(task,
                                         entry,
                                         'get',
                                         urljoin(entry['url'], img_src),
                                         headers=entry['headers'])
        else:
            entry['result'] = 'Cannot find key: image_hash, url: {}'.format(
                entry['url'])
            entry.fail(entry['result'])
            return

        img = Image.open(BytesIO(img_response.content))
        width = img.size[0]
        height = img.size[1]
        for i in range(0, width):
            for j in range(0, height):
                noise = self._detect_noise(img, i, j, width, height)
                if noise:
                    img.putpixel((i, j), (255, 255, 255))
        img_byte_arr = BytesIO()
        img.save(img_byte_arr, format='png')
        response = client.basicAccurate(img_byte_arr.getvalue(),
                                        {"language_type": "ENG"})
        code = re.sub('\\W', '', response['words_result'][0]['words'])
        code = code.upper()
        logger.info(response)
        if len(code) == 6:
            params = {'cmd': 'signin'}
            data = {
                'imagehash': (None, image_hash),
                'imagestring': (None, code)
            }
            response = self._request(task,
                                     entry,
                                     'post',
                                     entry['url'],
                                     headers=entry['headers'],
                                     files=data,
                                     params=params)
            state = self.check_state(entry, response, response.request.url)
        if len(code) != 6 or state == SignState.WRONG_ANSWER:
            with open(path.dirname(__file__) + "/opencd_code.png",
                      "wb") as code_file:
                code_file.write(img_response.content)
            with open(path.dirname(__file__) + "/opencd_code2.png",
                      "wb") as code_file:
                code_file.write(img_byte_arr.getvalue())
            entry['result'] = 'ocr failed: {}, see opencd_code.png'.format(
                code)
            entry.fail(entry['result'])
Example #22
0
from aip import AipOcr

APP_ID = '11730410'
API_KEY = 'Teuyu8PygKTn8KEdUqLTTvh1'
SECRET_KEY = 'rIqEyFe6TkFTKrt7Isa9rvsG9vzTELCT '
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')

""" 调用通用文字识别(高精度版) """
client.basicAccurate(image);

""" 如果有可选参数 """
options = {}
options["detect_direction"] = "true"
options["probability"] = "true"

""" 带参数调用通用文字识别(高精度版) """
client.basicAccurate(image, options)
    def sign_in_by_code_hdsky(self, task, entry, config):
        app_id = config.get('aipocr_app_id')
        api_key = config.get('aipocr_api_key')
        secret_key = config.get('aipocr_secret_key')

        if not (AipOcr and Image):
            entry['result'] = 'baidu-aip or pillow not existed'
            entry.fail(entry['result'])
            return
        if not (app_id and api_key and secret_key):
            entry['result'] = 'Api not set'
            entry.fail(entry['result'])
            return

        client = AipOcr(app_id, api_key, secret_key)

        response = self._request(task,
                                 entry,
                                 'get',
                                 entry['base_url'],
                                 headers=entry['headers'])
        state = self.check_state(entry, response, entry['base_url'])
        if state != SignState.NO_SIGN_IN:
            return

        data = {
            'action': (None, 'new'),
        }
        response = self._request(task,
                                 entry,
                                 'post',
                                 'https://hdsky.me/image_code_ajax.php',
                                 headers=entry['headers'],
                                 files=data)
        content = self._decode(response)
        image_hash = json.loads(content)['code']

        if image_hash:
            img_response = self._request(
                task,
                entry,
                'get',
                'https://hdsky.me/image.php?action=regimage&imagehash={}'.
                format(image_hash),
                headers=entry['headers'])
        else:
            entry['result'] = 'Cannot find: image_hash, url: {}'.format(
                entry['url'])
            entry.fail(entry['result'])
            return

        img = Image.open(BytesIO(img_response.content))
        width = img.size[0]
        height = img.size[1]
        for i in range(0, width):
            for j in range(0, height):
                noise = self._detect_noise(img, i, j, width, height)
                if noise:
                    img.putpixel((i, j), (255, 255, 255))
        img_byte_arr = BytesIO()
        img.save(img_byte_arr, format='png')
        response = client.basicAccurate(img_byte_arr.getvalue(),
                                        {"language_type": "ENG"})
        code = re.sub('\\W', '', response['words_result'][0]['words'])
        code = code.upper()
        logger.info(response)
        if len(code) == 6:
            data = {
                'action': (None, 'showup'),
                'imagehash': (None, image_hash),
                'imagestring': (None, code)
            }
            response = self._request(task,
                                     entry,
                                     'post',
                                     entry['url'],
                                     headers=entry['headers'],
                                     files=data)
            print(response.text)
            state = self.check_state(entry, response, response.request.url)
        if len(code) != 6 or state == SignState.WRONG_ANSWER:
            with open(path.dirname(__file__) + "/temp.png", "wb") as code_file:
                code_file.write(img_response.content)
            with open(path.dirname(__file__) + "/temp2.png",
                      "wb") as code_file:
                code_file.write(img_byte_arr.getvalue())
            entry['result'] = 'ocr failed: {}, see temp.png'.format(code)
            entry.fail(entry['result'])
Example #24
0
SECRET_KEY = '2YOAu5p6MEpq9iWKR3yKRERfxkduWFWN'

client = AipOcr(APP_ID, API_KEY, SECRET_KEY)
""" 读取图片 """


def get_file_content(filePath):
    with open(filePath, 'rb') as fp:
        return fp.read()


while 1:
    # 监听键盘按键
    keyboard.wait(hotkey='f1')
    keyboard.wait(hotkey='ctrl+c')

    time.sleep(0.1)

    # 保存件剪贴板的图片保存到本地
    image = ImageGrab.grabclipboard()
    image.save('./images/shoot.jpg')

    image = get_file_content('./images/shoot.jpg')
    """ 调用通用文字识别(高精度版) """
    text = client.basicAccurate(image)
    # print(text)
    result = text['words_result']

    for info in result:
        print(info['words'])
Example #25
0
import os
import time

""" 你的 APPID AK SK """
APP_ID = ''
API_KEY = ''
SECRET_KEY = ''

client = AipOcr(APP_ID, API_KEY, SECRET_KEY)

path = os.getcwd()
listimg = [os.path.join(path, f) for f in os.listdir(path) if f.endswith(".jpeg")]

for foo in range(0,260):
    a = 2*foo
    for _ in range(a,a+2):
        file = listimg[_]
        i = open(file, 'rb').read()
        #result = client.basicGeneral(i)#通用
        result = client.basicAccurate(i)#精准
        print(result)

        with open("result.txt", "a", encoding='utf-8') as resfile:
            while "words_result" not in result:
                result = client.basicAccurate(i)
            for item in result['words_result']:
                resfile.write(item['words'])
                resfile.write('\n')
                print(file)
    time.sleep(1)
Example #26
0
    image = open(filepath, 'rb').read()
    data = {'image': base64.b64encode(image).decode()}

    request_url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/classification/parkingFee" + "?access_token=" + access_token
    response = requests.post(request_url, data=json.dumps(data))
    content = response.json()
    results = content['results']
    score = 0
    for result in results:
        if result['name'] == 'parkingFee':
            score = result['score']
    print(results)
    if score > 0.9:
        print(filepath)
        fo.write("New Parking Lot Begins Here\n")
        name = re.sub(
            r'/Users/chenlingna/Desktop/Project/pictureCrawling/pictures/', '',
            filepath)
        fo.write(name + '\n')
        print(score)
        # 定义参数变量
        options = {
            #	'detect_direction': 'true',
            'language_type': 'ENG'
        }
        # 网络图片文字文字识别接口
        result = aipOcr.basicAccurate(get_file_content(filepath), options)
        lists = result['words_result']
        for element in lists:
            fo.write(element['words'] + '\n')
Example #27
0
# 一眼就能看出来,百度的识别成功率高

### 使用百度云识别整个文件夹中的图片并写到word文档中

path3 = 'F:\\file_2018_05_17\\'
# 读取文件夹中的所有文件,生成文件名list
file = os.listdir(path3)

ok_list = [] # 用来存储有正确返回的
err_list = [] # 用来存储报错的,可用来多次上传百度云再识别
document = Document() # 用来将生成结果写到word文档中去

for fi in file:
    image = get_file_content(path3 + fi)
    result = client.basicAccurate(image, options) # 这里用的是不需要位置信息的接口
    try:
        word_result = result['words_result']
        heading = u'{}'.format(fi)
        document.add_heading(heading,1) # 一级标题

        for i in word_result:
            document.add_paragraph(u'{}'.format(i['words']))

        document.add_page_break() # 两张图片生成的内容之间用分页符
        ok_list.append(fi)

    except:
        err_list.append(fi)
        print(fi, result)
Example #28
0
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import json

from aip import AipOcr

if __name__ == '__main__':
    APP_ID = '15306981'
    API_KEY = '5ZDzsFGj6cl6wa3QQSbF2xrn'
    SECRET_KEY = 'VUiYSYg12yImNROMaXG8LITDDG3O1SiT'

    client = AipOcr(APP_ID, API_KEY, SECRET_KEY)

    # 读取图片
    def get_file_content(file_path):
        with open(file_path, 'rb') as fp:
            return fp.read()

    image = get_file_content(
        '/Users/liwenhao/Desktop/douban-captcha-example2.jpg')
    """ 调用通用文字识别(高精度), 图片参数为本地图片 """
    result = json.dumps(client.basicAccurate(image))
    print(result)
Example #29
0
from aip import  AipOcr
import re
# 定义常量
#识别图像中的文字
APP_ID = 	'xxxxxxxxxxxx'
API_KEY = 'xxxxxxxxxxxxxxxx'
SECRET_KEY = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
# 初始化AipFace对象
client = AipOcr(APP_ID, API_KEY, SECRET_KEY)

i = open(r'F:\百度aip\1.jpg','rb')
img = i.read()
# message = client.basicGeneral(img)
message = client.basicAccurate(img,options=None)
# print(message)
#高精度版  basicAccurate(self, image, options=None):
for i in message.get('words_result'):
     print(i.get('words'))
Example #30
0
def get_file_content(filePath):
    with open(filePath, 'rb') as fp:
        return fp.read()


def traverse(f):
    fs = os.listdir(f)
    for f1 in fs:
        tmp_path = os.path.join(f, f1)
        if not os.path.isdir(tmp_path):
            print('file : %s' % tmp_path)
        else:
            print('folder :%s' % tmp_path)
            traverse(tmp_path)


convert_pdf_to_jpg('./projects/ocr/bad.pdf')
folder = './projects/ocr/img'
fs = os.listdir(folder)
for f1 in fs:
    tmp_path = os.path.join(folder, f1)
    if not os.path.isdir(tmp_path):
        if not 'DS_Store' in tmp_path:
            image = get_file_content(tmp_path)
            dict_code = client.basicAccurate(image)
            json_code = json.dumps(dict_code)
            text_code = json.loads(json_code)
            for item in text_code['words_result']:
                print(item.get('words'))
def OCR_Core(Image, **kwargs):
    #: PIL.Image
    with open("./important/setting.json", 'r+') as f:
        setting = json.load(f)
    global GALMode, ResultJson  # 为了文字处理使用
    global LastImageValue, OCRText, OCRResultSetting
    if kwargs.get("EXTRA"):
        SelectOCR = OcrAll[OCRResultSetting.get()]
    else:
        SelectOCR = OcrAll[OcrSetting.get()]
        #写入内存,太慢,更换保存本地方式
        Image.save('important/LastImage.jpg')
        with open('important/LastImage.jpg', 'rb+') as f:
            LastImageValue = f.read()
    OCRText = ""
    if SelectOCR == "bd_normal" or SelectOCR == "bd_accurate":
        AppID = setting["userInfo"]["bd_info"]["AppID"]
        APIKey = setting["userInfo"]["bd_info"]["APIKey"]
        SecretKey = setting["userInfo"]["bd_info"]["SecretKey"]
        BDOcr = AipOcr(AppID, APIKey, SecretKey)
        if not GALMode:  #在gal模式下获取下拉框内容
            if SelectOCR == "bd_normal":
                OCRLanguage = setting["defaultOCRLanguage"]
                ResultJson = BDOcr.basicGeneral(
                    LastImageValue, {"language_type": OCRLanguage})  #格式错误
            else:
                ResultJson = BDOcr.basicAccurate(LastImageValue)
        else:
            GALLanguage = setting["defaultGALLanguage"]
            ResultJson = BDOcr.basicGeneral(
                LastImageValue, {"language_type": GALLanguage})  # 格式错误
        if not (ResultJson["words_result_num"]):  # 没有结果
            if GALMode:
                return ""
            else:
                messagebox.showinfo(u"识别错误", u"未识别到文字")
        if ResultJson.get("words_result"):  #能获取结果
            # 文本处理
            for i in ResultJson["words_result"]:
                OCRText += i['words'] + "\n"
            return OCRText
        elif ResultJson.get('error_code') == 14:  #证书失效,检查用户信息
            messagebox.showerror(title="Error",
                                 message=u"检查APPID,APIKEY,以及SECRET_KEY,程序退出")
            sys.exit()
        elif ResultJson.get('error_code') == 17:  #今天超额
            messagebox.showerror(title="Error", message=u"今日次数超额")
            sys.exit()
        else:
            messagebox.showerror(title="Error",
                                 message=u"错误代码:" + str(ResultJson))
            sys.exit()
    else:  #腾讯OCR
        TX_INFO = setting["userInfo"]["tx_info"]
        SecretId = TX_INFO["SecretId"]
        SecretKey = TX_INFO["SecretKey"]
        try:
            cred = credential.Credential(SecretId, SecretKey)
            httpProfile = HttpProfile()
            httpProfile.endpoint = "ocr.tencentcloudapi.com"

            clientProfile = ClientProfile()
            clientProfile.httpProfile = httpProfile
            # zh\auto\jap\kor
            client = ocr_client.OcrClient(cred, "ap-guangzhou", clientProfile)
            params = '{"ImageBase64":"' + str(
                bytes.decode(
                    base64.b64encode(LastImageValue),
                    encoding='utf-8')) + '","LanguageType":"auto"}'  #生成传输参数
            # 可修改
            # GeneralFasterOCR == 通用印刷体识别高速版,没有语言选项,有方位
            # GeneralBasicOCR == 通用印刷体识别,有语言选项,有方位
            # GeneralAccurateOCR == 通用印刷体高精度版,没有语言选项,有方位
            if SelectOCR == "tx_normal":
                req = models.GeneralBasicOCRRequest()
                req.from_json_string(params)
                resp = client.GeneralBasicOCR(req)
            elif SelectOCR == "tx_quick":
                req = models.GeneralFastOCRRequest()
                req.from_json_string(params)
                resp = client.GeneralFastOCR(req)
            else:
                req = models.GeneralAccurateOCRRequest()
                req.from_json_string(params)
                resp = client.GeneralAccurateOCR(req)
            ResultJson = json.loads(resp.to_json_string())  # 获取结果json
            OCRText = ""  # 纯文本
            for i in ResultJson["TextDetections"]:
                OCRText += i["DetectedText"] + "\n"
            return OCRText

        except TencentCloudSDKException as err:
            if err.get_code() == "FailedOperation.ImageNoText":
                if not GALMode:
                    messagebox.showinfo("识别失败", "没有识别到文字")
                return False
Example #32
0
from aip import AipOcr
""" 你的 APPID AK SK """
APP_ID = '11559967'
API_KEY = 'ehhaRYCnGpRyY27n83VrPRlw'
SECRET_KEY = 'IlZqr30Fxy98FmFVEMbjG4ODFhFLuRt8'

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('demo.jpg')
""" 调用通用文字识别(高精度版) """
client.basicAccurate(image)
""" 如果有可选参数 """
options = {}
options["detect_direction"] = "true"
options["probability"] = "true"
""" 带参数调用通用文字识别(高精度版) """
aa = client.basicAccurate(image, options)
Example #33
0
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)