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
import pyautogui as pag
from threading import Thread
x1, y1 = pag.position()
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: ret, frame = cap.read()
rgb_frame = frame[:, :, ::-1]
face_locations = face_recognition.face_locations(rgb_frame)
for top, right, bottom, left in face_locations: cv2.rectangle(frame, (left, top),(right, bottom), (0, 0, 255), 2)
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'): break
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: ret, frame = cap.read() rgb_frame = frame[:, :, ::-1]
face_locations =face_recognition.face_locations(rgb_frame) face_encodings =face_recognition.face_encodings(rgb_frame, face_locations)
face_names = [] for face_encoding in face_encodings: matches =face_recognition.compare_faces(known_faces, face_encoding, tolerance=0.60)
name = None 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)
for (top, right, bottom, left), name in zip(face_locations, face_names): if not name: continue
cv2.rectangle(frame, (left, top),(right, bottom), (0, 0, 255), 2) 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)
if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows()
def proof(x): camera()
def monitor_keyboard(): keyboard.hook(proof) keyboard.wait()
def monitor_mouse(): x2, y2 = pag.position() while x1 == x2: x2, y2 = pag.position() else: camera()
if __name__ == '__main__': k = Thread(target=monitor_keyboard, args=()) m = Thread(target=monitor_mouse, args=()) k.start() m.start() k.join() m.join()
|