OpenCV是计算机视觉库,Tensorflow是深度学习框架。 OpenCV包含了大量的图像处理和计算机视觉的算法,但是在机器学习方面明显不足,ML模块只有SVM,MLP, kNN等有限的几种算法。dnn模块也是调用别的框架。 Tensorflow是专为深度学习而生,可以方便的实现各种深度学习算法。 二者不属于同一领域,做视觉用OpenCV,做深度学习用Tensorflow。或者二者结合做图像识别等等。
一、版本配置要求
1. Windows 7 和 VS2015
2. OpenCV3.3.1以上,本人使用的OpenCV 3.4.1,下载地址:https://github.com/opencv/opencv/releases
3.至于TensoFlow,鄙人只是想测试已经训练好的模型,那就可以不安装TensoFlow了,偷懒一下~!
二、模型下载和配置
序号 | .pb文件:预训练模型 | .pbtxt文件 | 备注 |
1 | ssd_mobilenet_v1_coco_11_06_2017 | ssd_mobilenet_v1_coco.pbtxt | |
2 | ssd_inception_v2_coco_2017_11_17 | ssd_inception_v2_coco_2017_11_17.pbtxt | |
3 | |||
4 | |||
/** @brief Reads a network model stored in <a href="https://www.tensorflow.org/">TensorFlow</a> framework's format. * @param model path to the .pb file with binary protobuf description of the network architecture * @param config path to the .pbtxt file that contains text graph definition in protobuf format. * Resulting Net object is built by text graph using weights from a binary one that * let us make it more flexible. * @returns Net object. */ CV_EXPORTS_W Net readNetFromTensorflow(const String &model, const String &config = String());
#include<opencv2\opencv.hpp> #include<opencv2\dnn.hpp> #include <iostream> using namespace std; using namespace cv; const size_t inWidth = 300; const size_t inHeight = 300; const float WHRatio = inWidth / (float)inHeight; const char* classNames[] = { "background","face" };//这个需要根据训练的类别定义 int main() { Mat frame = cv::imread("2.jpg"); Size frame_size = frame.size(); String weights = "models\\ssd_inception_v2_coco_2017_11_17\\frozen_inference_graph.pb"; String prototxt = "models\\ssd_inception_v2_coco_2017_11_17.pbtxt"; dnn::Net net = cv::dnn::readNetFromTensorflow(weights, prototxt); Size cropSize; if (frame_size.width / (float)frame_size.height > WHRatio) { cropSize = Size(static_cast<int>(frame_size.height * WHRatio), frame_size.height); } else { cropSize = Size(frame_size.width, static_cast<int>(frame_size.width / WHRatio)); } Rect crop(Point((frame_size.width - cropSize.width) / 2, (frame_size.height - cropSize.height) / 2), cropSize); cv::Mat blob = cv::dnn::blobFromImage(frame, 1. / 255, Size(300, 300)); //cout << "blob size: " << blob.size << endl; net.setInput(blob); Mat output = net.forward(); //cout << "output size: " << output.size << endl; Mat detectionMat(output.size[2], output.size[3], CV_32F, output.ptr<float>()); frame = frame(crop); float confidenceThreshold = 0.20; for (int i = 0; i < detectionMat.rows; i++) { float confidence = detectionMat.at<float>(i, 2); if (confidence > confidenceThreshold) { size_t objectClass = (size_t)(detectionMat.at<float>(i, 1)); int xLeftBottom = static_cast<int>(detectionMat.at<float>(i, 3) * frame.cols); int yLeftBottom = static_cast<int>(detectionMat.at<float>(i, 4) * frame.rows); int xRightTop = static_cast<int>(detectionMat.at<float>(i, 5) * frame.cols); int yRightTop = static_cast<int>(detectionMat.at<float>(i, 6) * frame.rows); ostringstream ss; ss << confidence; String conf(ss.str()); Rect object((int)xLeftBottom, (int)yLeftBottom, (int)(xRightTop - xLeftBottom), (int)(yRightTop - yLeftBottom)); rectangle(frame, object, Scalar(0, 255, 0), 2); String label = String(classNames[objectClass]) + ": " + conf; int baseLine = 0; Size labelSize = getTextSize(label, FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine); rectangle(frame, Rect(Point(xLeftBottom, yLeftBottom - labelSize.height), Size(labelSize.width, labelSize.height + baseLine)), Scalar(0, 255, 0), CV_FILLED); putText(frame, label, Point(xLeftBottom, yLeftBottom), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 0)); } } imshow("image", frame); waitKey(0); return 0; }
四、常见的错误和坑
OpenCV(3.4.1) Error: Unspecified error (FAILED: fs.is_open(). Can't open "models\ssd_inception_v2_coco_2017_1117\frozen_inference_graph.pb") in cv::dnn::ReadProtoFromBinaryFile, file C:\build\master_winpack-build-win64-vc14\opencv\modules\dnn\src\caffe\caffe_io.cpp, line 1126
OpenCV(3.4.1) Error: Unspecified error (Const input blob for weights not found) in cv::dnn::experimental_dnn_v4::`anonymous-namespace'::TFImporter::getConstBlob, file C:\build\master_winpack-build-win64-vc14\opencv\modules\dnn\src\tensorflow\tf_importer.cpp, line 579
五、参考资料:
本文转载自:
https://blog.csdn.net/guyuealian/article/details/80570120

