#include <fstream>
#include <iostream>
#include <string>
#include "MxBase/Log/Log.h"
#include "MxBase/MxBase.h"
using namespace MxBase;
namespace
{
constexpr int MAX_FILE_SIZE = 20 * 1024 * 1024;
constexpr int RESIZE_HEIGHT = 200;
constexpr int RESIZE_WIDTH = 200;
constexpr int CROP_SIZE = 200;
constexpr int EXPECTED_ARGC = 2;
constexpr int DECODE_ENCODE_BY_PATH = 1;
constexpr int DECODE_ENCODE_BY_PTR = 2;
constexpr int CROP_IMAGE_SYNC = 3;
constexpr int CROP_IMAGE_ASYNC = 4;
constexpr int RESIZE_IMAGE = 5;
}
APP_ERROR DecodeEncodeByPath(const std::string& inputPath, const std::string& outputPath, int deviceId)
{
ImageProcessor imageProcessor(deviceId);
Image decodedImage;
APP_ERROR ret = imageProcessor.Decode(inputPath, decodedImage, ImageFormat::YUV_SP_420);
if (ret != APP_ERR_OK)
{
LogError << "Decode image '" << inputPath << "' failed, error code: " << ret;
return ret;
}
LogInfo << "Image decoded successfully.";
ret = imageProcessor.Encode(decodedImage, outputPath);
if (ret != APP_ERR_OK)
{
LogError << "Saving image failed, error code: " << ret;
return ret;
}
LogInfo << "Output Image saved in '" << outputPath << "'";
return APP_ERR_OK;
}
APP_ERROR DecodeEncodeByPtr(const std::string& inputPath, const std::string& outputPath, int deviceId)
{
std::ifstream file(inputPath, std::ios::binary);
if (!file.is_open())
{
LogError << "Failed to open file: " << inputPath;
return 1;
}
file.seekg(0, std::ios::end);
std::streampos fileSize = file.tellg();
file.seekg(0, std::ios::beg);
if (fileSize >= MAX_FILE_SIZE)
{
LogError << "Max image size is " << MAX_FILE_SIZE << "B, but actual size is " << fileSize << "B";
return 1;
}
std::shared_ptr<uint8_t> dataPtr(new uint8_t[fileSize], [](uint8_t* p) { delete[] p; });
file.read(reinterpret_cast<char*>(dataPtr.get()), fileSize);
file.close();
ImageProcessor imageProcessor(deviceId);
Image decodedImage;
APP_ERROR ret = imageProcessor.Decode(dataPtr, fileSize, decodedImage, ImageFormat::YUV_SP_420);
if (ret != APP_ERR_OK)
{
LogError << "Decode image '" << inputPath << "' failed, error code: " << ret;
return ret;
}
LogInfo << "Image decoded successfully.";
ret = imageProcessor.Encode(decodedImage, outputPath);
if (ret != APP_ERR_OK)
{
LogError << "Saving image failed, error code: " << ret;
return ret;
}
LogInfo << "Output Image saved in '" << outputPath << "'";
return APP_ERR_OK;
}
APP_ERROR CropImage(const std::string& inputPath, const std::string& outputPath, int deviceId)
{
ImageProcessor imageProcessor(deviceId);
Image decodedImage;
APP_ERROR ret = imageProcessor.Decode(inputPath, decodedImage, ImageFormat::YUV_SP_420);
if (ret != APP_ERR_OK)
{
LogError << "Decode image '" << inputPath << "' failed, error code: " << ret;
return ret;
}
LogInfo << "Image decoded successfully.";
Image cropImage;
Rect cropRect{0, 0, CROP_SIZE, CROP_SIZE};
ret = imageProcessor.Crop(decodedImage, cropRect, cropImage);
if (ret != APP_ERR_OK)
{
LogError << "Crop image '" << inputPath << "' failed, error code: " << ret;
return ret;
}
ret = imageProcessor.Encode(cropImage, outputPath);
if (ret != APP_ERR_OK)
{
LogError << "Saving image failed, error code: " << ret;
return ret;
}
LogInfo << "Output Image saved in '" << outputPath << "'";
return APP_ERR_OK;
}
APP_ERROR CropImageAsync(const std::string& inputPath, const std::string& outputPath, int deviceId)
{
ImageProcessor imageProcessor(deviceId);
Image decodedImage;
APP_ERROR ret = imageProcessor.Decode(inputPath, decodedImage, ImageFormat::YUV_SP_420);
if (ret != APP_ERR_OK)
{
LogError << "Decode image '" << inputPath << "' failed, error code: " << ret;
return ret;
}
LogInfo << "Image decoded successfully.";
Image cropImage;
Rect cropRect{0, 0, CROP_SIZE, CROP_SIZE};
AscendStream stream(deviceId);
stream.CreateAscendStream();
ret = imageProcessor.Crop(decodedImage, cropRect, cropImage, stream);
if (ret != APP_ERR_OK)
{
LogError << "Crop image '" << inputPath << "' failed, error code: " << ret;
return ret;
}
stream.Synchronize();
ret = imageProcessor.Encode(cropImage, outputPath);
if (ret != APP_ERR_OK)
{
LogError << "Saving image failed, error code: " << ret;
return ret;
}
LogInfo << "Output Image saved in '" << outputPath << "'";
return APP_ERR_OK;
}
APP_ERROR ResizeImage(const std::string& inputPath, const std::string& outputPath, int deviceId)
{
ImageProcessor imageProcessor(deviceId);
Image decodedImage;
APP_ERROR ret = imageProcessor.Decode(inputPath, decodedImage, ImageFormat::YUV_SP_420);
if (ret != APP_ERR_OK)
{
LogError << "Decode image '" << inputPath << "' failed, error code: " << ret;
return ret;
}
LogInfo << "Image decoded successfully.";
Image resizedImage;
Size size(RESIZE_WIDTH, RESIZE_HEIGHT);
ret = imageProcessor.Resize(decodedImage, size, resizedImage, Interpolation::HUAWEI_HIGH_ORDER_FILTER);
if (ret != APP_ERR_OK)
{
LogError << "Resize image '" << inputPath << "' failed, error code: " << ret;
return ret;
}
ret = imageProcessor.Encode(resizedImage, outputPath);
if (ret != APP_ERR_OK)
{
LogError << "Saving image failed, error code: " << ret;
return ret;
}
LogInfo << "Output Image saved in '" << outputPath << "'";
return APP_ERR_OK;
}
int main(int argc, char* argv[])
{
if (argc != EXPECTED_ARGC)
{
LogError << "Only accept ONE parameter";
return -1;
}
int deviceId = 0;
std::string inputPath = "./input.jpg";
std::string outputPath = "./output.jpg";
APP_ERROR ret = MxInit();
if (ret != APP_ERR_OK)
{
LogError << "MxVision failed to initialize, error code:" << ret;
return ret;
}
if (strcmp(argv[1], "decode_path") == 0)
{
ret = DecodeEncodeByPath(inputPath, outputPath, deviceId);
}
else if (strcmp(argv[1], "decode_ptr") == 0)
{
ret = DecodeEncodeByPtr(inputPath, outputPath, deviceId);
}
else if (strcmp(argv[1], "crop") == 0)
{
ret = CropImage(inputPath, outputPath, deviceId);
}
else if (strcmp(argv[1], "crop_async") == 0)
{
ret = CropImageAsync(inputPath, outputPath, deviceId);
}
else if (strcmp(argv[1], "resize") == 0)
{
ret = ResizeImage(inputPath, outputPath, deviceId);
}
else
{
LogError << "Please enter command in ( decode_path, decode_ptr, crop, crop_async, resize )";
}
if (ret != APP_ERR_OK)
{
LogError << "sample execute failed";
}
MxDeInit();
return ret;
}