-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
50 lines (37 loc) · 1.36 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
from flask import Flask, render_template, request, send_from_directory
import os
from model import image_pre, predict
app = Flask(__name__)
UPLOAD_FOLDER = 'static/uploads/'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
if not os.path.exists(UPLOAD_FOLDER):
os.makedirs(UPLOAD_FOLDER)
@app.route('/')
def home():
return render_template('home.html')
@app.route('/upload', methods=['GET', 'POST'])
def upload_file():
result = ""
file_url = None # Default no image
if request.method == 'POST':
if 'file1' not in request.files:
return "No file uploaded!"
file = request.files['file1']
if file.filename == '':
return "No selected file!"
filepath = os.path.join(app.config['UPLOAD_FOLDER'], file.filename)
file.save(filepath)
# Process Image
data = image_pre(filepath)
age, gen = predict(data)
gender = "Male" if gen == 1 else "Female"
result = f"Predicted Age: {age} years | Gender: {gender}"
# Set file_url for display
file_url = f"/{filepath}"
return render_template('index.html', result=result, file_url=file_url)
# Serve uploaded images
@app.route('/static/uploads/<filename>')
def uploaded_file(filename):
return send_from_directory(app.config['UPLOAD_FOLDER'], filename)
if __name__ == "__main__":
app.run(debug=True)