概述
使用opencv 加载训练好的SSD模型
# 文件下载地址
# deploy.prototxt.txt:
# https://github.com/opencv/opencv/tree/master/samples/dnn/face_detector
# res10_300x300_ssd_iter_140000.caffemodel:
# https://github.com/Shiva486/facial_recognition/blob/master/res10_300x300_ssd_iter_140000.caffemodel
import cv2
import numpy as np
def cv_show(neme, img):
cv2.namedWindow(neme, cv2.WINDOW_NORMAL)
cv2.imshow(neme, img)
cv2.waitKey(0)
cv2.destroyAllWindows()
# 读取照片
img = cv2.imread('./images/faces2.jpg')
# 加载模型
face_detector = cv2.dnn.readNetFromCaffe('./weights/deploy.prototxt.txt',
'./weights/res10_300x300_ssd_iter_140000.caffemodel')
# 原图尺寸
img_height = img.shape[0]
img_width = img.shape[1] # 缩放至模型输入尺寸
img_resize = cv2.resize(img, (500, 300))
# 图像转为blob
img_blob = cv2.dnn.blobFromImage(img_resize, 1.0, (500, 300), (104.0, 177.0, 123.0))
# 输入
face_detector.setInput(img_blob)
# 推理
detections = face_detector.forward()
# 查看检测人脸数量
num_of_detections = detections.shape[2]
# 原图复制,一会绘制用
img_copy = img.copy()
for index in range(num_of_detections):
# 置信度
detection_confidence = detections[0, 0, index, 2]
# 挑选置信度
if detection_confidence > 0.15:
# 位置
locations = detections[0, 0, index, 3:7] * np.array([img_width, img_height, img_width, img_height])
# 打印
print(detection_confidence * 100)
lx, ly, rx, ry = locations.astype('int')
# 绘制
cv2.rectangle(img_copy, (lx, ly), (rx, ry), (0, 255, 0), 5)
cv_show('neme', img_copy)
最后
以上就是怕黑汉堡为你收集整理的人脸检测(SSD模型)的全部内容,希望文章能够帮你解决人脸检测(SSD模型)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复