使用python的opencv库使用yolo算法进行物体检测

Yolov3是一个目标检测算法项目,而目标检测的本质,就是识别与回归,而处理图像用的最多的就是卷积神经网络CNN,所以,Yolov3本质上,就是一个实现了回归功能的深度卷积神经网络。
Yolo是一种实时检测方法,多年来备受欢迎。这意味着我们不仅可以对一个对象进行分类,还可以定位它并提取包围该对象的边框。
运行yolov3有多种方法。我们今天将使用的方法是最简单的方法之一,因为除了opencv和numpy之外,它不需要外部安装。

当然,我们需要下载模型文件,yolo中的模型文件将architecture(结构)和权重参数(weight)进行了分离,我们可以在YOLO: Real-Time Object Detection中下载对应的模型文件,在这里,我们推荐使用YOLOv3-320的配置(下载可能需要一点时间),因为这个模型被认为是速度和精度之间的平衡。

以下就是例程的代码:

import cv2
import numpy as np
cap = cv2.VideoCapture(1)
whT = 320
confThreshold =0.5
nmsThreshold= 0.2
classsesFile = "coco.names"
classNames = []
with open(classsesFile,'rt') as f:
	classNames = f.read().rstrip('\n').split('\n')
print(classNames)
modelConfiguration = "yolov3-320.cfg"
modelWeights = "yolov3.weights"
net = cv2.dnn.readNetFromDarknet(modelConfiguration,modelWeights)
net.setPreferableBackend(cv2.dnn.DNN_BACKEND_OPENCV)
net.setPreferableTarget(cv2.dnn.DNN_TARGET_CPU)

# bbox = []  # bounding box corner points
# classIds = []  # class id with the highest confidence
# confs = []  # confidence value of the highest class

def findObjects(outputs, img):
	hT, wT, cT = img.shape
	bbox = []
	classIds = []
	confs = []
	for output in outputs:
		for det in output:
			scores = det[5:]
			classId = np.argmax(scores)
			confidence = scores[classId]
			if confidence > confThreshold:
				w, h = int(det[2] * wT), int(det[3] * hT)
				x, y = int((det[0] * wT) - w / 2), int((det[1] * hT) - h / 2)
				bbox.append([x, y, w, h])
				classIds.append(classId)
				confs.append(float(confidence))

	indices = cv2.dnn.NMSBoxes(bbox, confs, confThreshold, nmsThreshold)

	for i in indices:
		i = i[0]
		box = bbox[i]
		x, y, w, h = box[0], box[1], box[2], box[3]
		# print(x,y,w,h)
		cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 255), 2)
		cv2.putText(img, f'{classNames[classIds[i]].upper()} {int(confs[i] * 100)}%',
				   (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255, 0, 255), 2)

while True:
	success, img = cap.read()
	blob = cv2.dnn.blobFromImage(img,1/255,(whT,whT),[0,0,0],1,crop=False)
	net.setInput(blob)
	layersNames = net.getLayerNames()
	outputNames = [(layersNames[i[0]-1]) for i in net.getUnconnectedOutLayers()]
	outputs = net.forward(outputNames)
	for output in outputs:
		print(output.shape)
	findObjects(outputs, img)
	cv2.imshow('Image',img)
	cv2.waitKey(1)

我们用摄像头对准需要检测的物体,就可以得到以下检测结果:
使用python的opencv库使用yolo算法进行物体检测_第1张图片

你可能感兴趣的:(#,机器学习初阶)