| 138 | } |
| 139 | |
| 140 | void Helper::preprocessInput(MNN::CV::ImageProcess* pretreat, PreprocessConfig preprocessConfig, const std::string& filename, MNN::Tensor* input, InputType inputType, MNN::Express::VARP var) { |
| 141 | if (inputType == InputType::IMAGE) { |
| 142 | int originalWidth, originalHeight, comp; |
| 143 | auto bitmap32bits = stbi_load(filename.c_str(), &originalWidth, &originalHeight, &comp, 4); |
| 144 | |
| 145 | DCHECK(bitmap32bits != nullptr) << "input image error!"; |
| 146 | |
| 147 | const int hCropSize = int(originalHeight * preprocessConfig.centerCropHeight); |
| 148 | const int wCropSize = int(originalWidth * preprocessConfig.centerCropWidth); |
| 149 | MNN_ASSERT(hCropSize > 0 && wCropSize > 0); |
| 150 | // default center crop |
| 151 | int startH = (originalHeight - hCropSize) / 2; |
| 152 | int startW = (originalWidth - wCropSize) / 2; |
| 153 | |
| 154 | const int endH = startH + hCropSize; |
| 155 | const int endW = startW + wCropSize; |
| 156 | |
| 157 | float srcPoints[] = { |
| 158 | float(startW), float(startH), |
| 159 | float(startW), float(endH - 1), |
| 160 | float(endW - 1), float(startH), |
| 161 | float(endW - 1), float(endH - 1), |
| 162 | }; |
| 163 | const int oh = preprocessConfig.targetHeight; |
| 164 | const int ow = preprocessConfig.targetWidth; |
| 165 | |
| 166 | float dstPoints[] = { |
| 167 | 0.0f, 0.0f, |
| 168 | 0.0f, float(oh - 1), |
| 169 | float(ow - 1), 0.0f, |
| 170 | float(ow - 1), float(oh - 1), |
| 171 | }; |
| 172 | MNN::CV::Matrix trans; |
| 173 | trans.setPolyToPoly((MNN::CV::Point*)dstPoints, (MNN::CV::Point*)srcPoints, 4); |
| 174 | |
| 175 | pretreat->setMatrix(trans); |
| 176 | pretreat->convert(bitmap32bits, originalWidth, originalHeight, 0, input); |
| 177 | |
| 178 | stbi_image_free(bitmap32bits); |
| 179 | } |
| 180 | if (inputType == InputType::SEQUENCE) { |
| 181 | if (!stringEndWith(filename, ".txt")) { |
| 182 | MNN_ERROR("Error: only '.txt' files are supported for sequence input.\n"); |
| 183 | return; |
| 184 | } |
| 185 | |
| 186 | std::ifstream f(filename); |
| 187 | if (!f.is_open()) { |
| 188 | MNN_ERROR("open file %s failed.\n", filename.c_str()); |
| 189 | return; |
| 190 | } |
| 191 | |
| 192 | std::string line; |
| 193 | std::vector<std::vector<float> > rawData; |
| 194 | while (std::getline(f, line)) { |
| 195 | std::stringstream ss(line); |
| 196 | float v; |
| 197 | std::vector<float> lineData; |
nothing calls this directly
no test coverage detected