DSPRiverInspection/code_bak/client.py

193 lines
6.2 KiB
Python
Raw Permalink Normal View History

2022-07-19 15:38:00 +08:00
from PIL import Image
import numpy as np
import cv2
import base64
import io,os
import requests
import time
def test():
image_path='test/P0017.png'
image_array=cv2.imread(image_path)
print(image_array.shape)
image_encode=base64.b64encode(image_array).decode('utf-8')
image_bytes=bytes(image_encode,encoding='utf-8')
image_decode=np.frombuffer(base64.decodebytes(image_bytes),dtype=np.uint8)
print(image_decode.shape)
request_url='http://192.168.109.49:5000/'
headers={'content-type':'application/json'}
data={'image':image_encode}
#response=requests.post(request_url,data=data,headers=headers)
response=requests.post(request_url,"POST",files=data)
print(response)
#image=open(image_path,'rb').read()
#image=Image.open(io.BytesIO(image))
#image=image.resize((224,224))
#image=np.asarray(image
def test2():
image_path='test/P0017.png'
image_ori=cv2.imread(image_path)
api = 'http://192.168.16.45:8000/detector'
img = cv2.imencode('.jpg', image_ori)[-1]
#image_encode=base64.b64encode(image_ori).decode('utf-8')
#img=bytes(image_encode,encoding='utf-8')
#h,w,c=image_ori.shape
files = {'file': img}
files = {'file': img,'name':'P0017'
}
res = requests.request("POST", api, files=files).json()
if res['msg'] == 'success':
bboxes = res['data']['bboxes']
print(bboxes)
#bboxes = np.asarray(bboxes, dtype=np.int32)
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return fp.read()
def test3():
image_path='test/P0017.png'
image_ori=cv2.imread(image_path)
api = 'http://192.168.16.45:8000/detector'
#img = cv2.imencode('.jpg', img_ori)[-1]
input_ ={
'img_data':'',
'img_width':512,
'img_height':512,
'img_chs':3
}
with open(image_path,'rb') as f:
input_['img_data']=base64.b64encode(f.read()).decode('utf-8')
image_encode=base64.b64encode(image_ori).decode('utf-8')
image_bytes=bytes(image_encode,encoding='utf-8')
#input_['img_data']=image_bytes
response=requests.post(api,json=input_).json()
print(response['msg'],response['data']['bboxes'])
###---客户端:读取图片:image_ori=cv2.imread(image_path)--->编码base64.b64encode(f.read()).decode('utf-8') ,(C,H,W)
###---服务器端字节化image_bytes=bytes(img_data,encoding='utf-8')-->解码img_data=np.frombuffer(base64.decodebytes(image_bytes),dtype=np.uint8)-->reshape:mg_data=img_data.reshape(h,w,3)
def test4():
image_path='test/P0017.png'
image_ori=cv2.imread(image_path)
api = 'http://192.168.16.45:8000/detector'
#img = cv2.imencode('.jpg', img_ori)[-1]
h,w,c = image_ori.shape
input_ ={
'img_data':'',
'img_width':h,
'img_height':w,
'img_chs':c
}
print('input:',input_)
##decode('utf-8'),有没有好像都行。
input_['img_data']=base64.b64encode(image_ori).decode('utf-8')
response=requests.post(api,json=input_).json()
print(response['msg'],response['data']['bboxes'])
def decode_encode():
##对文件b64编码b64解码存储
image_path='test/P0017.png'
with open(image_path,'rb') as fp:
encode_img = base64.b64encode(fp.read())
with open('t2.png','wb') as f2:
f2.write(base64.b64decode(encode_img))
##输入时数组-->opencv编码jpg字节流-->存储图片文件
image_path='test/P0017.png'
image_ori=cv2.imread(image_path)
image_bytes = cv2.imencode(".png",image_ori)[1].tobytes()
with open('bytes2image.png') as fp:
fp.write(image_bytes)
###字节流到数组用cv2.imdecode(np.frombuffer(image_bytes,np.uint8),1)
with open(image_path,'rb') as fp:
image_bytes2 = fp.read()
image_array = cv2.imdecode(np.frombuffer(image_bytes2,np.uint8),1)
##输入时数组-->opencv编码jpg字节流-->存储图片文件
image_path='test/P0017.png'
image_ori=cv2.imread(image_path)
image_bytes = cv2.imencode(".png",image_ori)[1].tobytes()
with open('bytes2image.png') as fp:
fp.write(image_bytes)
##image_array--
image_path='test/P0017.png'
image_ori=cv2.imread(image_path)
image_encode=base64.b64encode(image_ori).decode('utf-8')
image_bytes=bytes(image_encode,encoding='utf-8')
img_data=np.frombuffer(base64.decodebytes(image_bytes),dtype=np.uint8)
def test5():
import json
##输入时数组-->opencv编码jpg字节流-->存储图片文件
image_path='imgs/DJI_0445.JPG'
image_dir = '/home/thsw2/WJ/data/THexit/val/images/'
filelist = os.listdir(image_dir)
time0 = time.time()
for filename in filelist:
image_path = os.path.join(image_dir,filename)
image_ori=cv2.imread(image_path)
image_ori = cv2.resize(image_ori, (0, 0), fx=0.25, fy=0.25, interpolation=cv2.INTER_NEAREST)
image_pngcode = cv2.imencode(".jpg",image_ori)[-1]
api = 'http://192.168.10.10:8000/detector'
#api = 'http://47.98.157.120:9040/api/taskFile/submitUAVKHQuestion'
#api = 'http://192.168.0.100:9040'
h,w,c = image_ori.shape
input_ ={
'imgData':'',
#'img_width':h,
#'img_height':w,
#'img_chs':c
'imgName':filename
}
#print('input:',input_ )
t1 = time.time()
image_code = str(base64.b64encode(image_pngcode))[2:-1]
#print( image_code)
input_['imgData']=image_code
t2 = time.time()
response=requests.post(api,json=input_).json()
t3 = time.time()
print('bs encodetime:%.5f request time:%.5f \n'%(t2-t1,t3-t2))
t1_bytes = bytes(image_code,encoding='utf-8')
t2_bs64decode = base64.b64decode(t1_bytes)
img_data = cv2.imdecode(np.frombuffer(base64.b64decode( bytes(image_code,encoding='utf-8')),dtype=np.uint8),1)
#print(response['code'],response['data']['bboxes'])
print('Return:',response['data']['bboxes'],' img data shape:',img_data.shape)
time2 = time.time()
print('average time:',(time2-time0)/len(filelist))
if __name__=='__main__':
test5()