Spaces:
Runtime error
Runtime error
initial commit
Browse files- index.py +61 -0
- model/model_float16_quant.tflite +3 -0
- requirements.txt +0 -0
index.py
ADDED
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import keras.utils as image
|
3 |
+
import numpy as np
|
4 |
+
import io
|
5 |
+
import tensorflow as tf
|
6 |
+
import base64
|
7 |
+
|
8 |
+
# Load TFLite model and allocate tensors
|
9 |
+
interpreter = tf.lite.Interpreter(model_path="model/model_float16_quant.tflite")
|
10 |
+
interpreter.allocate_tensors()
|
11 |
+
|
12 |
+
st.title("Image Classifier")
|
13 |
+
|
14 |
+
uploaded_file = st.file_uploader("Upload an image...", type=["jpg", "png", "jpeg"])
|
15 |
+
img_data = None
|
16 |
+
|
17 |
+
if uploaded_file is not None:
|
18 |
+
# Use in-memory image
|
19 |
+
image_stream = io.BytesIO(uploaded_file.read())
|
20 |
+
img = image.load_img(image_stream, target_size=(224, 224))
|
21 |
+
|
22 |
+
# Convert PIL Image to data URL
|
23 |
+
img_buffer = io.BytesIO()
|
24 |
+
img.save(img_buffer, format="PNG")
|
25 |
+
img_data = base64.b64encode(img_buffer.getvalue()).decode()
|
26 |
+
|
27 |
+
# Preprocess the image
|
28 |
+
img_array = image.img_to_array(img)
|
29 |
+
x_mean = img_array.mean()
|
30 |
+
x_std = img_array.std()
|
31 |
+
img_array = (img_array - x_mean) / x_std
|
32 |
+
img_array = np.expand_dims(img_array, axis=0)
|
33 |
+
|
34 |
+
# Set input tensor for the TFLite interpreter
|
35 |
+
input_details = interpreter.get_input_details()
|
36 |
+
interpreter.set_tensor(input_details[0]['index'], img_array)
|
37 |
+
|
38 |
+
# Invoke the interpreter
|
39 |
+
interpreter.invoke()
|
40 |
+
|
41 |
+
# Get the prediction result
|
42 |
+
output_details = interpreter.get_output_details()
|
43 |
+
predictions = interpreter.get_tensor(output_details[0]['index'])
|
44 |
+
predicted_class = np.argmax(predictions, axis=1)
|
45 |
+
|
46 |
+
class_dict = {
|
47 |
+
0: 'Actinic keratoses',
|
48 |
+
1: 'Basal cell carcinoma',
|
49 |
+
2: 'Benign keratosis-like lesions',
|
50 |
+
3: 'Dermatofibroma_df',
|
51 |
+
4: 'Melanocytic nevi',
|
52 |
+
5: 'Vascular lesions',
|
53 |
+
6: 'Dermatofibroma_mel'
|
54 |
+
}
|
55 |
+
|
56 |
+
result = class_dict[predicted_class[0]]
|
57 |
+
|
58 |
+
st.write(f"Prediction: {result}")
|
59 |
+
|
60 |
+
# Display the uploaded image
|
61 |
+
st.image(img, caption='Uploaded Image', use_column_width=True)
|
model/model_float16_quant.tflite
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:c112cc0308137a5ab6321c054e40c6e0282f74b3ec81ae4b157272145060cb3d
|
3 |
+
size 51239848
|
requirements.txt
ADDED
Binary file (2.9 kB). View file
|
|