Arjooohn commited on
Commit
d4a888f
·
verified ·
1 Parent(s): b5d1c61

Add timer to capture images every 0.5 seconds

Browse files
Files changed (1) hide show
  1. app.py +10 -9
app.py CHANGED
@@ -8,7 +8,7 @@ import io
8
 
9
  def preprocess(image):
10
  img = np.array(image)
11
- img = cv2.flip(img, 1)
12
  gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
13
  kernel = np.array([[0,-1,0], [-1,5,-1], [0,-1,0]])
14
  gray = cv2.filter2D(gray, -1, kernel)
@@ -42,20 +42,21 @@ with gr.Blocks() as demo:
42
  with gr.Row():
43
  webcam = gr.Image(
44
  type="pil",
45
- sources=["webcam"],
46
- label="Webcam Input",
47
- live=True # <-- enables real-time updates
48
  )
49
  processed_preview = gr.Image(type="pil", label="Processed Preview")
50
 
51
  ocr_text = gr.Textbox(label="Extracted Text")
52
  audio_output = gr.Audio(label="Text-to-Speech Output")
53
 
54
- webcam.change(
55
- fn=extract_text_and_speak,
56
- inputs=webcam,
57
- outputs=[processed_preview, ocr_text, audio_output]
58
- )
 
 
59
 
60
  if __name__ == "__main__":
61
  demo.launch()
 
8
 
9
  def preprocess(image):
10
  img = np.array(image)
11
+ img = cv2.flip(img, 1) # mirror
12
  gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
13
  kernel = np.array([[0,-1,0], [-1,5,-1], [0,-1,0]])
14
  gray = cv2.filter2D(gray, -1, kernel)
 
42
  with gr.Row():
43
  webcam = gr.Image(
44
  type="pil",
45
+ sources=["webcam"], # user captures frames manually
46
+ label="Webcam Input"
 
47
  )
48
  processed_preview = gr.Image(type="pil", label="Processed Preview")
49
 
50
  ocr_text = gr.Textbox(label="Extracted Text")
51
  audio_output = gr.Audio(label="Text-to-Speech Output")
52
 
53
+ # Timer triggers the function every 0.5 seconds
54
+ def timer_fn():
55
+ if webcam.value is None:
56
+ return None, None, None
57
+ return extract_text_and_speak(webcam.value)
58
+
59
+ gr.Timer(interval=0.5, fn=timer_fn, outputs=[processed_preview, ocr_text, audio_output])
60
 
61
  if __name__ == "__main__":
62
  demo.launch()