Xakamaynta codka ee PC iyo RaspberryPi oo leh Whisper

xakamaynta codka ee pc iyo raspberry pi

Fikradda mashruucu waa Sii tilmaamo cod ah si aad ula falgasho kombayutaradayada ama Raspberry Pi adoo isticmaalaya qaabka codka-ilaa-qoraalka ah.

Waxaan bixin doonaa amar la duubi doono, loo rogi doono qoraal, oo leh Whisper ka dibna la falanqeynayo si loo fuliyo amarka ku habboon, kaas oo noqon kara fulinta barnaamijka ilaa siinta korontada biinanka RaspberryPi.

Waxaan isticmaali doonaa Raspberry Pi 2 duug ah, micro USB waxaanan isticmaali doonaa qaabka Cod-ku-qoraal ee dhawaan ay soo saartay OpenAI, Weyn. Dhamaadka maqaalka waxaad arki kartaa in yar oo xan ah.

oo dhan waa lagu barnaamijyay Python.

Waxaan kaaga tagayaa muujinta sida uu u shaqeeyo muuqaalkan, iyadoo kombuyuutarka lagu xakameynayo cod.

Golaha

Si aan ugu isticmaalno kombayutarka, waxaan u baahan doonaa makarafoon kaliya.

Haddii aad rabto inaad ku dhejiso RaspberryPi, waxaad u baahan doontaa makarafoon USB ah, sababtoo ah jaakadda ay leedahay ayaa kaliya loogu talagalay soo saarista.

Baahan:

Maaddaama ujeedada guud ee qalabku ay tahay aqoonsiga codka. Waxaan u arkaa inay aad u faa'iido badan tahay in lagu daro hawlgalka aaladaha kale.

  • Micro USB
  • Raspberry PI oo leh nidaamka hawlgalka (tusaale Raspbian pro)
  • Qalabka elektiroonigga ah (LED, fiilooyinka, 480 ohm resistor iyo sabuurad rooti)

Waxaan ku xireynaa LED-ka pin 17, kaas oo ah midka aan kicin doono oo aan dami doono waayo-aragnimadan.

horumarinta code

Waxa loo qaybiyaa saddex qaybood, kow, cod la duubay oo aan kood ka soo qaatay geeksforgeeks, sababtoo ah ma aqaan dukaamada buugaagta. Midda labaad, u beddelashada maqalka qoraalka oo leh Whisper iyo tan saddexaad, daawaynta qoraalkaas iyo jawaabta RaspberryPi

Tusaalaha tijaabada ah waxaan kaliya la fal galayaa Led, oo ka dhigaya mid iftiimaya ama libiqsi leh, laakiin waxaan horumarin karnaa qoraalka si aan ugu hagaajino baahideena.

Waan ka warqabaa in kani yahay Raspberry Pi 2 oo ay aad uga gaabin doonto Raspberry Pi 4, laakiin tijaabinta way fiicantahay.

Kahor intaadan shaqayn, waxaad u baahan doontaa inaad ku rakibto kuwan soo socda

#Instalar whisper
pip install git+https://github.com/openai/whisper.git
sudo apt update && sudo apt install ffmpeg

#para que funcione la grabación de audio
python3 -m pip install sounddevice --user
pip install git+https://github.com/WarrenWeckesser/wavio.git

#si vas a instalarlo en la raspberry
#dar permisos para usar la GPIO
sudo apt install python3-gpiozero
sudo usermode -aG gpio <username>

code oo dhan

#!/usr/bin/env python3
import whisper
import time
from gpiozero import LED
import sounddevice as sd
from scipy.io.wavfile import write
import wavio as wv

        
def main ():
    inicio = time.time()
    record_audio ()

    model = whisper.load_model("tiny")
    result = model.transcribe("audio1.wav")
    words = result["text"].split()

    for word in words:
        word = word.replace(',', '').replace('.', '').lower()
        if word == 'enciende' or 'encender':
            encender()
            break
        if word == 'parpadea' or 'parpadear':
            parpadear()
            break      
    fin = time.time()
    print(fin-inicio)

def encender ():
    LED(17).on()

def parpadear ():
    light = LED(17)
    while True:
        light.on()
        sleep(1)
        light.off()
        sleep(1)

def record_audio ():
    # Sampling frequency
    freq = 44100
    # Recording duration
    duration = 5
    # Start recorder with the given values
    # of duration and sample frequency
    recording = sd.rec(int(duration * freq),
                    samplerate=freq, channels=2)
    # Record audio for the given number of seconds
    sd.wait()
    # This will convert the NumPy array to an audio
    # file with the given sampling frequency
    write("audio0.wav", freq, recording)
    # Convert the NumPy array to audio file
    wv.write("audio1.wav", recording, freq, sampwidth=2)
        
main ()


#dar permisos para usar la GPIO
#sudo apt install python3-gpiozero
#sudo usermode -aG gpio <username>

#Instalar whisper
#pip install git+https://github.com/openai/whisper.git
#sudo apt update &amp;&amp; sudo apt install ffmpeg

Ma awoodin inaan tijaabiyo sababtoo ah ma haysto microSD-ka RaspberryPi, ama ku hadla USB-ga si aan ugu xidho, laakiin isla marka aan isku dayo waxaan saxaa khaladaadka ay fududahay in la galo.

Tallaabo-tallaabo sharraxaadda koodka

#!/usr/bin/env python3

Shebangku waa inuu u sheego qalabka luqadda aanu ku diyaarinay iyo turjumaanka la isticmaalayo. Inkasta oo ay u muuqato mid aan waxtar lahayn, haddii aan la dhigin waxay keentaa khaladaad marar badan.

maktabadaha dibadda laga keeno

import whisper
import time
from gpiozero import LED
import sounddevice as sd
from scipy.io.wavfile import write
import wavio as wv

Hoos u dhig si aad ula shaqeyso qaabka

waqti, sababtoo ah waxaan u isticmaalaa si aan u xakameeyo wakhtiga ay qaadanayso fulinta qoraalka, gpiozero si ay ula shaqeyso biinanka GPIO ee Raspberry iyo qalabka dhawaaqa, scipy iyo wavio si loo duubo codka

Hawlaha

Waxaan abuuray 4 hawlood:

  • ugu weyn ()
  • iftiin ()
  • in la libiqsado ()
  • record_maqal()

shid () si fudud waxay ku siinaysaa danab si ay u pin 17 ee raspberry meesha aan ku xidhnay kiiskan LED si loo tijaabiyo

def encender ():
    LED(17).on()

blink() waa sida daaran () laakiin waxay ka dhigtaa ledhku ilbidhiqsi iyadoo daaran oo damisa wareeg ahaan.

def parpadear ():
    light = LED(17)
    while True:
        light.on()
        sleep(1)
        light.off()
        sleep(1)

Record_audio() waxaanu ku duubnaa faylka codka

def record_audio ():
    # Sampling frequency
    freq = 44100
    # Recording duration
    duration = 5
    # Start recorder with the given values
    # of duration and sample frequency
    recording = sd.rec(int(duration * freq),
                    samplerate=freq, channels=2)
    # Record audio for the given number of seconds
    sd.wait()
    # This will convert the NumPy array to an audio
    # file with the given sampling frequency
    write("audio0.wav", freq, recording)
    # Convert the NumPy array to audio file
    wv.write("audio1.wav", recording, freq, sampwidth=2)

Muhiimadu waa shaqada ugu weyn, ogaysii waxa kaliya ee aan haysano ee ka baxsan shaqooyinka waa wicitaanka ugu weyn () dhamaadka qoraalka. Sidan marka la bilaabayo, waxay soo dejin doontaa maktabadaha ka dibna waxay samayn doontaa wicitaanka shaqada.

def main ():
    inicio = time.time()
    record_audio ()

    model = whisper.load_model("tiny")
    result = model.transcribe("audio1.wav")
    words = result["text"].split()

    for word in words:
        word = word.replace(',', '').replace('.', '').lower()
        if word == 'enciende' or 'encender':
            encender()
            break
        if word == 'parpadea' or 'parpadear':
            parpadear()
            break      
    fin = time.time()
    print(fin-inicio)

Waxaan kaydineynaa waqtiga aan bilaabayno fulinta shaqada ka dibna waxaan u yeernaa shaqada maqalka duubista taas oo ku duubi doonta tilmaamahayaga faylka .wav, .mp3, iwm oo aan hadhow u beddeli doono qoraal.

    inicio = time.time()
    record_audio ()

  

Markaan maqalka helno, xanbaar baa la wacayaa, waxaana u sheegaynaa qaabka aan rabno inaan isticmaalno, waxaa jira 5, waxaana adeegsan doonnaa kuwa yaryar, inkastoo ay tahay tan ugu cilladda badan, sababtoo ah waa kan ugu dhaqsaha badan, codkuna wuxuu noqonayaa mid fudud. kaliya 3 ama 4 kelmadood .

     model = whisper.load_model("tiny")
    result = model.transcribe("audio1.wav")

  

Sidan ayaanu ku haynaa codkii loo beddelay qoraal oo lagu kaydiyay doorsoome. Aynu wax ka beddelno.

Waxaan natiijada u beddelnaa liis ay ku jiraan eray kasta oo maqal ah

     words = result["text"].split()

  

Iyo wax kasta oo diyaar u ah inay la falgalaan qalabkeena. Hadda waa inaan abuurnaa shuruudaha aan rabno.

Haddii codku leeyahay ereyga X, samee Y. Sida aynu erayada liiska ku hayno, aad bay u fududahay in shuruudo lagu daro

         for word in words:
        word = word.replace(',', '').replace('.', '').lower()
        if word == 'enciende' or 'encender':
            encender()
            break
        if word == 'parpadea' or 'parpadear':
            parpadear()
            break   

  

Sadarka

         
        word = word.replace(',', '').replace('.', '').lower()


  

Waxa aan u isticmaalaa in aan erayada codka ku jira u beddelo far-hoosaad oo aan ka saaro hakadyada iyo xilliyada. Oo sidaas daraaddeed iska ilaali khaladaadka ku jira isbarbardhigga

Mid kasta oo haddii shuruudaha mid ka mid ah ereyada aan doorannay la buuxiyo, waxay u yeedhaa shaqo samayn doonta waxaan rabno,

Tani waa halka aan u sheegno in ay kiciso PIN-ka iftiiminaya LED ama ka dhigaya mid libiqsan. Ama fur qaar kood ah, ama dami kombiyuutarka.

Waxaas oo dhan waa fikrad aasaasi ah. Halkan waxaad ka horumarin kartaa mashruuca oo aad u hagaajin kartaa sidaad rabto. Qof kastaa wuxuu u heli karaa isticmaal ka duwan.

Waxyaabaha aan ku samayn karno montage this

Kuwani waa fikrado ii yimid si aan uga faa'iidaysto montajkan. Marka qalfoofku hubeeyo, waxaan u isticmaali karnaa si aan u dhaqaajino wax kasta oo maskaxda ku soo dhaca cod ahaan, waxaan kicin karnaa relay ka bilaabaya matoor ama waxaan bilaabi karnaa qoraal fulinaya qoraal, iimaylka ama wax kasta.

Waa maxay xan

Shaqsigu waa nooc aqoonsi volt ah, wuxuu ku shaqeeyaa luqado badan oo tiro badan oo luuqado ah wuxuuna ogolaadaa tarjumaada Ingiriisiga. Waa waxa aan u naqaano qalab qoraal-ku-cod ah, laakiin kani waa Isha Furan, oo ay sii daayeen kooxda OpenAI, hal-abuurayaasha Stable Diffusion.

Haddii aad tahay qof nasasho la'aan sida annaga oo kale oo aad rabto in aad iska kaashato dayactirka iyo hagaajinta mashruuca, waxaad samayn kartaa tabaruc. Dhammaan lacagta ayaa ku bixi doonta in lagu soo iibiyo buugaag iyo agab si loo tijaabiyo oo loo sameeyo casharro

Tomato comentario un