使用python程序加bat一鍵運行腳本,媽媽再也不用擔心我的電腦桌面了
import os
import time
import cv2
import pyautogui
import psutil
from datetime import datetimeclass UnlockMonitor : def __init__ ( self) : """初始化監控器""" self. save_dir = os. path. dirname( os. path. abspath( __file__) ) self. log_file = os. path. join( self. save_dir, "unlock_capture_log.txt" ) self. last_state = self. is_locked( ) self. running = True def log_message ( self, message) : """記錄日志到文件和終端""" timestamp = datetime. now( ) . strftime( "%Y-%m-%d %H:%M:%S" ) log_entry = f"[ { timestamp} ] { message} " print ( log_entry) try : with open ( self. log_file, "a" , encoding= "utf-8" ) as f: f. write( log_entry + "\n" ) except Exception as e: print ( f"[ { timestamp} ] 寫入日志失敗: { str ( e) } " ) def is_camera_available ( self) : """檢查攝像頭是否可用""" try : cap = cv2. VideoCapture( 0 , cv2. CAP_DSHOW) if not cap. isOpened( ) : return False cap. release( ) return True except Exception as e: self. log_message( f"攝像頭檢測異常: { str ( e) } " ) return False def capture_camera ( self) : """直接拍照(不對圖像做任何處理)""" timestamp = datetime. now( ) . strftime( "%Y-%m-%d_%H-%M-%S" ) camera_path = os. path. join( self. save_dir, f"camera_ { timestamp} .jpg" ) try : cap = cv2. VideoCapture( 0 , cv2. CAP_DSHOW) cap. set ( cv2. CAP_PROP_FRAME_WIDTH, 1280 ) cap. set ( cv2. CAP_PROP_FRAME_HEIGHT, 720 ) cap. set ( cv2. CAP_PROP_AUTOFOCUS, 1 ) time. sleep( 2 ) if not cap. isOpened( ) : self. log_message( "無法打開攝像頭" ) return ret, frame = cap. read( ) cap. release( ) if ret: cv2. imwrite( camera_path, frame) self. log_message( f"拍照保存至: { camera_path} " ) else : self. log_message( "攝像頭讀取失敗" ) except Exception as e: self. log_message( f"拍照異常: { str ( e) } " ) def capture_screenshot ( self) : """截屏""" timestamp = datetime. now( ) . strftime( "%Y-%m-%d_%H-%M-%S" ) screenshot_path = os. path. join( self. save_dir, f"screenshot_ { timestamp} .png" ) try : screenshot = pyautogui. screenshot( ) screenshot. save( screenshot_path) self. log_message( f"截屏保存至: { screenshot_path} " ) except Exception as e: self. log_message( f"截屏失敗: { str ( e) } " ) def is_already_running ( self) : """檢查是否已有相同進程在運行""" current_pid = os. getpid( ) script_name = os. path. basename( __file__) for proc in psutil. process_iter( [ 'pid' , 'name' , 'cmdline' ] ) : try : if ( proc. info[ 'pid' ] != current_pid and proc. info[ 'cmdline' ] and script_name in ' ' . join( proc. info[ 'cmdline' ] ) ) : return True except ( psutil. NoSuchProcess, psutil. AccessDenied, KeyError) : continue return False def is_locked ( self) : """檢查系統是否處于鎖定狀態""" try : for proc in psutil. process_iter( [ 'name' ] ) : if proc. info[ 'name' ] == 'LogonUI.exe' : return True return False except Exception: return False def run ( self) : """主監控循環""" if self. is_already_running( ) : self. log_message( "已有實例運行,退出當前進程" ) return self. log_message( "===== 解鎖監控服務啟動 =====" ) self. log_message( f"初始狀態: { '鎖定' if self. last_state else '解鎖' } " ) try : while self. running: current_state = self. is_locked( ) if self. last_state and not current_state: self. log_message( "檢測到解鎖事件,開始捕獲..." ) if self. is_camera_available( ) : self. capture_camera( ) self. capture_screenshot( ) self. last_state = current_statetime. sleep( 1 ) except KeyboardInterrupt: self. log_message( "服務被用戶中斷" ) except Exception as e: self. log_message( f"服務異常: { str ( e) } " ) finally : self. log_message( "===== 監控服務停止 =====" ) if __name__ == '__main__' : monitor = UnlockMonitor( ) monitor. run( )
@echo off
chcp 65001 > nul
title 解鎖拍照監控服務
color 0A:: 設置循環標志
set RESTART_COUNT = 0 :restart
echo 正在啟動解鎖拍照監控服務.. . ( 第%RESTART_COUNT%次啟動)
echo 按 Ctrl+C 停止服務:: 設置Python路徑(根據您的環境修改)
set PYTHON_PATH = "C:\Users\lhyyds\.conda\e nvs\hust\python.exe" :: 設置腳本路徑
set SCRIPT_PATH = "%~dp0wake_monitor.py" :: 啟動Python腳本
%PYTHON_PATH% %SCRIPT_PATH%:: 檢查是否需要重啟
set /a RESTART_COUNT += 1
echo 服務意外停止,5秒后自動重啟.. .
timeout /t 5 /nobreak > nul
goto restartecho 服務已手動停止
pause