手把手教你利用opencv实现人脸识别功能(附源码+文档)

  # coding=gbk

  """

  图片人脸识别

  作者:川川

  @时间 : 2021/9/5 17:22

  """

  import cv2

  import numpy as np

  # 下载链接:https://raw.githubusercontent.com/opencv/opencv/master/samples/dnn/face_detector/deploy.prototxt

  prototxt_path = r"http://www.jb51.net/article/deploy.prototxt.txt"

  # 下载链接:https://raw.githubusercontent.com/opencv/opencv_3rdparty/dnn_samples_face_detector_20180205_fp16/res10_300x300_ssd_iter_140000_fp16.caffemodel

  model_path =r"http://www.jb51.net/article/res10_300x300_ssd_iter_140000_fp16.caffemodel"

  model = cv2.dnn.readNetFromCaffe(prototxt_path, model_path)

  image = cv2.imread("2.jpg")

  h, w = image.shape[:2]

  blob = cv2.dnn.blobFromImage(image, 1.0, (300, 300),(104.0, 177.0, 123.0))

  model.setInput(blob)

  output = np.squeeze(model.forward())

  font_scale = 1.0

  for i in range(0, output.shape[0]):

  confidence = output[i, 2]

  if confidence > 0.5:

  box = output[i, 3:7] * np.array([w, h, w, h])

  start_x, start_y, end_x, end_y = box.astype(np.int)

  cv2.rectangle(image, (start_x, start_y), (end_x, end_y), color=(255, 0, 0), thickness=2)

  cv2.putText(image, f"{confidence*100:.2f}%", (start_x, start_y-5), cv2.FONT_HERSHEY_SIMPLEX, font_scale, (255, 0, 0), 2)

  cv2.imshow("image", image)

  cv2.waitKey(0)

  cv2.imwrite("beauty_detected.jpg", image)