File size: 3,340 Bytes
4feb29a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ad52348
 
 
 
 
 
 
 
 
 
4feb29a
 
 
 
 
 
ad52348
 
4feb29a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ad52348
 
 
 
 
 
 
 
 
4feb29a
 
 
 
 
895543c
4feb29a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
daa097c
 
 
 
 
 
 
 
 
 
 
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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: #5c5a5a;">Click to Read Model's Documentation</button></a>
''',
unsafe_allow_html=True)