4
4
import tflite_runtime .interpreter as tflite
5
5
import numpy as np
6
6
from loguru import logger as logging
7
+ from typing import Tuple , List
7
8
8
9
9
10
def csv_2_bytearray (s : str ) -> bytearray :
@@ -32,7 +33,7 @@ def csv_2_bytearray(s: str) -> bytearray:
32
33
return r
33
34
34
35
35
- def get_grid_anchors (interpreter : tflite .Interpreter , grids : list [int ]) -> tuple [int , list ]:
36
+ def get_grid_anchors (interpreter : tflite .Interpreter , grids : List [int ]) -> Tuple [int , list ]:
36
37
"""
37
38
Retrieves grid anchors from the TensorFlow Lite interpreter.
38
39
@@ -116,8 +117,8 @@ def __arrayname_2_filename(self, array_name: str) -> str:
116
117
>>> self.__arrayname_2_filename('unsigned char ei_ei_addrmap_intm_txt[] = {')
117
118
'model/model_addrmap_intm.txt'
118
119
"""
119
- array_name = array_name .removeprefix ("unsigned char " )
120
- array_name = array_name .removeprefix ("ei_" )
120
+ array_name = array_name .replace ("unsigned char " , "" , 1 )
121
+ array_name = array_name .replace ("ei_" , "" , 1 )
121
122
array_name = array_name .replace ("ei_" , f"{ self .model_name } _" )
122
123
array_name = array_name .replace ("[]" , "" )
123
124
array_name = array_name .replace ("=" , "" )
@@ -170,7 +171,7 @@ def gen_drpai_model_files(self):
170
171
# Store it in the `var_list` dictionary.
171
172
line_sections = line .split (" " )
172
173
key = line_sections [- 3 ]
173
- value = line_sections [- 1 ].removesuffix ("\n " ). removesuffix (";" )
174
+ value = line_sections [- 1 ].replace ("\n " , "" ). replace (";" , " " )
174
175
self .var_list [key ] = value
175
176
if key .endswith ("_len" ):
176
177
# Ensure the length of the last array is correct.
@@ -187,7 +188,7 @@ def gen_drpai_model_files(self):
187
188
gc .collect ()
188
189
else :
189
190
# The C array has not ended yet, so convert hex values to binary and append.
190
- self .var_list [output_file_path ] += csv_2_bytearray (line .removesuffix ("\n " ))
191
+ self .var_list [output_file_path ] += csv_2_bytearray (line .replace ("\n " , " " ))
191
192
192
193
def read_variables (self ):
193
194
"""
@@ -224,7 +225,7 @@ def read_variables(self):
224
225
if len (line_sections ) >= 2 :
225
226
# The line probably looks like `type name;`, `type * name;`, `type *name;` or `} name;`
226
227
# We only care about the last word which is a name.
227
- name = line_sections [- 1 ].removeprefix ("*" ). removesuffix (";" )
228
+ name = line_sections [- 1 ].replace ("*" , "" ). replace (";" , " " )
228
229
if line_sections [0 ] == "}" :
229
230
# The struct definition is finished
230
231
# Let's add all the variable names to the dictionary with a prefix of the struct name.
@@ -239,7 +240,7 @@ def read_variables(self):
239
240
file_path = f"{ self .working_directory } /model-parameters/model_variables.h"
240
241
logging .info ("Reading file: " + file_path )
241
242
struct_variables = list () # A list of variable names when they are grouped in a structure
242
- with ( open (file_path , "rt" ) as f ) :
243
+ with open (file_path , "rt" ) as f :
243
244
# Read the C header file line by line.
244
245
for line in f :
245
246
# By splitting the line into words, we can look for C language keywords
@@ -255,7 +256,7 @@ def read_variables(self):
255
256
if "[]" in line :
256
257
# It is defining an array
257
258
# Let's extract its contents into a list and save it in the `var_list`
258
- name = line_sections [line_sections .index ("=" )- 1 ].removesuffix ("[]" )
259
+ name = line_sections [line_sections .index ("=" )- 1 ].replace ("[]" , " " )
259
260
value = line [line .find ("{" )+ 1 : line .find ("}" )]
260
261
value = value .replace ("\" " , "" ).replace (" " , "" )
261
262
self .var_list [name ] = value .split ("," )
@@ -280,13 +281,13 @@ def read_variables(self):
280
281
if line_sections [0 ] == "const" :
281
282
# Delete the const keyword
282
283
del line_sections [0 ]
283
- self .var_list [line_sections [1 ]] = line_sections [- 1 ].removesuffix (";" )
284
+ self .var_list [line_sections [1 ]] = line_sections [- 1 ].replace (";" , " " )
284
285
else :
285
286
# We are in the middle of a struct initialization.
286
287
# The order of values match the order of variable names at the declaration time.
287
288
# Variable names are already selected in `struct_variables`.
288
289
# Let's assign values to the first item and remove. It will eventually become empty.
289
- value = line_sections [0 ].removesuffix ("," ). removeprefix ("\" " ). removesuffix ("\" " )
290
+ value = line_sections [0 ].replace ("," , "" ). replace ("\" " , "" ). replace ("\" " , "" )
290
291
self .var_list [struct_variables [0 ]] = value
291
292
del struct_variables [0 ]
292
293
@@ -408,7 +409,7 @@ def gen_postprocess_params_txt(self):
408
409
model_version = "5"
409
410
410
411
# Retrieve and validate the IoU threshold from var_list dictionary
411
- iou_threshold = self .var_list ["ei_object_detection_nms_config_t_iou_threshold" ]. removesuffix ( "f" )
412
+ iou_threshold = self .var_list ["ei_object_detection_nms_config_t_iou_threshold" ][: - 1 ]
412
413
assert float (iou_threshold ) >= 0 , \
413
414
"The loaded model_variables.h doesn't have the required output iou_threshold."
414
415
0 commit comments