AttributeError: partially initialized module 'keras.src' has no attribute 'utils' (most likely due to a circular import)
this are my import files
- from flask import Flask, render_template, request, jsonify
- from keras.models import load_model
- from PIL import Image, ImageOps
- import numpy as np
from flask import Flask, render_template, request, jsonifyfrom keras.models import load_modelfrom PIL import Image, ImageOpsimport numpy as npapp = Flask(__name__)# Load the model and labelsmodel = load_model('/Users/Pratik/PycharmProjects/SIG_Project/keras_model.h55', compile=False)class_names = open("/Users/Pratik/PycharmProjects/SIG_Project/labels.txt", "r").readlines()# Set the confidence thresholdconfidence_threshold = 0.2@app.route('/')def index(): return '''<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Image Classifier</title></head><body><h1>Image Classifier</h1><form id="upload-form" enctype="multipart/form-data"><input type="file" name="file" accept="image/*"><button type="button" onclick="predict()">Predict</button></form><div id="result"></div><script> function predict() { var form = document.getElementById('upload-form'); var formData = new FormData(form); fetch('/predict', { method: 'POST', body: formData }) .then(response => response.json()) .then(data => { var resultDiv = document.getElementById('result'); resultDiv.innerHTML = ''; data.forEach(prediction => { var classDiv = document.createElement('div'); classDiv.innerHTML = `<p>Class: ${prediction.class}</p><p>Confidence Score: ${prediction.confidence.toFixed(8)}</p>`; resultDiv.appendChild(classDiv); }); }) .catch(error => console.error('Error:', error)); }</script></body></html>'''@app.route('/predict', methods=['POST'])def predict(): # Get image from frontend file = request.files['file'] # Preprocess the image image = Image.open(file).convert("RGB") size = (224, 224) image = ImageOps.fit(image, size, Image.Resampling.LANCZOS) image_array = np.asarray(image) normalized_image_array = (image_array.astype(np.float32) / 127.5) - 1 data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32) data[0] = normalized_image_array # Predict prediction = model.predict(data) # Prepare response response = [] for i in range(len(class_names)): class_name = class_names[i].strip() confidence_score = prediction[0][i] if confidence_score > confidence_threshold: response.append({'class': class_name[2:],'confidence': float(confidence_score) }) return jsonify(response)if __name__ == '__main__': app.run(debug=True)
#I am runing this code in jupyter