• python上传图片并识别图片


    from json_response import JsonResponse
    from aip import AipOcr
    import os
    import time
    
    BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    OUT = "/static/upload"
    ABS_OUT = os.path.join(BASE_DIR, OUT[1:])
    
    # 定义常量
    APP_ID = '9851066'
    API_KEY = 'LUGBatgyRGoerR9FZbV4SQYk'
    SECRET_KEY = 'fB2MNz1c2UHLTximFlC4laXPg7CVfyjV'
    
    # 初始化AipFace对象
    aipOcr = AipOcr(APP_ID, API_KEY, SECRET_KEY)
    
    def upload_img(request):
    
        img = request.FILES.get('image')
        if not all(img):
            return JsonResponse({
                        'code':'-1',
                        'msg':'no image'
                    })
        abs_path = ''
        try:
            # 存储文件
            file_name = "%s.png" % (time.time() * 1000)
            # web_path = os.path.join(OUT, file_name)
            abs_path = os.path.join(ABS_OUT, file_name)
            with open(abs_path, "wb") as fp:
                for chunk in img.chunks():
                    fp.write(chunk)
    
        except Exception as msg:
            return JsonResponse({
                "code": -3, "msg": "Upload filed! Msg:%s" % msg
            })
    
        options = {
            'detect_direction': 'true',
            'language_type': 'CHN_ENG',
        }
    
        # 调用通用文字识别接口
        result = aipOcr.basicGeneral(get_file_content(abs_path), options)
        # print(json.dumps(result).decode("unicode-escape"))
        return JsonResponse(result)
    
    def get_file_content(filePath):
        with open(filePath, 'rb') as fp:
            return fp.read()
  • 相关阅读:
    024.Zabbix告警等级机制
    023.Zabbix自定义(邮箱)脚本告警-02
    022.Zabbix自定义(邮箱)脚本告警01
    021.Zabbix的邮件告警-01
    020.Zabbix的Actions配置
    019.Zabbix的Trigger及相关函数
    018.Zabbix维护时间和模板导入
    017.Zabbix宏介绍
    016.Zabbix聚合监控
    015.Zabbix的日志监控配置
  • 原文地址:https://www.cnblogs.com/Mvloveyouforever/p/8980359.html
Copyright © 2020-2023  润新知