# -*- coding: utf-8 -*-
import face_recognition
import cv2
rtsp_url='rtsp://admin:123456@192.168.2.244:554/Streaming/Channels/101?transportmode=multicast'
#rtsp_url='G:/cascades/aobama.mp4'
#rtsp_url='G:/cascades/video1.mp4'
#rtsp_url='G:/cascades/limingzhou.mp4'
video_capture = cv2.VideoCapture(rtsp_url)

#obama_img = face_recognition.load_image_file("G:/cascades/obama11.jpg") 修改第一处
#wzr_img = face_recognition.load_image_file("G:/cascades/wzr.jpg")
lmz_img = face_recognition.load_image_file("G:/cascades/lmz.jpg")
#obama_face_encoding = face_recognition.face_encodings(obama_img)[0]   修改第二处
lmz_face_encoding = face_recognition.face_encodings(lmz_img)[0]
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)

    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([lmz_face_encoding], face_encoding)
            #match = face_recognition.compare_faces([wzr_face_encoding], face_encoding)
            #match = face_recognition.compare_faces([obama_face_encoding], face_encoding) 修改第三处

            if match[0]:
                name = "limingzhou"
            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()

参考网站:https://blog.csdn.net/hongbin_xu/article/details/76284134

参考问题:如果自己的录的视频无法识别,请用格式工厂修改一下视频格式和图片格式

Logo

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。

更多推荐