JHigg commited on
Commit
3c0c4ff
·
verified ·
1 Parent(s): 03f3bfb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -11
app.py CHANGED
@@ -11,15 +11,11 @@ def extract_text_from_image(image):
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
 
@@ -27,18 +23,19 @@ def extract_text_from_image(image):
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
@@ -46,7 +43,6 @@ 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
 
11
 
12
  # Perform OCR on the grayscale image
13
  text = pytesseract.image_to_string(gray)
14
+ print("OCR Output:", text) # Print raw OCR output
15
 
16
+ # Filter out potential math expressions
 
 
 
17
  math_problems = re.findall(r'[\d+\-*/().÷]+', text)
18
+ print("Recognized Problems:", math_problems) # Print recognized problems
 
 
19
 
20
  return math_problems
21
 
 
23
  def solve_math_problem(problem):
24
  try:
25
  # Replace any OCR misinterpretations if needed
26
+ problem = problem.replace("÷", "/")
27
+ print("Processing Problem:", problem) # Print each problem being processed
28
 
29
  # Convert the string expression to a symbolic expression
30
  expression = sympify(problem)
31
 
32
  # Evaluate the expression
33
  result = expression.evalf()
34
+ print("Result:", result) # Print result
35
 
36
  return result
37
  except Exception as e:
38
+ print(f"Error solving problem '{problem}':", e) # Print specific error
 
39
  return f"Error: {e}"
40
 
41
  # Main function to recognize and solve math problems from an image
 
43
  problems = extract_text_from_image(image)
44
  solutions = [f"{p} = {solve_math_problem(p)}" for p in problems]
45
 
 
46
  return "\n".join(solutions) if solutions else "No math problems detected."
47
 
48
  # Gradio interface