SoFunction
Updated on 2024-11-16

Two methods for face recognition in python3

In this article, we share the example of python3 to achieve the specific code of face recognition, for your reference, the details are as follows

The first:

import cv2
import numpy as np

filename = ''
path = r'D:\face'


def detect(filename):
  face_cascade = ('haarcascade_frontalface_default.xml')
  face_cascade.load(path + '\haarcascade_frontalface_default.xml')

  img = (filename)
  gray = (img, cv2.COLOR_BGR2GRAY)
  faces = face_cascade.detectMultiScale(gray, 1.3, 5)
  for (x, y, w, h) in faces:
    img = (img, (x, y), (x + w, y + h), (255, 0, 0), 2)
  ("vikings detected")
  ("vikings detected", img)
  (0)


detect(filename)

Results:

The second refer to Jia Zhigang opencv tutorials

# -*- coding:utf-8 -*-
import cv2 as cv
import numpy as np

src = ('')
path = r'D:\face'

def face_detect_demo():
  gray = (src,cv.COLOR_BGR2GRAY)

  face_detector = ('haarcascade_frontalface_default.xml')
  face_detector.load(path + '\haarcascade_frontalface_default.xml')
  faces = face_detector.detectMultiScale(gray,1.3,5)
  for x,y,w,h in faces:
    (src,(x,y),(x+w,y+h),(0,0,255),2)
  ("result",src)

print("--------------python face detect-------------")
("input image",0)
("result",0)
("input image",src)
face_detect_demo()
(0)
()

Results:

This is the whole content of this article.