0% found this document useful (0 votes)
5 views

Assignment 10

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Assignment 10

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

assignment-10

November 12, 2024

[ ]: # app.py
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 = tf.keras.models.load_model(r"C:
↪\Users\fool0\OneDrive\Desktop\code\traffic_sign_model.h5")

# Define the prediction endpoint


@app.route('/predict', methods=['POST'])
def predict():
# Ensure an image file is part of the request
if 'file' not in request.files:
return jsonify({"error": "No file provided"}), 400

# Read the image file


file = request.files['file']
image = Image.open(io.BytesIO(file.read())).convert('RGB')

# Preprocess the image (resize and normalize)


image = image.resize((32, 32)) # Assuming model expects 32x32 images
image_array = np.array(image) / 255.0 # Normalize to [0, 1]
image_array = np.expand_dims(image_array, axis=0) # Add batch dimension

# Perform prediction
prediction = model.predict(image_array)
predicted_class = np.argmax(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__':
app.run(host='0.0.0.0', 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 (0.0.0.0)
* Running on http://127.0.0.1:5000
* Running on http://192.168.0.107:5000
INFO:werkzeug:Press CTRL+C to quit
INFO:werkzeug:192.168.0.107 - - [01/Nov/2024 21:48:49] "GET / HTTP/1.1"
404 -
INFO:werkzeug:192.168.0.107 - - [01/Nov/2024 21:48:50] "GET /favicon.ico
HTTP/1.1" 404 -
INFO:werkzeug:127.0.0.1 - - [01/Nov/2024 21:48:54] "GET / HTTP/1.1" 404
-
INFO:werkzeug:127.0.0.1 - - [01/Nov/2024 21:48:54] "GET /favicon.ico
HTTP/1.1" 404 -

You might also like