AisDeployC
Deploy Library for Artificial Intelligence System
Loading...
Searching...
No Matches
interface.py
Go to the documentation of this file.
1# -*- coding: UTF-8 -*-
2
3import ctypes
4import json
5import platform
6
7AisDeployCVersion="v0.3.3b"
8
9class AisDeployC():
10 """
11 AisDeployC python接口类 AisDeployC
12
13 """
14 def __init__(self, lib_pth: str):
15 """! The AisDeployC class initializer.
16 @see
17 示例代码如下
18 @code
19 lib_path = "./cmake-build-release/AisDeployC.dll"
20 deploy_obj = AisDeployC(lib_path)
21 @endcode
22 @param lib_pth AisDeployC library path.
23 @return An instance of the AisDeployC class initialized.
24 """
25
26 if "macOS" in platform.platform():
27 self.lib = ctypes.cdll.LoadLibrary(lib_pth)
28 elif "Windows" in platform.platform():
29 self.lib = ctypes.windll.LoadLibrary(lib_pth)
30 elif "Linux" in platform.platform():
31 self.lib = ctypes.cdll.LoadLibrary(lib_pth)
32 self.handle = None
33
34 def __del__(self):
35 """! The AisDeployC class delete function.
36 """
37 if self.handle != None:
38 self.lib.release.argtypes = [ctypes.c_void_p]
39 self.lib.release( self.handle )
40
41 def model_initialize(self, path_str: str, gpu_id: int):
42 """! The AisDeployC class model initializer.
43 @see
44 示例代码如下
45 @code
46 path_str = "tests/assets/models/det_setting_oen.aism"
47 gpu_id = 0
48 ret = deploy_obj.model_initialize(path_str, gpu_id)
49 @endcode
50 @param path_str model file path.
51 @param gpu_id gpu id for model initialize
52 @return model_initialize result, 0 for success, 1 for failure.
53 """
54
55 self.lib.initialize.restype = ctypes.c_void_p
56 ret = ctypes.c_int(-1)
57 gpu_id = ctypes.c_int(gpu_id)
58
59 path_char = ctypes.c_char_p(path_str.encode('utf-8'))
60 self.handle = self.lib.initialize(path_char, gpu_id, ctypes.pointer(ret))
61
62 self.lib.update_license.argtypes = [ctypes.c_void_p, ctypes.c_char_p]
63 self.lib.generate_license.argtypes = [ctypes.c_void_p]
64 self.lib.py_process_json_str.argtypes = [ctypes.c_void_p, ctypes.c_char_p, ctypes.c_int]
65 self.lib.py_get_json_str_results.argtypes = [ctypes.c_void_p, ctypes.POINTER(ctypes.c_char_p), ctypes.POINTER(ctypes.c_int)]
66 self.lib.py_free_result.argtypes = [ctypes.c_char_p]
67 self.lib.release.argtypes = [ctypes.c_void_p]
68 self.lib.py_load_keys_embeddings.argtypes = [ctypes.c_void_p, ctypes.c_char_p, ctypes.c_int]
69 self.lib.py_load_keys_embeddings.restype = ctypes.c_int
70 self.lib.py_compare_with_ground_embeddings.argtypes = [ctypes.c_void_p, ctypes.c_char_p, ctypes.c_int, ctypes.POINTER(ctypes.c_char_p), ctypes.POINTER(ctypes.c_int)]
71 self.lib.py_process_decoder.argtypes = [ctypes.c_void_p, ctypes.c_char_p, ctypes.c_int, ctypes.POINTER(ctypes.c_char_p), ctypes.POINTER(ctypes.c_int)]
72
73 return ret.value
74
76 """! The AisDeployC class license generate function.
77 @see
78 示例代码如下
79 @code
80 ret = deploy_obj.generate_license()
81 @endcode
82 @return generate_license result, 0 for success, 1 for failure.
83 """
84 return self.lib.generate_license(self.handle)
85
86 def update_license(self, path_str: str):
87 """! The AisDeployC class license update function.
88 @see
89 示例代码如下
90 @code
91 license_path = "tests/assets/licenses/registed/windows_registed_info.aisl"
92 ret = deploy_obj.update_license(license_path)
93 @endcode
94
95 @param path_str license file path.
96 @return update_license result, 0 for success, 1 for failure.
97 """
98 path_char = ctypes.c_char_p(path_str.encode('utf-8'))
99 return self.lib.update_license(self.handle, path_char)
100
101 def process(self, input_json: dict):
102 """! The AisDeployC class process function.
103 @see
104 batch=1 示例代码如下
105 @code
106 import base64
107 import json
108
109 imgPth = "tests/assets/images/1.jpg"
110 f= open(imgPth, 'rb')
111 qrcode = base64.b64encode(f.read()).decode()
112 f.close()
113 file_json = {"type": "base64", "data": qrcode, "ch":3}
114 input_json = {"data_list": [file_json]}
115
116 ret_val = deploy_obj.process(input_json)
117 @endcode
118 @see
119 batch=2 示例代码如下
120 @code
121 import base64
122 import json
123
124 imgPth = "tests/assets/images/1.jpg"
125 f= open(imgPth, 'rb')
126 qrcode = base64.b64encode(f.read()).decode()
127 f.close()
128 file_json = {"type": "base64", "data": qrcode, "ch":3}
129 input_json = {"data_list": [file_json]}
130
131 imgPth = "tests/assets/images/63_1024.jpg"
132 f= open(imgPth, 'rb')
133 qrcode = base64.b64encode(f.read()).decode()
134 f.close()
135 file_json = {"type": "base64", "data": qrcode, "ch":3}
136 input_json["data_list"].append(file_json)
137
138 ret_val = deploy_obj.process(input_json)
139 @endcode
140
141 @param input_json input json like dict.
142 @return process result, output_json output json like dict. for success, None for failure.
143 """
144 if self.handle is None:
145 print("[ERROR] Check handle failed in AisDeployC.process. maybe lack initialization.")
146 return None
147 data_str = json.dumps(input_json)
148 data_char = ctypes.c_char_p(data_str.encode('utf-8'))
149 ret = self.lib.py_process_json_str(self.handle, data_char, len(data_str))
150 if ret != 0:
151 print("[ERROR] py_process_json_str failed in AisDeployC. maybe check input of process.")
152 return None
153 ret_char_c = ctypes.c_char_p()
154 ret = self.lib.py_get_json_str_results( self.handle, ctypes.pointer(ret_char_c), None )
155 if ret != 0:
156 print("[ERROR] py_get_json_str_results failed in AisDeployC. maybe lack py_process_json_str.")
157 return None
158 ret_str = ret_char_c.value.decode("utf-8")
159 ret_value = json.loads(ret_str)
160 self.lib.py_free_result(ret_char_c)
161 return ret_value
162
163 def process_decoder(self, input_json: dict):
164 if self.handle is None:
165 print("[ERROR] Check handle failed in AisDeployC.process_decoder. maybe lack initialization.")
166 return None
167 data_str = json.dumps(input_json)
168 data_char = ctypes.c_char_p(data_str.encode('utf-8'))
169 ret_char_c = ctypes.c_char_p()
170 ret = self.lib.py_process_decoder(self.handle, data_char, len(data_str),ctypes.pointer(ret_char_c), None)
171 if ret != 0:
172 print("[ERROR] py_process_decoder failed in AisDeployC. maybe check input of process_decoder.")
173 return None
174 ret_str = ret_char_c.value.decode("utf-8")
175 ret_value = json.loads(ret_str)
176 self.lib.py_free_result(ret_char_c)
177 return ret_value
178
179 def load_keys_embeddings(self, input_json: dict):
180 """! The AisDeployC class load_keys_embeddings function.
181 @see
182 batch=1 示例代码如下
183 @code
184 import json
185 key = ""
186 value = list()
187 file_json = {"key": key, "embedding_vector":value}
188 input_json = {"data_list": [file_json]}
189
190 ret_val = deploy_obj.load_keys_embeddings(input_json)
191 @endcode
192
193 @param input_json input json like dict.
194 @return process result, 0 for success, 1 for failure.
195 """
196 if self.handle is None:
197 print("[ERROR] Check handle failed in AisDeployC.process. maybe lack initialization.")
198 return None
199
200 data_str = json.dumps(input_json)
201 data_char = ctypes.c_char_p(data_str.encode('utf-8'))
202
203 ret = self.lib.py_load_keys_embeddings(self.handle, data_char, len(data_str))
204 if ret != 0:
205 print("[ERROR] load_keys_embeddings failed in AisDeployC. maybe check input of process.")
206 return None
207 return ret
208
209 def compare_with_ground_embeddings(self, input_json: dict):
210 """! The AisDeployC class compare_with_ground_embeddings function.
211 @see
212 示例代码如下
213 @code
214 import json
215 value = list()
216 file_json = {"embedding_vector":value}
217 input_json = {"data_list": [file_json]}
218
219 ret_val = deploy_obj.process(input_json)
220 @endcode
221
222 @param input_json input json like dict.
223 @return process result, output_json output json like dict. for success, None for failure.
224 """
225 if self.handle is None:
226 print("[ERROR] Check handle failed in AisDeployC.process. maybe lack initialization.")
227 return None
228 data_str = json.dumps(input_json)
229 data_char = ctypes.c_char_p(data_str.encode('utf-8'))
230 ret_char_c = ctypes.c_char_p()
231 ret = self.lib.py_compare_with_ground_embeddings(self.handle, data_char, len(data_str), ctypes.pointer(ret_char_c), None )
232 if ret != 0:
233 print("[ERROR] compare_with_ground_embeddings failed in AisDeployC. maybe check input of process.")
234 return None
235 ret_str = ret_char_c.value.decode("utf-8")
236 ret_value = json.loads(ret_str)
237 self.lib.py_free_result(ret_char_c)
238 return ret_value
process(self, dict input_json)
The AisDeployC class process function.
Definition interface.py:101
update_license(self, str path_str)
The AisDeployC class license update function.
Definition interface.py:86
__init__(self, str lib_pth)
The AisDeployC class initializer.
Definition interface.py:14
load_keys_embeddings(self, dict input_json)
The AisDeployC class load_keys_embeddings function.
Definition interface.py:179
compare_with_ground_embeddings(self, dict input_json)
The AisDeployC class compare_with_ground_embeddings function.
Definition interface.py:209
process_decoder(self, dict input_json)
Definition interface.py:163
__del__(self)
The AisDeployC class delete function.
Definition interface.py:34
generate_license(self)
The AisDeployC class license generate function.
Definition interface.py:75
model_initialize(self, str path_str, int gpu_id)
The AisDeployC class model initializer.
Definition interface.py:41
AisDeployC_API int py_get_json_str_results(void *base, char **output, int *output_size)
python使用的获取json string输出格式的接口
AisDeployC_API int release(void *base)
释放实例
AisDeployC_API int py_free_result(char *output)
python使用的释放返回结果的接口
AisDeployC_API int py_compare_with_ground_embeddings(void *base, const char *input, int input_size, char **output, int *output_size)
python使用的批量 字段 和 特征嵌入 对,与底库中特征嵌入做比对,json string输入格式的接口,json string输出格式的接口
AisDeployC_API int py_process_json_str(void *base, const char *input, int input_size)
python使用的处理json string输入格式的接口
AisDeployC_API void * initialize(const char *model_path, int gpu_id, int *state)
模型初始化
AisDeployC_API int py_process_decoder(void *base, const char *input, int input_size, char **output, int *output_size)
python使用的解码器处理接口,输入特征嵌入和提示prompt,json string输入格式的接口,json string输出格式的接口