Spaces:
Runtime error
Runtime error
Commit ·
4feb29a
1
Parent(s): 27d6a8d
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import warnings
|
| 2 |
+
warnings.filterwarnings("ignore", category=UserWarning)
|
| 3 |
+
import streamlit as st
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import tempfile
|
| 8 |
+
from tempfile import NamedTemporaryFile
|
| 9 |
+
from io import BytesIO
|
| 10 |
+
|
| 11 |
+
import pickle
|
| 12 |
+
import cv2
|
| 13 |
+
import numpy as np
|
| 14 |
+
from sklearn.ensemble import RandomForestClassifier
|
| 15 |
+
|
| 16 |
+
st.title("Image Bluriness Prediction")
|
| 17 |
+
|
| 18 |
+
# Load the saved random forest classifier model
|
| 19 |
+
with open('image_blur_model.pkl', 'rb') as f:
|
| 20 |
+
clf = pickle.load(f)
|
| 21 |
+
|
| 22 |
+
# For sample images as a sidebar
|
| 23 |
+
images = ["test2.jpg","test1.jpg","test3.jpg","test4.jpg","test5.jpg","test6.jpg","download1.jpg","download2.jpg","sample1.jpg","download3.jpg","download4.jpg","download.png","img1.jpg","img17.jpg"]
|
| 24 |
+
with st.sidebar:
|
| 25 |
+
st.write("Choose an image")
|
| 26 |
+
st.image(images)
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
# Function to predict bluriness
|
| 30 |
+
def predict_bluriness(image):
|
| 31 |
+
# Convert the image to grayscale and compute the VoL metric
|
| 32 |
+
gray = cv2.cvtColor(np.array(image), cv2.COLOR_BGR2GRAY)
|
| 33 |
+
vol = cv2.Laplacian(gray, cv2.CV_64F).var()
|
| 34 |
+
|
| 35 |
+
# Make a prediction using the loaded model
|
| 36 |
+
prediction = clf.predict([[vol]])
|
| 37 |
+
|
| 38 |
+
# Return the prediction result and VoL value
|
| 39 |
+
return prediction, vol
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
# # CSS code for changing color of the button
|
| 43 |
+
# st.markdown("""
|
| 44 |
+
# <style>
|
| 45 |
+
# .stButton button {
|
| 46 |
+
# background-color: #668f45;
|
| 47 |
+
# color: white;
|
| 48 |
+
# }
|
| 49 |
+
# </style>
|
| 50 |
+
# """, unsafe_allow_html=True)
|
| 51 |
+
|
| 52 |
+
# File uploader
|
| 53 |
+
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
| 54 |
+
|
| 55 |
+
# Predict button
|
| 56 |
+
if st.button("Predict"):
|
| 57 |
+
image = None
|
| 58 |
+
|
| 59 |
+
# Read the uploaded image if available
|
| 60 |
+
if uploaded_file is not None:
|
| 61 |
+
image = Image.open(uploaded_file)
|
| 62 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 63 |
+
|
| 64 |
+
# Perform prediction if image is available
|
| 65 |
+
if image is not None:
|
| 66 |
+
# Perform prediction
|
| 67 |
+
prediction, vol = predict_bluriness(image)
|
| 68 |
+
|
| 69 |
+
# Display prediction result and VoL value
|
| 70 |
+
st.write("**Prediction:**", "The image is not blurry." if prediction == 1 else "The image is blurry.")
|
| 71 |
+
st.write("**Variance of Laplacian Score:**", vol)
|