File size: 8,291 Bytes
0a537a8
 
 
 
 
 
ec6413d
0a537a8
9355ddd
0a537a8
9355ddd
0a537a8
 
 
 
9355ddd
 
 
 
 
ec6413d
0a537a8
 
 
 
9355ddd
0a537a8
 
 
 
9355ddd
0a537a8
9355ddd
 
ec6413d
0a537a8
9355ddd
 
0a537a8
9355ddd
 
 
 
0a537a8
ec6413d
9355ddd
 
 
0a537a8
ec6413d
9355ddd
 
 
ec6413d
 
9355ddd
0a537a8
9355ddd
0a537a8
 
c721fa7
5c45c2a
7d36fed
5c45c2a
 
 
 
 
 
 
 
 
0a537a8
9355ddd
0a537a8
 
9355ddd
0a537a8
9355ddd
0a537a8
ec6413d
9355ddd
 
 
0a537a8
9355ddd
ec6413d
7ea687d
ec6413d
7ea687d
ec6413d
7ea687d
0a537a8
7ea687d
 
5c45c2a
 
7ea687d
5c45c2a
 
7ea687d
 
5c45c2a
e7d81a8
5c45c2a
 
d347b8a
 
 
 
9b4a01e
9355ddd
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import streamlit as st
import tensorflow as tf
import numpy as np
from PIL import Image
from mtcnn import MTCNN
import cv2
import io

# Assuming model is included in your repository or loaded from a URL
# Load the model (ensure correct path for loading)
model = tf.keras.models.load_model('oily_dry.h5')

# Load the MTCNN face detection model
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."""
    # Load image from bytes
    img = Image.open(io.BytesIO(image_bytes))
    img_np = np.array(img)
    img_rgb = cv2.cvtColor(img_np, cv2.COLOR_BGR2RGB)

    # Detect faces in the image
    detections = mtcnn.detect_faces(img_rgb)

    # Check if any faces were detected
    if detections:
        x, y, width, height = detections[0]['box']

        # Crop the face region
        face_img_np = img_np[y:y+height, x:x+width]

        # Convert to PIL Image for return
        pil_img = Image.fromarray(face_img_np)
        return pil_img
    else:
        # Return original image if no face was detected
        return img

def classify_image(image_bytes):
    """Loads, preprocess, and classifies the image from bytes."""
    # Process the image using face detection
    pil_img = detect_and_process_skin(image_bytes)

    # Resize and preprocess the image for the model
    image = pil_img.resize((224, 224))
    image = tf.keras.preprocessing.image.img_to_array(image) / 255.0
    image = np.expand_dims(image, axis=0)

    # Classify the image
    predictions = model.predict(image)

    # Process predictions for clarity
    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
    """)

    # Get the image file
    image_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])

    # Classify and display the result
    if image_file is not None:
        predicted_class, dry_percentage, oily_percentage, normal_percentage = classify_image(image_file.getvalue())

        # Convert processed image to bytes for display
        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")

        # Display progress bars
        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))

        # logic for oiliness level
        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()