python调用摄像头人脸识别代码_利用face_recognition,dlib与OpenCV调用摄像头进行人脸识别...
用已經搭建好 face_recognition,dlib 環境來進行人臉識別
未搭建好環境請參考:
使用opencv 調用攝像頭
import face_recognition
import cv2
video_capture = cv2.videocapture(0)
# videocapture打開攝像頭,0為筆記本內置攝像頭,1為外usb攝像頭,或寫入視頻路徑
mayun_img = face_recognition.load_image_file("mayun.jpg")
jobs_img = face_recognition.load_image_file("jobs.jpg")
mayun_face_encoding = face_recognition.face_encodings(mayun_img)[0]
jobs_face_encoding = face_recognition.face_encodings(jobs_img)[0]
face_locations = []
face_encodings = []
face_names = []
process_this_frame = true
while true:
ret, frame = video_capture.read()
# video_capture.read()按幀讀取視頻,ret,frame是獲video_capture.read()方法的兩個返回值。
# 其中ret是布爾值,如果讀取幀是正確的則返回true,
# 如果文件讀取到結尾,它的返回值就為false。frame就是每一幀的圖像,是個三維矩陣。
small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
# 對截取到的圖像進行處理
if process_this_frame:
face_locations = face_recognition.face_locations(small_frame)
face_encodings = face_recognition.face_encodings(small_frame, face_locations)
face_names = []
for face_encoding in face_encodings:
match = face_recognition.compare_faces([mayun_face_encoding, jobs_face_encoding], face_encoding)
if match[0]:
name = "mayun"
elif match[1]:
name = "jobs"
else:
name = "unknown"
print(name)
face_names.append(name)
process_this_frame = not process_this_frame
for (top, right, bottom, left), name in zip(face_locations, face_names):
top *= 4
right *= 4
bottom *= 4
left *= 4
cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), 2)
font = cv2.font_hershey_duplex
cv2.puttext(frame, name, (left+6, bottom-6), font, 1.0, (255, 255, 255), 1)
cv2.imshow('video', frame)
if cv2.waitkey(1) & 0xff == ord('q'):
break
video_capture.release()
cv2.destroyallwindows()
我使用手機中的照片來進行驗證
因為是按每一幀進行讀取:
有些人在使用時會出現攝像頭打不開的情況
我使用的是win10系統給出一個建議:
在設置中找到隱私設置,在找到相機選項,把權限打開即可
希望與廣大網友互動??
點此進行留言吧!
總結
以上是生活随笔為你收集整理的python调用摄像头人脸识别代码_利用face_recognition,dlib与OpenCV调用摄像头进行人脸识别...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 常用编程语言简介大全
- 下一篇: yii+php+当前目录,Yii常用路径