Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
70 lines (61 loc) · 2.09 KB
/
Copy pathapp.py
File metadata and controls
70 lines (61 loc) · 2.09 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
from flask import Flask, render_template, request, redirect
import tensorflow as tf
from tensorflow.keras.preprocessing import image
import numpy as np
import os
import cv2
import base64
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = 'uploads/'
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
# Load your Teachable Machine model
model = tf.keras.models.load_model('model/keras_model.h5')
# Class mapping
class_names = {1: "biodegradable", 2: "non-biodegradable"}
# Preprocessing function for uploaded images
def prepare_image(img_path):
img = image.load_img(img_path, target_size=(224, 224))
img_array = image.img_to_array(img)
img_array = np.expand_dims(img_array, axis=0)
img_array = img_array / 255.0
return img_array
# Preprocessing function for webcam frames
def prepare_frame(frame):
img = cv2.resize(frame, (224, 224))
img = np.expand_dims(img, axis=0)
img = img / 255.0
return img
# Home page
@app.route('/')
def index():
return render_template('index.html')
# Upload image prediction
@app.route('/upload', methods=['POST'])
def upload():
if 'file' not in request.files:
return redirect('/')
file = request.files['file']
if file.filename == '':
return redirect('/')
path = os.path.join(app.config['UPLOAD_FOLDER'], file.filename)
file.save(path)
img = prepare_image(path)
prediction = model.predict(img)
class_index = np.argmax(prediction) + 1
result = class_names[class_index]
return render_template('index.html', upload_result=result)
# Webcam prediction
@app.route('/predict_webcam', methods=['POST'])
def predict_webcam():
data_url = request.form['image']
encoded_data = data_url.split(',')[1]
decoded_data = base64.b64decode(encoded_data)
nparr = np.frombuffer(decoded_data, np.uint8)
frame = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
img = prepare_frame(frame)
prediction = model.predict(img)
class_index = np.argmax(prediction) + 1
result = class_names[class_index]
return render_template('index.html', webcam_result=result)
if __name__ == '__main__':
app.run(debug=True)