Spaces:
Build error
Build error
| import gradio as gr | |
| import pytesseract | |
| import cv2 | |
| import re | |
| from sympy import sympify | |
| # Function to extract math problems from an image | |
| def extract_text_from_image(image): | |
| gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Convert image to grayscale | |
| text = pytesseract.image_to_string(gray) # Perform OCR to extract text | |
| math_problems = re.findall(r'[\d+\-*/().]+', text) # Filter for potential math expressions | |
| return math_problems | |
| # Function to solve the extracted math problems | |
| def solve_math_problem(problem): | |
| try: | |
| expression = sympify(problem) | |
| result = expression.evalf() | |
| return result | |
| except Exception as e: | |
| return f"Error: {e}" | |
| # Main function to recognize and solve math problems from an image | |
| def recognize_and_solve(image): | |
| problems = extract_text_from_image(image) | |
| solutions = [f"{p} = {solve_math_problem(p)}" for p in problems] | |
| return "\n".join(solutions) if solutions else "No math problems detected." | |
| # Gradio interface | |
| interface = gr.Interface( | |
| fn=recognize_and_solve, | |
| inputs="image", | |
| outputs="text", | |
| title="Math Problem Recognizer and Solver", | |
| description="Upload an image containing math problems, and this app will recognize and solve them." | |
| ) | |
| # Launch the Gradio app | |
| interface.launch() | |