import cv2
import google.generativeai as genai
from gtts import gTTS
import pygame
import time
# --- 1. ใส่ API Key ของคุณที่นี่ ---
genai.configure(api_key="ใส่_API_KEY_ที่นี่")
def scan_and_read():
# 2. เปิดกล้องและถ่ายรูป
print("กำลังถ่ายภาพ...")
cap = cv2.VideoCapture(0)
ret, frame = cap.read()
if ret:
cv2.imwrite('scan.jpg', frame)
print("ถ่ายสำเร็จ!")
cap.release()
# 3. ส่งให้ Gemini AI อ่านภาษาไทย
print("AI กำลังประมวลผล...")
model = genai.GenerativeModel('gemini-1.5-flash')
img = genai.upload_file(path="scan.jpg")
response = model.generate_content([img, "อ่านข้อความภาษาไทยในรูปนี้ให้ฟังหน่อย เอาแค่ข้อความที่อ่านได้เท่านั้น"])
text = response.text
print("ข้อความที่พบ:", text)
# 4. แปลงข้อจำเป็นเสียงและเล่นออกลำโพง
if text.strip():
tts = gTTS(text=text, lang='th')
tts.save("voice.mp3")
pygame.mixer.init()
pygame.mixer.music.load("voice.mp3")
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
continue
else:
print("ไม่พบข้อความ")
# รันโปรแกรม
if __name__ == "__main__":
scan_and_read()
Rasberry pi
import google.generativeai as genai
from gtts import gTTS
import pygame
import time
# --- 1. ใส่ API Key ของคุณที่นี่ ---
genai.configure(api_key="ใส่_API_KEY_ที่นี่")
def scan_and_read():
# 2. เปิดกล้องและถ่ายรูป
print("กำลังถ่ายภาพ...")
cap = cv2.VideoCapture(0)
ret, frame = cap.read()
if ret:
cv2.imwrite('scan.jpg', frame)
print("ถ่ายสำเร็จ!")
cap.release()
# 3. ส่งให้ Gemini AI อ่านภาษาไทย
print("AI กำลังประมวลผล...")
model = genai.GenerativeModel('gemini-1.5-flash')
img = genai.upload_file(path="scan.jpg")
response = model.generate_content([img, "อ่านข้อความภาษาไทยในรูปนี้ให้ฟังหน่อย เอาแค่ข้อความที่อ่านได้เท่านั้น"])
text = response.text
print("ข้อความที่พบ:", text)
# 4. แปลงข้อจำเป็นเสียงและเล่นออกลำโพง
if text.strip():
tts = gTTS(text=text, lang='th')
tts.save("voice.mp3")
pygame.mixer.init()
pygame.mixer.music.load("voice.mp3")
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
continue
else:
print("ไม่พบข้อความ")
# รันโปรแกรม
if __name__ == "__main__":
scan_and_read()