简体   繁体   English

如何使用 OpenCV 编写猫识别器?

[英]How do I code a cat recognizer with OpenCV?

I want to code a cat recognizer with OpenCV using the Python module face-recognition .我想使用 Python 模块face-recognition用 OpenCV 编写猫识别器。
This code works for human faces, using haarcascade_frontalface_default.xml (this is the trained model).此代码适用于脸,使用haarcascade_frontalface_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()

I tried to use haarcascade_frontalcatface.xml , to recognize cats .我尝试使用haarcascade_frontalcatface.xml来识别 I recorded my cats, but the recognition program didn't recognize any cat using the encodings.pickle .我记录了我的猫,但识别程序没有使用encodings.pickle识别任何猫。 Python didn't throw any error. Python 没有抛出任何错误。 The program worked fine for humans.该程序对人类运行良好。

Does the face_recognition module work for cats? face_recognition模块对猫有用吗?

Detection : Knowing if and where a photo contains a face检测:知道照片是否以及在哪里包含人脸
Recognition : Knowing who the face belongs to (is it Billy or Emily?)识别:知道这张脸属于谁(是比利还是艾米丽?)

Cat face detection is possible, recognition is not since the recognition model used in this module was trained on humans and I don't see any evidence of a cat trained recognition model.猫脸检测是可能的,识别不是因为这个模块中使用的识别模型是在人类身上训练的,我没有看到任何猫训练识别模型的证据。

Detection of human faces is in general harder than detection of animal faces, because human faces have more variety than animal faces.人脸的检测通常比动物人脸的检测更难,因为人脸比动物脸具有更多的多样性 While this variation makes detection harder, it actually makes correct recognition easier.虽然这种变化使检测更难,但它实际上使正确识别更容易。 The converse is true for cats - they have less facial variety making detection easier but recognition harder.猫的情况正好相反——它们的面部变化较少,使检测更容易,但识别更难。

Detection检测

The error you are getting sounds like the basic recognition is failing.您收到的错误听起来像是基本识别失败。 That needs to be fixed before you can attempt recognition.在您尝试识别之前,这需要修复。 Cat face detection is definitely feasible using the face-recognition module (it is listed in the examples, as well as demonstrated here and here ):使用人脸识别模块进行猫脸检测绝对是可行的(它在示例中列出,并在此处此处进行了演示):

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()

在此处输入图像描述

Recognition认出

Without models trained on cats, you won't be able to do recognition.如果没有对猫进行训练的模型,您将无法进行识别。 The face_recognition api relies on several models to function (all trained on human faces) which you could try replacing with models trained on cats if you have them. face_recognition api 依赖于几个模型来运行(所有模型都经过人脸训练),如果有的话,您可以尝试用经过猫训练的模型替换它们。

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

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