Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

193 lines
6.2KB

  1. from PIL import Image
  2. import numpy as np
  3. import cv2
  4. import base64
  5. import io,os
  6. import requests
  7. import time
  8. def test():
  9. image_path='test/P0017.png'
  10. image_array=cv2.imread(image_path)
  11. print(image_array.shape)
  12. image_encode=base64.b64encode(image_array).decode('utf-8')
  13. image_bytes=bytes(image_encode,encoding='utf-8')
  14. image_decode=np.frombuffer(base64.decodebytes(image_bytes),dtype=np.uint8)
  15. print(image_decode.shape)
  16. request_url='http://192.168.109.49:5000/'
  17. headers={'content-type':'application/json'}
  18. data={'image':image_encode}
  19. #response=requests.post(request_url,data=data,headers=headers)
  20. response=requests.post(request_url,"POST",files=data)
  21. print(response)
  22. #image=open(image_path,'rb').read()
  23. #image=Image.open(io.BytesIO(image))
  24. #image=image.resize((224,224))
  25. #image=np.asarray(image
  26. def test2():
  27. image_path='test/P0017.png'
  28. image_ori=cv2.imread(image_path)
  29. api = 'http://192.168.16.45:8000/detector'
  30. img = cv2.imencode('.jpg', image_ori)[-1]
  31. #image_encode=base64.b64encode(image_ori).decode('utf-8')
  32. #img=bytes(image_encode,encoding='utf-8')
  33. #h,w,c=image_ori.shape
  34. files = {'file': img}
  35. files = {'file': img,'name':'P0017'
  36. }
  37. res = requests.request("POST", api, files=files).json()
  38. if res['msg'] == 'success':
  39. bboxes = res['data']['bboxes']
  40. print(bboxes)
  41. #bboxes = np.asarray(bboxes, dtype=np.int32)
  42. def get_file_content(filePath):
  43. with open(filePath, 'rb') as fp:
  44. return fp.read()
  45. def test3():
  46. image_path='test/P0017.png'
  47. image_ori=cv2.imread(image_path)
  48. api = 'http://192.168.16.45:8000/detector'
  49. #img = cv2.imencode('.jpg', img_ori)[-1]
  50. input_ ={
  51. 'img_data':'',
  52. 'img_width':512,
  53. 'img_height':512,
  54. 'img_chs':3
  55. }
  56. with open(image_path,'rb') as f:
  57. input_['img_data']=base64.b64encode(f.read()).decode('utf-8')
  58. image_encode=base64.b64encode(image_ori).decode('utf-8')
  59. image_bytes=bytes(image_encode,encoding='utf-8')
  60. #input_['img_data']=image_bytes
  61. response=requests.post(api,json=input_).json()
  62. print(response['msg'],response['data']['bboxes'])
  63. ###---客户端:读取图片:image_ori=cv2.imread(image_path)--->编码base64.b64encode(f.read()).decode('utf-8') ,(C,H,W)
  64. ###---服务器端:字节化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)
  65. def test4():
  66. image_path='test/P0017.png'
  67. image_ori=cv2.imread(image_path)
  68. api = 'http://192.168.16.45:8000/detector'
  69. #img = cv2.imencode('.jpg', img_ori)[-1]
  70. h,w,c = image_ori.shape
  71. input_ ={
  72. 'img_data':'',
  73. 'img_width':h,
  74. 'img_height':w,
  75. 'img_chs':c
  76. }
  77. print('input:',input_)
  78. ##decode('utf-8'),有没有好像都行。
  79. input_['img_data']=base64.b64encode(image_ori).decode('utf-8')
  80. response=requests.post(api,json=input_).json()
  81. print(response['msg'],response['data']['bboxes'])
  82. def decode_encode():
  83. ##对文件b64编码,b64解码存储
  84. image_path='test/P0017.png'
  85. with open(image_path,'rb') as fp:
  86. encode_img = base64.b64encode(fp.read())
  87. with open('t2.png','wb') as f2:
  88. f2.write(base64.b64decode(encode_img))
  89. ##输入时数组-->opencv编码jpg字节流-->存储图片文件
  90. image_path='test/P0017.png'
  91. image_ori=cv2.imread(image_path)
  92. image_bytes = cv2.imencode(".png",image_ori)[1].tobytes()
  93. with open('bytes2image.png') as fp:
  94. fp.write(image_bytes)
  95. ###字节流到数组用cv2.imdecode(np.frombuffer(image_bytes,np.uint8),1)
  96. with open(image_path,'rb') as fp:
  97. image_bytes2 = fp.read()
  98. image_array = cv2.imdecode(np.frombuffer(image_bytes2,np.uint8),1)
  99. ##输入时数组-->opencv编码jpg字节流-->存储图片文件
  100. image_path='test/P0017.png'
  101. image_ori=cv2.imread(image_path)
  102. image_bytes = cv2.imencode(".png",image_ori)[1].tobytes()
  103. with open('bytes2image.png') as fp:
  104. fp.write(image_bytes)
  105. ##image_array--
  106. image_path='test/P0017.png'
  107. image_ori=cv2.imread(image_path)
  108. image_encode=base64.b64encode(image_ori).decode('utf-8')
  109. image_bytes=bytes(image_encode,encoding='utf-8')
  110. img_data=np.frombuffer(base64.decodebytes(image_bytes),dtype=np.uint8)
  111. def test5():
  112. import json
  113. ##输入时数组-->opencv编码jpg字节流-->存储图片文件
  114. image_path='imgs/DJI_0445.JPG'
  115. image_dir = '/home/thsw2/WJ/data/THexit/val/images/'
  116. filelist = os.listdir(image_dir)
  117. time0 = time.time()
  118. for filename in filelist:
  119. image_path = os.path.join(image_dir,filename)
  120. image_ori=cv2.imread(image_path)
  121. image_ori = cv2.resize(image_ori, (0, 0), fx=0.25, fy=0.25, interpolation=cv2.INTER_NEAREST)
  122. image_pngcode = cv2.imencode(".jpg",image_ori)[-1]
  123. api = 'http://192.168.10.10:8000/detector'
  124. #api = 'http://47.98.157.120:9040/api/taskFile/submitUAVKHQuestion'
  125. #api = 'http://192.168.0.100:9040'
  126. h,w,c = image_ori.shape
  127. input_ ={
  128. 'imgData':'',
  129. #'img_width':h,
  130. #'img_height':w,
  131. #'img_chs':c,
  132. 'imgName':filename
  133. }
  134. #print('input:',input_ )
  135. t1 = time.time()
  136. image_code = str(base64.b64encode(image_pngcode))[2:-1]
  137. #print( image_code)
  138. input_['imgData']=image_code
  139. t2 = time.time()
  140. response=requests.post(api,json=input_).json()
  141. t3 = time.time()
  142. print('bs encodetime:%.5f request time:%.5f \n'%(t2-t1,t3-t2))
  143. t1_bytes = bytes(image_code,encoding='utf-8')
  144. t2_bs64decode = base64.b64decode(t1_bytes)
  145. img_data = cv2.imdecode(np.frombuffer(base64.b64decode( bytes(image_code,encoding='utf-8')),dtype=np.uint8),1)
  146. #print(response['code'],response['data']['bboxes'])
  147. print('Return:',response['data']['bboxes'],' img data shape:',img_data.shape)
  148. time2 = time.time()
  149. print('average time:',(time2-time0)/len(filelist))
  150. if __name__=='__main__':
  151. test5()