我们知道当今最火的莫过于人工智能了,人工智能指在计算机科学的基础上,综合信息论、心理学、生理学、语言学、逻辑学和数学等知识,制造能模拟人类智能行为的计算机系统的边缘学科。在人工智能的范畴内有两个方向:计算机视觉、自然语音处理(NLP,国内外也有人称NPL)。
# -*- coding: utf-8 -*-
# @Time : 2019/1/4 19:59
# @Author : 胡子旋
# @FileName: Recognition.py
# @Software: PyCharm
# @Email :1017190168@qq.com
import face_recognition
import cv2
import datetime
import glob2 as gb
video_capture = cv2.VideoCapture(0)
img_path = gb.glob(r'D:\pycharmproject\F_recognition\photo\\*.jpg')
known_face_names = []
known_face_encodings = []
for i in img_path:
picture_name = i.replace('D:\pycharmproject\F_recognition\photo\\*.jpg', '')
picture_newname = picture_name.replace('.jpg', '')
someone_img = face_recognition.load_image_file(i)
someone_face_encoding = face_recognition.face_encodings(someone_img)[0]
known_face_names.append(picture_newname)
known_face_encodings.append(someone_face_encoding)
someone_img = []
someone_face_encoding = []
face_locations = []
face_encodings = []
face_names = []
process_this_frame = True
while True:
ret, frame = video_capture.read()
small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
rgb_small_frame = small_frame[:, :, ::-1]
if process_this_frame:
face_locations = face_recognition.face_locations(rgb_small_frame)
face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)
face_names = []
for i in face_encodings:
match = face_recognition.compare_faces(known_face_encodings, i, tolerance=0.39)
if True in match:
match_index = match.index(True)
name = "match"
# To print name and time
cute_clock = datetime.datetime.now()
print(known_face_names[match_index] + ':' + str(cute_clock))
else:
name = "unknown"
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()
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有