Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import numpy as np
|
| 3 |
+
import pandas as pd
|
| 4 |
+
from sklearn.datasets import fetch_openml
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
import matplotlib as mpl
|
| 7 |
+
import scipy.ndimage.interpolation as ndi_int
|
| 8 |
+
|
| 9 |
+
# Streamlit App title
|
| 10 |
+
st.title("Image Shifting with MNIST Dataset")
|
| 11 |
+
|
| 12 |
+
# Load the MNIST dataset
|
| 13 |
+
st.write("Loading the MNIST dataset...")
|
| 14 |
+
mnist = fetch_openml('mnist_784', version=1)
|
| 15 |
+
|
| 16 |
+
# Separate the data and target variables
|
| 17 |
+
X, y = mnist["data"], mnist["target"].astype(int)
|
| 18 |
+
|
| 19 |
+
# Select a sample digit to display
|
| 20 |
+
st.sidebar.write("## Shift the Image")
|
| 21 |
+
digit_index = st.sidebar.slider("Select a digit index", 0, X.shape[0] - 1, 0)
|
| 22 |
+
digit = X.iloc[digit_index, :].values
|
| 23 |
+
digit_img = digit.reshape(28, 28)
|
| 24 |
+
|
| 25 |
+
# Display original digit image
|
| 26 |
+
st.write("### Original Image")
|
| 27 |
+
fig, ax = plt.subplots()
|
| 28 |
+
ax.imshow(digit_img, cmap=mpl.cm.binary)
|
| 29 |
+
ax.axis("off")
|
| 30 |
+
st.pyplot(fig)
|
| 31 |
+
|
| 32 |
+
# Function to shift the image
|
| 33 |
+
def shift_image(digit_img, x_shift, y_shift):
|
| 34 |
+
return ndi_int.shift(digit_img, [x_shift, y_shift])
|
| 35 |
+
|
| 36 |
+
# Input for shifting the image
|
| 37 |
+
x_shift = st.sidebar.slider("Shift in X (horizontal)", -10, 10, 0)
|
| 38 |
+
y_shift = st.sidebar.slider("Shift in Y (vertical)", -10, 10, 0)
|
| 39 |
+
|
| 40 |
+
# Shift the image based on user input
|
| 41 |
+
shifted_image = shift_image(digit_img, x_shift, y_shift)
|
| 42 |
+
|
| 43 |
+
# Display shifted image
|
| 44 |
+
st.write("### Shifted Image")
|
| 45 |
+
fig_shifted, ax_shifted = plt.subplots()
|
| 46 |
+
ax_shifted.imshow(shifted_image, cmap=mpl.cm.binary)
|
| 47 |
+
ax_shifted.axis("off")
|
| 48 |
+
st.pyplot(fig_shifted)
|