Spaces:
Sleeping
Sleeping
Fix bugs
Browse files12345678910111213141516171819202122232425262728294 unmodified lines3435363738394041424344454647484950515253545556575859606162import gradio as grimport pytesseractimport cv2import numpy as npfrom PIL import Imagefrom gtts import gTTSimport iodef preprocess(image):img = np.array(image)img = cv2.flip(img, 1) # mirrorgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)kernel = np.array([[0,-1,0], [-1,5,-1], [0,-1,0]])gray = cv2.filter2D(gray, -1, kernel)gray = cv2.fastNlMeansDenoising(gray, None, 30, 7, 21)thresh = cv2.adaptiveThreshold(gray, 255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,31, 10)return threshdef extract_text_and_speak(image):processed = preprocess(image)processed_pil = Image.fromarray(processed)text = pytesseract.image_to_string(processed, lang="eng").strip()if text == "":text = "No readable text found."4 unmodified linestts.write_to_fp(buffer)buffer.seek(0)audio_file = bufferreturn processed_pil, text, audio_filewith gr.Blocks() as demo:gr.Markdown("## GabAI - AI Assistive Reading System (Live OCR)")with gr.Row():webcam = gr.Image(type="pil",sources=["webcam"], # user captures frames manuallylabel="Webcam Input")processed_preview = gr.Image(type="pil", label="Processed Preview")ocr_text = gr.Textbox(label="Extracted Text")audio_output = gr.Audio(label="Text-to-Speech Output")# Timer triggers the function every 0.5 secondsdef timer_fn():if webcam.value is None:return None, None, Nonereturn extract_text_and_speak(webcam.value)gr.Timer(interval=0.5, fn=timer_fn, outputs=[processed_preview, ocr_text, audio_output])if __name__ == "__main__":demo.launch()12345678910111213141516171819202122232425262728293031324 unmodified lines37383940414243444546474849505152535455565758596061import gradio as grimport cv2import numpy as npfrom PIL import Imageimport pytesseractfrom gtts import gTTSimport iodef preprocess(frame):gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)kernel = np.array([[0,-1,0], [-1,5,-1], [0,-1,0]])gray = cv2.filter2D(gray, -1, kernel)gray = cv2.fastNlMeansDenoising(gray, None, 30, 7, 21)thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY, 31, 10)return threshdef process_video(frame):# frame is a numpy array from the webcam feedif frame is None:return None, "No frame captured", Noneframe = cv2.flip(frame, 1) # mirrorprocessed = preprocess(frame)processed_pil = Image.fromarray(processed)# OCRtext = pytesseract.image_to_string(processed, lang="eng").strip()if text == "":text = "No readable text found."4 unmodified linestts.write_to_fp(buffer)buffer.seek(0)audio_file = bufferreturn processed_pil, text, audio_filewith gr.Blocks() as demo:gr.Markdown("## GabAI - Real-Time OCR with Webcam")with gr.Row():webcam = gr.Video(source="webcam", type="numpy", label="Webcam Feed")processed_preview = gr.Image(type="pil", label="Processed Preview")ocr_text = gr.Textbox(label="Extracted Text")audio_output = gr.Audio(label="Text-to-Speech Output")# Real-time processing: call function on each framewebcam.change(fn=process_video,inputs=webcam,outputs=[processed_preview, ocr_text, audio_output])if __name__ == "__main__":demo.launch()
app.py+23-24