-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.py
More file actions
116 lines (97 loc) · 3.75 KB
/
app.py
File metadata and controls
116 lines (97 loc) · 3.75 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
109
110
111
112
113
114
115
116
import os
import numpy as np
import tensorflow as tf
from flask import Flask, request, jsonify
from werkzeug.utils import secure_filename
from PIL import Image
import io
import base64
app = Flask(__name__)
# Load the model
model = None
def load_model():
global model
model = tf.keras.models.load_model('Model2.h5')
return model
# Class labels
class_names = ['Shirt', 'T-Shirt', 'Hoodies', 'Jeans', 'Shorts', 'Kurtas', 'Blazers']
@app.route('/')
def index():
return """
<h1>Fashion Item Classifier API</h1>
<p>API for classifying fashion items into Hoodie, Shirt, T-shirt, or Jeans.</p>
<h2>Endpoints:</h2>
<ul>
<li><code>/predict</code> - POST an image file to get classification results</li>
<li><code>/predict/base64</code> - POST a base64 encoded image to get classification results</li>
</ul>
"""
@app.route('/predict', methods=['POST'])
def predict():
if 'file' not in request.files:
return jsonify({'error': 'No file provided'}), 400
file = request.files['file']
if file.filename == '':
return jsonify({'error': 'No file selected'}), 400
try:
# Read and preprocess the image
img = Image.open(file.stream)
img = img.resize((224, 224)) # Adjust size according to your model's input
img_array = tf.keras.preprocessing.image.img_to_array(img)
img_array = np.expand_dims(img_array, axis=0)
img_array = img_array / 255.0 # Normalize
# Make prediction
predictions = model.predict(img_array)
predicted_class_index = np.argmax(predictions[0])
predicted_class = class_names[predicted_class_index]
confidence = float(predictions[0][predicted_class_index])
return jsonify({
'predicted_class': predicted_class,
'confidence': confidence,
'all_probabilities': {
class_name: float(prob)
for class_name, prob in zip(class_names, predictions[0])
}
})
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.route('/predict/base64', methods=['POST'])
def predict_base64():
try:
data = request.json
if not data or 'image' not in data:
return jsonify({'error': 'No base64 image provided'}), 400
# Decode the base64 image
image_data = data['image']
# Remove the data URL prefix if present
if ',' in image_data:
image_data = image_data.split(',', 1)[1]
# Decode and process the image
image_bytes = base64.b64decode(image_data)
img = Image.open(io.BytesIO(image_bytes))
img = img.resize((224, 224)) # Adjust based on your model's input size
img_array = tf.keras.preprocessing.image.img_to_array(img)
img_array = np.expand_dims(img_array, axis=0)
img_array = img_array / 255.0 # Normalize
# Make prediction
predictions = model.predict(img_array)
predicted_class_index = np.argmax(predictions[0])
predicted_class = class_names[predicted_class_index]
confidence = float(predictions[0][predicted_class_index])
return jsonify({
'predicted_class': predicted_class,
'confidence': confidence,
'all_probabilities': {
class_name: float(prob)
for class_name, prob in zip(class_names, predictions[0])
}
})
except Exception as e:
return jsonify({'error': str(e)}), 500
@app.before_first_request
def before_first_request():
load_model()
if __name__ == '__main__':
# Use PORT environment variable for compatibility with cloud platforms
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)