从一个人的图像中提取眼睛的一部分,得到眼的上下点之间的高度

2024-05-04 07:02:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要一个算法来提取图像的眼睛部分,并检测出我的图像附件中显示的两个点。在探测到这两点之后,我想得到这两点之间的确切距离。在

Two points that i want to get the exact gap

我已经尝试了许多在互联网上可用的算法。我已经建立了一个提取眼睛部分的算法。但那张裁剪过的照片并不是一张高质量的照片。下面是我尝试过的代码和一些链接。在

https://www.pyimagesearch.com/2017/04/03/facial-landmarks-dlib-opencv-python/

 # import the necessary packages
 from imutils import face_utils
 import numpy as np
 import argparse
 import imutils
 import dlib
 import cv2

   # construct the argument parser and parse the arguments
   ap = argparse.ArgumentParser()
   ap.add_argument("-p", "--shape-predictor", required=True,
   help="path to facial landmark predictor")
   ap.add_argument("-i", "--image", required=True,
   help="path to input image")
   args = vars(ap.parse_args())

# initialize dlib's face detector (HOG-based) and then create
# the facial landmark predictor
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(args["shape_predictor"])

# load the input image, resize it, and convert it to grayscale
image = cv2.imread(args["image"])
image = imutils.resize(image, width=500)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# detect faces in the grayscale image
rects = detector(gray, 1)
FACIAL_LANDMARKS_IDXS = [
("Right Eye", (36, 42)),
    ("Left Eye", (42, 48))
]

# loop over the face detections
 for (i, rect) in enumerate(rects):
# determine the facial landmarks for the face region, then
# convert the landmark (x, y)-coordinates to a NumPy array
shape = predictor(gray, rect)
shape = face_utils.shape_to_np(shape)

# loop over the face parts individually
for (name, (i, j)) in FACIAL_LANDMARKS_IDXS:
    # clone the original image so we can draw on it, then
    # display the name of the face part on the image
    clone = image.copy()
    cv2.putText(clone, name, (10, 30), cv2.FONT_HERSHEY_SIMPLEX,
        0.7, (0, 0, 255), 2)

    # loop over the subset of facial landmarks, drawing the
    # specific face part
    for (x, y) in shape[i:j]:
        cv2.circle(clone, (x, y), 1, (0, 0, 255), -1)

    # extract the ROI of the face region as a separate image
    (x, y, w, h) = cv2.boundingRect(np.array([shape[i:j]]))
    roi = image[y:y + h, x:x + w]
    roi = imutils.resize(roi, width=250, inter=cv2.INTER_CUBIC)

    # show the particular face part
    cv2.imshow("Extracted image", roi)
    cv2.imshow("Image", clone)

    cv2.waitKey(0)
    cv2.imwrite("/Users/User/Downloads/detect-face- 
   parts/images/new.jpg", roi);

提取的图像质量不高。我想要一个高质量的。在


Tags: thetoimageimportcloneargscv2predictor