200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > Flask学习笔记(二):基于Flask框架上传图片到服务器端并原名保存

Flask学习笔记(二):基于Flask框架上传图片到服务器端并原名保存

时间:2021-01-01 11:38:30

相关推荐

Flask学习笔记(二):基于Flask框架上传图片到服务器端并原名保存

文章目录

1.什么是Flask2.通过Flask上传图片到服务器端(以原名保存)

1.什么是Flask

Flask是一个基于python开发并依赖于 jinja2 模板和 werkzeug WSGI 服务器的一个微型框架。而werkzeug本质上是一个 socket 服务端,用于接收浏览器发送过来的请求,并进行预处理,然后再触发flask,这个时候我们就通过flask给我们提供的功能去对浏览器发送的请求做一个处理,当要处理的文件相对比较复杂时,则需要通过jinja2模板来处理,也就是我们常说的 渲染 ,之后再把处理过的数据返回给浏览器

学习链接:

2.通过Flask上传图片到服务器端(以原名保存)

server.py

from flask import requestfrom flask import Flaskimport jsonimport numpy as npimport cv2import base64from gevent.pywsgi import WSGIServerapp = Flask(__name__)# 定义路由@app.route("/photo", methods=['POST'])def get_frame():# 接收图片# upload_file = json.loads(request.json)# print(upload_file['file'])# upload_file=request.form['file']filename=request.get_json()print(filename)# print(type(filename)=='str')if (isinstance(filename,str)):filname1=json.loads(filename)tmp=filname1['file']name=filname1['name']img = base64.b64decode(str(tmp))image_data = np.fromstring(img, np.uint8)image_data = cv2.imdecode(image_data, cv2.IMREAD_COLOR)cv2.imwrite('F:/pycharm/feature_match/face_result/{}'.format(name), image_data)else:tmp=filename['file']img = base64.b64decode(str(tmp))image_data = np.fromstring(img, np.uint8)image_data = cv2.imdecode(image_data, cv2.IMREAD_COLOR)cv2.imwrite('F:/pycharm/feature_match/face_result/1.jpg', image_data)# fff=json.load(filename)# print(upload_file)# img=cv2.imread(tmp)# cv2.imwrite("222.jpg",img)# print(className,prob)json_info = json.dumps(dic, ensure_ascii=False)return json_infoif __name__ == "__main__":dic = {}app.run(host='10.16.55.26',port=9000)## Serve the app with gevent# http_server = WSGIServer(('0.0.0.1',5000),app)# http_server.serve_forever()

client.py

import requestsimport base64,globimport json,cv2,osimport numpy as np# API地址url = "http://10.16.55.26:9000/photo"# opencv读取出来的图片相当于numpy数组def cv2_to_base64(image):image1 = cv2.imencode('.jpg', image)[1]image_code = str(base64.b64encode(image1))[2:-1]return image_codedef base64_to_cv2(image_code):#解码img_data=base64.b64decode(image_code)#转为numpyimg_array=np.fromstring(img_data,np.uint8)#转成opencv可用格式img=cv2.imdecode(img_array,cv2.COLOR_RGB2BGR)return img# with open(r'D:\pycharm\car_fee_system\images\result\2.jpg', 'rb') as f:#img = base64.b64encode(f.read()).decode()path=r'D:\pycharm\car_fee_system\images\result/'images_paths = glob.glob(os.path.join(path + '*.jpg')) #*.jpg中的*,表示能匹配多个字符for images_path in images_paths:a,b= os.path.splitext(os.path.split(images_path)[1])name=str(a)+str(b)print(name)image=cv2.imread(images_path)image_code=cv2_to_base64(image)image = []image.append(image_code)# 拼接参数files = {'file': image,'name': name}fff=json.dumps(files, ensure_ascii=False)# 发送post请求到服务器端json.dumps(files, ensure_ascii=False)# r = requests.post(url, json=json.dumps(files, ensure_ascii=False))r = requests.post(url, json=fff)print(r.status_code)print(r.content)print(type(json.dumps(files, ensure_ascii=False)))

client_camera.py

import requestsimport base64,globimport json,cv2,osimport numpy as np# API地址url = "http://10.16.55.26:9000/photo"# opencv读取出来的图片相当于numpy数组def cv2_to_base64(image):image1 = cv2.imencode('.jpg', image)[1]image_code = str(base64.b64encode(image1))[2:-1]return image_codedef base64_to_cv2(image_code):#解码img_data=base64.b64decode(image_code)#转为numpyimg_array=np.fromstring(img_data,np.uint8)#转成opencv可用格式img=cv2.imdecode(img_array,cv2.COLOR_RGB2BGR)return img# with open(r'D:\pycharm\car_fee_system\images\result\2.jpg', 'rb') as f:#img = base64.b64encode(f.read()).decode()# frame 就是每一帧图像,是个三维矩阵# 参数是0,表示打开笔记本的内置摄像头,参数是视频文件路径则打开视频capture = cv2.VideoCapture(0)ret, frame = capture.read()a='cemare1'b=1while ret:b+=1if b%5==0:ret, frame = capture.read()name=str(a)+'-'+str(b)image_code=cv2_to_base64(frame)image = []image.append(image_code)# 拼接参数files = {'file': image,'name': name}fff=json.dumps(files, ensure_ascii=False)# 发送post请求到服务器端json.dumps(files, ensure_ascii=False)# r = requests.post(url, json=json.dumps(files, ensure_ascii=False))r = requests.post(url, json=fff)print('200为成功 500为失败',r.status_code)print('正在上传第{}张图片,图片名为{}'.format(b,name))# print(r.content)# print(type(json.dumps(files, ensure_ascii=False)))

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。