generated from cogniflow/object-detection-streamlit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcogniflow_utils.py
94 lines (81 loc) · 2.66 KB
/
cogniflow_utils.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import requests
import json
from time import sleep
def cogniflow_request(model_url, api_key, image_base64, image_format, attempt=3):
headers = {
'accept': 'application/json',
'Content-Type': 'application/json',
'x-api-key': api_key
}
data = {
"format": image_format,
"base64_image": image_base64
}
data_json = json.dumps(data)
while attempt > 0:
try:
response = requests.post(model_url, headers=headers, data=data_json)
result = response.json()
except Exception as ex:
attempt = attempt - 1
if attempt > 0:
print(f'Error trying to get cogniflow prediction endpoint. Retrying again in 3 seconds. '
f'Error: {str(ex)}')
sleep(3)
else:
raise ex
else:
return result
def cogniflow_request_audio(model_url, api_key, audio_base64, audio_format, audio_text, attempt=3):
headers = {
'accept': 'application/json',
'Content-Type': 'application/json',
'x-api-key': api_key
}
data = {
"format": audio_format,
"base64_audio": audio_base64,
"ground_truth": audio_text
}
data_json = json.dumps(data)
while attempt > 0:
try:
response = requests.post(model_url, headers=headers, data=data_json)
result = response.json()
except Exception as ex:
attempt = attempt - 1
if attempt > 0:
print(f'Error trying to get cogniflow prediction endpoint. Retrying again in 3 seconds. '
f'Error: {str(ex)}')
sleep(3)
else:
raise ex
else:
return result
def cogniflow_request_object(model_url, api_key, image_base64, image_format, attempt=3):
headers = {
'accept': 'application/json',
'Content-Type': 'application/json',
'x-api-key': api_key
}
data = {
"format": image_format,
"base64_image": image_base64,
"confidence_threshold": 0.2,
"normalize_boxes": False
}
data_json = json.dumps(data)
while attempt > 0:
try:
response = requests.post(model_url, headers=headers, data=data_json)
result = response.json()
except Exception as ex:
attempt = attempt - 1
if attempt > 0:
print(f'Error trying to get cogniflow prediction endpoint. Retrying again in 3 seconds. '
f'Error: {str(ex)}')
sleep(3)
else:
raise ex
else:
return result