人脸检测(SSD模型)

使用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)

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

默执_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值