Python 监控

由于时效问题,该文某些代码、技术可能已经过期,请注意!!!本文最后更新于:2 年前

使用python监控电脑键盘、鼠标并拍照录像

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import keyboard
from cv2 import cv2
# from pynput.mouse import Listener
import pyautogui as pag #监听鼠标
# from pynput.keyboard import Key, Listener
from threading import Thread

x1, y1 = pag.position()
# print(x1, y1)

def camera():
'''
拍照
'''
cap = cv2.VideoCapture(0)
ret,frame = cap.read() #读取摄像头内容
cv2.imwrite("./test.jpg",frame) #保存到磁盘
#释放摄像头
cap.release()

def record_video():
'''
录制视频
'''
cap = cv2.VideoCapture(0)
fps = 30
size=(int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
videoWriter=cv2.VideoWriter('./test.avi',cv2.VideoWriter_fourcc('X','V','I','D'),fps,size)
success,frame = cap.read()
numFrameRemaining = 5 * fps #摄像头捕获持续时间
while success and numFrameRemaining > 0:
videoWriter.write(frame)
success,frame = cap.read()
numFrameRemaining -= 1

cap.release()

def display_video():
'''
实时窗口
'''
face_locations = []
cap = cv2.VideoCapture(0)

while True:
# Grab a single frame of video
ret, frame = cap.read()

# Convert the image from BGR color (whichOpenCV uses) to RGB color (which face_recognition uses)
rgb_frame = frame[:, :, ::-1]

# Find all the faces in the current frameof video
face_locations = face_recognition.face_locations(rgb_frame)

# Display the results
for top, right, bottom, left in face_locations:
# Draw a box around the face
cv2.rectangle(frame, (left, top),(right, bottom), (0, 0, 255), 2)

# Display the resulting image
cv2.imshow('Video', frame)

# Hit 'q' on the keyboard to quit!
if cv2.waitKey(1) & 0xFF == ord('q'):
break

# Release handle tothe webcam
cap.release()
cv2.destroyAllWindows()

def display_video2():
'''
实时检测
'''
#存储知道人名列表
known_names=['yahaha1', 'yahaha2']
#存储知道的特征值
known_faces=[]

image1 =face_recognition.load_image_file("yahaha2.jpg")
face_encoding1 =face_recognition.face_encodings(image1)

image2 =face_recognition.load_image_file("yahaha1.jpg")
face_encoding2 =face_recognition.face_encodings(image1)

if face_encoding1 and face_encoding2:
face_encoding1 = face_encoding1[0]
face_encoding2 = face_encoding2[0]
else:
sys.exit()

known_faces = [face_encoding1, face_encoding2]

cap = cv2.VideoCapture(0)

while True:
# Grab a single frame of video
ret, frame = cap.read()
# Convert the image from BGR color (whichOpenCV uses) to RGB color (which face_recognition uses)
rgb_frame = frame[:, :, ::-1]

# Find all the faces and face encodings inthe current frame of video
face_locations =face_recognition.face_locations(rgb_frame) # 如有gpu可添加参数model='cnn'提升精度
face_encodings =face_recognition.face_encodings(rgb_frame, face_locations)

face_names = []
for face_encoding in face_encodings:
# See if the face is a match for theknown face(s)
matches =face_recognition.compare_faces(known_faces, face_encoding, tolerance=0.60)

name = None
# if match[0]:
# name = "Yahaha"
print(matches)
if True in matches:
first_match_index = matches.index(True)
name = known_names[first_match_index]
else:
name = 'Unkonwn'

face_names.append(name)

# Label the results
for (top, right, bottom, left), name in zip(face_locations, face_names):
if not name:
continue

# Draw a box around the face
cv2.rectangle(frame, (left, top),(right, bottom), (0, 0, 255), 2)
# Draw a label with a name below theface
cv2.rectangle(frame, (left, bottom -25), (right, bottom), (0, 0, 255), cv2.FILLED)
font = cv2.FONT_HERSHEY_DUPLEX
cv2.putText(frame, name, (left + 6,bottom - 6), font, 0.5, (255, 255, 255), 1)

cv2.imshow('Video', frame)

# Hit 'q' on the keyboard to quit!
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# All done!
cap.release()
cv2.destroyAllWindows()

def proof(x):
# print(x)
# record_video()
camera()

def monitor_keyboard():
keyboard.hook(proof)
#按下任何按键时,都会调用proof,其中一定会传一个值,就是键盘事件
keyboard.wait()


def monitor_mouse():
x2, y2 = pag.position()
while x1 == x2:
x2, y2 = pag.position()
else:
# record_video()
camera()

if __name__ == '__main__':
k = Thread(target=monitor_keyboard, args=())
m = Thread(target=monitor_mouse, args=())
k.start()
m.start()
k.join()
m.join()


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!