zoukankan      html  css  js  c++  java
  • app自动化测试---自动化测试框架搭建之使用腾讯文字识别

    1.说明

     在app测试中,app中一些元素我们无法定位获取到,此时就需要截图,通过文字识别的方式,获取我们所需要的内容

     app自动化测试---用户登录自动化    app自动化测试---自动化测试框架搭建  中,我们已经使用自动化完成了登陆功能,现在将对登陆失败的情况做进一步处理

    2.腾讯ocr文字识别

    腾讯云图片识别:https://cloud.tencent.com/act/event/ocrdemo

    Python- SDK 使用教程:https://cloud.tencent.com/document/sdk/Python

    (1)安装第三方依赖包

    # 安装依赖包
    pip install tencentcloud-sdk-python

    (2)点击查看 API文档

            找到自己要调用的接口,填入自己使用中需要的参数,生成对应的代码

     (3)封装代码

              utils/ocr_handler.py

    """
      图片文字识别类
    """
    import json
    from tencentcloud.common import credential
    from tencentcloud.common.profile.client_profile import ClientProfile
    from tencentcloud.common.profile.http_profile import HttpProfile
    from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
    from tencentcloud.ocr.v20181119 import ocr_client, models
    from config import image_base64,SecretId,SecretKey
    
    class OCR_Handler:
    
        def image_ocr_text(self,imageBase64):
            try:
                cred = credential.Credential(SecretId, SecretKey)    # SecretId, SecretKey 从配置文件中获取对应的值
                httpProfile = HttpProfile()
                httpProfile.endpoint = "ocr.tencentcloudapi.com"
    
                clientProfile = ClientProfile()
                clientProfile.httpProfile = httpProfile
                client = ocr_client.OcrClient(cred, "ap-beijing", clientProfile)
    
                req = models.GeneralBasicOCRRequest()
                # 参数均为非必填参数
                params = {
                    "ImageBase64": imageBase64             # 图片/PDF的 Base64 值
                    # "ImageUrl": "",                      # 图片/PDF的 Url 地址
                }
                req.from_json_string(json.dumps(params))
    
                resp = client.GeneralBasicOCR(req)
                # print('识别出来的文字内容:',resp.to_json_string())
                return  resp.to_json_string()
    
            except TencentCloudSDKException as err:
                print('ocr图片识别:',err)
    
    
    # 单个方法调试代码
    # ocr = OCR_Handler()
    # text = ocr.image_ocr_text(image_base64)
    # jsonobj = json.loads(text)   # type : dict
    # text1 = jsonobj['TextDetections'][2]['DetectedText']
    # print(text1)

       config.py

    """
      项目配置文件
    """
    # ocr 腾讯云秘钥
    SecretId = "AK......IuGL"
    SecretKey = "pNf......VnnW"
    
    # 调试ocr文字识别的时候使用
    image_base64 = "AANSU......ErkJggg=="

     

  • 相关阅读:
    SAS学习经验总结分享:篇三—SAS函数
    SAS学习经验总结分享:篇二—input语句
    微信指尖海报制作流程——中秋佳节
    SAS学习经验总结分享:篇一—数据的读取
    SAS连接MYSQL的步骤及引用数据表
    动态PPT制作
    cmake实战第一篇:初试 cmake
    由浅到深理解ROS(5)- launch启动文件的理解与编写
    由浅到深理解ROS(4)
    由浅到深理解ROS(3)-命名空间
  • 原文地址:https://www.cnblogs.com/Z-Queen/p/14954406.html
Copyright © 2011-2022 走看看