吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2760|回复: 19
收起左侧

[Python 转载] 利用opencv实现一个电脑人脸解锁效果

  [复制链接]
bean0283 发表于 2022-8-19 11:51
本帖最后由 bean0283 于 2022-8-19 15:07 编辑

学了3天python,看上了一个opencv的玩意,想着就利用这个功能自己摸索实现一个功能,主要效果是当摄像头前不存在人,或者存在的人不是你事先录入的人,我设定的阈值是10s,屏幕就弹出一下全屏窗口,保护隐私
台式电脑没有摄像头的可以用DroidCam.Client,使用手机来当电脑的摄像头
image.png
项目架构很简单,主要是在img文件夹内放入你自己的自拍照即可
下面是源码,代码很多是网上cv拼凑然后微调出来了,大佬不要喷我
[Python] 纯文本查看 复制代码
import os
import time

import cv2
import face_recognition
import numpy as np
import win32con
import win32gui

video_capture = cv2.VideoCapture(0)
img = cv2.imread("res/img.png")

path = os.getcwd()
os.chdir(path)
images_file = os.listdir(path+'/img')

known_face_encodings = []
known_face_names = []

for each in images_file:
    image_path = path + '/img/'+each
    known_face_encodings.append(face_recognition.face_encodings(face_recognition.load_image_file(image_path))[0])
    name = os.path.splitext(each)[0]
    known_face_names.append(name)


face_names = []
face_locations = []
face_encodings = []
process_this_frame = True

cascade = cv2.CascadeClassifier("./res/haarcascade_frontalface_default.xml")  ## 读入分类器数据
i = 0

statTime = time.time()
endTime = time.time()
ck1 = False

while True:

    if endTime - statTime > 10:
        print("人脸消失超过10s,自动锁屏")
        out_win = "output_style_full_screen"
        cv2.namedWindow(out_win, cv2.WINDOW_NORMAL)
        cv2.setWindowProperty(out_win, cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
        cv2.imshow(out_win, img)
        #window前置
        if ck1 == False:
            hwnd = win32gui.FindWindow(None, out_win)  # 获取句柄,然后置顶
            CVRECT = cv2.getWindowImageRect(out_win)
            win32gui.SetWindowPos(hwnd, win32con.HWND_TOPMOST, 0, 0, CVRECT[2], CVRECT[3], win32con.SWP_SHOWWINDOW)
        # cv2.waitKey(0)
        ck1 = True
    elif ck1 and endTime - statTime < 10:
        cv2.destroyWindow(out_win)
        ck1 = False
    ret, frame = video_capture.read()
    sample_image =  cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)  ## 图片地址
    faces = cascade.detectMultiScale(sample_image, scaleFactor=1.1, minNeighbors=5, minSize=(50, 50))

    if len(faces) > 0:
        print("存在" + str(len(faces)) + "张人脸")

    else:
      #  print("不存在人脸")
       endTime = time.time()

    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 face_encoding in face_encodings:
            matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
            name = "Unknown"
            face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)
            best_match_index = np.argmin(face_distances)
            if matches[best_match_index]:
                name = known_face_names[best_match_index]
            face_names.append(name)
    for name in face_names:
        #print(name[0])
        print(name)
        if name in known_face_names:
            statTime = time.time()
        else:
            endTime = time.time()

    process_this_frame = not process_this_frame
    for (top, right, bottom, left), name in zip(face_locations, face_names):
        top *= 4
        left *= 4
        right *= 4
        bottom *= 4
        font = cv2.FONT_HERSHEY_DUPLEX
        cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
        cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
        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()


模块涉及的主要包,需要按顺序安装,不然可能报错
pip install cmake -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install dlib -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install face_recognition -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install opencv-python -i https://pypi.tuna.tsinghua.edu.cn/simple

其中dlib包有些电脑安装可能会报错,建议百度

最后源码上传:[url=]FaceMonitoring.rar[/url]

FaceMonitoring.rar

1.38 MB, 下载次数: 78, 下载积分: 吾爱币 -1 CB

免费评分

参与人数 3吾爱币 +2 热心值 +3 收起 理由
ShuyaoDong + 1 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
k37555 + 1 谢谢@Thanks!
ccwuax + 1 + 1 我很赞同!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

luliucheng 发表于 2022-8-19 13:02
提醒:常规安装dlib需要cmake,vs等复杂操作。最简单的办法是安装Python2.7或3.4~3.6。另外,dlib的清华源可能下载不了,建议更换。

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
bean0283 + 1 + 1 多谢提醒

查看全部评分

 楼主| bean0283 发表于 2022-10-7 18:26
BUCTPJP 发表于 2022-10-7 18:03
为什么放入自己的照片后别人的人脸也会解锁

这个代码是练手项目,需要自己改一改判断逻辑,这样效果更好,我现在改用虹软的sdk了,那个识别效率更高,想要研究的可以去看看
ccwuax 发表于 2022-8-19 12:13
这个真不错,可以让电脑也实现面容解锁了,赞,收藏了,感谢分享
iawyxkdn8 发表于 2022-8-19 12:18
搞个图片能过吗?
 楼主| bean0283 发表于 2022-8-19 12:26
iawyxkdn8 发表于 2022-8-19 12:18
搞个图片能过吗?

能过的,不然就不是这个水平了
zzkz 发表于 2022-8-19 12:35
摸鱼神器
丨miss丶星星 发表于 2022-8-19 12:43
看起来很强大的感觉嗯
头像被屏蔽
bk20220721 发表于 2022-8-19 13:12
提示: 作者被禁止或删除 内容自动屏蔽
trouman 发表于 2022-8-19 14:04
对电脑性能影响大吗
Prozacs 发表于 2022-8-19 14:05
更安全点就在上锁后加上手势识别解锁
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-12 10:53

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表