| 61 | } |
| 62 | |
| 63 | std::vector<Array<float>> CvMatToOpInput::createArray( |
| 64 | const Matrix& inputData, const std::vector<double>& scaleInputToNetInputs, |
| 65 | const std::vector<Point<int>>& netInputSizes) |
| 66 | { |
| 67 | try |
| 68 | { |
| 69 | // Sanity checks |
| 70 | if (inputData.empty()) |
| 71 | error("Wrong input element (empty inputData).", __LINE__, __FUNCTION__, __FILE__); |
| 72 | if (inputData.channels() != 3) |
| 73 | error("Input images must be 3-channel BGR.", __LINE__, __FUNCTION__, __FILE__); |
| 74 | if (scaleInputToNetInputs.size() != netInputSizes.size()) |
| 75 | error("scaleInputToNetInputs.size() != netInputSizes.size().", __LINE__, __FUNCTION__, __FILE__); |
| 76 | // inputNetData - Reescale keeping aspect ratio and transform to float the input deep net image |
| 77 | const auto numberScales = (int)scaleInputToNetInputs.size(); |
| 78 | std::vector<Array<float>> inputNetData(numberScales); |
| 79 | cv::Mat cvInputData = OP_OP2CVCONSTMAT(inputData); |
| 80 | for (auto i = 0u ; i < inputNetData.size() ; i++) |
| 81 | { |
| 82 | // CPU version (faster if #Gpus <= 3 and relatively small images) |
| 83 | if (!mGpuResize) |
| 84 | { |
| 85 | cv::Mat frameWithNetSize; |
| 86 | resizeFixedAspectRatio(frameWithNetSize, cvInputData, scaleInputToNetInputs[i], netInputSizes[i]); |
| 87 | // Fill inputNetData[i] |
| 88 | inputNetData[i].reset({1, 3, netInputSizes.at(i).y, netInputSizes.at(i).x}); |
| 89 | uCharCvMatToFloatPtr( |
| 90 | inputNetData[i].getPtr(), OP_CV2OPMAT(frameWithNetSize), |
| 91 | (mPoseModel == PoseModel::BODY_19N ? 2 : 1)); |
| 92 | |
| 93 | // // OpenCV equivalent |
| 94 | // const auto scale = 1/255.; |
| 95 | // const cv::Scalar mean{128,128,128}; |
| 96 | // const cv::Size outputSize{netInputSizes[i].x, netInputSizes[i].y}; |
| 97 | // // cv::Mat cvMat; |
| 98 | // cv::dnn::blobFromImage( |
| 99 | // // frameWithNetSize, cvMat, scale, outputSize, mean); |
| 100 | // frameWithNetSize, inputNetData[i].getCvMat(), scale, outputSize, mean); |
| 101 | // // opLog(cv::norm(cvMat - inputNetData[i].getCvMat())); // ~0.25 |
| 102 | } |
| 103 | // CUDA version (if #Gpus > n) |
| 104 | else |
| 105 | { |
| 106 | // Note: This version reduces the global accuracy about 0.1%, so it is disabled for now |
| 107 | error("This version reduces the global accuracy about 0.1%, so it is disabled for now.", |
| 108 | __LINE__, __FUNCTION__, __FILE__); |
| 109 | #ifdef USE_CUDA |
| 110 | // (Re)Allocate temporary memory |
| 111 | const unsigned int inputImageSize = 3 * cvInputData.rows * cvInputData.cols; |
| 112 | const unsigned int outputImageSize = 3 * netInputSizes[i].x * netInputSizes[i].y; |
| 113 | if (pInputMaxSize < inputImageSize) |
| 114 | { |
| 115 | pInputMaxSize = inputImageSize; |
| 116 | // Free temporary memory |
| 117 | cudaFree(pInputImageCuda); |
| 118 | cudaFree(pInputImageReorderedCuda); |
| 119 | // Re-allocate memory |
| 120 | cudaMalloc((void**)&pInputImageCuda, sizeof(unsigned char) * inputImageSize); |