Spaces:
Runtime error
Runtime error
Commit
·
79621bd
1
Parent(s):
3b17146
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import time
|
| 3 |
+
from PIL import Image, ImageOps
|
| 4 |
+
import numpy as np
|
| 5 |
+
import streamlit as st
|
| 6 |
+
import tensorflow as tf
|
| 7 |
+
|
| 8 |
+
@st.cache(allow_output_mutation=True)
|
| 9 |
+
def load_model():
|
| 10 |
+
model=tf.keras.models.load_model('MN21.h5')
|
| 11 |
+
return model
|
| 12 |
+
with st.spinner('Model is being loaded..'):
|
| 13 |
+
model=load_model()
|
| 14 |
+
|
| 15 |
+
st.write("""# SaferNet with AI""")
|
| 16 |
+
|
| 17 |
+
file = st.file_uploader("Please upload an image to classify", type=["jpg", "png", "jpeg"])
|
| 18 |
+
def import_and_predict(image_data, model):
|
| 19 |
+
size = (224,224)
|
| 20 |
+
image = ImageOps.fit(image_data, size, Image.ANTIALIAS)
|
| 21 |
+
image = np.asarray(image)
|
| 22 |
+
img = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 23 |
+
#img_resize = (cv2.resize(img, dsize=(75, 75), interpolation=cv2.INTER_CUBIC))/255.
|
| 24 |
+
|
| 25 |
+
img_reshape = img[np.newaxis,...]
|
| 26 |
+
|
| 27 |
+
st.write(img_reshape.shape)
|
| 28 |
+
|
| 29 |
+
start = time.time()
|
| 30 |
+
prediction = model.predict(img_reshape)
|
| 31 |
+
end = time.time()
|
| 32 |
+
time_take = end - start
|
| 33 |
+
return prediction, time_take
|
| 34 |
+
|
| 35 |
+
if file is None:
|
| 36 |
+
st.text("Please upload an image file")
|
| 37 |
+
else:
|
| 38 |
+
image = Image.open(file)
|
| 39 |
+
st.image(image, use_column_width=True)
|
| 40 |
+
predictions, time_take = import_and_predict(image, model)
|
| 41 |
+
st.write("Time taken to predict is ", time_take, "second")
|
| 42 |
+
st.write(predictions)
|
| 43 |
+
print(predictions)
|
| 44 |
+
|
| 45 |
+
print(np.argmax(predictions))
|
| 46 |
+
st.write(np.argmax(predictions))
|
| 47 |
+
|
| 48 |
+
if predictions[0][0] < 0.5:
|
| 49 |
+
st.write("This is SFW image :sunglasses:")
|
| 50 |
+
else:
|
| 51 |
+
st.write("This is NSFW image")
|