Spaces:
Runtime error
Runtime error
| from flask import Flask,render_template | |
| from flask_socketio import SocketIO,emit | |
| import base64 | |
| import numpy as np | |
| import cv2 | |
| import numpy as np | |
| from keras.models import load_model | |
| app = Flask(__name__) | |
| app.config['SECRET_KEY'] = 'secret!' | |
| socket = SocketIO(app,async_mode="eventlet") | |
| # load model and labels | |
| np.set_printoptions(suppress=True) | |
| model = load_model(r"keras_model.h5", compile=False) | |
| class_names = open(r"labels.txt", "r").readlines() | |
| def base64_to_image(base64_string): | |
| # Extract the base64 encoded binary data from the input string | |
| base64_data = base64_string.split(",")[1] | |
| # Decode the base64 data to bytes | |
| image_bytes = base64.b64decode(base64_data) | |
| # Convert the bytes to numpy array | |
| image_array = np.frombuffer(image_bytes, dtype=np.uint8) | |
| # Decode the numpy array as an image using OpenCV | |
| image = cv2.imdecode(image_array, cv2.IMREAD_COLOR) | |
| return image | |
| def test_connect(): | |
| print("Connected") | |
| emit("my response", {"data": "Connected"}) | |
| def receive_image(image): | |
| # Decode the base64-encoded image data | |
| image = base64_to_image(image) | |
| image = cv2.resize(image, (224, 224), interpolation=cv2.INTER_AREA) | |
| # emit("processed_image", image) | |
| # Make the image a numpy array and reshape it to the models input shape. | |
| image = np.asarray(image, dtype=np.float32).reshape(1, 224, 224, 3) | |
| image = (image / 127.5) - 1 | |
| # Predicts the model | |
| prediction = model.predict(image) | |
| index = np.argmax(prediction) | |
| class_name = class_names[index] | |
| confidence_score = prediction[0][index] | |
| emit("result",{"name":str(class_name),"score":str(confidence_score)}) | |
| def home(): | |
| return render_template("index.html") | |
| if __name__ == '__main__': | |
| # app.run(debug=True) | |
| socket.run(app,host="0.0.0.0", port=7860) |