在這里,我用了anaconda,OpenCV,python3.6來實現代碼的,代碼在附件中,可以自己下載了,自己試試
python源程序如下:
- # -*- coding: UTF-8 -*-
- import face_recognition
- import cv2
- import os
- # 這是一個超級簡單(但很慢)的例子,在你的網絡攝像頭上實時運行人臉識別
- # PLEASE NOTE: This example requires OpenCV (the `cv2` library) to be installed only to read from your webcam.
- # 請注意:這個例子需要安裝OpenCV
- # 具體的演示。如果你安裝它有困難,試試其他不需要它的演示。
- # 得到一個參考的攝像頭# 0(默認)
- video_capture = cv2.VideoCapture(0)
- # 加載示例圖片并學習如何識別它。
- path ="images"#在同級目錄下的images文件中放需要被識別出的人物圖
- total_image=[]
- total_image_name=[]
- total_face_encoding=[]
- for fn in os.listdir(path): #fn 表示的是文件名
- total_face_encoding.append(face_recognition.face_encodings(face_recognition.load_image_file(path+"/"+fn))[0])
- fn=fn[:(len(fn)-4)]#截取圖片名(這里應該把images文件中的圖片名命名為為人物名)
- total_image_name.append(fn)#圖片名字列表
- while True:
- # 抓取一幀視頻
- ret, frame = video_capture.read()
- # 發現在視頻幀所有的臉和face_enqcodings
- face_locations = face_recognition.face_locations(frame)
- face_encodings = face_recognition.face_encodings(frame, face_locations)
- # 在這個視頻幀中循環遍歷每個人臉
- for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
- # 看看面部是否與已知人臉相匹配。
- for i,v in enumerate(total_face_encoding):
- match = face_recognition.compare_faces([v], face_encoding,tolerance=0.5)
- name = "Unknown"
- if match[0]:
- name = total_image_name[i]
- break
- # 畫出一個框,框住臉
- cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
- # 畫出一個帶名字的標簽,放在框下
- cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
- font = cv2.FONT_HERSHEY_DUPLEX
- cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)
- # 顯示結果圖像
- cv2.imshow('Video', frame)
- # 按q退出
- if cv2.waitKey(1) & 0xFF == ord('q'):
- break
- # 釋放攝像頭中的流
- video_capture.release()
- cv2.destroyAllWindows()
復制代碼
以上代碼下載:
人臉識別.zip
(1.32 KB, 下載次數: 214)
2019-5-18 22:00 上傳
點擊文件名下載附件
|