-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
49 lines (38 loc) · 1.49 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
from flask import Flask, render_template, request, jsonify
import os
from utils import preprocess_image, predict_waste_category
app = Flask(__name__)
# Configure upload folder
UPLOAD_FOLDER = 'static/uploads'
if not os.path.exists(UPLOAD_FOLDER):
os.makedirs(UPLOAD_FOLDER)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
# Allowed file extensions
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg'}
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/')
def home():
return render_template('index.html')
@app.route('/predict', methods=['POST'])
def predict():
if 'file' not in request.files:
return jsonify({'error': 'No file uploaded'}), 400
file = request.files['file']
if file.filename == '':
return jsonify({'error': 'No file selected'}), 400
if file and allowed_file(file.filename):
# Save the uploaded file
filename = os.path.join(app.config['UPLOAD_FOLDER'], file.filename)
file.save(filename)
# Preprocess image and make prediction
try:
category = predict_waste_category(filename)
return render_template('result.html',
image_path=filename,
category=category)
except Exception as e:
return jsonify({'error': str(e)}), 500
return jsonify({'error': 'Invalid file type'}), 400
if __name__ == '__main__':
app.run(debug=True)