app = Flask(__name__)
api = Api(app, version='1.0', title='文字识别接口')
predict = api.namespace('predict')
ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif','doc']) # 允许上传的文件类型
@predict.route('/')
class CMDcommand(Resource):
@api.doc()
def post(self):
file = request.files['img']
if file is None:
return "未上传文件"
base64_data = base64.b64encode(file.read())
s = base64_data.decode()
url = "http://192.168.5.218:8866/predict/ocr_system"
data = "{\"images\": [\"%s\"]}"%s
print(data)
headers = {
'Content-Type': 'application/json'
}
#字符串格式
res = requests.post(url=url,headers=headers,timeout = 20,data=data)
return res.text
def allowed_file(filename): # 验证上传的文件名是否符合要求,文件名必须带点并且符合允许上传的文件类型要求,两者都满足则返回 true
return '.' in filename and \
filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
python 简单使用post文件上传并测试PaddleOCR接口
最新推荐文章于 2024-07-26 19:47:22 发布