-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
113 lines (94 loc) · 3.58 KB
/
app.py
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
from flask import Flask, render_template, request, redirect, url_for, flash
import cv2
import numpy as np
from dotenv import load_dotenv
import cloudinary
from cloudinary.uploader import upload
from cloudinary.exceptions import Error as CloudinaryError
import os
import mongo_utils, model_utils
load_dotenv()
app = Flask('__name__')
app.secret_key = 'verysecret'
cloudinary.config(
cloud_name = os.getenv('CLOUD_NAME'),
api_key=os.getenv('API_KEY'),
api_secret=os.getenv('API_SECRET')
)
@app.route('/')
def index():
students_list = list(mongo_utils.students_collection.find({}, {'_id': 0, 'embedding': 0}))
print(students_list)
return render_template('index.html', students_list=students_list)
@app.route('/add-student', methods = ['GET', 'POST'])
def add_student():
if request.method == 'GET':
return render_template('student_form.html', student = None)
else:
name = request.form['name']
student_id = request.form['student_id']
branch = request.form['branch']
photo = request.files['photo']
if photo.filename == '':
flash('Error: Empty file', 'error')
return 'Error: Empty file'
try:
upload_result = upload(photo)
photo_url = upload_result['secure_url']
photo.seek(0)
img = cv2.imdecode(np.frombuffer(photo.read(), np.uint8), cv2.IMREAD_COLOR)
embedding = model_utils.getEmbedding(img)
mongo_utils.students_collection.insert_one({
'name': name,
'studentId': student_id,
'branch': branch,
'embedding': embedding,
'photoUrl': photo_url}
)
flash('Added new student', 'success')
return redirect(url_for('index'))
except CloudinaryError as e:
flash('Could not add new student', 'error')
return f'Error: {str(e)}'
@app.route('/edit-student/<student_id>', methods=['GET', 'POST'])
def edit_student(student_id):
student = mongo_utils.getStudentDetails(student_id=student_id)
if request.method == 'GET':
return render_template('student_form.html', student = student)
else:
name = request.form['name']
student_id = request.form['student_id']
branch = request.form['branch']
photo = request.files['photo']
if photo.filename == '':
return 'Error: Empty file'
try:
upload_result = upload(photo)
photo_url = upload_result['url']
photo.seek(0)
img = cv2.imdecode(np.frombuffer(photo.read(), np.uint8), cv2.IMREAD_COLOR)
embedding = model_utils.getEmbedding(img)
mongo_utils.students_collection.update_one(
{'studentId': student_id},
{
'$set': {
'name': name,
'studentId': student_id,
'branch': branch,
'embedding': embedding,
'photoUrl': photo_url
}
}
)
flash('Updated student details', 'success')
return redirect(url_for('index'))
except CloudinaryError as e:
flash('Could not update student details', 'error')
return f'Error: {str(e)}'
@app.route('/delete-student/<student_id>')
def delete_student(student_id):
mongo_utils.deleteStudent(student_id=student_id)
flash('Removed student', 'success')
return redirect(url_for("index"))
if __name__ == '__main__':
app.run(debug=True)