文 | 某某白米饭

来源:Python 技术「ID: pythonall」


当你在上班摸鱼的时候,领导总会偷偷摸摸的出现在你的背后,例如小编曾经偷偷摸摸看《轻音》被抓包了。今天我们就用 Python 来破解这个摸鱼被抓的套路,主要的思路是用 opencv 调用电脑摄像头检测和比对人脸,当领导出现在后面的时候打开指定的应用程序浑水摸鱼。

安装模块

在写程序之前得把 opencv 调用摄像头模块和 face_locations 人脸识别模块安装好。

# opencv 模块pip3 insatll opencv-python
# face_locations 模块pip3 install cmakepip3 install face_recognition

人脸识别

先把领导的人脸进行编码,放入内存中以便随时和摄像头捕捉到的人脸识别进行编码比对。

例如对下图进行面部编码

import face_recognition# 加载图片pic_boss = face_recognition.load_image_file("/Users/xx/Desktop/face/0.png")# 得到面部编码boss_face_encoding = face_recognition.face_encodings(pic_boss)[0]

示例结果

[-0.02630499  0.12300251  0.01698755  0.01275834 -0.07418888 -0.00981654 -0.03014973 -0.16349442  0.11407443 -0.03254088  0.26810074 -0.10167226 -0.15427223 -0.11180711  0.01873804  0.18030289 -0.14980686 -0.12194286 -0.02620432 -0.03438358  0.04720668  0.05182001  0.009987    0.09340413 -0.11347414 -0.26094455 -0.07678577 -0.09128669  0.08928929 -0.07264765 -0.11346096  0.1254302  -0.20916753 -0.10639326  0.09938065  0.05473833 -0.04935627 -0.06184902  0.17554277 -0.02231439 -0.19398358  0.01744412  0.10445927  0.26399308  0.21656345  0.05588599  0.00760998 -0.13855973  0.12407181 -0.09017442  0.09778374  0.11776066  0.08498169  0.07626694 -0.01237833 -0.20856641  0.02468708  0.06579788 -0.12179989  0.02987257  0.1338616  -0.07621925  0.01559527 -0.03452411  0.18915619  0.01698355 -0.04450341 -0.2167782   0.09793964 -0.11409818 -0.10012487  0.13745219 -0.17124982 -0.15017164 -0.34856451 -0.01826046  0.41654593  0.09037441 -0.21255262  0.04289294  0.01760755 -0.01859214  0.20364219  0.14642054  0.00619181 -0.02451363 -0.15138477  0.00500458  0.25368348 -0.02767867 -0.09737059  0.17870165 -0.02200082  0.03460512  0.03690759  0.06052291 -0.0686012   0.04330266 -0.15649761 -0.09057935  0.00870521  0.04586265 -0.04279764  0.18815981 -0.15697879  0.17292421  0.03271531  0.08370531 -0.04779428 -0.05095051 -0.08721299  0.01937558  0.10537415 -0.21216688  0.16163379  0.07646587  0.09025833  0.08259746  0.08811771  0.06535679 -0.01029789 -0.06432205 -0.25512969 -0.03111095  0.12503427 -0.02948561  0.15236887  0.03259711]

调用摄像头

用 cv2.VideoCapture(0) 方法调起摄像头,并把摄像头的每一个帧的图像进行面部编码,最后使用 face_recognition.compare_faces() 方法进行人脸比对。

import face_recognitionimport cv2import osimport time# 调用摄像头,外部摄像头为:1cap = cv2.VideoCapture(0)while True:    # 按帧读取视频    # 其中ret是布尔值,如果读取帧是正确的则返回 True,    # frame就是每一帧的图像    ret, frame = cap.read()    # 进行面部编码    face_locations = face_recognition.face_locations(frame)    face_encodings = face_recognition.face_encodings(frame, face_locations)    for face_encoding in face_encodings:        # 人脸对比        results = face_recognition.compare_faces([boss_face_encoding], face_encoding)        if results[0]:            print("boss来了,快打开其他应用")            os.system('open /Applications/PyCharm.app')            time.sleep(300)

需要注意的是 cv2.VideoCapture(0) 方法在 VSCode 中用终端调用会报:Abort trap: 6 的错误。

在系统自带的终端中运行则没这个问题。

总结

在上班可以摸鱼的时候好好摸鱼,该拼命奋斗的时候千万千万别摸鱼了。

©著作权归作者所有:来自51CTO博客作者mob604756e8ada8的原创作品,如需转载,请注明出处,否则将追究法律责任

更多相关文章

  1. 阿里云 RTC QoS 弱网对抗之变分辨率编码
  2. 探索emoji字符串长度之谜
  3. 一篇文章带你了解Android Usb摄像头
  4. 女朋友问我:为什么会乱码?
  5. HBase优化 | 合理的使用编码压缩
  6. 利用自编码神经网络,检测未标记数据集中的异常值
  7. maven-resources-plugin修改了我的文件
  8. URL百分比编码与符号字符
  9. 这两款摄像头都有FPGA,可以支持实时图像处理!

随机推荐

  1. 【Android 进阶】Android Home 键监听
  2. Android获取在桌面上有图标的app列表的方
  3. Android Camera 模块分析
  4. android CallCard.java/updateState(Phon
  5. android 连接webservice
  6. android 通过百度地图定位获取坐标在导航
  7. android:layout_gravity和android:gravit
  8. Android视图框架
  9. 【Android自学笔记】android实现TextView
  10. Android的地图服务