Uh oh!
There was an error while loading. Please reload this page.
forked from jashwantraj92/cocktail
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpclient.py
More file actions
Latest commit
executable file
·68 lines (62 loc) · 2.78 KB
/
Copy pathhttpclient.py
File metadata and controls
executable file
·68 lines (62 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
importaiohttp
importasyncio
importasync_timeout
importrequests,json,sys,time
fromkeras.preprocessing.imageimportload_img
importrandom
importjson
importtime
fromos.pathimportabspath, dirname, join
frombase64importb64encode, b64decode
importpandasaspd
importrequests
importbase64,os
fromPILimportImage
importnumpyasnp
asyncdeffetch(session, url):
withasync_timeout.timeout(10):
asyncwithsession.get(url) asresponse:
returnawaitresponse.text()
asyncdefmain():
host=sys.argv[1]
port=sys.argv[2]
images=[]
forfilenameinos.listdir('/home/cc/val'):
images.append(filename)
foriinrange(int(sys.argv[3])):
filename=str(random.choice(images))
file="/home/cc/val/"+str(filename)
receive_time=time.time()
#file = tf.keras.utils.get_file("grace_hopper.jpg","https://storage.googleapis.com/download.tensorflow.org/example_images/grace_hopper.jpg")
#dl_request = requests.get("/home/cc/cocktail/CYAN/val/ILSVRC2010_val_00024988.JPEG", stream=True)
#dl_request.raise_for_status()
#with open(file, mode='rb') as file:
# img = file.read()
#img = load_img(file)
#base64_bytes = b64encode(img)
#base64_string = base64_bytes.decode('utf-8')
image=Image.open(file)
image=image.convert('RGB')
image=image.resize([224,224])
#data = {}
#data['img'] = base64.b64encode(img)
#jpeg_bytes = b64encode(img).decode('utf-8')
# data = "{'http://ec2-35-174-5-243.compute-1.amazonaws.com:8000/predict', 'data': '{\"data\": \"https://tensorflow.org/images/blogs/serving/cat.jpg\"}', 'timeout': 2, 'headers': '{\"Content-type\": \"application/json\"}'}"
ip="ec2-35-174-5-243.compute-1.amazonaws.com"
#data = json.dumps({'data':"https://tensorflow.org/images/blogs/serving/cat.jpg"})
data=json.dumps({'data':np.array(image).tolist(), 'file':filename})
#print(data)
asyncwithaiohttp.ClientSession() assession:
start_time=time.time()
resp=awaitsession.post(url=f'http://{host}:{port}/predict', data=data, headers={"Content-type": "application/json"})
print(f'the posted response is : {resp}')
ifresp.status==200:
r=awaitresp.json()
print("response is ",filename, r, "end time is ", time.time() -receive_time, time.time()-start_time, start_time)
"""requests.post(f'http://{host}:{port}/predict/',
headers={"Content-type": "application/json"},
data=json.dumps({
'data':"\"https://storage.googleapis.com/download.tensorflow.org/example_images/grace_hopper.jpg\""})
)"""
loop=asyncio.get_event_loop()
loop.run_until_complete(main())