無頭蒼蠅
無頭蒼蠅

高二生,在馬特市內發揮奇想!

網課自動舉手!

為了應付幾天後可能再次發生的網課,我用 python 的語音識別套件,做了一個可以自動舉手的小程式,雖然還有一些問題尚未解決,但大致可以用了,程式如下。

在移動到matters時,因為我的分行跳掉了,所以我上傳圖片使用前請先分行。

import speech_recognition as sr

import win32api, win32con, pyaudio, time

import sounddevice as sd

r = sr.Recognizer()

def find_device_index():

print(sd.query_devices())

def click(x,y):

win32api.SetCursorPos((x,y))

win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0)

win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)

def class_auto_answer(name, number, x, y, device):

while True:

with sr.Microphone(device_index = device) as source:

try:

audio = r.listen(source,phrase_time_limit=2)

speech = r.recognize_google(audio, language='zh-TW')

if not speech == None:

print(speech)

if (str(number) in speech) or (name in speech):

print(speech)

click(x, y)

time.sleep(3)

click(x, y)

except:

continue

要先安裝speech_recognition, win32api, win32con, pyaudio, time這幾個數據庫,如果是用anaconda的話可以用pip install 但pyaudio要用conda install 不然可能會無法運行,且要讓耳機收到揚聲器的聲音,因為我目前還尚未找到把揚聲器當音源的辦法。

def find_device_index():

print(sd.query_devices())

↑上面的函式可以幫你尋找你的輸入設備的號碼,也就是你的麥克風的號碼。

def click(x,y):

win32api.SetCursorPos((x,y))

win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0)

win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)

↑上面的函式可以控制滑鼠。

接下才是重頭戲,下面的這個函式幫我們判斷了老師講的是不是你的名子或座號。

def class_auto_answer(name, number, x, y, device):

while True:

with sr.Microphone(device_index = device) as source:

try:

audio = r.listen(source,phrase_time_limit=2)

speech = r.recognize_google(audio, language='zh-TW')

if not speech == None:

print(speech)

if (str(number) in speech) or (name in speech):

print(speech)

click(x, y)

time.sleep(3)

click(x, y)

except:

continue

要使用的話只要打

def class_auto_answer('名子', 座號, 舉手在你的螢幕上的座標, 你的輸入設備的號碼)

這就可以呼叫函式出來讓你使用,以此實現自動舉手。


目前尚未找到把揚聲器當音源的方法,若有人知道怎麼把揚聲器當成音源請告訴我,感激不盡😀

CC BY-NC-ND 2.0 版权声明

喜欢我的文章吗?
别忘了给点支持与赞赏,让我知道创作的路上有你陪伴。

加载中…

发布评论