2019-04-17 10:49:11 +08:00
|
|
|
//
|
2019-07-11 13:56:52 +08:00
|
|
|
// pictureRecognition.cpp
|
2019-04-17 10:49:11 +08:00
|
|
|
// MNN
|
|
|
|
|
//
|
|
|
|
|
// Created by MNN on 2018/05/14.
|
|
|
|
|
// Copyright © 2018, Alibaba Group Holding Limited
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2019-12-27 22:16:57 +08:00
|
|
|
#include <MNN/ImageProcess.hpp>
|
|
|
|
|
#include <MNN/Interpreter.hpp>
|
2019-04-17 10:49:11 +08:00
|
|
|
#define MNN_OPEN_TIME_TRACE
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
#include <fstream>
|
|
|
|
|
#include <functional>
|
2019-05-08 15:44:57 +08:00
|
|
|
#include <memory>
|
2019-04-17 10:49:11 +08:00
|
|
|
#include <sstream>
|
|
|
|
|
#include <vector>
|
2019-12-27 22:16:57 +08:00
|
|
|
#include <MNN/AutoTime.hpp>
|
2019-06-17 20:10:35 +08:00
|
|
|
#define STB_IMAGE_IMPLEMENTATION
|
|
|
|
|
#include "stb_image.h"
|
|
|
|
|
#include "stb_image_write.h"
|
2019-05-08 15:44:57 +08:00
|
|
|
|
2019-04-17 10:49:11 +08:00
|
|
|
using namespace MNN;
|
|
|
|
|
using namespace MNN::CV;
|
|
|
|
|
|
|
|
|
|
int main(int argc, const char* argv[]) {
|
2019-05-05 20:27:57 +08:00
|
|
|
if (argc < 3) {
|
|
|
|
|
MNN_PRINT("Usage: ./pictureTest.out model.mnn input.jpg [word.txt]\n");
|
2019-04-17 10:49:11 +08:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
std::shared_ptr<Interpreter> net(Interpreter::createFromFile(argv[1]));
|
|
|
|
|
ScheduleConfig config;
|
2019-06-17 20:10:35 +08:00
|
|
|
config.type = MNN_FORWARD_AUTO;
|
2020-03-22 20:16:29 +08:00
|
|
|
// BackendConfig bnconfig;
|
|
|
|
|
// bnconfig.precision = BackendConfig::Precision_Low;
|
|
|
|
|
// config.backendConfig = &bnconfig;
|
2019-04-17 10:49:11 +08:00
|
|
|
auto session = net->createSession(config);
|
|
|
|
|
|
2019-06-17 20:10:35 +08:00
|
|
|
auto input = net->getSessionInput(session, NULL);
|
|
|
|
|
auto shape = input->shape();
|
|
|
|
|
shape[0] = 1;
|
|
|
|
|
net->resizeTensor(input, shape);
|
|
|
|
|
net->resizeSession(session);
|
2019-04-17 10:49:11 +08:00
|
|
|
auto output = net->getSessionOutput(session, NULL);
|
2019-05-05 20:27:57 +08:00
|
|
|
std::vector<std::string> words;
|
|
|
|
|
if (argc >= 4) {
|
|
|
|
|
std::ifstream inputOs(argv[3]);
|
|
|
|
|
std::string line;
|
|
|
|
|
while (std::getline(inputOs, line)) {
|
|
|
|
|
words.emplace_back(line);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-17 10:49:11 +08:00
|
|
|
{
|
|
|
|
|
auto dims = input->shape();
|
|
|
|
|
int inputDim = 0;
|
|
|
|
|
int size_w = 0;
|
|
|
|
|
int size_h = 0;
|
|
|
|
|
int bpp = 0;
|
2019-05-05 20:27:57 +08:00
|
|
|
bpp = input->channel();
|
|
|
|
|
size_h = input->height();
|
|
|
|
|
size_w = input->width();
|
2019-04-17 10:49:11 +08:00
|
|
|
if (bpp == 0)
|
|
|
|
|
bpp = 1;
|
|
|
|
|
if (size_h == 0)
|
|
|
|
|
size_h = 1;
|
|
|
|
|
if (size_w == 0)
|
|
|
|
|
size_w = 1;
|
2019-05-05 20:27:57 +08:00
|
|
|
MNN_PRINT("input: w:%d , h:%d, bpp: %d\n", size_w, size_h, bpp);
|
2019-04-17 10:49:11 +08:00
|
|
|
|
2019-06-17 20:10:35 +08:00
|
|
|
auto inputPatch = argv[2];
|
|
|
|
|
int width, height, channel;
|
|
|
|
|
auto inputImage = stbi_load(inputPatch, &width, &height, &channel, 4);
|
|
|
|
|
if (nullptr == inputImage) {
|
|
|
|
|
MNN_ERROR("Can't open %s\n", inputPatch);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2019-05-05 20:27:57 +08:00
|
|
|
MNN_PRINT("origin size: %d, %d\n", width, height);
|
2019-04-17 10:49:11 +08:00
|
|
|
Matrix trans;
|
2019-07-11 13:56:52 +08:00
|
|
|
// Set transform, from dst scale to src, the ways below are both ok
|
|
|
|
|
#ifdef USE_MAP_POINT
|
|
|
|
|
float srcPoints[] = {
|
|
|
|
|
0.0f, 0.0f,
|
|
|
|
|
0.0f, (float)(height-1),
|
|
|
|
|
(float)(width-1), 0.0f,
|
|
|
|
|
(float)(width-1), (float)(height-1),
|
|
|
|
|
};
|
|
|
|
|
float dstPoints[] = {
|
|
|
|
|
0.0f, 0.0f,
|
|
|
|
|
0.0f, (float)(size_h-1),
|
|
|
|
|
(float)(size_w-1), 0.0f,
|
|
|
|
|
(float)(size_w-1), (float)(size_h-1),
|
|
|
|
|
};
|
|
|
|
|
trans.setPolyToPoly((Point*)dstPoints, (Point*)srcPoints, 4);
|
|
|
|
|
#else
|
2019-07-02 18:01:08 +08:00
|
|
|
trans.setScale((float)(width-1) / (size_w-1), (float)(height-1) / (size_h-1));
|
2019-07-11 13:56:52 +08:00
|
|
|
#endif
|
2019-04-17 10:49:11 +08:00
|
|
|
ImageProcess::Config config;
|
|
|
|
|
config.filterType = BILINEAR;
|
|
|
|
|
float mean[3] = {103.94f, 116.78f, 123.68f};
|
2019-06-17 20:10:35 +08:00
|
|
|
float normals[3] = {0.017f, 0.017f, 0.017f};
|
2019-07-02 18:01:08 +08:00
|
|
|
// float mean[3] = {127.5f, 127.5f, 127.5f};
|
|
|
|
|
// float normals[3] = {0.00785f, 0.00785f, 0.00785f};
|
|
|
|
|
::memcpy(config.mean, mean, sizeof(mean));
|
2019-04-17 10:49:11 +08:00
|
|
|
::memcpy(config.normal, normals, sizeof(normals));
|
|
|
|
|
config.sourceFormat = RGBA;
|
|
|
|
|
config.destFormat = BGR;
|
|
|
|
|
|
|
|
|
|
std::shared_ptr<ImageProcess> pretreat(ImageProcess::create(config));
|
|
|
|
|
pretreat->setMatrix(trans);
|
2019-06-17 20:10:35 +08:00
|
|
|
pretreat->convert((uint8_t*)inputImage, width, height, 0, input);
|
|
|
|
|
stbi_image_free(inputImage);
|
2019-04-17 10:49:11 +08:00
|
|
|
}
|
|
|
|
|
net->runSession(session);
|
|
|
|
|
{
|
2019-06-17 20:10:35 +08:00
|
|
|
auto dimType = output->getDimensionType();
|
|
|
|
|
if (output->getType().code != halide_type_float) {
|
|
|
|
|
dimType = Tensor::TENSORFLOW;
|
|
|
|
|
}
|
|
|
|
|
std::shared_ptr<Tensor> outputUser(new Tensor(output, dimType));
|
|
|
|
|
MNN_PRINT("output size:%d\n", outputUser->elementSize());
|
|
|
|
|
output->copyToHostTensor(outputUser.get());
|
|
|
|
|
auto type = outputUser->getType();
|
2019-04-17 10:49:11 +08:00
|
|
|
|
2019-06-17 20:10:35 +08:00
|
|
|
auto size = outputUser->elementSize();
|
2019-04-17 10:49:11 +08:00
|
|
|
std::vector<std::pair<int, float>> tempValues(size);
|
|
|
|
|
if (type.code == halide_type_float) {
|
2019-06-17 20:10:35 +08:00
|
|
|
auto values = outputUser->host<float>();
|
2019-04-17 10:49:11 +08:00
|
|
|
for (int i = 0; i < size; ++i) {
|
|
|
|
|
tempValues[i] = std::make_pair(i, values[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (type.code == halide_type_uint && type.bytes() == 1) {
|
2019-06-17 20:10:35 +08:00
|
|
|
auto values = outputUser->host<uint8_t>();
|
2019-04-17 10:49:11 +08:00
|
|
|
for (int i = 0; i < size; ++i) {
|
|
|
|
|
tempValues[i] = std::make_pair(i, values[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-11 11:23:31 +08:00
|
|
|
if (type.code == halide_type_int && type.bytes() == 1) {
|
|
|
|
|
auto values = outputUser->host<int8_t>();
|
|
|
|
|
for (int i = 0; i < size; ++i) {
|
|
|
|
|
tempValues[i] = std::make_pair(i, values[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-17 10:49:11 +08:00
|
|
|
// Find Max
|
|
|
|
|
std::sort(tempValues.begin(), tempValues.end(),
|
|
|
|
|
[](std::pair<int, float> a, std::pair<int, float> b) { return a.second > b.second; });
|
|
|
|
|
|
|
|
|
|
int length = size > 10 ? 10 : size;
|
2019-05-05 20:27:57 +08:00
|
|
|
if (words.empty()) {
|
|
|
|
|
for (int i = 0; i < length; ++i) {
|
|
|
|
|
MNN_PRINT("%d, %f\n", tempValues[i].first, tempValues[i].second);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
for (int i = 0; i < length; ++i) {
|
|
|
|
|
MNN_PRINT("%s: %f\n", words[tempValues[i].first].c_str(), tempValues[i].second);
|
|
|
|
|
}
|
2019-04-17 10:49:11 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|