File size: 1,452 Bytes
7f55a39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import numpy as np
import pandas as pd
from sklearn.datasets import fetch_openml
import matplotlib.pyplot as plt
import matplotlib as mpl
import scipy.ndimage.interpolation as ndi_int

# Streamlit App title
st.title("Image Shifting with MNIST Dataset")

# Load the MNIST dataset
st.write("Loading the MNIST dataset...")
mnist = fetch_openml('mnist_784', version=1)

# Separate the data and target variables
X, y = mnist["data"], mnist["target"].astype(int)

# Select a sample digit to display
st.sidebar.write("## Shift the Image")
digit_index = st.sidebar.slider("Select a digit index", 0, X.shape[0] - 1, 0)
digit = X.iloc[digit_index, :].values
digit_img = digit.reshape(28, 28)

# Display original digit image
st.write("### Original Image")
fig, ax = plt.subplots()
ax.imshow(digit_img, cmap=mpl.cm.binary)
ax.axis("off")
st.pyplot(fig)

# Function to shift the image
def shift_image(digit_img, x_shift, y_shift):
    return ndi_int.shift(digit_img, [x_shift, y_shift])

# Input for shifting the image
x_shift = st.sidebar.slider("Shift in X (horizontal)", -10, 10, 0)
y_shift = st.sidebar.slider("Shift in Y (vertical)", -10, 10, 0)

# Shift the image based on user input
shifted_image = shift_image(digit_img, x_shift, y_shift)

# Display shifted image
st.write("### Shifted Image")
fig_shifted, ax_shifted = plt.subplots()
ax_shifted.imshow(shifted_image, cmap=mpl.cm.binary)
ax_shifted.axis("off")
st.pyplot(fig_shifted)