opencv yolov8 实现人脸检测
时间: 2023-07-07 08:36:40 浏览: 283
首先需要说明的是,YOLOv8 并不是一个现成的模型,这个名称也不常见。可能您想说的是 YOLOv3 或 YOLOv4 。
使用 OpenCV 实现 YOLOv3 或 YOLOv4 进行人脸检测的步骤如下:
1. 下载 YOLOv3 或 YOLOv4 的权重文件和配置文件,例如:
- YOLOv3:[yolov3.cfg](https://github.com/pjreddie/darknet/blob/master/cfg/yolov3.cfg) 和 [yolov3.weights](https://pjreddie.com/media/files/yolov3.weights)
- YOLOv4:[yolov4.cfg](https://github.com/AlexeyAB/darknet/blob/master/cfg/yolov4.cfg) 和 [yolov4.weights](https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v3_optimal/yolov4.weights)
2. 使用 OpenCV 加载模型和配置文件:
```python
import cv2
# 加载模型和配置文件
net = cv2.dnn.readNetFromDarknet('yolov3.cfg', 'yolov3.weights')
# 或者使用 yolov4
# net = cv2.dnn.readNetFromDarknet('yolov4.cfg', 'yolov4.weights')
```
3. 读取图像并创建 blob:
```python
# 读取图像
img = cv2.imread('test.jpg')
# 创建 blob
blob = cv2.dnn.blobFromImage(img, 1/255.0, (416, 416), swapRB=True, crop=False)
```
4. 将 blob 输入到模型中进行推理:
```python
# 将 blob 输入到模型中进行推理
net.setInput(blob)
outs = net.forward(net.getUnconnectedOutLayersNames())
```
5. 解析模型的输出,获取检测结果:
```python
conf_threshold = 0.5 # 置信度阈值
nms_threshold = 0.4 # 非极大值抑制阈值
# 解析输出,获取检测结果
boxes = []
confidences = []
class_ids = []
for out in outs:
for detection in out:
scores = detection[5:]
class_id = np.argmax(scores)
confidence = scores[class_id]
if confidence > conf_threshold:
center_x = int(detection[0] * img.shape[1])
center_y = int(detection[1] * img.shape[0])
w = int(detection[2] * img.shape[1])
h = int(detection[3] * img.shape[0])
x = int(center_x - w/2)
y = int(center_y - h/2)
boxes.append([x, y, w, h])
confidences.append(float(confidence))
class_ids.append(class_id)
# 非极大值抑制
indices = cv2.dnn.NMSBoxes(boxes, confidences, conf_threshold, nms_threshold)
```
6. 绘制检测结果:
```python
# 绘制检测结果
for i in indices:
i = i[0]
x, y, w, h = boxes[i]
cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2)
text = f'{class_ids[i]}: {confidences[i]:.2f}'
cv2.putText(img, text, (x, y-5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1)
```
完整示例代码如下:
```python
import cv2
import numpy as np
# 加载模型和配置文件
net = cv2.dnn.readNetFromDarknet('yolov3.cfg', 'yolov3.weights')
# 或者使用 yolov4
# net = cv2.dnn.readNetFromDarknet('yolov4.cfg', 'yolov4.weights')
# 读取图像
img = cv2.imread('test.jpg')
# 创建 blob
blob = cv2.dnn.blobFromImage(img, 1/255.0, (416, 416), swapRB=True, crop=False)
# 将 blob 输入到模型中进行推理
net.setInput(blob)
outs = net.forward(net.getUnconnectedOutLayersNames())
conf_threshold = 0.5 # 置信度阈值
nms_threshold = 0.4 # 非极大值抑制阈值
# 解析输出,获取检测结果
boxes = []
confidences = []
class_ids = []
for out in outs:
for detection in out:
scores = detection[5:]
class_id = np.argmax(scores)
confidence = scores[class_id]
if confidence > conf_threshold:
center_x = int(detection[0] * img.shape[1])
center_y = int(detection[1] * img.shape[0])
w = int(detection[2] * img.shape[1])
h = int(detection[3] * img.shape[0])
x = int(center_x - w/2)
y = int(center_y - h/2)
boxes.append([x, y, w, h])
confidences.append(float(confidence))
class_ids.append(class_id)
# 非极大值抑制
indices = cv2.dnn.NMSBoxes(boxes, confidences, conf_threshold, nms_threshold)
# 绘制检测结果
for i in indices:
i = i[0]
x, y, w, h = boxes[i]
cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2)
text = f'{class_ids[i]}: {confidences[i]:.2f}'
cv2.putText(img, text, (x, y-5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1)
# 显示结果
cv2.imshow('img', img)
cv2.waitKey()
cv2.destroyAllWindows()
```
阅读全文