Priyanka-Kumavat-At-TE's picture
Update app.py
373e585
raw
history blame
3.34 kB
import warnings
warnings.filterwarnings("ignore", category=UserWarning)
import streamlit as st
import os
from PIL import Image
import tempfile
from tempfile import NamedTemporaryFile
from io import BytesIO
import pickle
import cv2
import numpy as np
from sklearn.ensemble import RandomForestClassifier
st.title("Image Blur Prediction System")
st.write("""Image Bluriness Prediction Model allows users to analyze the bluriness of images.
It utilizes a pre-trained random forest classifier model to predict whether an image is blurry or not.
The application provides two options for image selection:
users can either upload their own image or choose from a set of sample images.
Once an image is selected, the application calculates the Variance of Laplacian (VoL) score,
a metric used to measure image bluriness. The classifier model then predicts whether the image is blurry or not based
on the VoL score. The prediction result and the VoL score are displayed to the user.
The application also includes a sidebar that showcases sample images for quick testing.""")
# Load the saved random forest classifier model
with open('image_blur_model.pkl', 'rb') as f:
clf = pickle.load(f)
# For sample images as a sidebar
images = ["test2.jpg","test1.jpg","test4.jpg","test5.jpg","test6.jpg","download1.jpg","download2.jpg","sample1.jpg",
"download3.jpg","download4.jpg","download.png","img1.jpg","img17.jpg"]
with st.sidebar:
st.write("Choose an image")
st.image(images)
# Function to predict bluriness
def predict_bluriness(image):
# Convert the image to grayscale and compute the VoL metric
gray = cv2.cvtColor(np.array(image), cv2.COLOR_BGR2GRAY)
vol = cv2.Laplacian(gray, cv2.CV_64F).var()
# Make a prediction using the loaded model
prediction = clf.predict([[vol]])
# Return the prediction result and VoL value
return prediction, vol
# CSS code for changing color of the button
st.markdown("""
<style>
.stButton button {
background-color: #668f45;
color: white;
}
</style>
""", unsafe_allow_html=True)
# File uploader
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
# Predict button
if st.button("Click to Predict"):
image = None
# Read the uploaded image if available
if uploaded_file is not None:
image = Image.open(uploaded_file)
st.image(image, caption="Uploaded Image", use_column_width=True)
# Perform prediction if image is available
if image is not None:
# Perform prediction
prediction, vol = predict_bluriness(image)
# Display prediction result and VoL value
st.write("**Prediction:**", "The image is not blurry." if prediction == 1 else "The image is blurry.")
st.write("**Variance of Laplacian Score:**", vol)
st.write("""
**For the detailed Description of the Model, Project and Technologies used, please go through our Documentation**
""")
url = 'https://huggingface.co/spaces/ThirdEyeData/Image-Blur-Prediction/blob/main/Documentation.md'
st.markdown(f'''
<a href={url}><button style="background-color: #668f45;">Click to Read Model's Documentation</button></a>
''',
unsafe_allow_html=True)