如何使用请求和烧瓶发送和接收图像作为 http 表单数据的一部分
How to send and receive image as part of http form-data using requests and flask
我想发出一个 http POST 请求,并将图像作为 multipart/form-data
的一部分。我想避免将此图像编码为 base64,而只是将其作为二进制 blob 发送,我知道 http 可以处理。
这是我在客户端的代码:
from __future__ import print_function
import requests
import json
import cv2
def post_encoded_image(url, headers):
img = open("cyrus.jpg", 'rb').read()
payload = {'image': img, 'identity': 'cyrus'}
r = requests.post(url + "encoded-image", data=payload)
print(r)
url = "http://localhost:8080/"
content_type = 'multipart/form-data'
headers = {'content-type': content_type}
post_encoded_image(url, headers)
在服务器端,我的代码是这样的:
from flask import Flask, request
import flask
import tfsdk
import numpy as np
from colorama import Fore
from colorama import Style
import os
import cv2
@app.route('/encoded-image', methods=['POST'])
def test():
image = request.form.get("image")
nparr = np.fromstring(image, np.uint8)
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
print(type(img))
return "success"
if __name__ == '__main__':
app.run()
当我发出请求时,服务器将 img
的类型打印为 NoneType
,这意味着对 imdecode
的调用无效,这意味着发生了一些奇怪的事情带有编码图像的服务器端。
执行此操作的正确方法是什么?
编辑
通过执行以下操作,我能够实现所需的功能:
客户:
from __future__ import print_function
import requests
import json
import cv2
def post_encoded_image(url, headers):
img = open("cyrus.jpg", 'rb').read()
file = {'image': img}
data = {'identity': 'cyrus'}
r = requests.post(url + "encoded-image", data=data, files=file)
print(r)
url = "http://localhost:8080/"
content_type = 'multipart/form-data'
headers = {'content-type': content_type}
post_encoded_image(url, headers)
服务器:
@app.route('/encoded-image', methods=['POST'])
def test():
ID = request.form.get("identity")
image = request.files['image'].read()
nparr = np.fromstring(image, np.uint8)
这是正确的做法吗?
我能够实现如下所需的功能:
客户:
from __future__ import print_function
import requests
import json
import cv2
def post_encoded_image(url, headers):
img = open("cyrus.jpg", 'rb').read()
file = {'image': img}
data = {'identity': 'cyrus'}
r = requests.post(url + "encoded-image", data=data, files=file)
print(r)
url = "http://localhost:8080/"
content_type = 'multipart/form-data'
headers = {'content-type': content_type}
post_encoded_image(url, headers)
服务器:
@app.route('/encoded-image', methods=['POST'])
def test():
ID = request.form.get("identity")
image = request.files['image'].read()
nparr = np.fromstring(image, np.uint8)
我想发出一个 http POST 请求,并将图像作为 multipart/form-data
的一部分。我想避免将此图像编码为 base64,而只是将其作为二进制 blob 发送,我知道 http 可以处理。
这是我在客户端的代码:
from __future__ import print_function
import requests
import json
import cv2
def post_encoded_image(url, headers):
img = open("cyrus.jpg", 'rb').read()
payload = {'image': img, 'identity': 'cyrus'}
r = requests.post(url + "encoded-image", data=payload)
print(r)
url = "http://localhost:8080/"
content_type = 'multipart/form-data'
headers = {'content-type': content_type}
post_encoded_image(url, headers)
在服务器端,我的代码是这样的:
from flask import Flask, request
import flask
import tfsdk
import numpy as np
from colorama import Fore
from colorama import Style
import os
import cv2
@app.route('/encoded-image', methods=['POST'])
def test():
image = request.form.get("image")
nparr = np.fromstring(image, np.uint8)
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
print(type(img))
return "success"
if __name__ == '__main__':
app.run()
当我发出请求时,服务器将 img
的类型打印为 NoneType
,这意味着对 imdecode
的调用无效,这意味着发生了一些奇怪的事情带有编码图像的服务器端。
执行此操作的正确方法是什么?
编辑 通过执行以下操作,我能够实现所需的功能:
客户:
from __future__ import print_function
import requests
import json
import cv2
def post_encoded_image(url, headers):
img = open("cyrus.jpg", 'rb').read()
file = {'image': img}
data = {'identity': 'cyrus'}
r = requests.post(url + "encoded-image", data=data, files=file)
print(r)
url = "http://localhost:8080/"
content_type = 'multipart/form-data'
headers = {'content-type': content_type}
post_encoded_image(url, headers)
服务器:
@app.route('/encoded-image', methods=['POST'])
def test():
ID = request.form.get("identity")
image = request.files['image'].read()
nparr = np.fromstring(image, np.uint8)
这是正确的做法吗?
我能够实现如下所需的功能:
客户:
from __future__ import print_function
import requests
import json
import cv2
def post_encoded_image(url, headers):
img = open("cyrus.jpg", 'rb').read()
file = {'image': img}
data = {'identity': 'cyrus'}
r = requests.post(url + "encoded-image", data=data, files=file)
print(r)
url = "http://localhost:8080/"
content_type = 'multipart/form-data'
headers = {'content-type': content_type}
post_encoded_image(url, headers)
服务器:
@app.route('/encoded-image', methods=['POST'])
def test():
ID = request.form.get("identity")
image = request.files['image'].read()
nparr = np.fromstring(image, np.uint8)