zoukankan      html  css  js  c++  java
  • 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()
  • 相关阅读:
    ionic2简单分析
    mvc的真实含义
    JavaSE学习总结(十七)—— IO流
    vs2010快捷键;sql server 2008快捷;IE9快捷键
    设计模式之六大设计原则
    通过peview分析PE文件
    游戏限制多开原理及对应方法
    inline hook原理和实现
    vm tools安装包为空
    Linux下PWN环境搭建
  • 原文地址:https://www.cnblogs.com/Mvloveyouforever/p/8980359.html
Copyright © 2011-2022 走看看