Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,24 +6,47 @@ from sympy import sympify
|
|
| 6 |
|
| 7 |
# Function to extract math problems from an image
|
| 8 |
def extract_text_from_image(image):
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
|
|
|
| 6 |
|
| 7 |
# Function to extract math problems from an image
|
| 8 |
def extract_text_from_image(image):
|
| 9 |
+
# Convert image to grayscale for better OCR performance
|
| 10 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 11 |
+
|
| 12 |
+
# Perform OCR on the grayscale image
|
| 13 |
+
text = pytesseract.image_to_string(gray)
|
| 14 |
+
|
| 15 |
+
# Print the raw OCR output for debugging purposes
|
| 16 |
+
print("OCR Output:", text)
|
| 17 |
+
|
| 18 |
+
# Filter out potential math expressions (numbers, operators, and parentheses)
|
| 19 |
+
math_problems = re.findall(r'[\d+\-*/().÷]+', text)
|
| 20 |
+
|
| 21 |
+
# Print recognized math problems for debugging
|
| 22 |
+
print("Recognized Problems:", math_problems)
|
| 23 |
+
|
| 24 |
return math_problems
|
| 25 |
|
| 26 |
# Function to solve the extracted math problems
|
| 27 |
def solve_math_problem(problem):
|
| 28 |
try:
|
| 29 |
+
# Replace any OCR misinterpretations if needed
|
| 30 |
+
problem = problem.replace("÷", "/") # Replace division symbol with "/"
|
| 31 |
+
|
| 32 |
+
# Convert the string expression to a symbolic expression
|
| 33 |
expression = sympify(problem)
|
| 34 |
+
|
| 35 |
+
# Evaluate the expression
|
| 36 |
result = expression.evalf()
|
| 37 |
+
|
| 38 |
return result
|
| 39 |
except Exception as e:
|
| 40 |
+
# Return a clear error message for debugging
|
| 41 |
+
print(f"Error solving problem '{problem}':", e)
|
| 42 |
return f"Error: {e}"
|
| 43 |
|
| 44 |
# Main function to recognize and solve math problems from an image
|
| 45 |
def recognize_and_solve(image):
|
| 46 |
problems = extract_text_from_image(image)
|
| 47 |
solutions = [f"{p} = {solve_math_problem(p)}" for p in problems]
|
| 48 |
+
|
| 49 |
+
# Format the output or return a message if no math problems were detected
|
| 50 |
return "\n".join(solutions) if solutions else "No math problems detected."
|
| 51 |
|
| 52 |
# Gradio interface
|