Spaces:
Sleeping
Sleeping
import streamlit as st | |
import numpy as np | |
import matplotlib.pyplot as plt | |
from PIL import Image | |
import io | |
st.set_page_config(page_title="MNIST Pixel Visualizer", layout="wide") | |
st.title("๐งฎ MNIST ํฝ์ ๊ฐ ์๊ฐํ ๋ฐ๋ชจ") | |
st.markdown(""" | |
์ฌ์ฉ์๊ฐ ์ ๋ก๋ํ ํ๋ฐฑ ์ด๋ฏธ์ง๋ฅผ ํฝ์ ๊ฐ์ผ๋ก ์๊ฐํํฉ๋๋ค. | |
- ํฝ์ ๊ฐ์ 0~255 ๋ฒ์์ด๋ฉฐ, ๋ฐ๊ธฐ์ ๋ฐ๋ผ ํฐ์(255), ๊ฒ์์(0)์ผ๋ก ํ์๋ฉ๋๋ค. | |
- ํฝ์ ๊ฐ๋ ์ด๋ฏธ์ง ์์ ์ซ์๋ก ํ์๋ฉ๋๋ค. | |
์์ MNIST ์ด๋ฏธ์ง๋ ํจ๊ป ์ ๊ณตํฉ๋๋ค. | |
""") | |
# ์์ ์ด๋ฏธ์ง ๋ก๋ (MNIST) | |
from tensorflow.keras.datasets import mnist | |
(x_train, y_train), _ = mnist.load_data() | |
example_index = st.selectbox("์์ ์ด๋ฏธ์ง ์ ํ (MNIST)", list(range(10)), format_func=lambda x: f"Label: {y_train[x]}") | |
example_image = x_train[example_index] | |
col1, col2 = st.columns(2) | |
with col1: | |
st.subheader("1. ์ด๋ฏธ์ง ์ ๋ก๋ or ์์ ์ด๋ฏธ์ง ์ฌ์ฉ") | |
uploaded_file = st.file_uploader("ํ๋ฐฑ ์ด๋ฏธ์ง ํ์ผ ์ ๋ก๋ (28x28 ๋๋ ๋ ํฐ ์ด๋ฏธ์ง)", type=["png", "jpg", "jpeg"]) | |
if uploaded_file is not None: | |
image = Image.open(uploaded_file).convert('L') # ํ๋ฐฑ์ผ๋ก ๋ณํ | |
image = np.array(image) | |
st.image(image, caption="์ ๋ก๋๋ ์ด๋ฏธ์ง", use_container_width=True) | |
else: | |
image = example_image | |
st.image(image, caption=f"์์ ์ด๋ฏธ์ง (Label: {y_train[example_index]})", use_container_width=True) | |
with col2: | |
st.subheader("2. ํฝ์ ๊ฐ ์๊ฐํ ๊ฒฐ๊ณผ") | |
fig, ax = plt.subplots(figsize=(6, 6)) | |
ax.imshow(image, cmap='gray', vmin=0, vmax=255) | |
ax.set_xticks([]) | |
ax.set_yticks([]) | |
h, w = image.shape | |
for i in range(h): | |
for j in range(w): | |
val = image[i, j] | |
color = 'white' if val < 128 else 'black' | |
ax.text(j, i, str(val), ha='center', va='center', fontsize=6, color=color) | |
st.pyplot(fig) | |
st.markdown(""" | |
--- | |
๐จโ๐ป ๋ง๋ ์ฌ๋: ์ ์ํ | Upstage | AI Edu | |
""") | |