Yescia's picture
Update app.py
749237c verified
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
""")