-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.py
More file actions
158 lines (126 loc) · 5.65 KB
/
Copy pathapp.py
File metadata and controls
158 lines (126 loc) · 5.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import os
from flask import Flask, request, redirect, url_for, render_template, flash
from werkzeug.utils import secure_filename
from PIL import Image
import pytesseract
from rankMenu import RankMenu
from evaluateMenu import MenuEvaluator
from readMenu import ReadMenu
import firebase_admin
from firebase_admin import credentials, auth
# Define the upload folder
UPLOAD_FOLDER = './uploads'
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg', 'gif'}
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
# Ensure the upload directory exists
if not os.path.exists(UPLOAD_FOLDER):
os.makedirs(UPLOAD_FOLDER)
# Initialize Firebase Admin SDK
cred = credentials.Certificate('palatemapfirebase.json')
firebase_admin.initialize_app(cred)
app.secret_key = os.urandom(24) # Generates a random key
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
def perform_ocr(filepath):
img = Image.open(filepath)
text = pytesseract.image_to_string(img)
return text
@app.route('/')
def index():
return render_template('index.html')
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
email = request.form.get('email')
password = request.form.get('password')
try:
if not email:
raise ValueError("Email is required.")
if not password:
raise ValueError("Password is required.")
user = auth.get_user_by_email(email)
flash("Login successful!", "success")
return redirect(url_for('homepage')) # Redirect to homepage after login
except ValueError as ve:
flash(f"Input Error: {str(ve)}", "danger")
except firebase_admin._auth_utils.UserNotFoundError:
flash("No user record found for the provided email.", "danger")
except Exception as e:
flash(f"An error occurred: {str(e)}", "danger")
return render_template('login.html')
@app.route('/register', methods=['GET', 'POST'])
def register():
if request.method == 'POST':
email = request.form.get('email')
password = request.form.get('password')
if not email or not password:
flash("Email and password are required.", "danger")
return redirect(url_for('register'))
try:
user = auth.create_user(email=email, password=password)
flash("Registration successful! Please log in.", "success")
return redirect(url_for('login'))
except firebase_admin.auth.EmailAlreadyExistsError:
flash("Email already in use.", "danger")
except Exception as e:
flash(f"Registration failed: {str(e)}", "danger")
return render_template('register.html')
@app.route('/homepage', methods=['GET', 'POST'])
def homepage():
if request.method == 'POST':
# Handle file upload from camera
if 'file_camera' in request.files:
file_camera = request.files['file_camera']
if file_camera.filename != '' and allowed_file(file_camera.filename):
filename = secure_filename(file_camera.filename)
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file_camera.save(filepath)
print(f"Saved camera file: {filename}")
# Process the file and get results
try:
eval_menu = MenuEvaluator()
dish_dict = eval_menu.evaluate_menu(filepath)
menuRanker = RankMenu()
user_vector = [3, 4, 6, 2, 8, 9]
rankedMenu = menuRanker.rankMenu(user_vector, dish_dict)
return render_template('result.html', text=rankedMenu, image_url=filepath)
except Exception as e:
print(f"Error processing file: {e}")
flash("An error occurred while processing the file.", "danger")
return redirect(request.url)
# Handle file upload from library
if 'file_library' in request.files:
file_library = request.files['file_library']
if file_library.filename != '' and allowed_file(file_library.filename):
filename = secure_filename(file_library.filename)
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file_library.save(filepath)
print(f"Saved library file: {filename}")
# Process the file and get results
try:
eval_menu = MenuEvaluator()
dish_dict = eval_menu.evaluate_menu(filepath)
menuRanker = RankMenu()
user_vector = [3, 4, 6, 2, 8, 9]
rankedMenu = menuRanker.rankMenu(user_vector, dish_dict)
return render_template('result.html', text=rankedMenu, image_url=filepath)
except Exception as e:
print(f"Error processing file: {e}")
flash("An error occurred while processing the file.", "danger")
return redirect(request.url)
return render_template('homepage.html')
def allowed_file(filename):
# Check for allowed file extensions
return '.' in filename and filename.rsplit('.', 1)[1].lower() in {'jpg', 'jpeg', 'png'}
@app.route('/breakfast')
def breakfast():
return render_template('breakfast.html')
@app.route('/userprofile')
def userprofile():
return render_template('userprofile.html')
@app.route('/result')
def result():
return render_template('result.html')
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True)