提交 bd6e2786 编写于 作者: AI浩's avatar AI浩

Update detect_faces.py

上级 13ed05ca
import numpy as np import numpy as np
import cv2 import cv2
low_confidence=0.5 if __name__ == '__main__':
image_path='2.jpg' low_confidence = 0.5
proto_txt='deploy.proto.txt' image_path = '2.jpg'
model_path='res10_300x300_ssd_iter_140000_fp16.caffemodel' proto_txt = 'deploy.proto.txt'
# 加载模型 model_path = 'res10_300x300_ssd_iter_140000_fp16.caffemodel'
print("[INFO] loading model...") # 加载模型
net = cv2.dnn.readNetFromCaffe(proto_txt, model_path) print("[INFO] loading model...")
# 加载输入图像并为图像构建一个输入 blob net = cv2.dnn.readNetFromCaffe(proto_txt, model_path)
# 将大小调整为固定的 300x300 像素,然后对其进行标准化 # 加载输入图像并为图像构建一个输入 blob
image = cv2.imread(image_path) # 将大小调整为固定的 300x300 像素,然后对其进行标准化
(h, w) = image.shape[:2] image = cv2.imread(image_path)
blob = cv2.dnn.blobFromImage(cv2.resize(image, (300, 300)), 1.0, (h, w) = image.shape[:2]
(300, 300), (104.0, 177.0, 123.0)) blob = cv2.dnn.blobFromImage(cv2.resize(image, (300, 300)), 1.0,
# 通过网络传递blob并获得检测和预测 (300, 300), (104.0, 177.0, 123.0))
print("[INFO] computing object detections...") # 通过网络传递blob并获得检测和预测
net.setInput(blob) print("[INFO] computing object detections...")
detections = net.forward() net.setInput(blob)
# 循环检测 detections = net.forward()
for i in range(0, detections.shape[2]): # 循环检测
# 提取与相关的置信度(即概率) for i in range(0, detections.shape[2]):
# 预测 # 提取与相关的置信度(即概率)
confidence = detections[0, 0, i, 2] # 预测
# 通过确保“置信度”来过滤掉弱检测 confidence = detections[0, 0, i, 2]
# 大于最小置信度 # 通过确保“置信度”来过滤掉弱检测
if confidence > low_confidence: # 大于最小置信度
# 计算边界框的 (x, y) 坐标 if confidence > low_confidence:
box = detections[0, 0, i, 3:7] * np.array([w, h, w, h]) # 计算边界框的 (x, y) 坐标
(startX, startY, endX, endY) = box.astype("int") box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
# 绘制人脸的边界框以及概率 (startX, startY, endX, endY) = box.astype("int")
text = "{:.2f}%".format(confidence * 100) # 绘制人脸的边界框以及概率
y = startY - 10 if startY - 10 > 10 else startY + 10 text = "{:.2f}%".format(confidence * 100)
cv2.rectangle(image, (startX, startY), (endX, endY), y = startY - 10 if startY - 10 > 10 else startY + 10
(0, 0, 255), 2) cv2.rectangle(image, (startX, startY), (endX, endY),
cv2.putText(image, text, (startX, y), (0, 0, 255), 2)
cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 0, 255), 2) cv2.putText(image, text, (startX, y),
# 展示图片并保存 cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 0, 255), 2)
cv2.imshow("Output", image) # 展示图片并保存
cv2.imwrite("01.jpg",image) cv2.imshow("Output", image)
cv2.waitKey(0) cv2.imwrite("01.jpg", image)
\ No newline at end of file cv2.waitKey(0)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册