photo镜像设置,photo软件操作

首页 > 经验 > 作者:YD1662022-11-08 17:52:17

之前写检查报告单ocr识别只是脚本运行的方式,如何提供别人方便的使用,那就需要提供一个接口服务了。利用FlasK实现简单图片上传,将识别后的结果返回,并打包成docker镜像运行在k8s集群中,就可以方便对外服务了,在代码有一点改动但核心不变。

Flask 实现代码

app = Flask(__name__) # 设置图片保存文件夹 UPLOAD_FOLDER = '/app/photo' # UPLOAD_FOLDER = 'resizephoto' app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER app.config['RESIZE_UPLOAD_FOLDER'] = '/app/resizephoto' # 设置允许上传的文件格式 ALLOW_EXTENSIONS = ['png', 'jpg', 'jpeg'] # 判断文件后缀是否在列表中 def allowed_file(filename): return '.' in filename and filename.rsplit('.', 1)[-1] in ALLOW_EXTENSIONS @app.route("/photo/upload", methods=['POST', "GET"]) def upload(): if request.method == 'POST': # 获取post过来的文件名称,从name=file参数中获取 file = request.files['file'] yyname = request.form.get('yyname') resp = {} if file and allowed_file(file.filename): print(file.filename) # secure_filename方法会去掉文件名中的中文 file_name = secure_filename(file.filename) # 保存图片 if not os.path.exists(app.config['UPLOAD_FOLDER']): print("建立目录:",app.config['UPLOAD_FOLDER']) os.makedirs(app.config['UPLOAD_FOLDER']) if not os.path.exists(app.config['RESIZE_UPLOAD_FOLDER']): print("建立目录:", app.config['RESIZE_UPLOAD_FOLDER']) os.makedirs(app.config['RESIZE_UPLOAD_FOLDER']) uploadImagePath = os.path.join(app.config['UPLOAD_FOLDER'], file_name) file.save(uploadImagePath) resizeuploadImagePath = os.path.join(app.config['RESIZE_UPLOAD_FOLDER']) resizeimage = resizeimgae(resizeuploadImagePath, uploadImagePath) # 修改尺寸代码 if yyname == "zl": nametmp = nameslistzl if yyname == "zyy": nametmp = nameslistzyy resdict = ocrsdk(resizeimage, nametmp) # 识别代码 resp["data"] = resdict return json.dumps(resp,ensure_ascii=False),200 else: resp["msg"] = "格式错误,请上传jpg格式文件" return json.dumps(resp,ensure_ascii=False),400 return render_template('index.html') # 查看图片 @app.route("/photo/<imageId>.jpg") def get_frame(imageId): # 图片上传保存的路径 with open(r'D:\pythonProject\autoTestYek\baiduAIOCR\photo\{}.jpg'.format(imageId), 'rb') as f: image = f.read() resp = Response(image, mimetype="image/jpg") return resp if __name__ == "__main__": app.run(host='0.0.0.0', port=5000, debug=True)依赖包文件

requirements.txt

pandas==0.24.2 Pillow==9.2.0 baidu-aip flask==1.1.2 chardet jinja2==2.11.2 markupsafe==1.1.1 itsdangerous==1.1.0 werkzeug==1.0.1Dockerfile

FROM python:3.7 RUN mkdir -pv /app WORKDIR /app COPY ./src /app/src # 安装python并配置环境 RUN pip3 config set global.index-url https://mirrors.aliyun.com/pypi/simple/ \ && pip3 install --upgrade pip RUN pip3 install -r /app/src/requirements.txt EXPOSE 5000 CMD ["python", "/app/src/main.py"]

运行后的镜像服务,部署在rancher中

photo镜像设置,photo软件操作(1)

html模板文件

<!DOCTYPE html> <html lang="zh-cmn-Hans"> <head> <meta charset="UTF-8"> <title>Upload</title> </head> <h1>请上传图片文件,并识别</h1> <form action="" method=post enctype=multipart/form-data> <p><input type=file name=file> <p><label>医院名称(默认长沙市中医医院)</label> <input type=text name="yyname" value=zyy/zl></p> <input type=submit value=Upload> </form>文件结构

photo镜像设置,photo软件操作(2)

实现效果

可以用Postman测试验证,也可以页面请求测试

页面效果

photo镜像设置,photo软件操作(3)

测试请求

页面请求测试:

photo镜像设置,photo软件操作(4)

首页 12下一页

栏目热文

文档排行

本站推荐

Copyright © 2018 - 2021 www.yd166.com., All Rights Reserved.