码迷,mamicode.com
首页 > 编程语言 > 详细

利用Python进行图片发送与接收的两种方法---包含客户端和服务器端代码

时间:2017-12-23 15:55:56      阅读:514      评论:0      收藏:0      [点我收藏+]

标签:frame   blog   ima   time()   tar   enc   base   duration   发送   

第一种方法 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)

 

利用Python进行图片发送与接收的两种方法---包含客户端和服务器端代码

标签:frame   blog   ima   time()   tar   enc   base   duration   发送   

原文地址:http://www.cnblogs.com/cnugis/p/8093246.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!