OpenCV4入门教程138:使用图像分类模型实现图像分类

索引地址:系列索引

加载训练好的图像分类模型,然后进行推断,输出图片中可能的目标。

这个是darknet的一张测试图片:

src

使用的是googlenet Caffe模型。

一般需要三个文件:

  • 模型参数:bvlc_googlenet.prototxt -> https://github.com/opencv/opencv_extra/blob/master/testdata/dnn/bvlc_googlenet.prototxt
  • 模型配置文件即模型框架:bvlc_googlenet.caffemodel -> http://dl.caffe.berkeleyvision.org/
  • ImageNet标签文件:classification_classes_ILSVRC2012.txt -> https://github.com/opencv/opencv/tree/master/data

测试代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
//use opencv_dnn module for image classification by using GoogLeNet trained network
#include
#include
#include
#include

using namespace cv;
using namespace cv::dnn;
using namespace std;
String modelTxt = "bvlc_googlenet.prototxt";
String modelBin = "bvlc_googlenet.caffemodel";
String labelFile = "classification_classes_ILSVRC2012.txt";

vector readClasslabels();

int main(int argc, char** argv) {
Mat testImage = imread("dog.jpg");
if (testImage.empty()) {
printf("could not load image...\n");
return -1;
}
// create googlenet with caffemodel text and bin
Net net = dnn::readNetFromCaffe(modelTxt, modelBin);
if (net.empty())
{
std::cerr labels = readClasslabels();
//GoogLeNet accepts only 224x224 RGB-images
Mat inputBlob = blobFromImage(testImage, 1, Size(224, 224), Scalar(104, 117, 123));//mean: Scalar(104, 117, 123)
// 支持1000个图像分类检测
Mat prob;
// 循环10+
for (int i = 0; i readClasslabels() {
std::vector classNames;
std::ifstream fp(labelFile);
if (!fp.is_open())
{
std::cerr

测试结果:

终端输出为:

1
2
3
Attempting to upgrade input file specified using deprecated V1LayerParameter: bvlc_googlenet.caffemodel
Successfully upgraded file specified using deprecated V1LayerParameter
current image classification : malemute, Alaskan malamute, possible : 0.42

图片效果:

dog

malemute指的是雪橇犬,Alaskan malamute意指阿拉斯加雪橇犬。


OpenCV4入门教程138:使用图像分类模型实现图像分类
https://feater.top/opencv/opencv-image-classification-with-googlenet-trained-network/
作者
JackeyLea
发布于
2020年11月5日
许可协议