-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
49 lines (40 loc) · 1.44 KB
/
app.py
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 pickle as pk
import numpy as np
import tensorflow as tf
import cv2 as cv
# Preprocessor and deeplearning model for feature extraction
preprocessor = tf.keras.applications.imagenet_utils.preprocess_input
vgg = tf.keras.applications.VGG16(include_top=False,weights='imagenet')
# Load Trained ML Model
clf = pk.load(open("vgg_clf.sav",'rb'))
st.title("""Cats-Dogs Classification
- Using Feature Extraction.`VGG16`""")
IMGS = st.file_uploader(label="Image To Predict", type=["png","jpg"],
accept_multiple_files=True,)
# Upload and Store then import image Tensors
data=[]
if IMGS:
for img in IMGS:
with open(img.name,'wb') as im:
im.write(img.read())
data.append(cv.imread(im.name))
IMG_SZ = 244
pred=st.button("Classify")
if pred:
# Resize Images
n_data = np.array([cv.resize((x),(IMG_SZ,IMG_SZ)) for x in data],dtype='uint8')
# Preprocess Images
pre_img = preprocessor(n_data)
# Extract Features and reshape for ML model
features = vgg.predict(pre_img)
sh = features.shape
features = features.reshape(-1,sh[1]*sh[2]*sh[-1])
# Classify Images
cla = clf.predict(features)
# 0,1 to Cat,Dog
tr_cla = lambda x:"Dog" if x==1 else "Cat"
# Fixing Colors for visualization
vis = [cv.cvtColor(x,cv.COLOR_BGR2RGB) for x in n_data]
# Display Images with class as caption
st.image(vis,caption=list(map(tr_cla,list(cla))))