-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
108 lines (89 loc) · 3.5 KB
/
app.py
File metadata and controls
108 lines (89 loc) · 3.5 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
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
from flask import Flask, request, jsonify
import os
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing.image import load_img, img_to_array
import numpy as np
from datetime import datetime
import uuid
from service_account_token_generator import generate_access_token
app = Flask(__name__)
# Configure upload folder
UPLOAD_FOLDER = 'uploaded_images'
if not os.path.exists(UPLOAD_FOLDER):
os.makedirs(UPLOAD_FOLDER)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
# Load the model
MODEL_PATH = './gym_model_densenet121_9958.h5'
CLASS_NAMES = [
'Bench Press', 'Dip Bar', 'Dumbells', 'Elliptical Machine',
'KettleBell', 'Lat Pulldown', 'Leg Press Machine',
'PullBar', 'Recumbent Bike', 'Stair Climber',
'Swiss Ball', 'Treadmill'
]
model = None
try:
model = load_model(MODEL_PATH)
print("Model loaded successfully.")
except Exception as e:
print(f"Error loading model: {e}")
def preprocess_image(file_path):
try:
img = load_img(file_path, target_size=(224, 224))
img_array = img_to_array(img) / 255.0
img_array = np.expand_dims(img_array, axis=0)
return img_array
except Exception as e:
raise Exception(f"Error preprocessing image: {str(e)}")
@app.route('/predict', methods=['POST'])
def predict():
if model is None:
return jsonify({'status': 'error', 'message': 'Model not loaded'}), 500
try:
if 'image' not in request.files:
return jsonify({'status': 'error', 'message': 'No image file found in the request'}), 400
image_file = request.files['image']
if image_file.filename == '':
return jsonify({'status': 'error', 'message': 'Empty file'}), 400
filename = f"image_{datetime.now().strftime('%Y%m%d_%H%M%S')}.png"
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
image_file.save(file_path)
processed_image = preprocess_image(file_path)
predictions = model.predict(processed_image)[0]
predicted_index = np.argmax(predictions)
predicted_class_name = CLASS_NAMES[predicted_index]
confidence = predictions[predicted_index]
unique_id = str(uuid.uuid4())
return jsonify({
'message': "Model is predicted successfully.",
'data': {
'id': unique_id,
'result': predicted_class_name,
'confidenceScore': round(confidence * 100, 2),
'isAboveThreshold': bool(confidence >= 0.5),
'createdAt': datetime.now().isoformat() + 'Z'
}
}), 200
except Exception as e:
return jsonify({
'status': 'error',
'message': f"Error processing request: {str(e)}"
}), 500
@app.route('/generate-token', methods=['GET'])
def generate_token_endpoint():
SERVICE_ACCOUNT_FILE = "./long-live.json" # Update the path
SCOPES = ["https://www.googleapis.com/auth/cloud-platform"]
try:
token = generate_access_token(SERVICE_ACCOUNT_FILE, SCOPES)
return jsonify({
'message': "Token generated successfully.",
'accessToken': token,
'expiresAt': (datetime.now().timestamp() + 3600),
'createdAt': datetime.now().isoformat() + 'Z'
}), 200
except Exception as e:
return jsonify({
'status': 'error',
'message': f"Error generating token: {str(e)}"
}), 500
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080)