第一种方法 opencv、requests、flask

此方法比较耗费时间 600毫秒左右

客户端代码

#coding:utf-8
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))

服务器端代码

#coding:utf-8
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

第二种 直接利用文件传输 时间在10毫秒以内

客户端代码

#coding:utf-8

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

服务器端代码

#coding:utf-8
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)

 

版权声明:本文为cnugis原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/cnugis/p/8093246.html