我想用OpenCV模块人脸识别编写一个cat识别器。
此代码适用于人工 faces,使用default.xml (这是经过训练的模型)。
imagePaths = list(paths.list_images("recognition/dataset"))
knownEncodings = []
knownNames = []
# For each image, we analyze the face on it
for (i, imagePath) in enumerate(imagePaths):
name = imagePath.split(os.path.sep)[-2]
image = cv2.imread(imagePath)
rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
boxes = face_recognition.face_locations(rgb, model="hog")
encodings = face_recognition.face_encodings(rgb, boxes)
for encoding in encodings:
knownEncodings.append(encoding)
knownNames.append(name)
data = {"encodings": knownEncodings, "names": knownNames}
# We save the data in a file
f = open("recognition/encodings.pickle", "wb")
f.write(pickle.dumps(data))
f.close()我试着用frontalcatface.xml来识别cats。我录了我的猫,但识别程序没有识别任何猫使用encodings.pickle。Python没有抛出任何错误。这个计划对人类很有效。
face_recognition模块对猫有用吗?
发布于 2022-07-07 19:02:39
检测:知道一张照片是否在哪里包含一张脸
Recognition:知道这张脸是谁的(是比利还是艾米丽?)
猫脸检测是可能的,识别是不可能的,因为在这个模块中使用的识别模型是针对人类的,而且我没有看到任何证据表明有猫训练过的识别模型。
人脸的检测一般比动物人脸的检测困难,因为人类的脸更多样化。比动物人脸更难检测。虽然这种变化使得检测更加困难,但它实际上使正确的识别更容易。猫的情况正好相反--它们的面部变化较少,使得检测更容易,而识别更难。
检测
你所得到的错误听起来像是基本的识别失败了。在尝试识别之前,需要对此进行修正。使用人脸识别模块检测猫脸绝对是可行的(示例中列出了它,并演示了这里和这里):
import cv2
catFaceCascade = cv2.CascadeClassifier('C:/Users/N/Desktop/haarcascade_frontalcatface.xml')
image = cv2.imread('C:/Users/N/Desktop/test.jpg')
faces = catFaceCascade.detectMultiScale(image)
if len(faces) == 0:
print("No faces found")
else:
print("Number of faces detected: " + str(faces.shape[0]))
for (x, y, w, h) in faces:
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0))
cv2.imshow('Image with faces', image)
cv2.waitKey(0)
cv2.destroyAllWindows()

识别
没有受过猫训练的模特,你就无法识别。face_recognition api依赖于几个模型来发挥功能(所有这些模型都是在人类面孔上进行的),如果您有这些模型,您可以尝试将它们替换为对猫进行培训的模型。
https://stackoverflow.com/questions/68318940
复制相似问题