-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
70 lines (59 loc) · 2.29 KB
/
app.py
File metadata and controls
70 lines (59 loc) · 2.29 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, jsonify
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Input, Resizing, Conv2D, MaxPooling2D, Flatten, Dense, GlobalAveragePooling2D
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.losses import MeanSquaredError
import numpy as np
from PIL import Image
# Define a model that accepts any input size
def create_model():
model = Sequential([
Input(shape=(None, None, 3)), # Accept any height and width, with 3 color channels
Resizing(128, 128), # Dynamically resize images to 128x128
Conv2D(32, (3, 3), activation='relu'),
MaxPooling2D((2, 2)),
Conv2D(64, (3, 3), activation='relu'),
MaxPooling2D((2, 2)),
Conv2D(128, (3, 3), activation='relu'),
GlobalAveragePooling2D(), # Pool across spatial dimensions (flexible to input size)
Dense(128, activation='relu'),
Dense(3, activation='linear') # Outputs height, weight, chest
])
return model
# Create and compile the model
model = create_model()
model.compile(optimizer=Adam(), loss=MeanSquaredError(), metrics=['mae'])
print("Model created and compiled successfully!")
# Flask app
app = Flask(__name__)
# Save the model
model.save("custom_body_scanner_model.h5")
# Home route
@app.route('/')
def index():
return render_template('index.html')
# AI Prediction route
@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 selected file"}), 400
try:
# Process the uploaded image
img = Image.open(file).convert('RGB')
img_array = np.array(img) / 255.0 # Normalize the image
img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
# Predict using the model
predictions = model.predict(img_array)
height, weight, chest = predictions[0]
return jsonify({
"height": float(height),
"weight": float(weight),
"chest": float(chest)
})
except Exception as e:
return jsonify({"error": f"Prediction failed: {e}"}), 500
if __name__ == "__main__":
app.run(debug=True)