使用 OpenCV 从图像中识别地标和裁剪嘴的脚本看不到人脸
Posted
技术标签:
【中文标题】使用 OpenCV 从图像中识别地标和裁剪嘴的脚本看不到人脸【英文标题】:Script for identifying landmarks and cropping mouth from images using OpenCV doesn't see faces 【发布时间】:2018-12-08 09:47:49 【问题描述】:所以我尝试使用 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)))
。
我假设它运行它之后的内容,但屏幕上没有输出,并且我的主文件夹中没有保存任何内容(这是通常存储的裁剪嘴巴的输出图片)。原本打算使用一张图片的原始脚本只能完美运行,但这个脚本似乎并没有奏效。
任何想法和建议都将受到高度赞赏。谢谢。
PS 如果问题出在打印行之后的代码上,我仍然没有开始为该脚本处理该部分,因为我认为打印语句上方的代码在某些方面有问题
【问题讨论】:
【参考方案1】:为什么不使用 dlib 人脸检测器来检测人脸?下面是使用 dlib 人脸检测器检测人脸并从扩展名为 .jpg 的人脸中保存嘴巴的代码。我刚刚修改了dlib的python示例文件夹中给出的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)
【讨论】:
完美运行。唯一的问题是,我该如何保留使用的 tiff 文件的名称?所以如果叫abc123.tif,我希望输出是abc123.jpg。 我为此编辑了代码。请记住,如果一张图像中有多个面孔,那么它只会保存一张嘴巴。如果你想要所有的嘴巴图像,那么你需要每次更改文件名。 我注意到了编辑,谢谢。这正是需要的。我正在处理只有一张脸的图像。以上是关于使用 OpenCV 从图像中识别地标和裁剪嘴的脚本看不到人脸的主要内容,如果未能解决你的问题,请参考以下文章
Android - 从图像中裁剪文本(使用 openCV 或其他任何东西)