Arjooohn commited on
Commit
cbfae3f
·
verified ·
1 Parent(s): 7378206
Files changed (1) hide show
  1. app.py +23 -27
app.py CHANGED
@@ -8,11 +8,9 @@ import os
8
 
9
  def preprocess(image):
10
  """
11
- Preprocess the image for better OCR results.
12
- Converts to grayscale, applies Gaussian blur, and thresholding.
13
  """
14
- img = np.array(image) # Do NOT mirror here
15
-
16
  gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
17
  blur = cv2.GaussianBlur(gray, (5, 5), 0)
18
  thresh = cv2.threshold(
@@ -22,35 +20,33 @@ def preprocess(image):
22
  return thresh
23
 
24
  def extract_and_speak(image):
25
- """
26
- Extract text from the image using pytesseract and generate speech using gTTS.
27
- """
28
  processed = preprocess(image)
29
  text = pytesseract.image_to_string(processed, lang="eng")
30
-
31
  if text.strip() == "":
32
  return "No readable text found.", None
33
-
34
  tts = gTTS(text)
35
  tts.save("output.mp3")
36
-
37
  return text, "output.mp3"
38
 
39
- # Gradio interface
40
- interface = gr.Interface(
41
- fn=extract_and_speak,
42
- inputs=gr.Image(
43
- type="pil",
44
- sources=["webcam"], # Only webcam
45
- mirror_webcam=True # Mirror live webcam feed only
46
- ),
47
- outputs=[
48
- gr.Textbox(label="Extracted Text"),
49
- gr.Audio(label="Text-to-Speech Output")
50
- ],
51
- title="GabAI - AI Assistive Reading System",
52
- description="Use your webcam to capture printed text. The preview is mirrored, but text is read from the original image."
53
- )
 
 
 
 
 
54
 
55
- if __name__ == "__main__":
56
- interface.launch()
 
8
 
9
  def preprocess(image):
10
  """
11
+ Preprocess the image for OCR.
 
12
  """
13
+ img = np.array(image)
 
14
  gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
15
  blur = cv2.GaussianBlur(gray, (5, 5), 0)
16
  thresh = cv2.threshold(
 
20
  return thresh
21
 
22
  def extract_and_speak(image):
 
 
 
23
  processed = preprocess(image)
24
  text = pytesseract.image_to_string(processed, lang="eng")
 
25
  if text.strip() == "":
26
  return "No readable text found.", None
 
27
  tts = gTTS(text)
28
  tts.save("output.mp3")
 
29
  return text, "output.mp3"
30
 
31
+ with gr.Blocks() as demo:
32
+ gr.Markdown("## GabAI - AI Assistive Reading System")
33
+ gr.Markdown(
34
+ "Use your webcam to capture printed text. The preview below is mirrored for convenience, "
35
+ "but OCR uses the original image."
36
+ )
37
+
38
+ # Webcam input
39
+ webcam = gr.Image(type="pil", source="webcam")
40
+
41
+ # Mirror the preview using CSS (client-side only)
42
+ webcam.style(**{"transform": "scaleX(-1)"})
43
+
44
+ # Outputs
45
+ text_output = gr.Textbox(label="Extracted Text")
46
+ audio_output = gr.Audio(label="Text-to-Speech Output")
47
+
48
+ # Button to process
49
+ submit = gr.Button("Read Text")
50
+ submit.click(fn=extract_and_speak, inputs=webcam, outputs=[text_output, audio_output])
51
 
52
+ demo.launch()