import RPi.GPIO as GPIO
import speech_recognition as sr
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
LED_PIN = 12
GPIO.setup(LED_PIN, GPIO.OUT)
def recognize_command():
recognizer = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
audio = recognizer.listen(source)
try:
return recognizer.recognize_google(audio, language="ko-KR")
except sr.UnknownValueError:
return "Could not understand audio"
except sr.RequestError as e:
return f"API unavailable {e}"
while True:
command = recognize_command().lower()
if "불 켜" in command:
GPIO.output(LED_PIN, True)
print("LED ON")
elif "불 꺼" in command:
GPIO.output(LED_PIN, False)
print("LED OFF")
import RPi.GPIO as GPIO
import os
import time
import pygame
import speech_recognition as sr
from gtts import gTTS
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
# 버튼 설정
GPIO.setup(26, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# pygame 초기화
pygame.init()
pygame.mixer.init()
# 오디오 알림 함수
def play_notification(path="/home/pi/study/notification.mp3"):
pygame.mixer.music.load(path)
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
time.sleep(0.1)
# 음성으로 텍스트를 말하는 함수
def say_text(text):
tts = gTTS(text=text, lang='ko')
tts.save("/tmp/temp_voice.mp3")
play_notification("/tmp/temp_voice.mp3")
# 버튼을 누를 때 호출되는 콜백 함수
def take_picture(channel=None):
say_text("사진을 찍겠습니다.")
time.sleep(2)
timestamp = time.strftime("%Y%m%d_%H%M%S")
os.system(f'fswebcam -r 2048x1536 --no-banner /home/pi/pictures/{timestamp}.jpg')
print(f"Picture taken: {timestamp}.jpg")
# 음성 명령을 듣는 함수
def listen_for_command():
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
audio = r.listen(source)
try:
command = r.recognize_google(audio, language="ko-KR")
return command
except sr.UnknownValueError:
return None
# GPIO 26번 핀에 falling edge detection을 추가하고, debounce 시간 설정
GPIO.add_event_detect(26, GPIO.FALLING, callback=take_picture, bouncetime=300)
try:
while True:
command = listen_for_command()
if command and "찍어" in command:
take_picture()
except KeyboardInterrupt:
pygame.mixer.quit()
GPIO.cleanup()
Copyright ⓒ TECH79 All right reserved