所需原料:opencv-python + yourmobilenet.caffemodel + yourdeploy.prototxt
利用opencv3.3.1之后推出的dnn模块,支持很多模型的导入.这次我选择Caffe的Mobilenet模型.
import numpy as np import time import cv2, os print('******model*********') net = cv2.dnn.readNetFromCaffe(r'./mobilenet_deploy.prototxt', r'./model/mbilenet_iter_4055.caffemodel') labels = ['cha', 'yang', 'xiaotiqin', 'xiong', 'none', 'zixingche', 'gangqin', 'gou', 'jingche', 'jita', 'jiuhuche', 'lunchuan', 'mao'] cap = cv2.VideoCapture(0) while 1: _, frame1 = cap.read() print('**********one frame***********') frame = cv2.resize(frame1, (224,224)) # 你的网络输入的尺寸 # grab the frame dimensions and convert it to a blob blob = cv2.dnn.blobFromImage(frame, 0.00390625, (224, 224), (112.24, 115.46, 117.84),swapRB=False) net.setInput(blob) detections = net.forward() label = labels[np.argmax(detections)] # label = str(np.argmax(detections)) cv2.putText(frame1, label,(111,111),cv2.FONT_HERSHEY_DUPLEX, 2, [0, 0, 111], 2) # show the output frame cv2.imshow("Frame", frame1) cv2.waitKey(1)
blob = cv2.dnn.blobFromImage(frame, 0.00390625, (224, 224), (112.24, 115.46, 117.84),swapRB=False)
第二个参数是scale,第三个是送入网络的尺寸,然后是每个通道的均值.