|
| 1 | +#include "common-clip.h" |
| 2 | + |
| 3 | +#include <cassert> |
| 4 | +#include <cstdlib> |
| 5 | +#include <cstring> |
| 6 | +#include <cmath> |
| 7 | +#include <iostream> |
| 8 | +#include <fstream> |
| 9 | +#include <vector> |
| 10 | +#include <map> |
| 11 | + |
| 12 | +#ifdef _WIN32 |
| 13 | +#include <windows.h> |
| 14 | +#include <tchar.h> |
| 15 | +#else |
| 16 | +#include <dirent.h> |
| 17 | +#include <sys/stat.h> |
| 18 | +#endif |
| 19 | + |
| 20 | +// common utility functions mainly intended for examples and debugging |
| 21 | + |
| 22 | +std::map<std::string, std::vector<std::string>> get_dir_keyed_files(const std::string &path, uint32_t max_files_per_dir = 0) |
| 23 | +{ |
| 24 | + std::map<std::string, std::vector<std::string>> result; |
| 25 | + |
| 26 | +#ifdef _WIN32 |
| 27 | + std::string wildcard = path + "\\*"; |
| 28 | + WIN32_FIND_DATAA fileData; |
| 29 | + HANDLE hFind = FindFirstFileA(wildcard.c_str(), &fileData); |
| 30 | + |
| 31 | + if (hFind == INVALID_HANDLE_VALUE) |
| 32 | + { |
| 33 | + std::cerr << "Failed to open directory: " << path << std::endl; |
| 34 | + return result; |
| 35 | + } |
| 36 | + |
| 37 | + uint32_t fileCount = 0; |
| 38 | + |
| 39 | + do |
| 40 | + { |
| 41 | + std::string name = fileData.cFileName; |
| 42 | + std::string fullPath = path + "\\" + name; |
| 43 | + |
| 44 | + if (fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) |
| 45 | + { |
| 46 | + // Skip . and .. |
| 47 | + if (name == "." || name == "..") |
| 48 | + continue; |
| 49 | + |
| 50 | + std::map<std::string, std::vector<std::string>> subResult = get_dir_keyed_files(fullPath, max_files_per_dir); |
| 51 | + result.insert(subResult.begin(), subResult.end()); |
| 52 | + } |
| 53 | + else |
| 54 | + { |
| 55 | + size_t pos = path.find_last_of("\\/"); |
| 56 | + std::string parentDir = (pos != std::string::npos) ? path.substr(pos + 1) : path; |
| 57 | + result[parentDir].push_back(fullPath); |
| 58 | + |
| 59 | + ++fileCount; |
| 60 | + if (max_files_per_dir > 0 && fileCount >= max_files_per_dir) |
| 61 | + break; |
| 62 | + } |
| 63 | + } while (FindNextFileA(hFind, &fileData)); |
| 64 | + |
| 65 | + FindClose(hFind); |
| 66 | +#else |
| 67 | + DIR *dir; |
| 68 | + struct dirent *entry; |
| 69 | + struct stat fileStat; |
| 70 | + |
| 71 | + if ((dir = opendir(path.c_str())) == NULL) |
| 72 | + { |
| 73 | + std::cerr << "Failed to open directory: " << path << std::endl; |
| 74 | + return result; |
| 75 | + } |
| 76 | + |
| 77 | + uint32_t fileCount = 0; |
| 78 | + |
| 79 | + while ((entry = readdir(dir)) != NULL) |
| 80 | + { |
| 81 | + std::string name = entry->d_name; |
| 82 | + std::string fullPath = path + "/" + name; |
| 83 | + |
| 84 | + if (stat(fullPath.c_str(), &fileStat) < 0) |
| 85 | + { |
| 86 | + std::cerr << "Failed to get file stat: " << fullPath << std::endl; |
| 87 | + continue; |
| 88 | + } |
| 89 | + |
| 90 | + if (S_ISDIR(fileStat.st_mode)) |
| 91 | + { |
| 92 | + // Skip . and .. |
| 93 | + if (name == "." || name == "..") |
| 94 | + continue; |
| 95 | + |
| 96 | + std::map<std::string, std::vector<std::string>> subResult = get_dir_keyed_files(fullPath, max_files_per_dir); |
| 97 | + result.insert(subResult.begin(), subResult.end()); |
| 98 | + } |
| 99 | + else |
| 100 | + { |
| 101 | + size_t pos = path.find_last_of("/"); |
| 102 | + std::string parentDir = (pos != std::string::npos) ? path.substr(pos + 1) : path; |
| 103 | + result[parentDir].push_back(fullPath); |
| 104 | + |
| 105 | + ++fileCount; |
| 106 | + if (max_files_per_dir > 0 && fileCount >= max_files_per_dir) |
| 107 | + break; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + closedir(dir); |
| 112 | +#endif |
| 113 | + |
| 114 | + return result; |
| 115 | +} |
| 116 | + |
| 117 | +bool app_params_parse(int argc, char **argv, app_params ¶ms) |
| 118 | +{ |
| 119 | + for (int i = 0; i < argc; i++) |
| 120 | + { |
| 121 | + std::string arg = std::string(argv[i]); |
| 122 | + if (arg == "-m" || arg == "--model") |
| 123 | + { |
| 124 | + params.model = argv[++i]; |
| 125 | + } |
| 126 | + else if (arg == "-t" || arg == "--threads") |
| 127 | + { |
| 128 | + params.n_threads = std::stoi(argv[++i]); |
| 129 | + } |
| 130 | + else if (arg == "--text") |
| 131 | + { |
| 132 | + params.texts.push_back(argv[++i]); |
| 133 | + } |
| 134 | + else if (arg == "--image") |
| 135 | + { |
| 136 | + params.image_paths.push_back(argv[++i]); |
| 137 | + } |
| 138 | + else if (arg == "-v" || arg == "--verbose") |
| 139 | + { |
| 140 | + params.verbose = std::stoi(argv[++i]); |
| 141 | + } |
| 142 | + else if (arg == "-h" || arg == "--help") |
| 143 | + { |
| 144 | + print_help(argc, argv, params); |
| 145 | + exit(0); |
| 146 | + } |
| 147 | + else |
| 148 | + { |
| 149 | + if (i != 0) |
| 150 | + { |
| 151 | + printf("%s: unrecognized argument: %s\n", __func__, arg.c_str()); |
| 152 | + return false; |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + return params.image_paths.size() >= 1 && params.texts.size() >= 1; |
| 157 | +} |
| 158 | + |
| 159 | +void print_help(int argc, char **argv, app_params ¶ms) |
| 160 | +{ |
| 161 | + printf("Usage: %s [options]\n", argv[0]); |
| 162 | + printf("\nOptions:"); |
| 163 | + printf(" -h, --help: Show this message and exit\n"); |
| 164 | + printf(" -m <path>, --model <path>: path to model. Default: %s\n", params.model.c_str()); |
| 165 | + printf(" -t N, --threads N: Number of threads to use for inference. Default: %d\n", params.n_threads); |
| 166 | + printf(" --text <text>: Text to encode. At least one text should be specified\n"); |
| 167 | + printf(" --image <path>: Path to an image file. At least one image path should be specified\n"); |
| 168 | + printf(" -v <level>, --verbose <level>: Control the level of verbosity. 0 = minimum, 2 = maximum. Default: %d\n", params.verbose); |
| 169 | +} |
| 170 | + |
| 171 | +void write_floats_to_file(float *array, int size, char *filename) |
| 172 | +{ |
| 173 | + // Open the file for writing. |
| 174 | + FILE *file = fopen(filename, "w"); |
| 175 | + if (file == NULL) |
| 176 | + { |
| 177 | + printf("Error opening file: %s\n", filename); |
| 178 | + return; |
| 179 | + } |
| 180 | + |
| 181 | + // Write the float values to the file. |
| 182 | + for (int i = 0; i < size; i++) |
| 183 | + { |
| 184 | + fprintf(file, "%f\n", array[i]); |
| 185 | + } |
| 186 | + |
| 187 | + // Close the file. |
| 188 | + fclose(file); |
| 189 | +} |
0 commit comments