概述
功能: 对视频中的演员进行识别标注
需要先建立演员的人脸编码库,才能识别出来额
# 视频中的人脸标注
#合照中的人脸标注
import numpy as np
import cv2
import face_recognition
from PIL import Image, ImageDraw, ImageFont
# 计算图片的对应编码
huang_xiao_ming = face_recognition.face_encodings(face_recognition.load_image_file('./pic/huang_xiao_ming.jpg'))[0]
deng_chao = face_recognition.face_encodings(face_recognition.load_image_file('./pic/deng_chao.jpg'))[0]
shen_teng = face_recognition.face_encodings(face_recognition.load_image_file('./pic/shen_teng.jpg'))[0]
biden = face_recognition.face_encodings(face_recognition.load_image_file('./pic/biden.jpeg'))[0]
feng_shao_feng = face_recognition.face_encodings(face_recognition.load_image_file('./pic/feng_shao_feng.jpg'))[0]
zheng_kai = face_recognition.face_encodings(face_recognition.load_image_file('./pic/zheng_kai.jpg'))[0]
li_yi_feng = face_recognition.face_encodings(face_recognition.load_image_file('./pic/li_yi_feng.jpg'))[0]
han_geng = face_recognition.face_encodings(face_recognition.load_image_file('./pic/han_geng.jpg'))[0]
tong_da_wei = face_recognition.face_encodings(face_recognition.load_image_file('./pic/tong_da_wei.jpg'))[0]
wang_han = face_recognition.face_encodings(face_recognition.load_image_file('./pic/wang_han.jpg'))[0]
lei_jia_yin = face_recognition.face_encodings(face_recognition.load_image_file('./pic/lei_jia_yin.jpg'))[0]
tong_li_ya = face_recognition.face_encodings(face_recognition.load_image_file('./pic/tong_li_ya.jpg'))[0]
di_li_re_ba = face_recognition.face_encodings(face_recognition.load_image_file('./pic/di_li_re_ba.jpg'))[0]
yang_mi = face_recognition.face_encodings(face_recognition.load_image_file('./pic/yang_mi.jpg'))[0]
tang_wei = face_recognition.face_encodings(face_recognition.load_image_file('./pic/tang_wei.jpg'))[0]
known_encodings = [
huang_xiao_ming,
deng_chao,
shen_teng,
biden,
feng_shao_feng,
zheng_kai,
li_yi_feng,
han_geng,
tong_da_wei,
wang_han,
lei_jia_yin,
tong_li_ya,
di_li_re_ba,
yang_mi,
tang_wei
]
people_names = [ 'Huang Xiao-ming','Deng Chao','Shen teng','Joe Biden','Feng Shao-feng','Zheng kai','Li Yi-feng','han_geng','tong_da_wei','wang_han','Lei Jia-yin','Tong_Liya','Di_li_re_ba','Yang_mi','Tang_wei']
people_names_zh = ['黄晓明','邓超','沈腾','拜登','冯绍峰','郑凯','李易峰','韩庚','佟大为','汪涵','雷佳音','佟丽娅','迪丽热巴','杨幂','汤唯']
# 将中文显示在图片上
def cv2ImgAddText(img, text, left, top, textColor=(0, 255, 0), textSize=20):
if (isinstance(img, np.ndarray)): #判断是否OpenCV图片类型
img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
draw = ImageDraw.Draw(img)
fontText = ImageFont.truetype(
"C:/Windows/Fonts/simsun.ttc", textSize, encoding="utf-8")
draw.text((left, top), text, textColor, font=fontText)
return cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)
cap = cv2.VideoCapture("camTest.mp4")
fps = cap.get(cv2.CAP_PROP_FPS)
process_thid_frame = True
while cap.isOpened():
ret, frame = cap.read()
if not ret: break
rgb_frame = cv2.cvtColor(frame,cv2.COLOR_BGR2RGB)
face_locations = face_recognition.face_locations(rgb_frame)
face_encodings = face_recognition.face_encodings(rgb_frame, face_locations)
face_names = [] #存储出现在画面中人脸的名字
for face_encoding in face_encodings:
matches = face_recognition.compare_faces(known_encodings, face_encoding,tolerance=0.4)
if True in matches:
first_match_index = matches.index(True)
print(first_match_index)
name =people_names_zh[first_match_index]
else:
name="unknown"
face_names.append(name)
# 将捕捉到的人脸显示出来
for (top, right, bottom, left), name in zip(face_locations, face_names):
cv2.rectangle(rgb_frame, (left, top), (right, bottom), (0, 255, 0), 2) # 画人脸矩形框
rgb_frame = cv2ImgAddText(rgb_frame,name, left+10, top-20, (0, 0,255), 20) #标注人名
cv2.imshow('video capture',cv2.cvtColor(rgb_frame,cv2.COLOR_RGB2BGR))
if cv2.waitKey(1000//int(fps)) & 0xff == ord('q'): break
cap.release()
cv2.destroyAllWindows()
比较鸡肋的地方: face_recognition对每一帧的图像处理速度很慢,导致用openCV播放的视频帧速变得很低,即使waitkey(1)也挽救不了,除了优化识别算法,还有什么途径能使视频以正常速度播放呢,欢迎讨论
最后
以上就是诚心麦片为你收集整理的人脸识别(四)视频中的人物识别的全部内容,希望文章能够帮你解决人脸识别(四)视频中的人物识别所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复