- 方式一,从本地读取到图片或帧,上传到flask服务器
# 客户端发送
def image_post():
data = {"type_code": type_code,
"area_id": area_id}
# 以文件的格式上传,节省传输时间
file = {"file": ("file_name.jpg",open("file_path", 'rb'), "image/jpg")}
res = requests.post(url=post_url, files=file, data=data)
# flask服务器端接收
# 接收客服端发来的图片,以时间戳格式命名保存到服务器对应目录下
@recognize.route("/save_scene", methods=['post'])
def save_scene():
# 以下一些参数根据实际情况而言,主要看获取图像和保存图像那里
type_code = request.form.get("type_code")
type_code = int(type_code)
area_id = request.form.get("area_id")
area_id = int(area_id)
######## 获取图像
scene_img = request.files["scene_img"]
# 拼接保存路径和图像名
dir_name = os.path.join(all_scene_picture_dir, type_code_d[type_code])
scene_picture_name = str(t) + '.jpg'
scene_picture_path = os.path.join(scene_picture_dir, scene_picture_name)
####### 保存图片到服务器
scene_img.save(scene_picture_path)
return “200”
- 方式二,从opencv图片或帧对象转换为二进制文件上传
# 客户端发送
def image_post():
data = {"type_code": type_code,
"area_id": area_id}
# 以文件的格式上传,节省传输时间
# cv2.imencode(".jpg", frame)[1].tobytes()是将ndarray对象以jpg格式编码,再转换为二进制格式数据
file = {"file": ("file_name.jpg", cv2.imencode(".jpg", frame)[1].tobytes(), "image/jpg")}
res = requests.post(url=post_url, files=file, data=data)
# flask服务器端接收
# 接收客服端发来的图片,以时间戳格式命名保存到服务器对应目录下
@recognize.route("/save_scene", methods=['post'])
def save_scene():
# 以下一些参数根据实际情况而言,主要看获取图像和保存图像那里
type_code = request.form.get("type_code")
type_code = int(type_code)
area_id = request.form.get("area_id")
area_id = int(area_id)
######## 获取图像
scene_img = request.files["scene_img"]
# 拼接保存路径和图像名
dir_name = os.path.join(all_scene_picture_dir, type_code_d[type_code])
scene_picture_name = str(t) + '.jpg'
scene_picture_path = os.path.join(scene_picture_dir, scene_picture_name)
####### 保存图片到服务器
scene_img.save(scene_picture_path)
return “200”