亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

Python基于Dlib的人臉識別系統的實現

瀏覽:4日期:2022-08-06 08:52:16

之前已經介紹過人臉識別的基礎概念,以及基于opencv的實現方式,今天,我們使用dlib來提取128維的人臉嵌入,并使用k臨近值方法來實現人臉識別。

人臉識別系統的實現流程與之前是一樣的,只是這里我們借助了dlib和face_recognition這兩個庫來實現。face_recognition是對dlib庫的包裝,使對dlib的使用更方便。所以首先要安裝這2個庫。

pip3 install dlibpip3 install face_recognition

然后,還要安裝imutils庫

pip3 install imutils

我們看一下項目的目錄結構:

.├── dataset│ ├── alan_grant [22 entries exceeds filelimit, not opening dir]│ ├── claire_dearing [53 entries exceeds filelimit, not opening dir]│ ├── ellie_sattler [31 entries exceeds filelimit, not opening dir]│ ├── ian_malcolm [41 entries exceeds filelimit, not opening dir]│ ├── john_hammond [36 entries exceeds filelimit, not opening dir]│ └── owen_grady [35 entries exceeds filelimit, not opening dir]├── examples│ ├── example_01.png│ ├── example_02.png│ └── example_03.png├── output│ ├── lunch_scene_output.avi│ └── webcam_face_recognition_output.avi├── videos│ └── lunch_scene.mp4├── encode_faces.py├── encodings.pickle├── recognize_faces_image.py├── recognize_faces_video_file.py├── recognize_faces_video.py└── search_bing_api.py 10 directories, 12 files

首先,提取128維的人臉嵌入:

命令如下:

python3 encode_faces.py --dataset dataset --encodings encodings.pickle -d hog

記住:如果你的電腦內存不夠大,請使用hog模型進行人臉檢測,如果內存夠大,可以使用cnn神經網絡進行人臉檢測。

看代碼:

# USAGE# python encode_faces.py --dataset dataset --encodings encodings.pickle # import the necessary packagesfrom imutils import pathsimport face_recognitionimport argparseimport pickleimport cv2import os # construct the argument parser and parse the argumentsap = argparse.ArgumentParser()ap.add_argument('-i', '--dataset', required=True,help='path to input directory of faces + images')ap.add_argument('-e', '--encodings', required=True,help='path to serialized db of facial encodings')ap.add_argument('-d', '--detection-method', type=str, default='hog',help='face detection model to use: either `hog` or `cnn`')args = vars(ap.parse_args()) # grab the paths to the input images in our datasetprint('[INFO] quantifying faces...')imagePaths = list(paths.list_images(args['dataset'])) # initialize the list of known encodings and known namesknownEncodings = []knownNames = [] # loop over the image pathsfor (i, imagePath) in enumerate(imagePaths):# extract the person name from the image pathprint('[INFO] processing image {}/{}'.format(i + 1,len(imagePaths)))name = imagePath.split(os.path.sep)[-2] # load the input image and convert it from RGB (OpenCV ordering)# to dlib ordering (RGB)image = cv2.imread(imagePath)rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # detect the (x, y)-coordinates of the bounding boxes# corresponding to each face in the input imageboxes = face_recognition.face_locations(rgb,model=args['detection_method']) # compute the facial embedding for the faceencodings = face_recognition.face_encodings(rgb, boxes) # loop over the encodingsfor encoding in encodings:# add each encoding + name to our set of known names and# encodingsknownEncodings.append(encoding)knownNames.append(name) # dump the facial encodings + names to diskprint('[INFO] serializing encodings...')data = {'encodings': knownEncodings, 'names': knownNames}f = open(args['encodings'], 'wb')f.write(pickle.dumps(data))f.close()

輸出結果是每張圖片輸出一個人臉的128維的向量和對于的名字,并序列化到硬盤,供后續人臉識別使用。

識別圖像中的人臉:

這里使用KNN方法實現最終的人臉識別,而不是使用SVM進行訓練。

命令如下:

python3 recognize_faces_image.py --encodings encodings.pickle --image examples/example_01.png

看代碼:

# USAGE# python recognize_faces_image.py --encodings encodings.pickle --image examples/example_01.png # import the necessary packagesimport face_recognitionimport argparseimport pickleimport cv2 # construct the argument parser and parse the argumentsap = argparse.ArgumentParser()ap.add_argument('-e', '--encodings', required=True,help='path to serialized db of facial encodings')ap.add_argument('-i', '--image', required=True,help='path to input image')ap.add_argument('-d', '--detection-method', type=str, default='cnn',help='face detection model to use: either `hog` or `cnn`')args = vars(ap.parse_args()) # load the known faces and embeddingsprint('[INFO] loading encodings...')data = pickle.loads(open(args['encodings'], 'rb').read()) # load the input image and convert it from BGR to RGBimage = cv2.imread(args['image'])rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # detect the (x, y)-coordinates of the bounding boxes corresponding# to each face in the input image, then compute the facial embeddings# for each faceprint('[INFO] recognizing faces...')boxes = face_recognition.face_locations(rgb,model=args['detection_method'])encodings = face_recognition.face_encodings(rgb, boxes) # initialize the list of names for each face detectednames = [] # loop over the facial embeddingsfor encoding in encodings:# attempt to match each face in the input image to our known# encodingsmatches = face_recognition.compare_faces(data['encodings'],encoding)name = 'Unknown' # check to see if we have found a matchif True in matches:# find the indexes of all matched faces then initialize a# dictionary to count the total number of times each face# was matchedmatchedIdxs = [i for (i, b) in enumerate(matches) if b]counts = {} # loop over the matched indexes and maintain a count for# each recognized face facefor i in matchedIdxs:name = data['names'][i]counts[name] = counts.get(name, 0) + 1 # determine the recognized face with the largest number of# votes (note: in the event of an unlikely tie Python will# select first entry in the dictionary)name = max(counts, key=counts.get)# update the list of namesnames.append(name) # loop over the recognized facesfor ((top, right, bottom, left), name) in zip(boxes, names):# draw the predicted face name on the imagecv2.rectangle(image, (left, top), (right, bottom), (0, 255, 0), 2)y = top - 15 if top - 15 > 15 else top + 15cv2.putText(image, name, (left, y), cv2.FONT_HERSHEY_SIMPLEX,0.75, (0, 255, 0), 2) # show the output imagecv2.imshow('Image', image)cv2.waitKey(0)

實際效果如下:

Python基于Dlib的人臉識別系統的實現

如果要詳細了解細節,請參考:https://www.pyimagesearch.com/2018/06/18/face-recognition-with-opencv-python-and-deep-learning/

到此這篇關于Python基于Dlib的人臉識別系統的實現的文章就介紹到這了,更多相關Python Dlib人臉識別內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 黄 色 成 年人在线 黄 色 成 年人网站 | 日韩免费观看的一级毛片 | 91九九 | a级黄色大片在线观看视频男男 | 欧美日韩 国产区 在线观看 | 97精品国产自在现线免费观看 | 欧美一级成人免费大片 | 中文字幕卡二和卡三的视频 | 国产永久免费视频m3u8 | 天天在线天天看成人免费视频 | 同性欧美可播放videos免费 | 欧美线人一区二区三区 | 国产精品亚洲国产三区 | 黄视频网站免费看 | 黄色片中文字幕 | 午夜黄页网站在线播放 | 国产综合视频在线观看一区 | 91香蕉国产 | 一97日本道伊人久久综合影院 | 久久综合久久综合久久 | 欧美日韩无线码免费播放 | 欧美日韩色视频在线观看 | 九九在线视频 | 国产精品va在线观看无 | 国产三级网站 | 久久久久婷婷国产综合青草 | 黄色地址| 羞羞影院免费观看网址在线 | 国内外成人免费视频 | 欧美精品黄页在线观看大全 | 免费黄色网址大全 | 精品久久不卡 | 国产精品久久久久网站 | 一级片视频网站 | 免费国产一区二区在免费观看 | 涩涩色中文综合亚洲 | 国模双双大尺度炮交g0go | 中国日韩欧美中文日韩欧美色 | 国产色综合一区二区三区 | 日韩 国产 在线 | 中文字幕第页 |