|
import streamlit as st |
|
|
|
import matplotlib.pyplot as plt |
|
import numpy as np |
|
import tensorflow as tf |
|
from tensorflow import keras |
|
from tensorflow.keras import layers |
|
from tensorflow.python.keras.layers import Dense, Flatten |
|
from tensorflow.keras.models import Sequential |
|
from tensorflow.keras.optimizers import Adam |
|
from sklearn.neighbors import NearestNeighbors |
|
|
|
|
|
|
|
import PIL |
|
import random |
|
|
|
|
|
n_classes = 5749 |
|
|
|
button = st.button("Choose Random Image from training data") |
|
|
|
feature_data = np.load('features.npy') |
|
x_data = np.load('pictures.npy') |
|
y_data = np.load('name_data.npy') |
|
|
|
knn = NearestNeighbors(n_neighbors=11) |
|
knn.fit(feature_data) |
|
|
|
model = Sequential() |
|
|
|
pretrained_model= tf.keras.applications.ResNet50(include_top=False, |
|
input_shape=(250,250,3), |
|
pooling='avg',classes=n_classes, |
|
weights='imagenet') |
|
for layer in pretrained_model.layers: |
|
layer.trainable=False |
|
|
|
model.add(pretrained_model) |
|
model.build([None, 250, 250, 3]) |
|
uploaded_files = st.file_uploader('Upload your files') |
|
|
|
if button: |
|
|
|
rand_idx = random.randint(0, 13232) |
|
res = knn.kneighbors([feature_data[rand_idx]], return_distance=False)[0] |
|
first = True |
|
for idx, i in enumerate(res): |
|
if first: |
|
first = False |
|
st.write("original image") |
|
st.image(x_data[i]) |
|
else: |
|
st.write(f"{idx}-th similar image") |
|
st.image(x_data[i]) |
|
|
|
elif uploaded_files: |
|
image = PIL.Image.open(uploaded_files) |
|
img_array = np.array(image) |
|
img_array = np.resize(img_array, (250, 250, 3)) |
|
img_array = img_array.reshape((1, 250, 250, 3)) |
|
im_features = model.predict(img_array) |
|
|
|
res = knn.kneighbors(im_features, return_distance=False)[0] |
|
first = True |
|
for idx, i in enumerate(res): |
|
if first: |
|
first = False |
|
st.write("original image") |
|
st.image(image) |
|
else: |
|
st.write(f"{idx}-th similar image") |
|
st.image(x_data[i]) |