File size: 1,620 Bytes
36cf9cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import cv2
import easyocr
import numpy as np
from gtts import gTTS
import os
import pygame
import gradio as gr

# Initialize OCR reader and TTS system
reader = easyocr.Reader(['tr'])
pygame.mixer.init()

def capture_and_process():
    # Open webcam, capture frame, and save it
    capture = cv2.VideoCapture(0)
    ret, frame = capture.read()
    capture.release()
    
    if not ret:
        return "Failed to capture image", None
    
    # Save the captured image
    filename = 'captured_image.png'
    cv2.imwrite(filename, frame)
    
    # Run OCR on the saved image
    results = reader.readtext(filename)
    
    # Prepare text-to-speech for each detected text
    detected_text = []
    for result in results:
        if result[1].strip() == "":
            continue
        text = result[1]
        detected_text.append(text)
        
        # Convert text to speech and play it
        tts = gTTS(text=text.lower(), lang='tr')
        tts.save("output.mp3")
        pygame.mixer.music.load("output.mp3")
        pygame.mixer.music.play()
        
        # Wait until the speech is done
        while pygame.mixer.music.get_busy():
            pygame.time.Clock().tick(10)
    
    # Return the captured image and detected text
    return detected_text, frame[..., ::-1]  # Convert BGR to RGB for display in Gradio

# Gradio interface
interface = gr.Interface(
    fn=capture_and_process, 
    inputs=None, 
    outputs=[gr.outputs.Textbox(label="Detected Text"), gr.outputs.Image(type="numpy", label="Captured Image")],
    live=True
)

# Launch the app
if __name__ == "__main__":
    interface.launch()