Commit aea4edb6c9856baaef7a5e5a7e67bc5236969f60
1 parent
23bc5efd
Deal with the ctypes pointer bullshit so users don't need to
Showing
1 changed file
with
111 additions
and
35 deletions
scripts/brpy/__init__.py
| ... | ... | @@ -16,20 +16,23 @@ def _string_args(n): |
| 16 | 16 | ''' |
| 17 | 17 | Returns n char* |
| 18 | 18 | ''' |
| 19 | - s = [] | |
| 20 | - for i in range(n): | |
| 21 | - s.append(c_char_p) | |
| 22 | - return s | |
| 19 | + return [c_char_p]*n | |
| 23 | 20 | |
| 24 | -def _var_string_args(n): | |
| 21 | +def _var_string_args_func(func, n, *args): | |
| 25 | 22 | ''' |
| 26 | - Returns one int and one char** followed by n char* | |
| 23 | + Translates the C functions that are | |
| 24 | + (int, char**, n char*, and optional extra types in *args) | |
| 25 | + to a more pythonic version that wraps the char** arg in | |
| 26 | + the appropriate type. | |
| 27 | 27 | ''' |
| 28 | - s = [c_int, POINTER(c_char_p)] | |
| 29 | - s.extend(_string_args(n)) | |
| 30 | - return s | |
| 28 | + def call_func(one, two, *rest): | |
| 29 | + arr_type = c_char_p*len(two) | |
| 30 | + func.argtypes = [c_int, arr_type] + _string_args(n) + args | |
| 31 | + arr = arr_type(*map(c_char_p, two)) | |
| 32 | + return func(one, arr, *rest) | |
| 33 | + return call_func | |
| 31 | 34 | |
| 32 | -def _handle_string_func(func): | |
| 35 | +def _handle_string_func(func, *moretypes): | |
| 33 | 36 | ''' |
| 34 | 37 | A helper function to make the C functions |
| 35 | 38 | that populate string buffers more pythonic. |
| ... | ... | @@ -42,6 +45,7 @@ def _handle_string_func(func): |
| 42 | 45 | Then it returns the string itself. This way, the user of brpy |
| 43 | 46 | doesn't need to do this stupid magic for themselves. Hooray! |
| 44 | 47 | ''' |
| 48 | + func.argtypes = [c_char_p, c_int] + list(moretypes) | |
| 45 | 49 | def call_func(*args): |
| 46 | 50 | howlong = func('', 0, *args) |
| 47 | 51 | msg = 'x'*(howlong-1) |
| ... | ... | @@ -73,107 +77,179 @@ def init_brpy(br_loc='/usr/local/lib'): |
| 73 | 77 | if not found: |
| 74 | 78 | raise ValueError('Neither .so nor .dylib libopenbr found in %s' % br_loc) |
| 75 | 79 | |
| 76 | - plot_args = _var_string_args(1) + [c_bool] | |
| 77 | 80 | br.br_about.restype = c_char_p |
| 78 | - br.br_cat.argtypes = _var_string_args(1) | |
| 79 | - br.br_cluster.argtypes = [c_int, POINTER(c_char_p), c_float, c_char_p] | |
| 80 | - br.br_combine_masks.argtypes = _var_string_args(2) | |
| 81 | + | |
| 82 | + br.br_cat = _var_string_args_func(br.br_cat, 1) | |
| 83 | + | |
| 84 | + br.br_cluster = _var_string_args_func(br.br_cluster, 0, c_float, c_char_p) | |
| 85 | + | |
| 86 | + br.br_combine_masks = _var_string_args_func(br.br_combine_masks, 2) | |
| 87 | + | |
| 81 | 88 | br.br_compare.argtypes = _string_args(3) |
| 82 | - br.br_compare_n.argtypes = [c_int, POINTER(c_char_p)] + _string_args(2) | |
| 89 | + | |
| 90 | + br.br_compare_n = _var_string_args_func(br.br_compare_n, 2) | |
| 91 | + | |
| 83 | 92 | br.br_pairwise_compare.argtypes = _string_args(3) |
| 93 | + | |
| 84 | 94 | br.br_convert.argtypes = _string_args(3) |
| 95 | + | |
| 85 | 96 | br.br_enroll.argtypes = _string_args(2) |
| 86 | - br.br_enroll_n.argtypes = _var_string_args(1) | |
| 97 | + | |
| 98 | + br.br_enroll_n = _var_string_args_func(br.br_enroll_n, 1) | |
| 99 | + | |
| 87 | 100 | br.br_eval.argtypes = _string_args(3) |
| 88 | 101 | br.br_eval.restype = c_float |
| 102 | + | |
| 89 | 103 | br.br_eval_classification.argtypes = _string_args(4) |
| 104 | + | |
| 90 | 105 | br.br_eval_clustering.argtypes = _string_args(3) |
| 106 | + | |
| 91 | 107 | br.br_eval_detection.argtypes = _string_args(3) |
| 92 | 108 | br.br_eval_detection.restype = c_float |
| 109 | + | |
| 93 | 110 | br.br_eval_landmarking.argtypes = _string_args(3) + [c_int, c_int] |
| 94 | 111 | br.br_eval_landmarking.restype = c_float |
| 112 | + | |
| 95 | 113 | br.br_eval_regression.argtypes = _string_args(4) |
| 96 | - br.br_fuse.argtypes = _var_string_args(3) | |
| 97 | - br.br_initialize.argtypes = _var_string_args(1) | |
| 114 | + | |
| 115 | + br.br_fuse = _var_string_args_func(br.br_fuse, 3) | |
| 116 | + | |
| 117 | + def br_initialize_wrap(func): | |
| 118 | + def call_func(argc, argv, sdk_path='', use_gui=False): | |
| 119 | + arr_type = c_char_p*len(argv) | |
| 120 | + func.argtypes = [POINTER(c_int),arr_type,c_char_p,c_bool] | |
| 121 | + arr = arr_type(*map(c_char_p, argv)) | |
| 122 | + return func(pointer(c_int(argc)), arr, sdk_path, use_gui) | |
| 123 | + return call_func | |
| 124 | + br.br_initialize = br_initialize_wrap(br.br_initialize) | |
| 125 | + | |
| 98 | 126 | br.br_is_classifier.argtypes = [c_char_p] |
| 99 | 127 | br.br_is_classifier.restype = c_bool |
| 128 | + | |
| 100 | 129 | br.br_make_mask.argtypes = _string_args(3) |
| 130 | + | |
| 101 | 131 | br.br_make_pairwise_mask.argtypes = _string_args(3) |
| 102 | - br.br_most_recent_message.argtypes = [c_char_p, c_int] | |
| 132 | + | |
| 103 | 133 | br.br_most_recent_message.restype = c_int |
| 104 | - func = br.br_most_recent_message.__call__ | |
| 105 | - br.br_most_recent_message = _handle_string_func(func) | |
| 106 | - br.br_objects.argtypes = [c_char_p, c_int] + _string_args(2) + [c_bool] | |
| 134 | + br.br_most_recent_message = _handle_string_func(br.br_most_recent_message) | |
| 135 | + | |
| 107 | 136 | br.br_objects.restype = c_int |
| 108 | - func2 = br.br_objects.__call__ | |
| 109 | - br.br_objects = _handle_string_func(func2) | |
| 110 | - br.br_plot.argtypes = plot_args | |
| 137 | + moreargs = _string_args(2) + [c_bool] | |
| 138 | + br.br_objects = _handle_string_func(br.br_objects, *moreargs) | |
| 139 | + | |
| 111 | 140 | br.br_plot.restype = c_bool |
| 112 | - br.br_plot_detection.argtypes = plot_args | |
| 141 | + br.br_plot = _var_string_args_func(br.br_plot, 1, c_bool) | |
| 142 | + | |
| 113 | 143 | br.br_plot_detection.restype = c_bool |
| 114 | - br.br_plot_landmarking.argtypes = plot_args | |
| 144 | + br.br_plot_detection = _var_string_args_func(br.br_plot_detection, 1, c_bool) | |
| 145 | + | |
| 115 | 146 | br.br_plot_landmarking.restype = c_bool |
| 116 | - br.br_plot_metadata.argtypes = plot_args | |
| 147 | + br.br_plot_landmarking = _var_string_args_func(br.br_plot_landmarking, 1, c_bool) | |
| 148 | + | |
| 117 | 149 | br.br_plot_metadata.restype = c_bool |
| 150 | + br.br_plot_metadata = _var_string_args_func(br.br_plot_metadata, 1, c_bool) | |
| 151 | + | |
| 118 | 152 | br.br_progress.restype = c_float |
| 153 | + | |
| 154 | + # TODO: ??? how do *** ??? | |
| 119 | 155 | br.br_read_pipe.argtypes = [c_char_p, POINTER(c_int), POINTER(POINTER(c_char_p))] |
| 156 | + | |
| 120 | 157 | br.br_scratch_path.argtypes = [c_char_p, c_int] |
| 121 | 158 | br.br_scratch_path.restype = c_int |
| 122 | - func3 = br.br_scratch_path.__call__ | |
| 123 | - br.br_scratch_path = _handle_string_func(func3) | |
| 159 | + br.br_scratch_path = _handle_string_func(br.br_scratch_path) | |
| 160 | + | |
| 124 | 161 | br.br_sdk_path.restype = c_char_p |
| 162 | + | |
| 163 | + def br_get_header_wrap(func): | |
| 164 | + def call_func(matrix, target_gallery, query_gallery): | |
| 165 | + arr_type1 = c_char_p*len(target_gallery) | |
| 166 | + arr_type2 = c_char_p*len(query_gallery) | |
| 167 | + func.argtypes = [c_char_p,arr_type1,arr_type2] | |
| 168 | + arr1 = arr_type1(*map(c_char_p, target_gallery)) | |
| 169 | + arr2 = arr_type2(*map(c_char_p, query_gallery)) | |
| 170 | + return func(matrix, arr1, arr2) | |
| 171 | + return call_func | |
| 125 | 172 | br.br_get_header.argtypes = [c_char_p, POINTER(c_char_p), POINTER(c_char_p)] |
| 173 | + | |
| 126 | 174 | br.br_set_header.argtypes = _string_args(3) |
| 175 | + | |
| 127 | 176 | br.br_set_property.argtypes = _string_args(2) |
| 177 | + | |
| 128 | 178 | br.br_time_remaining.restype = c_int |
| 179 | + | |
| 129 | 180 | br.br_train.argtypes = _string_args(2) |
| 130 | - br.br_train_n.argtypes = _var_string_args(1) | |
| 181 | + | |
| 182 | + br.br_train_n = _var_string_args_func(br.br_train_n, 1) | |
| 183 | + | |
| 131 | 184 | br.br_version.restype = c_char_p |
| 185 | + | |
| 132 | 186 | br.br_slave_process.argtypes = [c_char_p] |
| 187 | + | |
| 133 | 188 | br.br_load_img.argtypes = [c_char_p, c_int] |
| 134 | 189 | br.br_load_img.restype = c_void_p |
| 190 | + | |
| 135 | 191 | br.br_unload_img.argtypes = [c_void_p] |
| 136 | 192 | br.br_unload_img.restype = POINTER(c_ubyte) |
| 193 | + | |
| 137 | 194 | br.br_template_list_from_buffer.argtypes = [c_char_p, c_int] |
| 138 | 195 | br.br_template_list_from_buffer.restype = c_void_p |
| 196 | + | |
| 139 | 197 | br.br_free_template.argtypes = [c_void_p] |
| 198 | + | |
| 140 | 199 | br.br_free_template_list.argtypes = [c_void_p] |
| 200 | + | |
| 141 | 201 | br.br_free_output.argtypes = [c_void_p] |
| 202 | + | |
| 142 | 203 | br.br_img_rows.argtypes = [c_void_p] |
| 143 | 204 | br.br_img_rows.restype = c_int |
| 205 | + | |
| 144 | 206 | br.br_img_cols.argtypes = [c_void_p] |
| 145 | 207 | br.br_img_cols.restype = c_int |
| 208 | + | |
| 146 | 209 | br.br_img_channels.argtypes = [c_void_p] |
| 147 | 210 | br.br_img_channels.restype = c_int |
| 211 | + | |
| 148 | 212 | br.br_img_is_empty.argtypes = [c_void_p] |
| 149 | 213 | br.br_img_is_empty.restype = c_bool |
| 214 | + | |
| 150 | 215 | br.br_get_filename.argtypes = [c_char_p, c_int, c_void_p] |
| 151 | 216 | br.br_get_filename.restype = c_int |
| 152 | - func4 = br.br_get_filename.__call__ | |
| 153 | - br.br_get_filename = _handle_string_func(func4) | |
| 217 | + br.br_get_filename = _handle_string_func(br.br_get_filename) | |
| 218 | + | |
| 154 | 219 | br.br_set_filename.argtypes = [c_void_p, c_char_p] |
| 220 | + | |
| 155 | 221 | br.br_get_metadata_string.argtypes = [c_char_p, c_int, c_void_p, c_char_p] |
| 156 | 222 | br.br_get_metadata_string.restype = c_int |
| 157 | - func5 = br.br_get_metadata_string.__call__ | |
| 158 | - br.br_get_metadata_string = _handle_string_func(func5) | |
| 223 | + br.br_get_metadata_string = _handle_string_func(br.br_get_metadata_string) | |
| 224 | + | |
| 159 | 225 | br.br_enroll_template.argtypes = [c_void_p] |
| 160 | 226 | br.br_enroll_template.restype = c_void_p |
| 227 | + | |
| 161 | 228 | br.br_enroll_template_list.argtypes = [c_void_p] |
| 162 | 229 | br.br_enroll_template_list.restype = c_void_p |
| 230 | + | |
| 163 | 231 | br.br_compare_template_lists.argtypes = [c_void_p, c_void_p] |
| 164 | 232 | br.br_compare_template_lists.restype = c_void_p |
| 233 | + | |
| 165 | 234 | br.br_get_matrix_output_at.argtypes = [c_void_p, c_int, c_int] |
| 166 | 235 | br.br_get_matrix_output_at.restype = c_float |
| 236 | + | |
| 167 | 237 | br.br_get_template.argtypes = [c_void_p, c_int] |
| 168 | 238 | br.br_get_template.restype = c_void_p |
| 239 | + | |
| 169 | 240 | br.br_num_templates.argtypes = [c_void_p] |
| 170 | 241 | br.br_num_templates.restype = c_int |
| 242 | + | |
| 171 | 243 | br.br_make_gallery.argtypes = [c_char_p] |
| 172 | 244 | br.br_make_gallery.restype = c_void_p |
| 245 | + | |
| 173 | 246 | br.br_load_from_gallery.argtypes = [c_void_p] |
| 174 | 247 | br.br_load_from_gallery.restype = c_void_p |
| 248 | + | |
| 175 | 249 | br.br_add_template_to_gallery.argtypes = [c_void_p, c_void_p] |
| 250 | + | |
| 176 | 251 | br.br_add_template_list_to_gallery.argtypes = [c_void_p, c_void_p] |
| 252 | + | |
| 177 | 253 | br.br_close_gallery.argtypes = [c_void_p] |
| 178 | 254 | |
| 179 | 255 | return br | ... | ... |