我正在尝试测量照片上脸部和眼睛的大小。我正在使用Python和OpenCV库。
import cv2
import os
def find_faces(image_path):
image = cv2.imread(image_path)
# Make a copy to prevent us from modifying the original
color_img = image.copy()
filename = os.path.basename(image_path)
# OpenCV works best with gray images
gray_img = cv2.cvtColor(color_img, cv2.COLOR_BGR2GRAY)
# Use OpenCV's built-in Haar classifier
haar_classifier = cv2.CascadeClassifier('file.xml')
eye_cascade = cv2.CascadeClassifier('eyes.xml')
faces = haar_classifier.detectMultiScale(gray_img, scaleFactor=1.1, minNeighbors=5)
print('Number of faces found: {faces}'.format(faces=len(faces)))
len2 = 0
for (x, y, width, height) in faces:
cv2.rectangle(color_img, (x, y), (x+width, y+height), (0, 255, 0), 2)
roi_gray = gray_img[y:y+height, x:x+width]
roi_color = color_img[y:y+height, x:x+width]
eyes = eye_cascade.detectMultiScale(roi_gray)
for (ex,ey,ew,eh) in eyes:
cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
len2 = cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
# Show the faces / eyes found
cv2.imshow(filename, color_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
if __name__ == '__main__':
find_faces('Images/face3.jpg')
此代码的结果是面部和眼睛突出的照片:
所以我的问题是如何以厘米为单位测量脸和眼睛的大小?
很抱歉,如果这是一个基本问题,但由于某种原因,我在网上找不到太多的方法来指导我。我已经在Google上搜索了很多,阅读了不同的文章,但是没有任何帮助。
谢谢