使用 OpenCV 从图像中识别地标和裁剪嘴巴的脚本看不到人脸

Script for identifying landmarks and cropping mouth from images using OpenCV doesn't see faces

所以我尝试使用 OpenCV、dlib 和 Python 做的是使用 dlib 基本上识别一组图像上的面部标志,然后从这些相同的图像中裁剪嘴巴并保存它们作为带有“.jpg”扩展名的单独图像。

这是代码:

import numpy as np  
import cv2  
import dlib
import sys
import skimage 
from PIL import Image
import os
import glob

#Everything is imported here

folderpath = sys.argv[1]
cascPath = sys.argv[2]
PREDICTOR_PATH = "/home/victor/facial-landmarks/shape_predictor_68_face_landmarks.dat" 

#user supplies the folderpath and cascpath in a terminal/command prompt
#predictor_path is already set

imageformat = ".tif"
path = folderpath
imfilelist = [os.path.join(path,f) for f in os.listdir(path) if f.endswith(imageformat)]

#only images with ".tif" extensions in the folder interest us, we create a
#list with paths to those images    

data = np.array([])
for IMG in imfilelist:
    image = cv2.imread(IMG) #this for-loop iterates through images we need
    np.append(data, image) # reads them, and appends them to the data 
                           # numpy array

gray = np.array([])
for j in range(0, len(data)):
    cvtimg = cv2.cvtColor(np.array(data[j]), cv2.COLOR_BGR2GRAY)
    np.append(gray, cvtimg) #empty numpy array called gray is declared
                            # for-loop goes through all RGB pictures
                            # stored in data, converts them to grayscale
                            # and stores them in gray



MOUTH_OUTLINE_POINTS = list(range(48, 61))  
MOUTH_INNER_POINTS = list(range(61, 68))

#defines the landmarks for the Mouth Outline and the inner mouth points  

faceCascade = cv2.CascadeClassifier(cascPath)

#faceCascade is defined here, cascPath which is user supplied is the param  

predictor = dlib.shape_predictor(PREDICTOR_PATH)  

faces = np.array([])   
for i in gray:
    face = faceCascade.detectMultiScale(gray[i], scaleFactor=1.05, minNeighbors=5, minSize=(100,100))
    np.append(faces, face) #this for-loop tries to detect faces and append       
                           #them to the empty numpy array called faces

print("Found {0} faces!".format(len(faces)))

# nothing is displayed beyond this print statement               

for (x, y, w, h) in faces:  

  dlib_rect = dlib.rectangle(int(x), int(y), int(x + w), int(y + h))  

  landmarks = np.matrix([[p.x, p.y]  
              for p in predictor(IMAGES, dlib_rect).parts()])  

  landmarks_display = landmarks[MOUTH_OUTLINE_POINTS + MOUTH_INNER_POINTS]

  highX = 0
  lowX = 1000
  highY = 0
  lowY = 1000

  for idx, point in enumerate(landmarks_display):  
    pos = (point[0, 0], point[0, 1])  
    cv2.circle(image, pos, 2, color=(0, 0, 255), thickness=-1)
  if (pos[0] > highX):
   highX = pos[0]
  if (pos[0] < lowX):
   lowX = pos[0]
  if (pos[1] > highY):
   highY = pos[1]
  if (pos[1] < lowY):
   lowY = pos[1]
  print (lowX, lowY, highX, highY)


  CONSTANT_FACTOR = 0.325
  delta_x = highX-lowX
  delta_y = highY - lowY
  low_x_adj = lowX - int(delta_x * CONSTANT_FACTOR)
  high_x_adj = highX + int(delta_x * CONSTANT_FACTOR)
  low_y_adj = lowY - int(delta_y * 0.2)
  high_y_adj = highY + int(delta_y * CONSTANT_FACTOR)

  crop_img = image[low_y_adj:high_y_adj,low_x_adj:high_x_adj]
  cv2.imwrite("Cropped_Mouth.jpg", crop_img)
  cv2.imshow("Cropped_Mouth.jpg", crop_img)

  cv2.waitKey(0)    

现在,我检查了路径,它们是正确的。我没有收到任何语法错误、运行时错误,什么都没有。该脚本运行,但除了以下打印语句外没有产生任何输出:print("Found {0} faces!".format(len(faces)))

我假设它会运行它之后的内容,但屏幕上没有输出,我的主文件夹中也没有保存任何内容(这是通常存储裁剪嘴的输出图片)。原本用于处理一张图像的原始脚本只能完美运行,但这个脚本似乎无法解决问题。

如有任何想法和建议,我们将不胜感激。谢谢。

P.S 如果问题出在打印行之后的代码,我仍然没有开始处理这个脚本的那部分,因为我相信是打印语句上方的代码在某些方面有问题

为什么不使用 dlib 人脸检测器来检测人脸?。下面是使用 dlib 人脸检测器检测人脸并使用 .jpg 扩展名从人脸中保存嘴巴的代码。我刚刚修改了 dlib 的 python examples 文件夹中给出的 dlib face landmarks.py。

import sys
import os
import dlib
import glob
import cv2

predictor_path = "shape_predictor_68_face_landmarks.dat"
faces_folder_path = "path/to/faces/folder"

detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(predictor_path)
win = dlib.image_window()
i = 0
for f in glob.glob(os.path.join(faces_folder_path, "*.tiff")):
    print("Processing file: {}".format(f))
    img = cv2.imread(f)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 

    # to clear the previous overlay. Useful when multiple faces in the same photo
    win.clear_overlay()

    # to show the image
    win.set_image(img)

    # Ask the detector to find the bounding boxes of each face. The 1 in the
    # second argument indicates that we should upsample the image 1 time. This
    # will make everything bigger and allow us to detect more faces.
    dets = detector(img, 1)
    print("Number of faces detected: {}".format(len(dets)))
    for k, d in enumerate(dets):
        print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format(
            k, d.left(), d.top(), d.right(), d.bottom()))
        # Get the landmarks/parts for the face in box d.
        shape = predictor(img, d)
        i += 1
        # The next lines of code just get the coordinates for the mouth
        # and crop the mouth from the image.This part can probably be optimised
        # by taking only the outer most points.
        xmouthpoints = [shape.part(x).x for x in range(48,67)]
        ymouthpoints = [shape.part(x).y for x in range(48,67)]
        maxx = max(xmouthpoints)
        minx = min(xmouthpoints)
        maxy = max(ymouthpoints)
        miny = min(ymouthpoints) 

        # to show the mouth properly pad both sides
        pad = 10
        # basename gets the name of the file with it's extension
        # splitext splits the extension and the filename
        # This does not consider the condition when there are multiple faces in each image.
        # if there are then it just overwrites each image and show only the last image.
        filename = os.path.splitext(os.path.basename(f))[0]

        crop_image = img[miny-pad:maxy+pad,minx-pad:maxx+pad]
        cv2.imshow('mouth',crop_image)
        # The mouth images are saved in the format 'mouth1.jpg, mouth2.jpg,..
        # Change the folder if you want to. They are stored in the current directory
        cv2.imwrite(filename+'.jpg',crop_image)
        cv2.waitKey(0)
        cv2.destroyAllWindows()
        win.add_overlay(shape)

    win.add_overlay(dets)