Spaces:
Running
Running
import os | |
import cv2 | |
import gradio as gr | |
import numpy as np | |
from imgutils.data import load_image | |
def variance_of_laplacian(np_image): | |
""" | |
Inspired by https://pyimagesearch.com/2015/09/07/blur-detection-with-opencv/ | |
""" | |
return cv2.Laplacian(np_image, cv2.CV_64F).var() | |
def laplacian_score(image): | |
v = np.array(load_image(image, force_background='white', mode='L')) | |
return variance_of_laplacian(v) | |
def _fn(image, threshold: float): | |
v = laplacian_score(image) | |
text = 'Is Blur' if v < threshold else 'Not Blur' | |
return v, text | |
if __name__ == '__main__': | |
with gr.Blocks() as demo: | |
with gr.Row(): | |
with gr.Column(): | |
gr_input_image = gr.Image(type='pil', label='Original Image') | |
gr_threshold = gr.Slider(70, maximum=500, value=100, label='Threshold') | |
gr_submit = gr.Button(value='Submit', variant='primary') | |
with gr.Column(): | |
gr_score = gr.Text(label='Laplacian Score', value='') | |
gr_pred = gr.Text(label='Prediction', value='') | |
gr_submit.click( | |
_fn, | |
inputs=[gr_input_image, gr_threshold], | |
outputs=[gr_score, gr_pred], | |
) | |
demo.queue(os.cpu_count()).launch() | |