Spaces:
Runtime error
Runtime error
Update eda.py
Browse files
eda.py
CHANGED
|
@@ -1,121 +1,125 @@
|
|
| 1 |
-
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
import
|
| 5 |
-
import
|
| 6 |
-
import
|
| 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 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import numpy as np
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
import seaborn as sns
|
| 5 |
+
import os
|
| 6 |
+
import cv2
|
| 7 |
+
from skimage.feature import local_binary_pattern
|
| 8 |
+
|
| 9 |
+
# Fungsi untuk validasi path
|
| 10 |
+
def validate_path(path):
|
| 11 |
+
if not os.path.exists(path):
|
| 12 |
+
st.error(f"Path {path} does not exist. Please check your dataset directory.")
|
| 13 |
+
return False
|
| 14 |
+
return True
|
| 15 |
+
|
| 16 |
+
# Fungsi untuk menghitung histogram warna
|
| 17 |
+
def calculate_color_histogram(image):
|
| 18 |
+
hist_r = cv2.calcHist([image], [2], None, [256], [0, 256]).flatten() # Red
|
| 19 |
+
hist_g = cv2.calcHist([image], [1], None, [256], [0, 256]).flatten() # Green
|
| 20 |
+
hist_b = cv2.calcHist([image], [0], None, [256], [0, 256]).flatten() # Blue
|
| 21 |
+
return hist_r, hist_g, hist_b
|
| 22 |
+
|
| 23 |
+
# Fungsi untuk plotting LBP
|
| 24 |
+
def calculate_lbp(image, radius, n_points):
|
| 25 |
+
lbp = local_binary_pattern(image, n_points, radius, method='uniform')
|
| 26 |
+
hist, _ = np.histogram(lbp.ravel(), bins=range(0, n_points + 3))
|
| 27 |
+
return hist
|
| 28 |
+
|
| 29 |
+
# Fungsi utama aplikasi Streamlit
|
| 30 |
+
def run():
|
| 31 |
+
st.title("Exploratory Data Analysis of Skin Type")
|
| 32 |
+
st.write("This page contains Exploratory Data Analysis of Skin Type based on the previous model.")
|
| 33 |
+
st.write("---")
|
| 34 |
+
|
| 35 |
+
st.image('https://i.ytimg.com/vi/vic-EMOivpA/maxresdefault.jpg', caption='What is your skin type?', use_container_width=True)
|
| 36 |
+
|
| 37 |
+
# Set dataset path
|
| 38 |
+
main_path = "path/to/your/dataset"
|
| 39 |
+
train_path = os.path.join(main_path, 'train')
|
| 40 |
+
|
| 41 |
+
# Validasi path dataset
|
| 42 |
+
if not validate_path(train_path):
|
| 43 |
+
return
|
| 44 |
+
|
| 45 |
+
# Ambil daftar kelas dari folder train
|
| 46 |
+
classes = [d for d in os.listdir(train_path) if os.path.isdir(os.path.join(train_path, d))]
|
| 47 |
+
st.write("## Classes in Dataset:", classes)
|
| 48 |
+
|
| 49 |
+
# EDA 1: Histogram Warna
|
| 50 |
+
st.write("### EDA 1: Color Histogram Analysis")
|
| 51 |
+
for skin_type in classes:
|
| 52 |
+
path = os.path.join(train_path, skin_type)
|
| 53 |
+
hist_r, hist_g, hist_b = np.zeros(256), np.zeros(256), np.zeros(256)
|
| 54 |
+
for img_name in os.listdir(path):
|
| 55 |
+
img_path = os.path.join(path, img_name)
|
| 56 |
+
img = cv2.imread(img_path)
|
| 57 |
+
if img is not None:
|
| 58 |
+
r, g, b = calculate_color_histogram(img)
|
| 59 |
+
hist_r += r
|
| 60 |
+
hist_g += g
|
| 61 |
+
hist_b += b
|
| 62 |
+
|
| 63 |
+
# Plot histogram warna
|
| 64 |
+
plt.figure(figsize=(10, 5))
|
| 65 |
+
plt.plot(hist_r, color='r', label='Red')
|
| 66 |
+
plt.plot(hist_g, color='g', label='Green')
|
| 67 |
+
plt.plot(hist_b, color='b', label='Blue')
|
| 68 |
+
plt.title(f"Color Histogram for {skin_type}")
|
| 69 |
+
plt.legend()
|
| 70 |
+
st.pyplot(plt)
|
| 71 |
+
plt.clf()
|
| 72 |
+
|
| 73 |
+
st.write("Insight: No significant color difference was found across skin types.")
|
| 74 |
+
|
| 75 |
+
# EDA 2: Texture Analysis (LBP)
|
| 76 |
+
st.write("### EDA 2: Texture Analysis (LBP)")
|
| 77 |
+
radius = 3
|
| 78 |
+
n_points = 8 * radius
|
| 79 |
+
|
| 80 |
+
for skin_type in classes:
|
| 81 |
+
path = os.path.join(train_path, skin_type)
|
| 82 |
+
lbp_histograms = []
|
| 83 |
+
for img_name in os.listdir(path):
|
| 84 |
+
img_path = os.path.join(path, img_name)
|
| 85 |
+
img = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)
|
| 86 |
+
if img is not None:
|
| 87 |
+
hist = calculate_lbp(img, radius, n_points)
|
| 88 |
+
lbp_histograms.append(hist)
|
| 89 |
+
|
| 90 |
+
# Plot rata-rata histogram LBP
|
| 91 |
+
avg_hist = np.mean(lbp_histograms, axis=0)
|
| 92 |
+
plt.bar(range(len(avg_hist)), avg_hist)
|
| 93 |
+
plt.title(f"LBP Histogram for {skin_type}")
|
| 94 |
+
plt.xlabel("LBP Value")
|
| 95 |
+
plt.ylabel("Frequency")
|
| 96 |
+
st.pyplot(plt)
|
| 97 |
+
plt.clf()
|
| 98 |
+
|
| 99 |
+
st.write("Insight: Texture differences are not significant due to unstandardized data.")
|
| 100 |
+
|
| 101 |
+
# EDA 3: Spot/Pores Analysis
|
| 102 |
+
st.write("### EDA 3: Spot and Pores Analysis")
|
| 103 |
+
for skin_type in classes:
|
| 104 |
+
path = os.path.join(train_path, skin_type)
|
| 105 |
+
spot_counts = []
|
| 106 |
+
for img_name in os.listdir(path):
|
| 107 |
+
img_path = os.path.join(path, img_name)
|
| 108 |
+
img = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)
|
| 109 |
+
if img is not None:
|
| 110 |
+
_, thresh = cv2.threshold(img, 50, 255, cv2.THRESH_BINARY_INV)
|
| 111 |
+
contours, _ = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
| 112 |
+
spot_counts.append(len(contours))
|
| 113 |
+
|
| 114 |
+
# Plot distribusi jumlah spot
|
| 115 |
+
plt.hist(spot_counts, bins=20, alpha=0.7, label=f"{skin_type}")
|
| 116 |
+
plt.title(f"Spot Distribution for {skin_type}")
|
| 117 |
+
plt.xlabel("Number of Spots")
|
| 118 |
+
plt.ylabel("Frequency")
|
| 119 |
+
st.pyplot(plt)
|
| 120 |
+
plt.clf()
|
| 121 |
+
|
| 122 |
+
st.write("Insight: Pores and spot distribution varies across skin types.")
|
| 123 |
+
|
| 124 |
+
if __name__ == "__main__":
|
| 125 |
+
run()
|