File size: 1,274 Bytes
69ac9b0
 
 
acac51e
69ac9b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import numpy as np
import streamlit as st
import tensorflow as tf
from tensorflow.keras.preprocessing.image import load_img, img_to_array
from flask import Flask, request, jsonify

app = Flask(__name__)
st.title("Nova'23 Classification Model API")
st.write("Listening...")

@app.route('/predict', methods=['POST'])
def predict_image():
    # Check if an image was uploaded
    if 'file' not in request.files:
        return 'No file uploaded'
    file = request.files['file']

    # Save the image to a temporary file
    file_path = 'temp_image.jpg'
    file.save(file_path)

    # Load and preprocess the image
    height = 180
    width = 180
    channels = 3
    img = load_img(file_path, target_size=(height, width))
    img_array = img_to_array(img)
    img_array = img_array / 255.0
    img_array = tf.reshape(img_array, [1, height, width, channels])

    # Load the model and make a prediction
    model = tf.keras.models.load_model('nova.h5')
    prediction = model.predict(img_array)
    st.write("Prediction: ", prediction)
    
    # Delete the temporary file
    os.remove(file_path)

    # Return the prediction as JSON
    return jsonify({'prediction': 'Pneumonia' if prediction[0][0] > 0.5 else 'Normal'})

if __name__ == '__main__':
    app.run()