wordpress 好評插件優(yōu)化設計六年級下冊數(shù)學答案
前言
“接口也是口,算法也是算”,綜合來看就是口算!
看到全網都在炸魚也忍不住來玩一玩…
參考了大佬的OCR版,試用后發(fā)現(xiàn)影響速度的最大卡點并不是識別速度等,而是““由于檢測異常導致的等待”。
基于體驗幾局的感受——輸入對了可以極速跳題、答題成功會有提示音,便將思路放到了從音量檢測入手。
實現(xiàn)
思路大致幾點——
1、輸入對了可以極速跳題。
2、答題成功會有提示音。
3、目前沒有看到過輸入“=”的場景,僅有大于和小于。
基于上述三點,暴力作答所需猜測的情況也只有兩種,且作答成功后可快速進行下一題,減少卡頓,理論可行。
因此,嘗試不通過ORC識別或其他方式來判斷題目作答正確,而是采取“是否曝出了提示音“”來判斷是否作答正確。
流程大致為:
1、作答符號為">"。
- 如果有提示音,短停留繼續(xù)作答。
- 如果沒有提示音, 長停留繼續(xù)作答。
2、切換作答符號為另一個。
由此循環(huán)往復…
代碼
需自行安裝虛擬聲卡,用于讓Python檢測聲音。
import pyautogui
import time
import numpy as np
import pyaudio
from pynput.mouse import Controller, Button
from pynput.keyboard import Listener, Key# 初始化鼠標控制器
mouse = Controller()# 全局變量,控制繪制狀態(tài)
running = False
prev_volume = 0 # 上一個音量值def log_message(message):"""記錄日志消息"""current_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())print(f"[{current_time}] {message}")def draw_symbol(symbol):"""繪制符號"""screen_width, screen_height = pyautogui.size()x = int(screen_width * 0.4) # 水平居中y = int(screen_height * 0.7) # 將 y 坐標設置為屏幕高度的 70%duration = 0.03 # 每條線的持續(xù)時間rect_size = 40 # 控制繪制符號的大小(線條長度的控制)if symbol == '>':# 繪制 ">" 符號mouse.position = (x, y)mouse.press(Button.left)mouse.move(rect_size, rect_size) # 右下斜線time.sleep(duration)mouse.move(-rect_size, rect_size) # 左下斜線time.sleep(duration)mouse.release(Button.left)log_message("執(zhí)行了繪制 '>' 符號 操作")elif symbol == '<':# 繪制 "<" 符號mouse.position = (x, y)mouse.press(Button.left)mouse.move(-rect_size, rect_size) # 左下斜線time.sleep(duration)mouse.move(rect_size, rect_size) # 右下斜線time.sleep(duration)mouse.release(Button.left)log_message("執(zhí)行了繪制 '<' 符號 操作")def list_audio_devices():"""列出可用的音頻輸入設備"""p = pyaudio.PyAudio()log_message("可用的音頻設備:")for i in range(p.get_device_count()):info = p.get_device_info_by_index(i)log_message(f"設備 {i}: {info['name']} - {'輸入' if info['maxInputChannels'] > 0 else '輸出'}")p.terminate()def get_supported_sample_rates(device_index):"""獲取指定設備支持的采樣率"""p = pyaudio.PyAudio()device_info = p.get_device_info_by_index(device_index)log_message(f"設備 {device_index} ({device_info['name']}) 支持的采樣率: {device_info['defaultSampleRate']}")p.terminate()return int(device_info['defaultSampleRate'])def detect_system_sound(threshold=1000, device_index=24): # 使用設備索引 24"""檢測系統(tǒng)聲音并返回是否檢測到突然增加的聲音"""global prev_volume # 聲明使用全局變量chunk = 1024 # 每次讀取的音頻塊大小sample_rate = get_supported_sample_rates(device_index) # 獲取支持的采樣率p = pyaudio.PyAudio()# 打開音頻流,確保輸入設備是虛擬音頻設備stream = p.open(format=pyaudio.paInt16, channels=1,rate=sample_rate, input=True,frames_per_buffer=chunk, input_device_index=device_index)audio_data = stream.read(chunk)audio_data_np = np.frombuffer(audio_data, dtype=np.int16)volume_norm = np.linalg.norm(audio_data_np) / np.sqrt(chunk) # 計算音量(RMS)stream.stop_stream()stream.close()p.terminate()log_message(f"當前音量:{volume_norm:.2f}") # 打印當前音量以調試# 檢測音量的突然增加if volume_norm > prev_volume + threshold:prev_volume = volume_norm # 更新上一個音量值return True # 檢測到聲音突增else:prev_volume = volume_norm # 更新上一個音量值return False # 未檢測到聲音突增def toggle_running(key):"""切換運行狀態(tài)"""global runningif key == Key.enter: # 當按下 Enter 鍵時切換狀態(tài)running = not runningif running:log_message("繪圖已啟動...")else:log_message("繪圖已停止。")def main():global runninglog_message("按 Enter 鍵啟動或停止繪圖...")i = 0# 列出可用的音頻設備list_audio_devices()# 啟動鍵盤監(jiān)聽with Listener(on_press=toggle_running) as listener:while True:if running:# 檢測聲音sound_detected = detect_system_sound()if sound_detected:log_message("有檢測到聲音突增!")time.sleep(0.2) # 如果檢測到聲音突增,等待 0.2 秒else:log_message("未檢測到聲音突增,等待 1 秒...")time.sleep(0.8) # 如果未檢測到聲音,等待 1 秒# 繪制 "<" 符號if i % 2:draw_symbol('<')else:draw_symbol('>')i += 1# 換行分隔log_message("")time.sleep(0.1) # 避免占用過多 CPU 資源if __name__ == "__main__":main()
優(yōu)化點。
1、由于小猿口算的Bug——即背景音樂無法關閉。導致檢測聲音突增并不是那么準確。同時吐槽一下,小猿官方沒有對用戶名違禁進行檢測嗎?頭像+用戶名均sex居然可以過審,難以想象。
2、模擬器產生聲音 - Python檢測到變化,會有一定的延遲。
綜合測試來看,感覺可能并不比單純的暴力快多少,因此此嘗試更多的還是——整活。