JHigg commited on
Commit
b06490a
·
verified ·
1 Parent(s): 3475dad

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -0
app.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pytesseract
3
+ import cv2
4
+ import re
5
+ from sympy import sympify
6
+
7
+ # Function to extract math problems from an image
8
+ def extract_text_from_image(image):
9
+ gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Convert image to grayscale
10
+ text = pytesseract.image_to_string(gray) # Perform OCR to extract text
11
+ math_problems = re.findall(r'[\d+\-*/().]+', text) # Filter for potential math expressions
12
+ return math_problems
13
+
14
+ # Function to solve the extracted math problems
15
+ def solve_math_problem(problem):
16
+ try:
17
+ expression = sympify(problem)
18
+ result = expression.evalf()
19
+ return result
20
+ except Exception as e:
21
+ return f"Error: {e}"
22
+
23
+ # Main function to recognize and solve math problems from an image
24
+ def recognize_and_solve(image):
25
+ problems = extract_text_from_image(image)
26
+ solutions = [f"{p} = {solve_math_problem(p)}" for p in problems]
27
+ return "\n".join(solutions) if solutions else "No math problems detected."
28
+
29
+ # Gradio interface
30
+ interface = gr.Interface(
31
+ fn=recognize_and_solve,
32
+ inputs="image",
33
+ outputs="text",
34
+ title="Math Problem Recognizer and Solver",
35
+ description="Upload an image containing math problems, and this app will recognize and solve them."
36
+ )
37
+
38
+ # Launch the Gradio app
39
+ interface.launch()