-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest_inference.py
More file actions
48 lines (37 loc) · 1.21 KB
/
Copy pathrequest_inference.py
File metadata and controls
48 lines (37 loc) · 1.21 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
from PIL import Image
import base64
from io import BytesIO
import requests
import os
def encode_image_to_base64(image_path):
# Open the image file
with Image.open(image_path) as image:
# Convert the image to a BytesIO object
image = image.convert('RGB')
buffered = BytesIO()
# Determine the format based on the file extension
ext = os.path.splitext(image_path)[1].lower()
if ext in ['.jpg', '.jpeg']:
format = 'JPEG'
else:
format = 'PNG'
# Save the image in the determined format
image.save(buffered, format=format)
# Encode the BytesIO object to a base64 string
img_str = base64.b64encode(buffered.getvalue()).decode('utf-8')
return img_str
# Path to your image
image_path = 'bittensor.jpg'
# Encode the image to base64
encoded_image = encode_image_to_base64(image_path)
# URL of the forward_image route
url = 'https://subnet-client.onrender.com/forward_image'
# JSON payload
payload = {
"image": encoded_image
}
# Send the POST request
response = requests.post(url, json=payload)
# Print the response
print(response.status_code)
print('prediction', response.json())