-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
60 lines (44 loc) · 1.47 KB
/
client.py
File metadata and controls
60 lines (44 loc) · 1.47 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
#
# Cloud for ML Final Project
# Cole Smith
# client.py
#
import base64
import sys
from io import BytesIO
import requests
from PIL import Image
# Examples of input
# url = "http://192.168.99.102:32149"
# filepath = "n09835506_31225.JPEG"
# output path = "."
def decodePIL(s, outpath):
# Convert the blurred image base64 string to a RAW Python string
raw = "%r" % s
# Decode that raw base64 string to a BytesIO object
b = BytesIO(base64.b64decode(raw))
# Use PIL to open those image bytes
im = Image.open(b)
# Save that image to the output path (default response.jpg, or given by STDIN)
im.save(outpath)
return im
url = sys.argv[1]
filepath = sys.argv[2]
output_path = "./response.jpg"
if len(sys.argv) == 4:
output_path = sys.argv[3]
# Open the image file for which to send
with open(filepath, "rb") as fp:
# Read it in BYTES, encode those bytes in base64
encoded_img = base64.b64encode(fp.read())
# Package this into a JSON object, decode base64 bytes to UTF-8
data = {'img': encoded_img.decode("utf-8")}
# Send the POST request with JSON body to the server URL
# (provided from STDIN)
r = requests.post(url=url, json=data)
# Print the JSON keys we got back
print(r.json().keys())
print("Found:", r.json()['class'], "proba:", r.json()['probability'])
decodePIL(r.json()['blurredImage'], output_path)
decodePIL(r.json()['maskImage'], "mask.jpg")
decodePIL(r.json()['unblurredMask'], "origmask.jpg")