OpenCV使用GoogleNet网络模型实现图像分类
一、概述
1.案例:使用GoogleNet网络模型实现图像分类。此案例主要在于学习使用已经训练好的模型。
2.GoogleNet:卷积神经网络
3.模型介绍:这个模型中有1000个分类,但是其分类并不准确,只能用于学习使用。
模型中的三个重要文件:
1.网络模型文件:bvlc_googlenet.caffemodel
2.网络模型描述文件:bvlc_googlenet.prototxt
3.分类标签文件:synset_words.txt
4.模型文件下载地址
链接: https://pan.baidu.com/s/14v7YlDKCZqPObEZcxlIenQ?pwd=0bok 提取码: 0bok
5.特别说明:此模型文件是opencv自带的模型文件,我测试这个网络模型发现实现分类并不准确(有可能和我输入有关系),在实际的项目中不是没有办法的情况下建议不要直接使用。
6.这篇博客侧重点在于学习使用模型,是否准确大家可以忽略,因为在实际的项目开发中,肯定是使用公司训练好的模型。
二、代码示例
AI_Google_Net_Image_Division::AI_Google_Net_Image_Division(QWidget *parent) : MyGraphicsView{parent} { this->setWindowTitle("使用GoogleNet实现图像分割"); modelPath = "/Users/yangwei/Documents/dev_tools/opencv_ai_model/googlenet/bvlc_googlenet.caffemodel"; prototxtPath = "/Users/yangwei/Documents/dev_tools/opencv_ai_model/googlenet/bvlc_googlenet.prototxt"; labelPath = "/Users/yangwei/Documents/dev_tools/opencv_ai_model/googlenet/synset_words.txt"; } void AI_Google_Net_Image_Division::dropEvent(QDropEvent * event){ path = event->mimeData()->urls().at(0).toLocalFile(); qDebug()<<"图片路径:"<<path; showImageClassificatio(path.toStdString().c_str()); } void AI_Google_Net_Image_Division::showImageClassificatio(const char * filePath){ //加载原图 Mat src = imread(filePath); if(src.empty()){ qDebug()<<"图像路径为空"; return; } imshow("src",src); //组装分类标签 vector<String> labels = readLabels();//读取分类标签 //载入网络模型 Net net = readNetFromCaffe(prototxtPath.toStdString(),modelPath.toStdString()); if(net.empty()){ qDebug()<<"count not read net ...."; return; } //对要被检测的图像进行预处理 Mat inputBlob = blobFromImage(src,1.0,Size(224,224), Scalar(104, 117, 123)); Mat prob; //给网络模型设置输入并传递到网络模型尾部 for(int i=0;i<10;i++){//10层网络 net.setInput(inputBlob,"data"); prob = net.forward("prob"); } //将prob转换为一行一列 Mat probMat = prob.reshape(1,1); Point classNumber; double classProb; //找出模型识别到的最大概率 minMaxLoc(probMat,NULL,&classProb,NULL,&classNumber);//找出最大值(最大概率) int classIndex = classNumber.x; //输出检测到的结果 qDebug()<<"检测结果:"<<labels.at(classIndex).c_str()<<"--possible:"<<classProb; //将结果会址出来 putText(src, labels.at(classIndex), Point(20, 20), FONT_HERSHEY_SIMPLEX, 1.0, Scalar(0, 0, 255), 2, 8); imshow("src",src); } /** * 读取分类标签 * @brief AI_Google_Net_Image_Division::readLabels * @return */ vector<String> AI_Google_Net_Image_Division::readLabels(){ vector<String> classNames;//存放分类标签 QFile file(labelPath); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { qDebug()<<"could not open the file"; exit(-1); } QString line; QTextStream in(&file); line = in.readLine();//读取一行放到字符串中 while (!line.isEmpty()) { line = in.readLine(); string name = line.toStdString(); if (line.length()) { classNames.push_back(name.substr(name.find(' ') + 1)); } } file.close(); return classNames; }
三、演示图像
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探