簡體   English   中英

調整圖像大小時使用OpenCV進行的面部識別不正確

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

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的行。 就像我說的,由於缺少這條線,下巴檢測是准確的。 有了它,事實並非如此。 我需要在此分辨率下准確的下巴檢測。

嘗試使用DLIB的面部檢測器,用面部檢測器ROI初始化的地標檢測器,並且DLIB的檢測器ROI與OpenCV Haar級聯檢測器不同。 DLIB的地標檢測器使用了DLIB的面部檢測器中的ROI進行了訓練,應該會更好地工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM