The first method OpenCV, requests, flask
This method is time consuming about 600 milliseconds
Client code
#Coding:utf-8ImportCv2ImportJSONImportrequestsimg= Cv2.imread ("/home/aqonvs.jpg") Res= {"Image": Str (img.tolist ()). Encode ('Base64')}#img is Ndarray, can not be directly encoded with Base64, otherwise it will error_ = Requests.post ("http://192.71.30.172:8081", Data=json.dumps (RES))
Server-side code
#Coding:utf-8 fromFlaskImportrequest, FlaskImportJSONImportNumPy as NPImport TimeImportCv2app= Flask (__name__) @app. Route ("/", methods=['POST'])defget_frame (): Start_time=time.time () Res=json.loads (request.data) frame= eval (res["Image"].decode ("Base64"))#Dtype to Int32frame = Np.array (frame, dtype=np.uint8) Cv2.imwrite ('/home/tmp.jpg', frame) duration= Time.time ()-start_timePrint('DURATION:[%.0FMS]'% (duration*1000)) return '0000'if __name__=="__main__": App.run ("192.71.30.172", port=8081)#Port is 8081
The second direct-use file transfer time is within 10 milliseconds
Client code
#Coding:utf-8ImportRequestsurl="xxxxx"str000='/home/aqonvs.jpg'newname= Str000.split ('/')PrintNewname[len (newname)-1]files= {'file':(Newname,open ('/home/aqonvs.jpg','RB'),'image/jpg')}r= Requests.post (Url,files =files) Result=R.textPrintResult
Server-side code
#Coding:utf-8 fromFlaskImportrequest, FlaskImport TimeImportOsapp= Flask (__name__) @app. Route ("/", methods=['POST'])defget_frame ():Start_time =time.time () upload_file= request.files['file'] Old_file_name=Upload_file.filenameifUpload_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_timePrint('DURATION:[%.0FMS]'% (duration*1000)) return 'Success' Else: return 'failed'if __name__=="__main__":App.run ("127.0.0.1", port=5000)
Two ways to send and receive pictures using Python---include client and server-side code