提交 49ba9fc7 编写于 作者: F feilong

处理图片路径

上级 e18b424b
# opencv.dnn做图像分类 # opencv.dnn做图像分类
图像分类是基于深度学习的计算机视觉任务中最简单、也是最基础的一类,它其中用到的CNN特征提取技术也是目标检测、目标分割等视觉任务的基础。 图像分类是基于深度学习的计算机视觉任务中最简单、也是最基础的一类,它其中用到的CNN特征提取技术也是目标检测、目标分割等视觉任务的基础。
![](./result.png) ![](https://gitcode.net/csdn/skill_tree_opencv/-/raw/master/data/1.OpenCV初阶/7.OpenCV中的深度学习/1.图像分类/result.png)
具体到图像分类任务而言,其具体流程如下: 具体到图像分类任务而言,其具体流程如下:
1. 输入指定大小RGB图像,1/3通道,宽高一般相等 1. 输入指定大小RGB图像,1/3通道,宽高一般相等
2. 通过卷积神经网络进行多尺度特征提取,生成高维特征值 2. 通过卷积神经网络进行多尺度特征提取,生成高维特征值
3. 利用全连接网络、或其他结构对高维特征进行分类,输出各目标分类的概率值(概率和为1) 3. 利用全连接网络、或其他结构对高维特征进行分类,输出各目标分类的概率值(概率和为1)
4. 选择概率值最高的作为图像分类结果 4. 选择概率值最高的作为图像分类结果
![](./classification.png) ![](https://gitcode.net/csdn/skill_tree_opencv/-/raw/master/data/1.OpenCV初阶/7.OpenCV中的深度学习/1.图像分类/classification.png)
`opencv.dnn`模块可以直接加载深度学习模型,并进行推理输出运行结果。下面是opencv.dnn模块加载googlenet caffe模型进行图片分类的代码,请你完善其中TO-DO部分的代码。 `opencv.dnn`模块可以直接加载深度学习模型,并进行推理输出运行结果。下面是opencv.dnn模块加载googlenet caffe模型进行图片分类的代码,请你完善其中TO-DO部分的代码。
> 代码中LABEL_MAP是图像分类名称字典,给定索引得到具体分类名称(string)。 > 代码中LABEL_MAP是图像分类名称字典,给定索引得到具体分类名称(string)。
```python ```python
import cv2 import cv2
import numpy as np import numpy as np
from labels import LABEL_MAP # 1000 labels in imagenet dataset from labels import LABEL_MAP # 1000 labels in imagenet dataset
# caffe model, googlenet aglo # caffe model, googlenet aglo
weights = "bvlc_googlenet.caffemodel" weights = "bvlc_googlenet.caffemodel"
protxt = "bvlc_googlenet.prototxt" protxt = "bvlc_googlenet.prototxt"
# read caffe model from disk # read caffe model from disk
net = cv2.dnn.readNetFromCaffe(protxt, weights) net = cv2.dnn.readNetFromCaffe(protxt, weights)
# create input # create input
image = cv2.imread("ocean-liner.jpg") image = cv2.imread("ocean-liner.jpg")
blob = cv2.dnn.blobFromImage(image, 1.0, (224, 224), (104, 117, 123), False, crop=False) blob = cv2.dnn.blobFromImage(image, 1.0, (224, 224), (104, 117, 123), False, crop=False)
result = np.copy(image) result = np.copy(image)
# run! # run!
net.setInput(blob) net.setInput(blob)
out = net.forward() out = net.forward()
# TO-DO your code... # TO-DO your code...
# time cost # time cost
t, _ = net.getPerfProfile() t, _ = net.getPerfProfile()
label = 'cost time: %.2f ms' % (t * 1000.0 / cv2.getTickFrequency()) label = 'cost time: %.2f ms' % (t * 1000.0 / cv2.getTickFrequency())
cv2.putText(result, label, (0, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 255, 0), 2) cv2.putText(result, label, (0, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (255, 255, 0), 2)
# render on image # render on image
label = '%s: %.4f' % (LABEL_MAP[classId] if LABEL_MAP else 'Class #%d' % classId, confidence) label = '%s: %.4f' % (LABEL_MAP[classId] if LABEL_MAP else 'Class #%d' % classId, confidence)
cv2.putText(result, label, (0, 60), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2) cv2.putText(result, label, (0, 60), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
show_img = np.hstack((image, result)) show_img = np.hstack((image, result))
# normal codes in opencv # normal codes in opencv
cv2.imshow("Image", show_img) cv2.imshow("Image", show_img)
cv2.waitKey(0) cv2.waitKey(0)
``` ```
## 答案 ## 答案
```python ```python
# output probability, find the right index # output probability, find the right index
out = out.flatten() out = out.flatten()
classId = np.argmax(out) classId = np.argmax(out)
confidence = out[classId] confidence = out[classId]
``` ```
## 输出理解错误 ## 输出理解错误
```python ```python
# output probability, find the right index # output probability, find the right index
classId = out[0] classId = out[0]
confidence = out[1] confidence = out[1]
``` ```
## 输出维度理解错误 ## 输出维度理解错误
```python ```python
# output probability, find the right index # output probability, find the right index
classId = np.argmax(out) classId = np.argmax(out)
confidence = out[classId] confidence = out[classId]
``` ```
## 输出理解错误 ## 输出理解错误
```python ```python
# output probability, find the right index # output probability, find the right index
out = out.flatten() out = out.flatten()
classId = np.argmax(out[1:]) classId = np.argmax(out[1:])
confidence = out[classId + 1] confidence = out[classId + 1]
``` ```
\ No newline at end of file
...@@ -27,5 +27,5 @@ if __name__ == '__main__': ...@@ -27,5 +27,5 @@ if __name__ == '__main__':
# doc = DocWalker('doc') # doc = DocWalker('doc')
# doc.walk() # doc.walk()
# img = ImgWalker('data') img = ImgWalker('data')
# img.walk() img.walk()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册