赞
踩
# 深度学习网络模型 # DNN(Deep Neural Network,深度神经网络) # RNN(Recurrent Neural Network,循环神经网络) # 语音识别、机器翻译、生成图像描述 # 输入 - 隐藏 - 输出 # CNN(Convolutional Neural Network,卷积神经网络) # 图片分类、检索,目标定位检测,目标分割,人脸识别 # 输入 - 卷积 - 池化 - 卷积... - 全连接 - 输出 # DNN使用步骤 # 1、读取模型,并得到深度神经网络 # 2、读取图片/视频 # 3、将图片转成张量,送入深度神经网络 # 4、进行分析,并得到结果
- import cv2
- from cv2 import dnn
- import numpy as np
- import re
-
- # 导入模型,创建神经网络
-
- config = "../model/bvlc_googlenet.prototxt"
- model = "../model/bvlc_googlenet.caffemodel"
-
- net = dnn.readNetFromCaffe(config, model)
-
- # 读图片,转成张量
- img = cv2.imread('../img/5.jpg')
- # scalefactor:缩放因子,修改为固定张量(224,224),(104, 117, 123)默认输入值
- blob = dnn.blobFromImage(img, 1.0, (224, 224), (104, 117, 123))
-
- # 将张量输入到神经网络,并且进行预测
- net.setInput(blob)
- r = net.forward()
-
- # 读入类目
- classes = []
- path = '../model/dataset.txt' # 标签文件
- with open(path, 'rt') as f:
- classes = [x[x.find("") + 1:] for x in f]
-
- order = sorted(r[0], reverse=True)
- z = list(range(5))
- for i in list(range(0, 5)):
- z[i] = np.where(r[0] == order[i])[0][0]
- print('第', i + 1, '项,匹配', classes[z[i]], end='')
- print('类所在行', z[i] + 1, ' ', '可能性:', order[i])
-
- text1 = f"Match{classes[z[0]]}."
- text2 = f"In{z[0] + 1},Per:{order[0]}"
- cv2.putText(img, text1, (0, 50), cv2.FONT_HERSHEY_PLAIN, 2, (0, 0, 255), 2)
- cv2.putText(img, text2, (0, 80), cv2.FONT_HERSHEY_PLAIN, 2, (0, 0, 255), 2)
- cv2.imshow('img', img)
- cv2.waitKey(0)
-
- cv2.destroyAllWindows()
------------------------------------------------我是分割线------------------------------------------------------
在上方基础上,改为读取视频,代码如下:
- import cv2
- from cv2 import dnn
- import numpy as np
- import re
-
- # 导入模型,创建神经网络
-
- config = "../model/bvlc_googlenet.prototxt" # (描述)配置文件
- model = "../model/bvlc_googlenet.caffemodel" # 模型文件
-
- # 读取网络 包括模型描述文件和和模型文件
- net = dnn.readNetFromCaffe(config, model)
-
- # 读图片,转成张量
- # img = cv2.imread('../img/landescape1.png')
- # 读视频,转成张量
- cap = cv2.VideoCapture('../VideoFile/animal/6.mp4')
- while True:
- ret, frame = cap.read()
- if ret:
- img = frame
-
- # scalefactor:缩放因子,修改为固定张量(224,224),(104, 117, 123)默认输入值
- blob = dnn.blobFromImage(img, 1.0, (224, 224), (104, 117, 123))
-
- # 将张量输入到神经网络,并且进行预测
- net.setInput(blob)
- r = net.forward()
-
- # 读入类目
- classes = []
- path = '../model/dataset.txt' # 标签文件
- with open(path, 'rt') as f: # 读取标签文件
- classes = [x[x.find("") + 1:] for x in f]
-
- order = sorted(r[0], reverse=True)
- z = list(range(3))
- for i in list(range(0, 3)):
- z[i] = np.where(r[0] == order[i])[0][0]
- # print('第', i + 1, '项,匹配', classes[z[i]], end='')
- # print('类所在行', z[i] + 1, ' ', '可能性:', order[i])
-
- text1 = f"Match{classes[z[0]]}."
- text2 = f"In{z[0] + 1},Per:{order[0]}"
- cv2.putText(img, text1, (0, 50), cv2.FONT_HERSHEY_PLAIN, 2, (0, 0, 255), 2)
- cv2.putText(img, text2, (0, 80), cv2.FONT_HERSHEY_PLAIN, 2, (0, 0, 255), 2)
- print('匹配', classes[z[0]], end='')
- print('类所在行', z[0] + 1, ' ', '可能性:', order[0])
-
- cv2.imshow('img', img)
-
- k = cv2.waitKey(10)
- if k == 27:
- break
- cap.release()
- cv2.destroyAllWindows()
-
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。