Spaces:
Runtime error
Runtime error
samiee2213
commited on
Commit
•
3fabc11
1
Parent(s):
9003c77
Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import cv2
|
3 |
+
import numpy as np
|
4 |
+
from PIL import Image
|
5 |
+
import google.generativeai as genai
|
6 |
+
from sympy import sympify, solve
|
7 |
+
import os
|
8 |
+
from dotenv import load_dotenv
|
9 |
+
|
10 |
+
# Load environment variables
|
11 |
+
load_dotenv()
|
12 |
+
|
13 |
+
# Initialize Google Generative AI
|
14 |
+
def initialize_genai():
|
15 |
+
api_key = os.getenv("GOOGLE_API_KEY")
|
16 |
+
if not api_key:
|
17 |
+
raise ValueError("Google API Key not found in environment variables.")
|
18 |
+
genai.configure(api_key=api_key)
|
19 |
+
|
20 |
+
def create_prompt(image):
|
21 |
+
# Adjust the prompt based on how the model expects the input
|
22 |
+
return "Analyze the following image of an equation. Recognize and solve the equation. Image:"
|
23 |
+
|
24 |
+
def recognize_equation_with_genai(image):
|
25 |
+
try:
|
26 |
+
# Convert image to text using Google Generative AI with a prompt template
|
27 |
+
prompt = create_prompt(image)
|
28 |
+
response = genai.text_detect(image, prompt=prompt)
|
29 |
+
recognized_text = response.get('text', '')
|
30 |
+
return recognized_text.strip()
|
31 |
+
except Exception as e:
|
32 |
+
return f"Error recognizing text: {str(e)}"
|
33 |
+
|
34 |
+
def solve_equation(equation):
|
35 |
+
try:
|
36 |
+
expr = sympify(equation)
|
37 |
+
solutions = solve(expr)
|
38 |
+
return str(solutions)
|
39 |
+
except Exception as e:
|
40 |
+
return f"Error solving equation: {str(e)}"
|
41 |
+
|
42 |
+
def process_frame(frame):
|
43 |
+
# Convert frame to grayscale
|
44 |
+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
45 |
+
_, thresholded = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY_INV)
|
46 |
+
|
47 |
+
# Convert OpenCV image to PIL image
|
48 |
+
image = Image.fromarray(thresholded)
|
49 |
+
|
50 |
+
# Recognize and solve the equation
|
51 |
+
recognized_equation = recognize_equation_with_genai(image)
|
52 |
+
solutions = solve_equation(recognized_equation)
|
53 |
+
|
54 |
+
return recognized_equation, solutions, image
|
55 |
+
|
56 |
+
def main():
|
57 |
+
# Initialize Google Generative AI
|
58 |
+
initialize_genai()
|
59 |
+
|
60 |
+
with gr.Blocks() as demo:
|
61 |
+
gr.Markdown("## Virtual Math Calculator with Google Generative AI")
|
62 |
+
|
63 |
+
with gr.Row():
|
64 |
+
video_input = gr.Video(source="webcam", type="numpy", label="Record your video")
|
65 |
+
output_text = gr.Textbox(label="Recognized Equation")
|
66 |
+
output_solutions = gr.Textbox(label="Solution")
|
67 |
+
output_image = gr.Image(label="Captured Image")
|
68 |
+
|
69 |
+
def process_video(video):
|
70 |
+
frame = video[0] # Take the first frame from the video
|
71 |
+
recognized_equation, solutions, image = process_frame(frame)
|
72 |
+
return recognized_equation, solutions, image
|
73 |
+
|
74 |
+
video_input.change(process_video, inputs=video_input, outputs=[output_text, output_solutions, output_image])
|
75 |
+
|
76 |
+
demo.launch()
|
77 |
+
|
78 |
+
if __name__ == "__main__":
|
79 |
+
main()
|