[英]How do I code a cat recognizer with OpenCV?
我想使用 Python 模塊face-recognition用 OpenCV 編寫貓識別器。
此代碼適用於人臉,使用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()
我嘗試使用haarcascade_frontalcatface.xml來識別貓。 我記錄了我的貓,但識別程序沒有使用encodings.pickle
識別任何貓。 Python 沒有拋出任何錯誤。 該程序對人類運行良好。
face_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 依賴於幾個模型來運行(所有模型都經過人臉訓練),如果有的話,您可以嘗試用經過貓訓練的模型替換它們。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.