|
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.""") |
|
|
|
|
|
with open('image_blur_model.pkl', 'rb') as f: |
|
clf = pickle.load(f) |
|
|
|
|
|
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) |
|
|
|
|
|
|
|
def predict_bluriness(image): |
|
|
|
gray = cv2.cvtColor(np.array(image), cv2.COLOR_BGR2GRAY) |
|
vol = cv2.Laplacian(gray, cv2.CV_64F).var() |
|
|
|
|
|
prediction = clf.predict([[vol]]) |
|
|
|
|
|
return prediction, vol |
|
|
|
|
|
|
|
st.markdown(""" |
|
<style> |
|
.stButton button { |
|
background-color: #668f45; |
|
color: white; |
|
} |
|
</style> |
|
""", unsafe_allow_html=True) |
|
|
|
|
|
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"]) |
|
|
|
|
|
if st.button("Click to Predict"): |
|
image = None |
|
|
|
|
|
if uploaded_file is not None: |
|
image = Image.open(uploaded_file) |
|
st.image(image, caption="Uploaded Image", use_column_width=True) |
|
|
|
|
|
if image is not None: |
|
|
|
prediction, vol = predict_bluriness(image) |
|
|
|
|
|
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) |
|
|