Manith Marapperuma
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,78 +6,81 @@ from mtcnn import MTCNN
|
|
| 6 |
import cv2
|
| 7 |
import io
|
| 8 |
|
|
|
|
| 9 |
# Load the model (ensure correct path for loading)
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
input_shape = (224, 224, 3)
|
| 14 |
-
model = tf.keras.models.load_model('oily_dry.h5', compile=False, custom_objects={'input_shape': input_shape})
|
| 15 |
-
|
| 16 |
-
# model = tf.keras.models.load_model('oily_dry.h5')
|
| 17 |
|
| 18 |
# Load the MTCNN face detection model
|
| 19 |
mtcnn = MTCNN()
|
| 20 |
|
| 21 |
-
def detect_and_process_skin(
|
| 22 |
-
"""Detects faces in an image, crops the skin region, and
|
| 23 |
-
#
|
| 24 |
-
|
|
|
|
| 25 |
img_rgb = cv2.cvtColor(img_np, cv2.COLOR_BGR2RGB)
|
| 26 |
|
| 27 |
# Detect faces in the image
|
| 28 |
detections = mtcnn.detect_faces(img_rgb)
|
| 29 |
|
|
|
|
| 30 |
if detections:
|
| 31 |
x, y, width, height = detections[0]['box']
|
| 32 |
|
| 33 |
# Crop the face region
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
# Convert cropped face to PIL Image for compatibility with model preprocessing
|
| 37 |
-
pil_img = Image.fromarray(cv2.cvtColor(face_img, cv2.COLOR_BGR2RGB))
|
| 38 |
|
|
|
|
|
|
|
| 39 |
return pil_img
|
| 40 |
else:
|
| 41 |
-
# Return
|
| 42 |
-
return
|
| 43 |
|
| 44 |
-
def classify_image(
|
| 45 |
-
"""
|
| 46 |
-
|
|
|
|
| 47 |
|
| 48 |
# Resize and preprocess the image for the model
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
|
| 53 |
# Classify the image
|
| 54 |
-
predictions = model.predict(
|
|
|
|
|
|
|
| 55 |
predicted_class = np.argmax(predictions)
|
| 56 |
percentages = predictions[0] * 100
|
|
|
|
| 57 |
|
| 58 |
-
return predicted_class,
|
| 59 |
|
| 60 |
def app():
|
| 61 |
st.title("Oily/Dry Skin Level Predictor")
|
|
|
|
|
|
|
| 62 |
|
|
|
|
| 63 |
image_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
| 64 |
|
|
|
|
| 65 |
if image_file is not None:
|
| 66 |
-
|
| 67 |
-
image = Image.open(image_file)
|
| 68 |
-
predicted_class, dry_percentage, oily_percentage, normal_percentage = classify_image(image)
|
| 69 |
|
| 70 |
# Convert processed image to bytes for display
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
st.image(processed_image_bytes, width=250)
|
| 76 |
|
| 77 |
-
# Display
|
|
|
|
| 78 |
st.write(f"Dry Skin: {dry_percentage:.2f}%")
|
|
|
|
| 79 |
st.write(f"Oily Skin: {oily_percentage:.2f}%")
|
|
|
|
| 80 |
st.write(f"Normal Skin: {normal_percentage:.2f}%")
|
| 81 |
|
| 82 |
-
if
|
| 83 |
-
app()
|
|
|
|
| 6 |
import cv2
|
| 7 |
import io
|
| 8 |
|
| 9 |
+
# Assuming model is included in your repository or loaded from a URL
|
| 10 |
# Load the model (ensure correct path for loading)
|
| 11 |
+
model = tf.keras.models.load_model('oily_dry.h5')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
# Load the MTCNN face detection model
|
| 14 |
mtcnn = MTCNN()
|
| 15 |
|
| 16 |
+
def detect_and_process_skin(image_bytes):
|
| 17 |
+
"""Detects faces in an image, crops the skin region, and returns it as an image object."""
|
| 18 |
+
# Load image from bytes
|
| 19 |
+
img = Image.open(io.BytesIO(image_bytes))
|
| 20 |
+
img_np = np.array(img)
|
| 21 |
img_rgb = cv2.cvtColor(img_np, cv2.COLOR_BGR2RGB)
|
| 22 |
|
| 23 |
# Detect faces in the image
|
| 24 |
detections = mtcnn.detect_faces(img_rgb)
|
| 25 |
|
| 26 |
+
# Check if any faces were detected
|
| 27 |
if detections:
|
| 28 |
x, y, width, height = detections[0]['box']
|
| 29 |
|
| 30 |
# Crop the face region
|
| 31 |
+
face_img_np = img_np[y:y+height, x:x+width]
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
+
# Convert to PIL Image for return
|
| 34 |
+
pil_img = Image.fromarray(face_img_np)
|
| 35 |
return pil_img
|
| 36 |
else:
|
| 37 |
+
# Return original image if no face was detected
|
| 38 |
+
return img
|
| 39 |
|
| 40 |
+
def classify_image(image_bytes):
|
| 41 |
+
"""Loads, preprocess, and classifies the image from bytes."""
|
| 42 |
+
# Process the image using face detection
|
| 43 |
+
pil_img = detect_and_process_skin(image_bytes)
|
| 44 |
|
| 45 |
# Resize and preprocess the image for the model
|
| 46 |
+
image = pil_img.resize((224, 224))
|
| 47 |
+
image = tf.keras.preprocessing.image.img_to_array(image) / 255.0
|
| 48 |
+
image = np.expand_dims(image, axis=0)
|
| 49 |
|
| 50 |
# Classify the image
|
| 51 |
+
predictions = model.predict(image)
|
| 52 |
+
|
| 53 |
+
# Process predictions for clarity
|
| 54 |
predicted_class = np.argmax(predictions)
|
| 55 |
percentages = predictions[0] * 100
|
| 56 |
+
dry_percentage, normal_percentage, oily_percentage = percentages
|
| 57 |
|
| 58 |
+
return predicted_class, dry_percentage, oily_percentage, normal_percentage
|
| 59 |
|
| 60 |
def app():
|
| 61 |
st.title("Oily/Dry Skin Level Predictor")
|
| 62 |
+
st.write("Coded by Manith Jayaba")
|
| 63 |
+
st.write("This app can measure the oiliness and dryness of your skin")
|
| 64 |
|
| 65 |
+
# Get the image file
|
| 66 |
image_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
| 67 |
|
| 68 |
+
# Classify and display the result
|
| 69 |
if image_file is not None:
|
| 70 |
+
predicted_class, dry_percentage, oily_percentage, normal_percentage = classify_image(image_file.getvalue())
|
|
|
|
|
|
|
| 71 |
|
| 72 |
# Convert processed image to bytes for display
|
| 73 |
+
img_bytes = io.BytesIO()
|
| 74 |
+
detect_and_process_skin(image_file.getvalue()).save(img_bytes, format='JPEG')
|
| 75 |
+
st.image(img_bytes.getvalue(), width=250, caption="Processed Image")
|
|
|
|
|
|
|
| 76 |
|
| 77 |
+
# Display progress bars
|
| 78 |
+
st.progress(int(dry_percentage))
|
| 79 |
st.write(f"Dry Skin: {dry_percentage:.2f}%")
|
| 80 |
+
st.progress(int(oily_percentage))
|
| 81 |
st.write(f"Oily Skin: {oily_percentage:.2f}%")
|
| 82 |
+
st.progress(int(normal_percentage))
|
| 83 |
st.write(f"Normal Skin: {normal_percentage:.2f}%")
|
| 84 |
|
| 85 |
+
if _name_ == "_main_":
|
| 86 |
+
app()
|