|
import streamlit as st |
|
import tensorflow as tf |
|
import numpy as np |
|
from PIL import Image |
|
from mtcnn import MTCNN |
|
import cv2 |
|
import io |
|
|
|
|
|
|
|
model = tf.keras.models.load_model('oily_dry.h5') |
|
|
|
|
|
mtcnn = MTCNN() |
|
|
|
def detect_and_process_skin(image_bytes): |
|
"""Detects faces in an image, crops the skin region, and returns it as an image object.""" |
|
|
|
img = Image.open(io.BytesIO(image_bytes)) |
|
img_np = np.array(img) |
|
img_rgb = cv2.cvtColor(img_np, cv2.COLOR_BGR2RGB) |
|
|
|
|
|
detections = mtcnn.detect_faces(img_rgb) |
|
|
|
|
|
if detections: |
|
x, y, width, height = detections[0]['box'] |
|
|
|
|
|
face_img_np = img_np[y:y+height, x:x+width] |
|
|
|
|
|
pil_img = Image.fromarray(face_img_np) |
|
return pil_img |
|
else: |
|
|
|
return img |
|
|
|
def classify_image(image_bytes): |
|
"""Loads, preprocess, and classifies the image from bytes.""" |
|
|
|
pil_img = detect_and_process_skin(image_bytes) |
|
|
|
|
|
image = pil_img.resize((224, 224)) |
|
image = tf.keras.preprocessing.image.img_to_array(image) / 255.0 |
|
image = np.expand_dims(image, axis=0) |
|
|
|
|
|
predictions = model.predict(image) |
|
|
|
|
|
predicted_class = np.argmax(predictions) |
|
percentages = predictions[0] * 100 |
|
dry_percentage, normal_percentage, oily_percentage = percentages |
|
|
|
return predicted_class, dry_percentage, oily_percentage, normal_percentage |
|
|
|
def app(): |
|
st.title("SkinDet: Oily/Dry Skin Level Predictor 👨🏻🔬🧬") |
|
st.markdown("### Designed & Developed by `Manith Jayaba` & `Shakya Dissanayake`") |
|
st.image('skin.webp', caption='Skin ANN',use_column_width=True) |
|
|
|
st.write(""" |
|
This app can measure the oiliness and dryness of your skin in to 5 levels: |
|
1. Level 1: Normal Oily Skin |
|
2. Level 2: Mid Oily Skin |
|
3. Level 3: High Oily Skin |
|
4. Level 4: Very High Oily Skin |
|
5. High Level Dry Skin |
|
""") |
|
|
|
|
|
image_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"]) |
|
|
|
|
|
if image_file is not None: |
|
predicted_class, dry_percentage, oily_percentage, normal_percentage = classify_image(image_file.getvalue()) |
|
|
|
|
|
img_bytes = io.BytesIO() |
|
detect_and_process_skin(image_file.getvalue()).save(img_bytes, format='JPEG') |
|
st.image(img_bytes.getvalue(), width=250, caption="Processed Image") |
|
|
|
|
|
st.write(f"Dry Skin: {dry_percentage:.2f}%") |
|
st.progress(int(dry_percentage)) |
|
st.write(f"Oily Skin: {oily_percentage:.2f}%") |
|
st.progress(int(oily_percentage)) |
|
st.write(f"Normal Skin: {normal_percentage:.2f}%") |
|
st.progress(int(normal_percentage)) |
|
|
|
|
|
if int(oily_percentage) >= 80: |
|
st.warning('Level 4: Very High Oiliness Level') |
|
st.warning("To manage very high oiliness in your skin, start with a gentle, oil-free cleanser twice daily, followed by an alcohol-free toner. Opt for a lightweight, non-comedogenic moisturizer to hydrate without adding grease, and use a broad-spectrum sunscreen daily. Incorporate weekly exfoliation with salicylic acid to keep pores clear and occasionally use clay masks to absorb excess oil. Blotting papers can help manage shine throughout the day. Remember, a balanced diet and proper hydration also play a crucial role in controlling oil production. Adjust your skincare routine based on how your skin responds, and consider consulting a dermatologist for personalized advice or treatments.") |
|
elif 60 <= int(oily_percentage) < 80: |
|
st.warning('Level 3: High Oiliness Level') |
|
st.warning("To manage high oiliness, adopt a straightforward skincare routine: Start with a gentle, oil-free cleanser twice a day, followed by an alcohol-free toner to balance skin pH. Use a lightweight, non-comedogenic moisturizer to hydrate without adding shine. Apply a broad-spectrum, oil-free sunscreen daily. Weekly, gently exfoliate to prevent clogged pores and use a clay mask to absorb excess oil. Keep blotting papers on hand for midday touch-ups. Adjust your diet to minimize high-sugar and fatty foods, which can influence oil production. Regularly assess your skin's reaction to products and adjust as needed, considering a dermatologist's advice for persistent issues.") |
|
elif 50 < int(oily_percentage) < 60: |
|
st.info('Level 2: Mid Oiliness Level') |
|
st.info("For managing mid-level oiliness, balance is key. Start with a mild cleanser to wash away excess oil without stripping the skin, doing so morning and night. Incorporate a lightweight, non-comedogenic moisturizer to hydrate and protect the skin without adding greasiness. A broad-spectrum, oil-free sunscreen is essential for daily protection. Use a gentle exfoliant once or twice a week to remove dead skin cells and keep pores clear, opting for products with salicylic acid for its oil-dissolving properties. Consider using a clay mask weekly to draw out impurities and control shine. Adjust your routine based on seasonal changes or hormonal fluctuations, as these can affect your skin’s oil production. A balanced diet, rich in fruits, vegetables, and plenty of water, supports overall skin health. Monitor how your skin responds to different products and tweak your routine as necessary, seeking a dermatologist’s advice for stubborn or severe issues.") |
|
elif int(oily_percentage) <= 50 and int(dry_percentage)>= 55: |
|
st.warning('High Level Dry Skin') |
|
st.warning("For high-level dry skin, a nurturing and moisture-rich routine is essential. Begin with a hydrating, soap-free cleanser to gently remove impurities without stripping skin of its natural oils. Follow up with a rich, emollient moisturizer that contains ingredients like hyaluronic acid, glycerin, or ceramides to lock in moisture and strengthen the skin's barrier. Applying an oil-based serum before your moisturizer can offer an additional hydration boost. Don’t forget to apply a broad-spectrum sunscreen during the day to protect from UV rays, which can exacerbate dryness. Once or twice a week, use a gentle exfoliant to remove dead skin cells, improving absorption of moisturizers. Consider incorporating a weekly hydrating mask for deep moisture replenishment. To support skin hydration from the inside, drink plenty of water and consider using a humidifier in dry environments. Regularly assess your skin’s condition and adjust your skincare products as needed, especially with seasonal changes. For persistent dryness or irritation, consult a dermatologist for tailored advice.") |
|
elif int(oily_percentage) <= 50: |
|
st.info('Level 1: Normal Oiliness Level') |
|
st.info("For normal oiliness levels, a simple and balanced skincare routine works best. Begin with a gentle cleanser to remove dirt and impurities without over-drying, using it both in the morning and evening. Follow up with a lightweight moisturizer that maintains hydration without contributing to excess oil. Daily sunscreen application is crucial to protect your skin from UV damage; opt for a formula that feels comfortable on your skin type. Incorporate a mild exfoliant once a week to slough off dead skin cells and promote cell turnover, ensuring your skin remains clear and vibrant. Adjust your skincare products seasonally, as your skin's needs may change with the weather. Alongside your skincare routine, maintaining a healthy diet and staying hydrated contribute to the overall appearance and health of your skin. Regularly assess how your skin responds to your routine, and don't hesitate to tweak it as needed for the best results.") |
|
|
|
if __name__ == "__main__": |
|
app() |