-
Notifications
You must be signed in to change notification settings - Fork 0
/
local_image_input.py
49 lines (36 loc) · 1.2 KB
/
local_image_input.py
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
#!/usr/bin/env python
"""
Sample code to run image classification with local image as input
"""
import requests
import os
import sys
import base64
def imageToBase64(srcPath):
with open(srcPath, 'rb') as image:
b64Img = base64.b64encode(image.read()).decode('utf-8')
return b64Img
def infer():
# Get the API key for invoking Tiyaro API
apiKey = os.getenv("TIYARO_API_KEY")
if apiKey is None:
print("Please set TIYARO_API_KEY environment variable. You can generate your API key from here - https://console.tiyaro.ai/apikeys")
sys.exit(1)
# API endpoint
url = "https://api.tiyaro.ai/v1/ent/google/5/imagenet/resnet_v1_101/classification"
# Convert binary image to base64
imgPath = "../../testdata/object-detect-1.jpg"
b64Img = imageToBase64(imgPath)
payload = {"imageRef": {"Bytes": b64Img}}
headers = {
"accept": "*/*",
"content-type": "application/json",
"authorization": f"Bearer {apiKey}"
}
response = requests.request("POST", url, json=payload, headers=headers)
# Check for errors
response.raise_for_status()
# Inference response
print(response.text)
if __name__ == "__main__":
infer()