def DrawLandmarks(self, image):
        '''
		@parms: grayscale image
		Predict the lanmarks on image and return the image.
		'''
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        # detect faces in the grayscale image
        rects = self.detector(image, 1)

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

            # convert dlib's rectangle to a OpenCV-style bounding box
            # [i.e., (x, y, w, h)], then draw the face bounding box
            (x, y, w, h) = utils.rect_to_bb(rect)
            cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)

            # show the face number
            cv2.putText(image, "Face #{}".format(i + 1), (x - 10, y - 10),
                        cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)

            # loop over the (x, y)-coordinates for the facial landmarks
            # and draw them on the image
            for (x, y) in shape:
                cv2.circle(image, (x, y), 1, (0, 0, 255), -1)

        return image
예제 #2
0
    def lmDetect(self, img, bbox=None):
        ''' 检测人脸关键点,假定单张图片单人脸
        param
        img: str or nparray
        '''
        if type(img) not in [str, np.array]:
            raise NotImplementedError('type(img) not supported: {}'.format(
                type(img)))

        if isinstance(img, str):
            try:
                img = cv2.imread(img)
                rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
            except:
                print('ERROR read img @lmDetect', img)
        if 'dt' in self.detectMode:
            bbox = self.faceDetect(img)
        if not bbox:
            bbox = [0, 0, img.shape[1],
                    img.shape[0]]  # left, top, right, bottom

        landmarks = self.predictor(rgb, box=bbox)
        if landmarks is None: return None
        landmarks = shape_to_np(landmarks)
        # import pdb
        # pdb.set_trace()
        return landmarks
예제 #3
0
def get_landmarks(detector, predictor, rgb):
    # first get bounding box (dlib.rectangle class) of face.
    boxes = detector(rgb, 1)
    for box in boxes:
        landmarks = shape_to_np(predictor(rgb, box=box))
        break
    else:
        return None
    return landmarks.astype(np.int32)
예제 #4
0
def dlibdect(img):
    #     cv2.imshow('image',img)
    #     cv2.waitKey(0)
    #     cv2.destroyAllWindows()
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    rects = detector(gray, 1)
    for (i, rect) in enumerate(rects):
        shape = predictor(gray, rect)
        shape = utils.shape_to_np(shape)
        print(shape.shape)

    return shape
예제 #5
0
def generator_demo_example_lips(img_path):
    print(img_path)
    name = img_path.split('/')[-1]
    landmark_path = os.path.join('../image/', name.replace('jpg', 'npy'))
    region_path = os.path.join('../image/',
                               name.replace('.jpg', '_region.jpg'))
    roi, landmark = crop_image(img_path)
    if np.sum(landmark[37:39, 1] - landmark[40:42, 1]) < -9:

        # pts2 = np.float32(np.array([template[36],template[45],template[30]]))
        template = np.load('../basics/base_68.npy')
    else:
        template = np.load('../basics/base_68_close.npy')
    # pts2 = np.float32(np.vstack((template[27:36,:], template[39,:],template[42,:],template[45,:])))
    pts2 = np.float32(template[27:45, :])
    # pts2 = np.float32(template[17:35,:])
    # pts1 = np.vstack((landmark[27:36,:], landmark[39,:],landmark[42,:],landmark[45,:]))
    pts1 = np.float32(landmark[27:45, :])
    # pts1 = np.float32(landmark[17:35,:])
    tform = tf.SimilarityTransform()
    tform.estimate(pts2, pts1)
    dst = tf.warp(roi, tform, output_shape=(163, 163))

    dst = np.array(dst * 255, dtype=np.uint8)
    dst = dst[1:129, 1:129, :]
    cv2.imwrite(region_path, dst)

    gray = cv2.cvtColor(dst, cv2.COLOR_BGR2GRAY)

    # detect faces in the grayscale image
    rects = detector(gray, 1)
    for (i, rect) in enumerate(rects):

        shape = predictor(gray, rect)
        shape = utils.shape_to_np(shape)
        shape, _, _ = normLmarks(shape)
        np.save(landmark_path, shape)
        lmark = shape.reshape(68, 2)
        name = region_path.replace('region.jpg', 'lmark.png')

        utils.plot_flmarks(lmark,
                           name, (-0.2, 0.2), (-0.2, 0.2),
                           'x',
                           'y',
                           figsize=(10, 10))
예제 #6
0
def crop_image(image_path, gg, last):
    image = cv2.imread(image_path)
    if gg == 1:
        [new_y, new_x, r] = last
        roi = image[new_y:new_y + 2 * r, new_x:new_x + 2 * r]
        roi = cv2.resize(roi, (256, 256), interpolation=cv2.INTER_AREA)

        return roi, last

    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    rects = detector(gray, 1)
    for (i, rect) in enumerate(rects):

        shape = predictor(gray, rect)
        shape = utils.shape_to_np(shape)

        (x, y, w, h) = utils.rect_to_bb(rect)
        # cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
        # cv2.putText(image, "c", (x - 10, y - 10),
        # cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
        # for (x, y) in shape:
        #     cv2.circle(image, (x, y), 1, (0, 0, 255), -1)

        # cv2.putText(image, "a", (x , y ),cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
        center_x = x + int(0.5 * w)
        center_y = y + int(0.5 * h)
        # cv2.putText(image, "b", (center_x , center_y ),cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)
        r = int(0.8 * h)
        new_x = max(center_x - r, 0)
        new_y = max(center_y - r, 0)

        # cv2.putText(image, "f", (new_x , new_y ),cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
        # print (new_x, new_y, r)
        roi = image[new_y:new_y + 2 * r, new_x:new_x + 2 * r]

        roi = cv2.resize(roi, (256, 256), interpolation=cv2.INTER_AREA)
        # scale =  256. / (2 * r)

        # shape = ((shape - np.array([new_x,new_y])) * scale)
        return roi, [new_y, new_x, r]
예제 #7
0
def crop_image(image, id = 1, kxy = []):
#     kxy = []
    if id == 1:
        image = cv2.imread(image)
    if kxy != []:
        [k, x, y] = kxy
        roi = image[y - int(0.2 * k):y + int(1.6 * k), x- int(0.4 * k):x + int(1.4 * k)]
        roi = cv2.resize(roi, (224,224), interpolation = cv2.INTER_AREA)
        return roi, kxy 
    else:        
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        rects = detector(gray, 1)
        for (i, rect) in enumerate(rects):
            shape = predictor(gray, rect)
            shape = utils.shape_to_np(shape)
            (x, y, w, h) = utils.rect_to_bb(rect)
            center_x = x + int(0.5 * w)
            center_y = y + int(0.5 * h)
            k = min(w, h)
            roi = image[y - int(0.2 * k):y + int(1.6 * k), x- int(0.4 * k):x + int(1.4 * k)]
            roi = cv2.resize(roi, (224,224), interpolation = cv2.INTER_AREA)
            return roi ,[k,x,y]
예제 #8
0
 def getLandmark(self, bounding_box, img):
     gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
     rect = dlib.rectangle(bounding_box[0], bounding_box[1], bounding_box[2], bounding_box[3])
     shape = self.predictor(gray, rect)
     shape = shape_to_np(shape)
     return shape
예제 #9
0
def generating_demo_landmark_lips(folder='../demo'):
    global CC
    if not os.path.exists(os.path.join(folder, 'regions')):
        os.mkdir(os.path.join(folder, 'regions'))
    if not os.path.exists(os.path.join(folder, 'landmark1d')):
        os.mkdir(os.path.join(folder, 'landmark1d'))
    this = 'obama'

    image_txt = os.listdir(os.path.join(folder, 'image', this))
    image_txt = sorted(image_txt)

    for line in image_txt:
        img_path = os.path.join(folder, 'image', this, line)

        if not os.path.exists(os.path.join(folder, 'regions', this)):
            os.mkdir(os.path.join(folder, 'regions', this))

        if not os.path.exists(os.path.join(folder, 'landmark1d', this)):
            os.mkdir(os.path.join(folder, 'landmark1d', this))

        landmark_path = os.path.join(folder, 'landmark1d', this,
                                     line.replace('jpg', 'npy'))

        region_path = os.path.join(folder, 'regions', this, line)

        try:
            print(img_path)

            roi, landmark = crop_image(img_path)
            if np.sum(landmark[37:39, 1] - landmark[40:42, 1]) < -9:

                # pts2 = np.float32(np.array([template[36],template[45],template[30]]))
                template = np.load('../basics/base_68.npy')
            else:
                template = np.load('../basics/base_68_close.npy')
            # pts2 = np.float32(np.vstack((template[27:36,:], template[39,:],template[42,:],template[45,:])))
            pts2 = np.float32(template[27:45, :])
            # pts2 = np.float32(template[17:35,:])
            # pts1 = np.vstack((landmark[27:36,:], landmark[39,:],landmark[42,:],landmark[45,:]))
            pts1 = np.float32(landmark[27:45, :])
            # pts1 = np.float32(landmark[17:35,:])
            tform = tf.SimilarityTransform()
            tform.estimate(pts2, pts1)
            dst = tf.warp(roi, tform, output_shape=(163, 163))

            dst = np.array(dst * 255, dtype=np.uint8)
            dst = dst[1:129, 1:129, :]
            cv2.imwrite(region_path, dst)

            gray = cv2.cvtColor(dst, cv2.COLOR_BGR2GRAY)

            # detect faces in the grayscale image
            rects = detector(gray, 1)
            for (i, rect) in enumerate(rects):

                shape = predictor(gray, rect)
                shape = utils.shape_to_np(shape)

                np.save(landmark_path, shape)
            print(CC)
            CC += 1

        except:
            cv2.imwrite(region_path, dst)
            np.save(landmark_path, shape)
            continue
            print(line)
# loop over the frames from the video stream
while True:
    # grab the frame from the threaded video stream, resize it to
    # have a maximum width of 400 pixels, and convert it to grayscale
    frame = vs.read()
    frame = utils.resize(frame, width=400)
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # detect faces in the grayscale frame
    rects = detector(gray, 0)
    # loop over the face detections
    for rect in rects:
        # determine the facial landmarks for the face region, then
        # convert the facial landmark (x, y)-coordinates to a NumPy array
        shape = predictor(gray, rect)
        shape = utils.shape_to_np(shape)

        # loop over the (x, y)-coordinates for the facial landmarks
        # and draw them on the image
        for (x, y) in shape:
            cv2.circle(frame, (x, y), 1, (0, 0, 255), -1)

    # show the frame
    cv2.imshow("Frame", frame)
    key = cv2.waitKey(1) & 0xFF

    # if the `q` key was pressed, break from the loop
    if key == ord("q"):
        break