这次一个很早的一个小作业
里面的加载的模型见:
https://download.youkuaiyun.com/download/qq_32900237/11422245
代码:
import cv2 as cv
import time
# 检测人脸并绘制人脸bounding box
def getFaceBox(net, frame, conf_threshold=0.7):
frameOpencvDnn = frame.copy()
frameHeight = frameOpencvDnn.shape[0] # 高就是矩阵有多少行
frameWidth = frameOpencvDnn.shape[1] # 宽就是矩阵有多少列
blob = cv.dnn.blobFromImage(frameOpencvDnn, 1.0, (300, 300), [104, 117, 123], True, False)
# blobFromImage(image[, scalefactor[, size[, mean[, swapRB[, crop[, ddepth]]]]]]) -> retval 返回值 # swapRB是交换第一个和最后一个通道 返回按NCHW尺寸顺序排列的4 Mat值
net.setInput(blob)
detections = net.forward() # 网络进行前向传播,检测人脸
bboxes = []
for i in range(detections.shape[2]):
confidence = detections[0, 0, i, 2]
if confidence > conf_threshold:
x1 = int(detections[0, 0, i, 3] * frameWidth)
y1 = int(detections[0, 0, i, 4] * frameHeight)
x2 = int(detections[0, 0, i, 5] * frameWidth)
y2 = int(detections[0, 0, i, 6] * frameHeight)
bboxes.append([x1, y1, x2, y2]) # bounding box 的坐标
cv.rectangle(frameOpencvDnn, (x1, y1), (x2, y2), (0, 255, 0), int(round(frameHeight / 150)),
8) # rectangle(img, pt1, pt2, color[, thickness[, lineType[, shift]]]) -> img
return frameOpencvDnn, bboxes
# 网络模型 和 预训练模型
faceProto = "E:\Bigproject\opencv_face_detector.pbtxt"
faceModel =