Python 上班摸魚程序,再也不怕領(lǐng)導(dǎo)偷偷出現(xiàn)在身后了


當(dāng)你在上班摸魚的時候,領(lǐng)導(dǎo)總會偷偷摸摸的出現(xiàn)在你的背后,例如,我曾經(jīng)偷偷摸摸看《輕音》被抓包了
今天我們就用 Python 來破解這個摸魚被抓的套路,主要的思路是用 opencv 調(diào)用電腦攝像頭檢測和比對人臉,當(dāng)領(lǐng)導(dǎo)出現(xiàn)在后面的時候打開指定的應(yīng)用程序渾水摸魚

安裝模塊
在寫程序之前得把 opencv 調(diào)用攝像頭模塊和 face_locations 人臉識別模塊安裝好。
# opencv 模塊
pip3 insatll opencv-python
# face_locations 模塊
pip3 install cmake
pip3 install face_recognition
人臉識別
先把領(lǐng)導(dǎo)的人臉進行編碼,放入內(nèi)存中以便隨時和攝像頭捕捉到的人臉識別進行編碼比對。
例如對下圖進行面部編碼

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]
示例結(jié)果
[-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]
調(diào)用攝像頭
用 cv2.VideoCapture(0) 方法調(diào)起攝像頭,并把攝像頭的每一個幀的圖像進行面部編碼,最后使用 face_recognition.compare_faces() 方法進行人臉比對
import face_recognition
import cv2
import os
import time
# 調(diào)用攝像頭,外部攝像頭為:1
cap = 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來了,快打開其他應(yīng)用")
os.system('open /Applications/PyCharm.app')
time.sleep(300)
需要注意的是, cv2.VideoCapture(0) 方法在 VSCode 中用終端調(diào)用會報:Abort trap: 6 的錯誤。

在系統(tǒng)自帶的終端中運行則沒這個問題
總結(jié)
在上班可以摸魚的時候好好摸魚,該拼命奮斗的時候千萬千萬別摸魚了!
評論
圖片
表情
