Skip to content

Instantly share code, notes, and snippets.

@doleron
Last active January 24, 2022 12:20
Show Gist options
  • Save doleron/53ec8f00bc22e8e13704841bda16081d to your computer and use it in GitHub Desktop.
Save doleron/53ec8f00bc22e8e13704841bda16081d to your computer and use it in GitHub Desktop.
C++ format input
cv::Mat format_yolov5(const cv::Mat &source) {
// put the image in a square big enough
int col = source.cols;
int row = source.rows;
int _max = MAX(col, row);
cv::Mat resized = cv::Mat::zeros(_max, _max, CV_8UC3);
source.copyTo(resized(cv::Rect(0, 0, col, row)));
// resize to 640x640, normalize to [0,1[ and swap Red and Blue channels
cv::Mat result;
cv::dnn::blobFromImage(source, result, 1./255., cv::Size(INPUT_WIDTH, INPUT_HEIGHT), cv::Scalar(), true, false);
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment