File size: 2,324 Bytes
88fb180
4bbe7c5
4f8ccd7
 
 
 
88fb180
53af9f1
2dc9457
 
 
 
 
 
4f8ccd7
 
 
 
 
 
 
 
 
 
88fb180
4f8ccd7
 
 
c4c28ed
4bbe7c5
 
 
 
 
 
d75a914
c4c28ed
5723f1a
 
4f8ccd7
 
71168f5
4f8ccd7
 
 
 
 
c4c28ed
 
 
4f8ccd7
 
 
c4c28ed
 
4f8ccd7
c4c28ed
4f8ccd7
 
c4c28ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import subprocess
import tensorflow as tf
import numpy as np
from PIL import Image
import io
from flask import Flask, request, jsonify

# Function to install a package using pip
def install_package(package):
    try:
        subprocess.check_call(['pip', 'install', package])
    except subprocess.CalledProcessError as e:
        print(f"Error installing {package}: {e}")

# List of libraries to install
libraries = [
    'gradio',
    'tensorflow',
    'numpy',
    'Pillow',
    'opencv-python-headless',
    'Flask',
    'joblib'
]

# Install each library using pip
for library in libraries:
    install_package(library)

# Attempt to import required libraries after installation
try:
    import joblib
except ImportError:
    print("Error: joblib failed to install or import.")
    exit(1)

# Load the pre-trained TensorFlow model
model = tf.keras.models.load_model("imageclassifier.h5")

# Save the model as .pkl file
joblib.dump(model, "imageclassifier.pkl")

# Initialize Flask application
app = Flask(__name__)

# Load the model from .pkl file
model = joblib.load("imageclassifier.pkl")

# Define the function to predict the teeth health
def predict_teeth_health(image):
    # Convert the PIL image object to a numpy array
    image = np.array(image)
    # Perform any necessary preprocessing (resizing, normalization, etc.) here if needed

    # Make a prediction
    prediction = model.predict(image.reshape(1, -1))

    # Assuming binary classification, adjust as per your model's output
    probability_good = prediction[0]  # Assuming it's a binary classification

    # Define the prediction result
    result = {
        "prediction": "Your Teeth are Good & You Don't Need To Visit Doctor" if probability_good > 0.5 else "Your Teeth are Bad & You Need To Visit Doctor"
    }

    return result

# Define route to accept image and return prediction
@app.route('/predict', methods=['POST'])
def predict():
    # Ensure an image was properly uploaded to our endpoint
    if request.method == 'POST':
        file = request.files['image']
        if file:
            # Read the image using PIL
            img = Image.open(file.stream)
            # Perform prediction
            prediction = predict_teeth_health(img)
            return jsonify(prediction)

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