机器学习之人脸识别face_recognition使用
機(jī)器學(xué)習(xí)之人臉識(shí)別face_recognition使用
- 簡(jiǎn)介
- 一
- 二
- 主要方法介紹
- 1. load_image_file 加載圖像
- 2. face_locations 定位圖中所有人臉
- 3. face_landmarks 識(shí)別人臉關(guān)鍵點(diǎn)
- 4. face_encodings 獲取圖像文件中所有面部編碼
- 5. compare_faces 由面部編碼匹配臉
- 實(shí)踐
- demo1:識(shí)別人臉特征并打印出來(lái)
- demo2 : 識(shí)別圖片中的所有人臉并顯示
- demo3 : 顯示未知圖片中已知人物的臉
- demo4 : 攝像頭頭像識(shí)別
- demo5人臉識(shí)別簽到系統(tǒng)
- 謝謝大家,如有需要,請(qǐng)聯(lián)系我
- 郵箱為zeuskkk@163.com
簡(jiǎn)介
一
face_recognition項(xiàng)目是世界上最簡(jiǎn)潔的人臉識(shí)別庫(kù),可以使用Python和命令行工具提取、識(shí)別、操作人臉。
本項(xiàng)目的人臉識(shí)別是基于業(yè)內(nèi)領(lǐng)先的C++開(kāi)源庫(kù) dlib中的深度學(xué)習(xí)模型,用Labeled Faces in the Wild人臉數(shù)據(jù)集進(jìn)行測(cè)試,有高達(dá)99.38%的準(zhǔn)確率。但對(duì)小孩和亞洲人臉的識(shí)別準(zhǔn)確率尚待提升。
Labeled Faces in the Wild是美國(guó)麻省大學(xué)安姆斯特分校(University of Massachusetts Amherst)制作的人臉數(shù)據(jù)集,該數(shù)據(jù)集包含了從網(wǎng)絡(luò)收集的13,000多張面部圖像。
本項(xiàng)目提供了簡(jiǎn)易的face_recognition命令行工具
二
人臉識(shí)別實(shí)際上是對(duì)人臉進(jìn)行編碼后再去計(jì)算兩兩人臉的相似度,known_image是已知人臉庫(kù)的圖像,unknown_image是待檢測(cè)的圖像,分別利用face_encodings函數(shù)來(lái)映射成一個(gè)向量,再利用兩個(gè)向量的內(nèi)積來(lái)衡量相似度,compare_faces函數(shù)就是根據(jù)閾值確認(rèn)是否是同一人臉。上述函數(shù)都是支持多個(gè)人臉計(jì)算的。另外compare_faces有個(gè)tolerance參數(shù)是控制閾值的,tolerance值越低越嚴(yán)格,默認(rèn)為0.6。
主要方法介紹
1. load_image_file 加載圖像
import face_recognition之后直接調(diào)用face_recognition.load_image_file()讀入圖像,參數(shù)給文件名字符串,注意:jpg文件要和程序放在同一個(gè)文件夾下。輸出圖像是rgb格式(opencv中是bgr格式)。
import face_recognition #加載圖像文件 image = face_recognition.load_image_file("your_file.jpg")2. face_locations 定位圖中所有人臉
加載圖像文件后直接調(diào)用face_recognition.face_locations(image),能定位所有圖像中識(shí)別出的人臉位置信息,返回值是列表形式,列表中每一行是一張人臉的位置信息,包括[top, right, bottom, left],也可理解為每個(gè)人臉是一個(gè)tuple存儲(chǔ),分別代表框住人臉的矩形中左上角和右下角的坐標(biāo)(x1,y1,x2,y2)。可遍歷列表打印出每張臉的位置信息,也可以通過(guò)位置信息截出識(shí)別出的人臉的圖像顯示出來(lái),代碼如下:
#定位所有找到的臉的位置 face_locations = face_recognition.face_locations(image) # 循環(huán)找到的所有人臉 for face_location in face_locations:# 打印每張臉的位置信息top, right, bottom, left = face_locationprint("A face is located at pixel location Top: {}, Left: {}, Bottom: {}, Right: {}".format(top, left, bottom, right))# 指定人臉的位置信息,然后顯示人臉圖片face_image = image[top:bottom, left:right]pil_image = Image.fromarray(face_image)pil_image.show()3. face_landmarks 識(shí)別人臉關(guān)鍵點(diǎn)
加載圖像后,調(diào)用face_recognition.face_landmarks(image)可識(shí)別出人臉關(guān)鍵點(diǎn)信息,包括眼睛、鼻子、嘴巴和下巴等,參數(shù)仍是加載的圖像image,返回值是包含面部特征字典的列表,列表中每一項(xiàng)對(duì)應(yīng)一張人臉,包括nose_bridge、right_eyebrow、right_eye、chine、left_eyebrow、bottom_lip、nose_tip、top_lip、left_eye幾個(gè)部分,每個(gè)部分包含若干個(gè)特征點(diǎn)(x,y),總共有68個(gè)特征點(diǎn)。列表長(zhǎng)度就是圖中識(shí)別出的人臉數(shù),可遍歷此列表和字典的鍵值對(duì),打印出所有面部特征點(diǎn),也可在圖像上畫(huà)出來(lái),代碼如下:
from PIL import Image, ImageDraw face_landmarks_list = face_recognition.face_landmarks(image) pil_image = Image.fromarray(image) d = ImageDraw.Draw(pil_image) for face_landmarks in face_landmarks_list:#face_landmarks_list中每個(gè)元素都包含以下key的字典#打印此圖像中每個(gè)面部特征的位置facial_features = ['chin','left_eyebrow','right_eyebrow','nose_bridge','nose_tip','left_eye','right_eye','top_lip','bottom_lip']for facial_feature in facial_features:print("The {} in this face has the following points: {}".format(facial_feature, face_landmarks[facial_feature]))#在圖像中畫(huà)出每個(gè)人臉特征!for facial_feature in facial_features:d.line(face_landmarks[facial_feature], width=5) pil_image.show()4. face_encodings 獲取圖像文件中所有面部編碼
face_recognition.face_encodings(image)方法可獲取每個(gè)圖像文件中每個(gè)面部的面部編碼,參數(shù)仍是加載的圖像image,由于每個(gè)圖像中可能有多個(gè)臉,所以返回的是一個(gè)編碼列表,后續(xù)訪問(wèn)時(shí)注意加上索引號(hào)或者依次遍歷。
由打印可看出每張人臉是一個(gè)128維的向量。
5. compare_faces 由面部編碼匹配臉
用face_recognition.compare_faces()方法可匹配兩個(gè)面部特征編碼,利用兩個(gè)向量的內(nèi)積來(lái)衡量相似度,根據(jù)閾值確認(rèn)是否是同一人臉。
位置參數(shù):第一個(gè)參數(shù)給出一個(gè)面部編碼列表(很多張臉),第二個(gè)參數(shù)給出單個(gè)面部編碼(一張臉),compare_faces方法會(huì)將第二個(gè)參數(shù)的編碼與第一個(gè)參數(shù)中的編碼依次匹配,返回值是一個(gè)布爾值列表,匹配成功的臉?lè)祷豑rue,匹配失敗的返回False,順序與第一個(gè)參數(shù)中臉的順序一致。
默認(rèn)參數(shù):tolerance=0.6,可根據(jù)自己需求更改,tolerance越小匹配越嚴(yán)格,例如
matches = face_recognition.compare_faces(known_face_encodings, face_encoding,tolerance=0.39),將閾值改為了0.39,閾值太低容易造成無(wú)法成功識(shí)別人臉,太高容易造成人臉識(shí)別混淆,這個(gè)是根據(jù)我自己測(cè)試后慢慢試出來(lái)的一個(gè)值。
實(shí)踐
demo1:識(shí)別人臉特征并打印出來(lái)
from PIL import Image, ImageDraw import face_recognition# 將jpg文件加載到numpy 數(shù)組中 image = face_recognition.load_image_file("view_Mr_G.jpg")#查找圖像中所有面部的所有面部特征 face_landmarks_list = face_recognition.face_landmarks(image)print("I found {} face(s) in this photograph.".format(len(face_landmarks_list))) pil_image = Image.fromarray(image) d = ImageDraw.Draw(pil_image) for face_landmarks in face_landmarks_list:#打印此圖像中每個(gè)面部特征的位置facial_features = ['chin','left_eyebrow','right_eyebrow','nose_bridge','nose_tip','left_eye','right_eye','top_lip','bottom_lip']for facial_feature in facial_features:print("The {} in this face has the following points: {}".format(facial_feature, face_landmarks[facial_feature]))#在圖像中畫(huà)出每個(gè)人臉特征!for facial_feature in facial_features:d.line(face_landmarks[facial_feature], width=5)pil_image.show()demo2 : 識(shí)別圖片中的所有人臉并顯示
from PIL import Image import face_recognition# 將jpg文件加載到numpy 數(shù)組中 image = face_recognition.load_image_file("view_Mr_G.jpg")# 使用默認(rèn)的給予HOG模型查找圖像中所有人臉 # 這個(gè)方法已經(jīng)相當(dāng)準(zhǔn)確了,但還是不如CNN模型那么準(zhǔn)確,因?yàn)闆](méi)有使用GPU加速 # 另請(qǐng)參見(jiàn): find_faces_in_picture_cnn.py face_locations = face_recognition.face_locations(image)# 使用CNN模型 # face_locations = face_recognition.face_locations(image, number_of_times_to_upsample=0, model="cnn")# 打印:我從圖片中找到了 多少 張人臉 print("I found {} face(s) in this photograph.".format(len(face_locations)))# 循環(huán)找到的所有人臉 for face_location in face_locations:# 打印每張臉的位置信息top, right, bottom, left = face_locationprint("A face is located at pixel location Top: {}, Left: {}, Bottom: {}, Right: {}".format(top, left, bottom, right)) # 指定人臉的位置信息,然后顯示人臉圖片face_image = image[top:bottom, left:right]pil_image = Image.fromarray(face_image)pil_image.show()demo3 : 顯示未知圖片中已知人物的臉
# 識(shí)別人臉鑒定是哪個(gè)人import face_recognition from PIL import Image, ImageDraw import cv2 PATH = "/Users/tanyashi/Python/python project/face_recognition/一起同過(guò)窗" VIEW_PIC_NAME="view2.jpg" UNKNOWN_IMAGE="view_Mr_G.jpg" #將jpg文件加載到numpy數(shù)組中 view_image = face_recognition.load_image_file(VIEW_PIC_NAME) #要識(shí)別的圖片 unknown_image = face_recognition.load_image_file(UNKNOWN_IMAGE) results = [] #獲取每個(gè)圖像文件中每個(gè)面部的面部編碼 #由于每個(gè)圖像中可能有多個(gè)面,所以返回一個(gè)編碼列表。 #但是由于我知道每個(gè)圖像只有一個(gè)臉,我只關(guān)心每個(gè)圖像中的第一個(gè)編碼,所以我取索引0。 view_face_encoding = face_recognition.face_encodings(view_image)[0] print("view_face_encoding:{}\n\n".format(view_face_encoding)) known_faces = [view_face_encoding] face_locations = face_recognition.face_locations(unknown_image) print('got {} face(s) in {}:'.format(len(face_locations), UNKNOWN_IMAGE)) for face_location in face_locations:top, right, bottom, left = face_location#print(top, right, bottom, left)face_image = unknown_image[top:bottom, left:right]face_encoding = face_recognition.face_encodings(face_image)if face_encoding:result = {}result['face_encoding'] = face_encodingresult['is_view'] = face_recognition.compare_faces(known_faces, face_encoding[0])[0]result['location'] = face_locationresult['face_id'] = face_locations.index(face_location)results.append(result)if result['is_view']:print('face {} is view!!'.format(result['face_id']+1))#print(top, right, bottom, left) #print("results :{}".format([i['is_view'] for i in results])) print("please find out view in this image:") view_face_location = [i['location'] for i in results if i['is_view']] if view_face_location:top, right, bottom, left = view_face_location[0]#print(top, right, bottom, left)face_image = unknown_image[top:bottom, left:right]pil_image = Image.fromarray(face_image)pil_image.show() else:print('view is not in this pic!')demo4 : 攝像頭頭像識(shí)別
# 攝像頭頭像識(shí)別 import face_recognition import cv2video_capture = cv2.VideoCapture(0)# 本地圖像 chenduling_image = face_recognition.load_image_file("chenduling.jpg") chenduling_face_encoding = face_recognition.face_encodings(chenduling_image)[0]# 本地圖像二 sunyizheng_image = face_recognition.load_image_file("sunyizheng.jpg") sunyizheng_face_encoding = face_recognition.face_encodings(sunyizheng_image)[0]# 本地圖片三 zhangzetian_image = face_recognition.load_image_file("zhangzetian.jpg") zhangzetian_face_encoding = face_recognition.face_encodings(zhangzetian_image)[0]# Create arrays of known face encodings and their names # 臉部特征數(shù)據(jù)的集合 known_face_encodings = [chenduling_face_encoding,sunyizheng_face_encoding,zhangzetian_face_encoding ]# 人物名稱(chēng)的集合 known_face_names = ["michong","sunyizheng","chenduling" ]face_locations = [] face_encodings = [] face_names = [] process_this_frame = Truewhile True:# 讀取攝像頭畫(huà)面ret, frame = video_capture.read()# 改變攝像頭圖像的大小,圖像小,所做的計(jì)算就少small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)# opencv的圖像是BGR格式的,而我們需要是的RGB格式的,因此需要進(jìn)行一個(gè)轉(zhuǎn)換。rgb_small_frame = small_frame[:, :, ::-1]# Only process every other frame of video to save timeif process_this_frame:# 根據(jù)encoding來(lái)判斷是不是同一個(gè)人,是就輸出true,不是為flaseface_locations = face_recognition.face_locations(rgb_small_frame)face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)face_names = []for face_encoding in face_encodings:# 默認(rèn)為unknownmatches = face_recognition.compare_faces(known_face_encodings, face_encoding)name = "Unknown"# if match[0]:# name = "michong"# If a match was found in known_face_encodings, just use the first one.if True in matches:first_match_index = matches.index(True)name = known_face_names[first_match_index]face_names.append(name)process_this_frame = not process_this_frame# 將捕捉到的人臉顯示出來(lái)for (top, right, bottom, left), name in zip(face_locations, face_names):# Scale back up face locations since the frame we detected in was scaled to 1/4 sizetop *= 4right *= 4bottom *= 4left *= 4# 矩形框cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)#加上標(biāo)簽cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)font = cv2.FONT_HERSHEY_DUPLEXcv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)# Displaycv2.imshow('monitor', frame)# 按Q退出if cv2.waitKey(1) & 0xFF == ord('q'):breakvideo_capture.release() cv2.destroyAllWindows()demo5人臉識(shí)別簽到系統(tǒng)
# import face_recognition import face_recognition import cv2 import os import numpy as np from PIL import Image, ImageDraw, ImageFont import dlib import datetime import threading # 導(dǎo)入threading模塊 import time from imutils import face_utils from scipy.spatial import distanceimport yagmailclass Recorder:passrecord_dic = {} # 訪問(wèn)記錄字典 unknownjpg = [] # 保存來(lái)訪的陌生人的照片的列表 flagover = 0 # 用于全局判斷是否需要保存來(lái)訪信息def sendemail(title, contents, fileslist):yag = yagmail.SMTP(user="2215863456@qq.com", password="agjnddqxmibydjej", host='smtp.qq.com')yag.send('zeuskkk@163.com', title, contents, fileslist)# 雙眼上下眼距檢測(cè) def eye_aspect_ratio(eye):# print(eye)A = distance.euclidean(eye[1], eye[5]) # euclidean是計(jì)算歐氏距離B = distance.euclidean(eye[2], eye[4])C = distance.euclidean(eye[0], eye[3])ear = (A + B) / (2.0 * C)return ear# 字典轉(zhuǎn)換為字符串 def dicttostr():strlist = []listkey = list(sorted(record_dic.keys()))for item in listkey:strlist.extend([item + ',' + str(onetime) for onetime in record_dic[item].times])return strlistdef saveRecorder(name, frame):global record_dicglobal flagoverglobal unknownjpgif flagover == 1:returntry:rec = record_dic[name]secondsDiff = (datetime.datetime.now() - rec.times[-1]).total_seconds()if secondsDiff < 60 * 10: # 如果兩次比較的時(shí)間在10分鐘內(nèi)將被過(guò)濾掉returnrec.times.append(datetime.datetime.now())print('更新記錄', record_dic, rec.times)except (KeyError):newRec = Recorder()newRec.times = [datetime.datetime.now()]record_dic[name] = newRecprint('添加記錄', record_dic, newRec.times)if name == '未知人員':s = str(record_dic[name].times[-1])print('寫(xiě)入', s[:10] + s[-6:])filename = s[:10] + s[-6:] + '.jpg'cv2.imwrite(filename, frame)unknownjpg.append(filename)def loop_timer_handler(): # 定時(shí)器循環(huán)觸發(fā)函數(shù)print('————————Timer headle!————————', str(datetime.datetime.now()))global timer2global flagoverglobal record_dicglobal unknownjpgflagover = 1timer2 = threading.Timer(60 * 1, loop_timer_handler) # 創(chuàng)建定時(shí)器 每1分鐘執(zhí)行一次timer2.start()# 如果mail_content不為空,則發(fā)送郵件通知mail_content = '\n'.join(dicttostr())if mail_content.strip(): # 如果有新的記錄內(nèi)容就發(fā)送郵件sendemail("來(lái)訪統(tǒng)計(jì)記錄", mail_content, unknownjpg)print('來(lái)訪登記記錄郵件已發(fā)送', mail_content)record_dic.clear()unknownjpg.clear()print("清空")time.sleep(10)print("重新開(kāi)始")flagover = 0timer2 = threading.Timer(2, loop_timer_handler) timer2.start()def load_img(sample_dir):print('加載已知人員圖片...')for (dirpath, dirnames, filenames) in os.walk(sample_dir): # 一級(jí)一級(jí)的文件夾遞歸print(dirpath, dirnames, filenames)facelib = []for filename in filenames:filename_path = os.sep.join([dirpath, filename])faceimage = face_recognition.load_image_file(filename_path)# 但是由于我知道每個(gè)圖像只有一個(gè)臉,我只關(guān)心每個(gè)圖像中的第一個(gè)編碼,所以我取索引0face_encoding = face_recognition.face_encodings(faceimage)[0]facelib.append(face_encoding)return facelib, filenamesdef add_text_cv2(text, left, bottom):'''由于cv2不支持中文顯示,所以在視頻流上顯示中文時(shí),需要先將幀數(shù)據(jù)轉(zhuǎn)換成PIL圖像,然后利用PIL來(lái)添加中文:param text: 要在圖像上添加的文字 :param left: 添加文字離左邊框的距離:param bottom: 添加文字離下邊框距離:return: '''global frameimg_PIL = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)) # 轉(zhuǎn)換圖片格式font = ImageFont.truetype('simhei.ttf', 40) # 加載字體position = (left, bottom) # 指定文字輸出位置draw = ImageDraw.Draw(img_PIL) # 繪制照片draw.text(position, text, font=font, fill=(255, 255, 255)) # 繪制文字frame = cv2.cvtColor(np.asarray(img_PIL), cv2.COLOR_RGB2BGR) # 將圖片轉(zhuǎn)回OpenCV格式facelib, facename = load_img('../facelib')video_capture = cv2.VideoCapture(0) # 獲得攝像頭face_locations = [] # 定義列表存放人臉位置 face_encodings = [] # 定義列表存放人臉特征編碼 process_this_frame = True # 定義信號(hào)量frame_counter = 0 # 連續(xù)幀計(jì)數(shù) EYE_AR_THRESH = 0.3 # EAR閾值 EYE_AR_CONSEC_FRAMES = 3 # 當(dāng)EAR小于閾值時(shí),接連多少幀一定發(fā)生眨眼動(dòng)作# 對(duì)應(yīng)特征點(diǎn)的序號(hào) RIGHT_EYE_START = 37 - 1 RIGHT_EYE_END = 42 - 1 LEFT_EYE_START = 43 - 1 LEFT_EYE_END = 48 - 1while True:ret, frame = video_capture.read() # 捕獲一幀圖片small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25) # 將圖片縮小1/4,為人臉識(shí)別提速# opencv的圖像格式默認(rèn)是BGR格式,大家可以想象成像素排列跟RGB格式不一樣,所以我們必須做一點(diǎn)調(diào)整,將像素點(diǎn)進(jìn)行反向rgb_small_frame = small_frame[:, :, ::-1] # 將opencv的BGR格式轉(zhuǎn)為RGB格式# 找到這一幀圖像上所有的人臉位置face_locations = face_recognition.face_locations(rgb_small_frame)# 得到面部編碼(見(jiàn)資料文章)face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)# print('人臉特征編碼長(zhǎng)度', len(face_encodings))face_names = [] # 定義列表,放置識(shí)別出來(lái)的人員的名字for face_encoding in face_encodings: # 循環(huán)多張人臉, 這里一般只有一張人臉matches = face_recognition.compare_faces(facelib, face_encoding, tolerance=0.39)name = "未知人員" # 定義默認(rèn)的識(shí)別結(jié)果為Unknown# 有true說(shuō)明圖片對(duì)比已經(jīng)成功,取出識(shí)別的人員名字并開(kāi)始進(jìn)行活體眨眼檢測(cè)if True in matches: # 如果識(shí)別出來(lái),就將名稱(chēng)取出# 這里matches是個(gè)列表,包含每張已知圖片的對(duì)比結(jié)果,這里應(yīng)該只有一個(gè)為true# 所以我們要找出true的位置,這個(gè)位置就是找到的那個(gè)人的圖片first_match_index = matches.index(True)# 取出這個(gè)人的名字name = facename[first_match_index][:-4] # -4是去掉文件后綴print('開(kāi)始活體檢測(cè)...')# 在圖上加上請(qǐng)眨眼的提示add_text_cv2('{}請(qǐng)眨眼...'.format(name), 0, 0) # 在圖像上添加眨眼提示detector = dlib.get_frontal_face_detector() # 人臉檢測(cè)器# 特征檢測(cè)算法要做個(gè)介紹predictor = dlib.shape_predictor('../libs/shape_predictor_68_face_landmarks.dat') # 人臉特征點(diǎn)檢測(cè)器gray = cv2.cvtColor(rgb_small_frame, cv2.COLOR_BGR2GRAY)# 人臉檢測(cè),1代表把圖片像素放大1倍以便能夠搜集到更多的照片細(xì)節(jié),返回檢測(cè)到所有人臉區(qū)域的數(shù)組# 這個(gè)數(shù)不能設(shè)置過(guò)大,否則影響運(yùn)行速度rects = detector(gray, 1)if len(rects) > 0:print('#' * 20)shape = predictor(gray, rects[0]) # 檢測(cè)特征點(diǎn),rect[0]代表取第一個(gè)人臉# 將所有的臉部坐標(biāo)點(diǎn)轉(zhuǎn)換為numpy array的格式# convert the facial landmark (x, y)-coordinates to a NumPy arraypoints = face_utils.shape_to_np(shape)leftEye = points[LEFT_EYE_START:LEFT_EYE_END + 1] # 取出左眼對(duì)應(yīng)的特征點(diǎn)rightEye = points[RIGHT_EYE_START:RIGHT_EYE_END + 1] # 取出右眼對(duì)應(yīng)的特征點(diǎn)leftEAR = eye_aspect_ratio(leftEye) # 計(jì)算左眼EARrightEAR = eye_aspect_ratio(rightEye) # 計(jì)算右眼EARear = (leftEAR + rightEAR) / 2.0 # 求左右眼EAR的均值# 如果EAR小于閾值,開(kāi)始計(jì)算連續(xù)幀,只有連續(xù)幀計(jì)數(shù)超過(guò)EYE_AR_CONSEC_FRAMES時(shí),才會(huì)計(jì)做一次眨眼if ear < EYE_AR_THRESH:frame_counter += 1print('ear小于閾值了!!!', frame_counter)if frame_counter >= EYE_AR_CONSEC_FRAMES:# blink_counter += 1print('眨眼檢測(cè)成功,請(qǐng)進(jìn)入!')frame_counter = 0print('活體檢測(cè)結(jié)束')face_names.append(name) # 保存識(shí)別結(jié)果# process_this_frame = not process_this_frame #信號(hào)量保護(hù)結(jié)束(這個(gè)語(yǔ)句沒(méi)什么用)# 顯示結(jié)果for (top, right, bottom, left), name in zip(face_locations, face_names):top *= 4 # 還原人臉的原始尺寸,原來(lái)是1/4,現(xiàn)在放到原大小right *= 4bottom *= 4left *= 4# frame:要顯示的幀,(left, top), (right, bottom) 各方向坐標(biāo),(0, 0, 255) 人臉框顏色, 2是線條粗細(xì)cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2) # 標(biāo)注人臉add_text_cv2(name, left + 6, bottom - 6)saveRecorder(name, frame) # 過(guò)濾并保存記錄cv2.imshow('security check', frame) # 顯示圖片檢測(cè)框if cv2.waitKey(1) & 0xFF == ord('q'): # 等待鍵盤(pán)輸入,當(dāng)輸入q時(shí),整個(gè)程序退出breakvideo_capture.release() # 釋放攝像頭資源 time.sleep(2) # 休眠10秒 timer2.cancel() # 關(guān)閉定時(shí)器線程謝謝大家,如有需要,請(qǐng)聯(lián)系我
郵箱為zeuskkk@163.com
總結(jié)
以上是生活随笔為你收集整理的机器学习之人脸识别face_recognition使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 美间如何生成海报支持AI文本库?美间生成
- 下一篇: 神经网络与深度学习(邱锡鹏)-学习笔记