简体   繁体   中英

How do I code a cat recognizer with OpenCV?

I want to code a cat recognizer with OpenCV using the Python module face-recognition .
This code works for human faces, using haarcascade_frontalface_default.xml (this is the trained model).

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 . I recorded my cats, but the recognition program didn't recognize any cat using the encodings.pickle . Python didn't throw any error. The program worked fine for humans.

Does the face_recognition module work for cats?

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.

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