Download app.py from FraudDetection/Document_Blur_Detection: direct link, hf CLI and curl.
- Browser
- Download file 1.25 kB
-
https://huggingface.co/spaces/FraudDetection/Document_Blur_Detection/resolve/main/app.py
- Command line
-
hf download hf://spaces/FraudDetection/Document_Blur_Detection/app.py
-
curl -L -o app.py https://huggingface.co/spaces/FraudDetection/Document_Blur_Detection/resolve/main/app.py
1.25 kB
| import gradio as gr | |
| import cv2 | |
| import numpy as np | |
| from PIL import Image | |
| def blur_score_from_pil(pil_img: Image.Image) -> float: | |
| # Convert PIL -> OpenCV grayscale | |
| img = np.array(pil_img.convert("RGB")) | |
| gray = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) | |
| return float(cv2.Laplacian(gray, cv2.CV_64F).var()) | |
| def check_blur(image: Image.Image, blur_threshold: float = 100.0): | |
| if image is None: | |
| return None, "No image provided." | |
| score = blur_score_from_pil(image) | |
| if score < blur_threshold: | |
| return score, "🚫 Blurry image detected. Please re-upload a clearer image." | |
| else: | |
| return score, "✅ Image looks clear. Proceeding to next step." | |
| demo = gr.Interface( | |
| fn=check_blur, | |
| inputs=[ | |
| gr.Image(type="pil", label="Upload Image"), | |
| gr.Slider(0, 500, value=100, step=1, label="Blur Threshold (Laplacian Variance)"), | |
| ], | |
| outputs=[ | |
| gr.Number(label="Blur Score (Laplacian Variance)"), | |
| gr.Textbox(label="Result"), | |
| ], | |
| title="Image Blur Checker", | |
| description="Checks image blur using variance of the Laplacian. Lower score = blurrier image.", | |
| ) | |
| demo.launch(server_name="0.0.0.0", server_port=7860, show_error=True) |