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