File size: 2,148 Bytes
4feb29a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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 Bluriness Prediction")

# 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","test3.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("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)