imutils库源码解析,看它如何调用opencv(面部识别)

2022-08-10,,,

imutils 这个图像处理工具包,除了简化 opencv 的一些操作之外,还有专门配合 dlib 处理人脸数据的工具 face_utils。dlib 提取人脸数据后,五官都是用一些特征点来表示的,每个部位的点的索引是固定的,想要进一步操作就得对这些点进行处理,而 face_utils 就是简化这些点的表现方式

本文就来解析一下 face_utils 的处理方法,方便我们理解它的用处


特征点索引提取

查看源码

#For dlib’s 68-point facial landmark detector:
FACIAL_LANDMARKS_68_IDXS = OrderedDict([
	("mouth", (48, 68)),
	("inner_mouth", (60, 68)),
	("right_eyebrow", (17, 22)),
	("left_eyebrow", (22, 27)),
	("right_eye", (36, 42)),
	("left_eye", (42, 48)),
	("nose", (27, 36)),
	("jaw", (0, 17))
])

特征点区域图:

dlib 提取人脸特征点是用 68 个点包围每个部位,如上图,例如第 37 个点到第 42 个点就代表右眼,在图片上这几个点若显示出来就是把右眼那块区域包围着,可以通过这些点之间距离的变化来判断人脸的变化,比如是否眨眼等操作

imutils 通过 OrderedDict 把这些点的索引与其表示的区域直接通过字典形式联系起来,之后再提取某个部位的点时,就不用去查点的索引分布了,例如想提取嘴部特征点,其索引可以通过:

(mStart, mEnd) = face_utils.FACIAL_LANDMARKS_68_IDXS["mouth"]

FACIAL_LANDMARKS_68_IDXS 打印出来看看:


人脸区域坐标转换

查看源码:

def rect_to_bb(rect):
	# take a bounding predicted by dlib and convert it
	# to the format (x, y, w, h) as we would normally do
	# with OpenCV
	x = rect.left()
	y = rect.top()
	w = rect.right() - x
	h = rect.bottom() - y

	# return a tuple of (x, y, w, h)
	return (x, y, w, h)

dlib 提取人脸区域是后是用 4 个数表示,分别代表上下左右的边界。

rect_to_bb 将其转换为坐标信息,即(左上角横坐标, 左上角纵坐标, 矩形宽度, 矩形长度),之后就可以通过这个坐标将人脸区域用矩形框出来显示


特征点坐标提取

查看源码:

def shape_to_np(shape, dtype="int"):
	# initialize the list of (x, y)-coordinates
	coords = np.zeros((shape.num_parts, 2), dtype=dtype)

	# loop over all facial landmarks and convert them
	# to a 2-tuple of (x, y)-coordinates
	for i in range(0, shape.num_parts):
		coords[i] = (shape.part(i).x, shape.part(i).y)

	# return the list of (x, y)-coordinates
	return coords

dlib 提取人脸的 68 个特征点之后,是用自身的 dlib.full_object_detection 格式保存它们的坐标信息,每次提取坐标都需要用 (shape.part(i).x, shape.part(i).y)形式

shape_to_np 将 68 个特征点的坐标提取出来后,再用 numpy 保存为坐标点矩阵,方便其使用


标记人脸部位

查看源码:

def visualize_facial_landmarks(image, shape, colors=None, alpha=0.75):
	# create two copies of the input image -- one for the
	# overlay and one for the final output image
	overlay = image.copy()
	output = image.copy()

	# if the colors list is None, initialize it with a unique
	# color for each facial landmark region
	if colors is None:
		colors = [(19, 199, 109), (79, 76, 240), (230, 159, 23),
			(168, 100, 168), (158, 163, 32),
			(163, 38, 32), (180, 42, 220)]

	# loop over the facial landmark regions individually
	for (i, name) in enumerate(FACIAL_LANDMARKS_IDXS.keys()):
		# grab the (x, y)-coordinates associated with the
		# face landmark
		(j, k) = FACIAL_LANDMARKS_IDXS[name]
		pts = shape[j:k]

		# check if are supposed to draw the jawline
		if name == "jaw":
			# since the jawline is a non-enclosed facial region,
			# just draw lines between the (x, y)-coordinates
			for l in range(1, len(pts)):
				ptA = tuple(pts[l - 1])
				ptB = tuple(pts[l])
				cv2.line(overlay, ptA, ptB, colors[i], 2)

		# otherwise, compute the convex hull of the facial
		# landmark coordinates points and display it
		else:
			hull = cv2.convexHull(pts)
			cv2.drawContours(overlay, [hull], -1, colors[i], -1)

	# apply the transparent overlay
	cv2.addWeighted(overlay, alpha, output, 1 - alpha, 0, output)

	# return the output image
	return output

68 个特征点提取出来后,已经将人脸的每个部位标记出来,之后就可以每个部位进行标记,例如将每个部位用不同颜色覆盖。而上面代码中的函数就是进行这个操作

2 个必要参数:

  • image:输入图像
  • shape:特征点坐标矩阵

使用后效果图:

里面使用了 opencv 的 convexHulldrawContoursaddWeighted 方法,看一下它们的参数和作用:
cv2.convexHull(points,clockwise,returnpoints)

  • points:输入的坐标点,通常为1* n * 2 结构,n为所有的坐标点的数目
  • clockwise:转动方向,TRUE为顺时针,否则为逆时针
  • returnPoints:默认为TRUE,返回凸包上点的坐标,如果设置为FALSE,会返回与凸包点对应的轮廓上的点
    此函数是找寻凸包,不严谨的说是将最外层的点连接起来构成的凸多边形,即把边缘点连接起来

cv2.drawContours(image,contours,contourIdx,color,thickness=None,lineType=None,hierarchy=None,maxLevel=None,offset=None)

  • image:输入图像
  • contours:轮廓列表
  • contourIdx:列表中的哪条轮廓
  • color:绘制颜色
  • thickness:轮廓线的宽度,如果是 -1 (cv2.FILLED),则为填充模式
    此函数是将轮廓线条绘制出来,内部空间也可以填充

cv2.addWeighted(image1, alpha,image2,beta,gamma[,dst[,dtype]])

  • image1:输入图像1
  • alpha:图像1的权重
  • image2:输入图像2,与图像1的尺寸和通道数相同
  • beta:图像2的权重
  • gamma:加到每个总和上的标量,相当于调亮度
  • dst:输出
    此函数是将图像进行叠加,两个图像的权重自行选择

经过这 3 个方法后,人脸的每个部位就被标记出来了

本文地址:https://blog.csdn.net/weixin_44613063/article/details/107105690

《imutils库源码解析,看它如何调用opencv(面部识别).doc》

下载本文的Word格式文档,以方便收藏与打印。