Search code examples
pythonopencvface-detection

Is there any method to replace selectROI with auto selection?


I have finished detecting faces through videos and generating a bounding box if detected by Haar Cascade classifier. And now I only want to analyze the particular part of the face such as foreheads or cheeks, but I could just choose the place manually through selectROI in OpenCV. Is there any method to revise my code or I could just do it manually?

import cv2 as cv
import argparse
import numpy as np


parser = argparse.ArgumentParser()
parser.add_argument('--face_cascade', help='Path to face cascade.',default='opencv-3.4/data/haarcascades/haarcascade_frontalface_alt2.xml')
parser.add_argument('--camera', help='Camera divide number.', type=int, default=0)
args = parser.parse_args()
face_cascade_name = args.face_cascade
face_cascade = cv.CascadeClassifier()

if not face_cascade.load(cv.samples.findFile(face_cascade_name)):
print('Error loading face cascade')
exit(0)

camera_device =  args.camera  # for build-in camera

cap = cv.VideoCapture(camera_device)

if not cap.isOpened:
   print('Error opening video capture')
   exit(0)
tracker = cv.TrackerCSRT_create()
roi = None
while True:
   ret, frame = cap.read()
   if frame is None:
       print('No captured frame, Break!')
       break
   frame_gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
   frame_gray = cv.equalizeHist(frame_gray)


   faces = face_cascade.detectMultiScale(
    frame_gray, scaleFactor=1.1, minNeighbors=3)
   for (x, y, w, h) in faces:
    cv.rectangle(frame, (x, y), (x+w, y+h), (0, 0, 255), 3)

   if roi is None:
       roi = cv.selectROI('frame', frame, False, False)
       if roi != (0, 0, 0, 0):
           tracker.init(frame, roi)
   success, rect = tracker.update(frame)
   if success:
       (x, y, w, h) = [int(i) for i in rect]
       cv.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 3)
   cv.imshow('Face Detection', frame)
   if (cv.waitKey(1) == ord('q') or cv.waitKey(1) == 27):
       break

Solution

  • there can be different ways you can go around for detecting and analysing facial regions, I am listing a few:

    • you can use Dlib's Landmark Detector to detect facial landmarks and classify the facial regions based on landmark's position. Example: the face portion above eye-brow landmarks is forehead region etc. For more clarity see the image below. enter image description here
    • You can object detectors which can detect facials regions which you want but it will be difficult to find pre-trained model for this, you have to train your own model.