Spaces:
Running
Running
File size: 787 Bytes
84cd834 | 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 | import urllib.request
import io
import json
from PIL import Image
import numpy as np
img = Image.fromarray(np.random.randint(50, 200, (224, 224), dtype=np.uint8), 'L')
buf = io.BytesIO()
img.save(buf, 'PNG')
img_bytes = buf.getvalue()
boundary = 'boundary123'
header1 = b'--boundary123\r\nContent-Disposition: form-data; name="file"; filename="test.png"\r\nContent-Type: image/png\r\n\r\n'
header2 = b'\r\n--boundary123\r\nContent-Disposition: form-data; name="model_name"\r\n\r\nBaseline\r\n--boundary123--'
body = header1 + img_bytes + header2
req = urllib.request.Request(
'http://localhost:8000/classify',
data=body,
headers={'Content-Type': 'multipart/form-data; boundary=boundary123'},
method='POST'
)
r = urllib.request.urlopen(req)
print(json.loads(r.read()))
|