关于json相关内容参考
客户端请求方式:
如下:其中image_crop是ndarray类型的图像片段列表,传输前利用tolist()方法转换成了list格式
服务器端接收并响应方式:
第一种方法 opencv、requests、flask,此方法比较耗费时间 600毫秒左右
客户端代码:
import cv2
import json
import requests
img = cv2.imread("/home/aqonvs.jpg")
res = {"image": str(img.tolist()).encode('base64')} # img是ndarray,无法直接用base64编码,否则会报错
_ = requests.post("http://192.71.30.172:8081", data=json.dumps(res))
客户端代码:
from flask import request, Flask
import json
import numpy as np
import time
import cv2
app = Flask(__name__)
@app.route("/", methods=['POST'])
def get_frame():
start_time = time.time()
res = json.loads(request.data)
frame = eval(res["image"].decode("base64")) # dtype为int32
frame = np.array(frame, dtype=np.uint8)
cv2.imwrite('/home/tmp.jpg',frame)
duration = time.time() - start_time
print('duration:[%.0fms]' % (duration*1000))
return '0000'
if __name__ == "__main__":
app.run("192.71.30.172", port=8081) #端口为8081
客户端代码:
import requests
url = "xxxxx"
str000='/home/aqonvs.jpg'
newname = str000.split('/')
print(newname[len(newname)-1])
files = {'file':(newname,open('/home/aqonvs.jpg','rb'),'image/jpg')}
r = requests.post(url,files = files)
result = r.text
print(result)
服务器端代码:
from flask import request, Flask
import time
import os
app = Flask(__name__)
@app.route("/", methods=['POST'])
def get_frame():
start_time = time.time()
upload_file = request.files['file']
old_file_name = upload_file.filename
if upload_file:
file_path = os.path.join('/home/local/upload/', old_file_name)
upload_file.save(file_path)
print "success"
print('file saved to %s' % file_path)
duration = time.time() - start_time
print('duration:[%.0fms]' % (duration*1000))
return 'success'
else:
return 'failed'
if __name__ == "__main__":
app.run("127.0.0.1", port=5000)