调整图像大小时使用OpenCV进行面部识别不准确

时间:2017-08-17 15:29:03

标签: python opencv dlib

https://snag.gy/6MrLNi.jpg

这张照片中的下巴有点偏。

https://snag.gy/ORZHSe.jpg

不是这个。

代码差异:

image = cv2.resize(image,(2170, 2894), interpolation = cv2.INTER_AREA)

第二个没有这条线。

完整的源代码:

import cv2
import sys
import dlib
import numpy as np
from PIL import Image

import rawpy
# Get user supplied values
imagePath = sys.argv[1]
cascPath = "HS.xml"

pointOfInterestX = 200





detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor("okgood.dat")





raw = rawpy.imread(imagePath)
rgb = raw.postprocess()
image = Image.fromarray(rgb)
#image.save("WOO.jpg")
open_cv_image = np.array(image)


open_cv_image = open_cv_image[:, :, ::-1].copy()
image = open_cv_image
image = cv2.resize(image,(2170, 2894), interpolation = cv2.INTER_AREA)

widthO, heightO = image.shape[:2]


faceCascade = cv2.CascadeClassifier(cascPath)

# Read the image
#image = cv2.imread(imagePath)



gray = cv2.cvtColor((image), cv2.COLOR_RGB2BGR)


#height, width = image.shape[:2]



# Detect faces in the image
faces = faceCascade.detectMultiScale(
    gray,
    scaleFactor=1.1,
    minNeighbors=4,
    minSize=(500, 500)
    #flags = cv2.CV_HAAR_SCALE_IMAGE
)

newdigit = 0

def test():
    for l in range(y, y+h):
        for d in range(x, x+w):
            #   print(image[l,d])
            font = cv2.FONT_HERSHEY_SIMPLEX
            if all(item < 150 for item in image[l, d]):
                cv2.putText(image,"here",(d,l), font, .2,(255,255,255),1,cv2.LINE_AA)
                return l;
            image[l,d] = [0,0,0]

###
### put hairline 121 pixels from the top.
###



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

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

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







two = 1
# Draw a rectangle around the faces
for (x, y, w, h) in faces:
    print(str(len(faces)))
    cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)
    pointOfInterestX = test()
    break








dets = detector(image, 1)
one = 0
pointOfEight = 0


for k, d in enumerate(dets):
    shape = predictor(image, d)
    shape = shape_to_np(shape)
    for (x, y) in shape:
        if one == 8:
            pointOfEight = y
        font = cv2.FONT_HERSHEY_SIMPLEX
        cv2.putText(image,str(one),(x,y), font, .2,(255,255,255),1,cv2.LINE_AA)
        one = one + 1
        cv2.circle(image, (x, y), 1, (0, 0, 255), -1)

# loop over the (x, y)-coordinates for the facial landmarks
# and draw them on the image




new_dimensionX = heightO * 631 / (pointOfEight - pointOfInterestX)
new_dimensionY = widthO * 631 / (pointOfEight - pointOfInterestX)

print(str(new_dimensionY))

image = cv2.resize(image,(int(new_dimensionX), int(new_dimensionY)))



Rx = new_dimensionX / heightO
Ry = new_dimensionY / widthO



crop_img = image[int((pointOfInterestX * Rx)-121):int(new_dimensionY), 0:int(new_dimensionX-((Rx *pointOfInterestX)+121))]



font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(image,"xxxx",(100,pointOfInterestX ), font, 4,(255,255,255),1,cv2.LINE_AA)
cv2.imshow("Faces found", crop_img)
cv2.imwrite("cropped.jpg", crop_img)

cv2.waitKey(0)

在顶部,您将看到我将图像大小调整为2170,2894的行。就像我说的,没有这条线,下巴检测是准确的。有了它,它不是。我需要在这个分辨率下准确的下巴检测。

1 个答案:

答案 0 :(得分:2)

尝试使用DLIB的人脸检测器,使用人脸检测器ROI初始化的地标检测器,DLIB的检测器ROI与OpenCV Haar级联检测器不同。 DLIB的标志性探测器使用DLIB面部探测器的ROI进行训练,并且可以更好地使用它。