python opencv realize Face Detection

lab environment

  • A windows laptop with a camera can MacOS
  • anaconda+pycharm

    Configuration Software Environment

    anaconda to the official website to download, I chose Python3.7 version, you can download and install.
    PyCharm to the official website to download, install it after downloading.

    The source

    import cv2
    import numpy as np
    cap = cv2.VideoCapture(0)
    while True:
    ret, frame = cap.read()
    cv2.flip(frame, 1, frame)
    face_cascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
    eye_cascade = cv2.CascadeClassifier("haarcascade_eye.xml")
    face = face_cascade.detectMultiScale(frame)
    eye = eye_cascade.detectMultiScale(frame)
    for (x, y, w, h) in face:
    cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)

      for (x, y, w, h) in eye:
          cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 255, 0), 2)
    
      cv2.imshow('frame', frame)
      if cv2.waitKey(1) & 0xFF == ord('q'):
          break
      if cv2.waitKey(1) & 0xFF == ord('s'):
          cv2.imwrite("a.jpg", frame)

    cap.release()

Guess you like

Origin www.cnblogs.com/zalv/p/10969273.html