简体   繁体   English

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

[英]Inaccurate facial recognition using OpenCV when the image is resized

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

The chin is a bit off in this photo. 在这张照片中下巴有点过头了。

https://snag.gy/ORZHSe.jpg https://snag.gy/ORZHSe.jpg

Not this one. 不是这个。

Difference in Code: 代码差异:

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

The second one does not have this line. 第二个没有这一行。

Complete Source Code: 完整的源代码:

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)

Towards the top you will see the line where I resize the image to 2170,2894. 在顶部,您将看到我将图像尺寸调整为2170,2894的行。 Like I said, with this line absent, the chin detection is accurate. 就像我说的,由于缺少这条线,下巴检测是准确的。 With it, it is not. 有了它,事实并非如此。 I need the chin detection accurate at this resolution. 我需要在此分辨率下准确的下巴检测。

Try to use DLIB's face detector, landmarks detector initialized with face detector ROI, and DLIB's detector ROI is different from OpenCV Haar cascade one. 尝试使用DLIB的面部检测器,用面部检测器ROI初始化的地标检测器,并且DLIB的检测器ROI与OpenCV Haar级联检测器不同。 DLIB's landmark detector trained using ROI's from DLIB's face detector, and should work better with it. DLIB的地标检测器使用了DLIB的面部检测器中的ROI进行了训练,应该会更好地工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM