def write_cc_file(clip_filename, cc_filename, headerfiles, header_template_file, array_name, sampling_rate_value, mono_value, offset_value, duration_value, res_type_value, min_len): print(f"++ Converting {clip_filename} to {path.basename(cc_filename)}") with open(cc_filename, "w") as f: # Write the headers string, note write_license_header(f, header_template_file) write_autogen_comment(f, path.basename(__file__), path.basename(clip_filename)) write_includes(f, headerfiles) clip_data, samplerate = prepare_audio_clip( path.join(args.audio_folder_path, clip_filename), sampling_rate_value, mono_value, offset_value, duration_value, res_type_value, min_len) clip_data = (((clip_data + 1) / 2) * (2**16 - 1) - 2**15).flatten().astype(np.int16) # Convert the audio and write it to the cc file feature_vec_define = f"const int16_t {array_name} [{len(clip_data)}] IFM_BUF_ATTRIBUTE = " f.write(feature_vec_define) write_hex_array(f, clip_data) return len(clip_data)
def main(args): # Get the labels from text file labels = [] with open(args.labels_file, "r") as f: labels = f.readlines() # No labels? if len(labels) == 0: raise Exception(f"no labels found in {args.label_file}") # write this list to file with open(args.output_file, "w", newline="") as f: # Write license header write_license_header(f, args.license_template) # Create a list of writable items line = namedtuple("line", ['string', 'lf_cnt']) write_autogen_comment(f, path.basename(__file__), path.basename(args.labels_file)) # For the include guard (only if output is a header file) if is_a_header_path((path.basename(args.output_file))): f.write("#pragma once\n\n") # Description if provided: if args.desc is not None: f.write(args.desc + "\n\n") # includes write_includes(f, ["<vector>", "<string>"]) # initialisation: strvec = f"static std::vector <std::string> {args.vector_name} = " +\ list_to_cpp_vec_list_initialiser(labels, 4) f.write(strvec + "\n\n") # getter function getter_fn = f"""bool GetLabelsVector(std::vector<std::string>& labels) {{ labels = {args.vector_name}; return true; }}\n """ f.write(getter_fn)
def main(args): if not os.path.isfile(args.tflite_path): raise Exception(f"{args.tflite_path} not found") # Cpp filename: cpp_filename = Path( os.path.join(args.output_dir, os.path.basename(args.tflite_path) + ".cc")).absolute() print(f"++ Converting {os.path.basename(args.tflite_path)} to\ {os.path.basename(cpp_filename)}") os.makedirs(cpp_filename.parent, exist_ok=True) # Write file here with open(cpp_filename, "w") as f: write_license_header(f, args.license_template) write_autogen_comment(f, os.path.basename(__file__), os.path.basename(args.tflite_path)) write_model(f, tflite_path=args.tflite_path)
def write_hpp_file(header_file_path, header_template_file, num_audios, audio_filenames, audio_array_namesizes): with open(header_file_path, "w") as f: write_license_header(f, header_template_file) write_autogen_comment(f, path.basename(__file__), path.basename(header_file_path)) header_guard = "\n#ifndef GENERATED_AUDIOCLIPS_H\n#define GENERATED_AUDIOCLIPS_H\n\n" f.write(header_guard) write_includes(f, ['<cstdint>']) define_number_audios = "\n#define NUMBER_OF_AUDIOCLIPS (" + str( num_audios) + "U)\n" f.write(define_number_audios) start_filenames_vector = "static const char *audio_clip_filenames[] = {" f.write(start_filenames_vector) files_list = ['\n "' + item + '"' for item in audio_filenames] filenames_list = ', '.join(files_list) f.write(filenames_list) f.write('\n};\n\n') extern_declarations = [ f"extern const int16_t {arr_name[0]}[{arr_name[1]}];\n" for arr_name in audio_array_namesizes ] f.writelines(extern_declarations) f.write('\n') imgarr_names_vector = 'static const int16_t *audio_clip_arrays[] = {\n ' +\ (',\n ').join(f"{arr_name[0]}" for arr_name in audio_array_namesizes) + '\n};\n\n' f.write(imgarr_names_vector) end_header_guard = "\n#endif // GENERATED_AUDIOCLIPS_H\n" f.write(end_header_guard)
def write_hpp_file(header_file_path, header_template_file, num_images, image_filenames, image_array_names, image_size): with open(header_file_path, "w") as f: write_license_header(f, header_template_file) write_autogen_comment(f, path.basename(__file__), path.basename(header_file_path)) header_guard = "\n#ifndef GENERATED_IMAGES_H\n#define GENERATED_IMAGES_H\n\n" f.write(header_guard) write_includes(f, ['<cstdint>']) define_number_images = "\n#define NUMBER_OF_IMAGES ("+ str(num_images) +"U)\n" f.write(define_number_images) define_imsize = '\n#define IMAGE_DATA_SIZE (' +\ str(image_size[0] * image_size[1] * 3) + 'U)\n\n' f.write(define_imsize) start_filenames_vector = "static const char *img_filenames[] = {" f.write(start_filenames_vector) files_list = ['\n "' + item + '"' for item in image_filenames] filenames_list = ', '.join(files_list) f.write(filenames_list) f.write('\n};\n\n') extern_declarations = [f"extern const uint8_t {arr_name}[];\n" for arr_name in image_array_names] f.writelines(extern_declarations) f.write('\n') imgarr_names_vector = 'static const uint8_t *img_arrays[] = {\n ' +\ (',\n ').join(image_array_names) + '\n};\n\n' f.write(imgarr_names_vector) end_header_guard = "\n#endif // GENERATED_IMAGES_H\n" f.write(end_header_guard)
def write_cc_file(image_filename, cc_filename, headerfiles, header_template_file, original_image, image_size, array_name): print(f"++ Converting {image_filename} to {path.basename(cc_filename)}") with open(cc_filename, "w") as f: # Write the headers string, note write_license_header(f, header_template_file) write_autogen_comment(f, path.basename(__file__), path.basename(image_filename)) write_includes(f, headerfiles) # Resize the image, in order to avoid skew the image # the new size is calculated maintaining the aspect ratio and then pad on just one axis if necessary original_image.thumbnail(image_size) delta_w = abs(image_size[0] - original_image.size[0]) delta_h = abs(image_size[1] - original_image.size[1]) resized_image = Image.new('RGB', args.image_size, (255, 255, 255, 0)) resized_image.paste(original_image, (int(delta_w / 2), int(delta_h / 2))) # Convert the image and write it to the cc file rgb_data = np.array(resized_image, dtype=np.uint8).flatten() feature_vec_define = "const uint8_t "+ array_name + "[] IFM_BUF_ATTRIBUTE = " f.write(feature_vec_define) write_hex_array(f, rgb_data)