简体   繁体   中英

Inaccurate facial recognition using OpenCV when the image is resized

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

The chin is a bit off in this photo.

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. 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's landmark detector trained using ROI's from DLIB's face detector, and should work better with it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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