assignment-10
November 12, 2024
[ ]: # [Link]
from flask import Flask, request, jsonify
import tensorflow as tf
import numpy as np
from PIL import Image
import io
# Initialize the Flask app
app = Flask(__name__)
# Load the trained model
model = [Link].load_model(r"C:
↪\Users\fool0\OneDrive\Desktop\code\traffic_sign_model.h5")
# Define the prediction endpoint
@[Link]('/predict', methods=['POST'])
def predict():
# Ensure an image file is part of the request
if 'file' not in [Link]:
return jsonify({"error": "No file provided"}), 400
# Read the image file
file = [Link]['file']
image = [Link]([Link]([Link]())).convert('RGB')
# Preprocess the image (resize and normalize)
image = [Link]((32, 32)) # Assuming model expects 32x32 images
image_array = [Link](image) / 255.0 # Normalize to [0, 1]
image_array = np.expand_dims(image_array, axis=0) # Add batch dimension
# Perform prediction
prediction = [Link](image_array)
predicted_class = [Link](prediction, axis=1)[0]
confidence = prediction[0][predicted_class]
# Return the result as a JSON response
return jsonify({
1
"predicted_class": int(predicted_class),
"confidence": float(confidence)
})
# Run the Flask app
if __name__ == '__main__':
[Link](host='[Link]', port=5000)
WARNING:absl:Compiled the loaded model, but the compiled metrics have yet to be
built. `model.compile_metrics` will be empty until you train or evaluate the
model.
* Serving Flask app '__main__'
* Debug mode: off
INFO:werkzeug:WARNING: This is a development server. Do not use it in a
production deployment. Use a production WSGI server instead.
* Running on all addresses ([Link])
* Running on [Link]
* Running on [Link]
INFO:werkzeug:Press CTRL+C to quit
INFO:werkzeug:[Link] - - [01/Nov/2024 [Link] "GET / HTTP/1.1"
404 -
INFO:werkzeug:[Link] - - [01/Nov/2024 [Link] "GET /[Link]
HTTP/1.1" 404 -
INFO:werkzeug:[Link] - - [01/Nov/2024 [Link] "GET / HTTP/1.1" 404
-
INFO:werkzeug:[Link] - - [01/Nov/2024 [Link] "GET /[Link]
HTTP/1.1" 404 -