Plana-Archive commited on
Commit
de03e3c
·
verified ·
1 Parent(s): 2662d3d

Upload laplacian_blur/app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. laplacian_blur/app.py +45 -0
laplacian_blur/app.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ import cv2
4
+ import gradio as gr
5
+ import numpy as np
6
+ from imgutils.data import load_image
7
+
8
+
9
+ def variance_of_laplacian(np_image):
10
+ """
11
+ Inspired by https://pyimagesearch.com/2015/09/07/blur-detection-with-opencv/
12
+ """
13
+ return cv2.Laplacian(np_image, cv2.CV_64F).var()
14
+
15
+
16
+ def laplacian_score(image):
17
+ v = np.array(load_image(image, force_background='white', mode='L'))
18
+ return variance_of_laplacian(v)
19
+
20
+
21
+ def _fn(image, threshold: float):
22
+ v = laplacian_score(image)
23
+ text = 'Is Blur' if v < threshold else 'Not Blur'
24
+ return v, text
25
+
26
+
27
+ if __name__ == '__main__':
28
+ with gr.Blocks() as demo:
29
+ with gr.Row():
30
+ with gr.Column():
31
+ gr_input_image = gr.Image(type='pil', label='Original Image')
32
+ gr_threshold = gr.Slider(70, maximum=500, value=100, label='Threshold')
33
+ gr_submit = gr.Button(value='Submit', variant='primary')
34
+
35
+ with gr.Column():
36
+ gr_score = gr.Text(label='Laplacian Score', value='')
37
+ gr_pred = gr.Text(label='Prediction', value='')
38
+
39
+ gr_submit.click(
40
+ _fn,
41
+ inputs=[gr_input_image, gr_threshold],
42
+ outputs=[gr_score, gr_pred],
43
+ )
44
+
45
+ demo.queue(os.cpu_count()).launch()