|
import json |
|
from PIL import Image |
|
import numpy as np |
|
from transformers import TFAutoModelForSequenceClassification, AutoTokenizer |
|
from tensorflow.keras.models import load_model |
|
import ipywidgets as widgets |
|
from IPython.display import display |
|
|
|
model_path = 'final_teath_classifier.h5' |
|
|
|
model = tf.keras.models.load_model(model_path) |
|
|
|
|
|
|
|
def preprocess_image(image: Image.Image) -> np.ndarray: |
|
|
|
image = image.resize((256, 256)) |
|
|
|
img_array = np.array(image) / 255.0 |
|
|
|
img_array = np.expand_dims(img_array, axis=0) |
|
return img_array |
|
|
|
def predict_image(image_path): |
|
img = Image.open(image_path) |
|
|
|
img_array = preprocess_image(img) |
|
|
|
inputs = tokenizer.encode(img_array, return_tensors="tf") |
|
|
|
outputs = model(inputs) |
|
predictions = tf.nn.softmax(outputs.logits, axis=-1) |
|
predicted_class = np.argmax(predictions) |
|
if predicted_class == 0: |
|
predict_label = "Clean" |
|
else: |
|
predict_label = "Carries" |
|
|
|
return predict_label, predictions.numpy().flatten() |
|
|
|
|
|
uploader = widgets.FileUpload(accept="image/*", multiple=False) |
|
|
|
|
|
display(uploader) |
|
|
|
|
|
def on_upload(change): |
|
|
|
image_file = list(uploader.value.values())[0]["content"] |
|
|
|
with open("temp_image.jpg", "wb") as f: |
|
f.write(image_file) |
|
|
|
predict_label, logits = predict_image("temp_image.jpg") |
|
|
|
predictions_json = { |
|
"predicted_class": predict_label, |
|
"evaluations": [f"{logit*100:.4f}%" for logit in logits] |
|
} |
|
|
|
print(json.dumps(predictions_json, indent=4)) |
|
|
|
|
|
uploader.observe(on_upload, names="value") |
|
|