[英]Face Recognization in Python & Open CV
我可以使用 python 找到人臉並將它們保存在我的本地目錄中,並按照下面視頻中的代碼打開 cv
import cv2 import numpy as np import os vc = cv2.VideoCapture('new1.avi') c=1 if vc.isOpened(): rval , frame = vc.read() else: rval = False while rval: rval, frame = vc.read() cv2.imwrite(str(c) + '.jpg',frame) image_name=str(c)+'.jpg' cascPath = "haarcascade_frontalface_default.xml" faceCascade = cv2.CascadeClassifier(cascPath) image=cv2.imread(image_name) gray=cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) faces = faceCascade.detectMultiScale( gray, scaleFactor=1.2, minNeighbors=5, minSize=(30, 30), flags = cv2.cv.CV_HAAR_SCALE_IMAGE ) print "Found {0} faces!".format(len(faces)) if len(faces)>=1: for (x, y, w, h) in faces: cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2) cv2.imshow("Faces found" ,image) cv2.waitKey(0) else: a="rm "+image_name os.popen(a) c = c + 1 cv2.waitKey(1) vc.release()
但是現在我想識別那個視頻中那個有臉的人......
我如何定義一個人的身份?
喜歡掃描人臉並將其匹配到我的本地人臉數據庫中,如果找到匹配,請提供名稱等
區分照片中的人物並非易事,但有一些例子。 正如德曼在之前的評論中提到的,最好的方法是使用機器學習來教程序不同的人臉是什么樣子。 一種方法是手動查找和提取人臉的特征,例如眼睛之間的距離與眼睛和嘴巴之間的距離等。 這雖然需要注意鏡頭畸變和透視的影響。 有多篇研究論文討論了最佳技術,例如這篇論文使用來自一組人臉的特征向量來找到最可能的匹配人臉識別使用特征人臉
Python 有一個機器學習工具箱,稱為 scikit-learn,它實現了對分類、回歸、聚類等的支持。 您可以使用它來訓練神經網絡和支持向量機等。 以下是如何使用 SVM 和 scikit-learn 和 python 實現特征臉方法的完整示例: Complete implementation using Python
您可以使用 EigenFaceRecognizer 或 FisherFaceRecognizer 或 LBHP
所有這三種算法都內置在python中
# Create a recognizer object
recognizer = cv2.face.createEigenFaceRecognizer()
# But Remember for EigenFaces all the images whether training or testing has to be of same shape
#==========================================================================
# get_images_and_labels function will give us list of images and list of labels to train our recognizer that we created in the first line
# function requires the path of the directory where all the images is stored
#===========================================================================
def get_images_and_labels(path):
# Append all the absolute image paths in a list image_paths
image_paths = [os.path.join(path, f) for f in os.listdir(path) if not
f.endswith('.sad')]
# images will contains face images
images = []
# labels will contains the label that is assigned to the image
labels = []
final_images = []
largest_image_size = 0
largest_width = 0
largest_height = 0
for image_path in image_paths:
# Read the image and convert to grayscale
image_pil = Image.open(image_path).convert('L')
# Convert the image format into numpy array
image = np.array(image_pil, 'uint8')
# Get the label of the image
nbr = int(os.path.split(image_path)[1].split(".")[0].replace("subject", ""))
# Detect the face in the image
faces = faceCascade.detectMultiScale(image)
# If face is detected, append the face to images and the label to labels
for (x, y, w, h) in faces:
images.append(image[y: y + h, x: x + w])
labels.append(nbr)
cv2.imshow("Adding faces to traning set...", image[y: y + h, x: x + w])
cv2.waitKey(50)
# return the images list and labels list
for image in images:
if image.size > largest_image_size:
largest_image_size = image.size
largest_width, largest_height = image.shape
for image in images:
image = cv2.resize(image, (largest_width, largest_height), interpolation=cv2.INTER_CUBIC)
final_images.append(image)
return final_images, labels, largest_width, largest_height
#===================================================================
# Perform the tranining
# trainer takes two parameters as input
# first parameter is the list of images
# second parameter is a numpy array of their corresponding labels
#===================================================================
recognizer.train(images, np.array(labels)) # training takes as input the list
image_paths = [os.path.join(path, f) for f in os.listdir(path) if f.endswith('.sad')]
for image_path in image_paths:
predict_image_pil = Image.open(image_path).convert('L')
predict_image = np.array(predict_image_pil, 'uint8')
faces = faceCascade.detectMultiScale(predict_image)
for (x, y, w, h) in faces:
result = cv2.face.MinDistancePredictCollector()
predict_image = predict_image[y: y + h, x: x + w]
predict_image = cv2.resize(predict_image, (max_width, max_heigth), interpolation=cv2.INTER_CUBIC)
# =========================================================
# predict method will give us the prediction
# we will get the label in the next statement
# predicted_image is the image that you want to recognize
# =========================================================
recognizer.predict(predict_image, result, 0) # this statement will give the prediction
# ==========================================
# This statement below will give us label
# ==========================================
nbr_predicted = result.getLabel()
# ==========================================
# conf will tell us how much confident our recognizer is in it's prediction
# ==========================================
conf = result.getDist()
nbr_actual = int(os.path.split(image_path)[1].split(".")[0].replace("subject", ""))
if nbr_actual == nbr_predicted:
print("{} is Correctly Recognized with confidence {}".format(nbr_actual, conf))
else:
print("{} is Incorrect Recognized as {}".format(nbr_actual, nbr_predicted))
sys.exit()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.