Spaces:
Build error
Build error
Download app.py from ShubhangiAg/random_ocr: direct link, hf CLI and curl.
- Browser
- Download file 2.36 kB
-
https://huggingface.co/spaces/ShubhangiAg/random_ocr/resolve/main/app.py
- Command line
-
hf download hf://spaces/ShubhangiAg/random_ocr/app.py
-
curl -L -o app.py https://huggingface.co/spaces/ShubhangiAg/random_ocr/resolve/main/app.py
2.36 kB
| import gradio as gr | |
| import numpy as np | |
| import cv2 | |
| from scipy.stats import entropy | |
| from PIL import Image | |
| from skimage.feature import greycomatrix, greycoprops | |
| from skimage.filters import gabor | |
| # Function to compute Haralick texture features (better for structured textures) | |
| def texture_analysis(img): | |
| """Distinguishes structured vs. random textures using Haralick features.""" | |
| gray_img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) | |
| glcm = greycomatrix(gray_img, distances=[5], angles=[0], levels=256, symmetric=True, normed=True) | |
| contrast = greycoprops(glcm, 'contrast')[0, 0] | |
| return "Structured" if contrast < 50 else "Random" | |
| # Function to apply Gabor filter for randomness detection | |
| def gabor_filter_analysis(img): | |
| """Detects randomness using Gabor texture patterns.""" | |
| gray_img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) | |
| gabor_response, _ = gabor(gray_img, frequency=0.6) | |
| return np.mean(gabor_response) | |
| # Function to check color distribution uniformity | |
| def color_distribution(img): | |
| """Measures color randomness by checking histogram uniformity.""" | |
| hist_r = cv2.calcHist([img], [0], None, [256], [0, 256]) | |
| hist_g = cv2.calcHist([img], [1], None, [256], [0, 256]) | |
| hist_b = cv2.calcHist([img], [2], None, [256], [0, 256]) | |
| color_std = np.std([np.std(hist_r), np.std(hist_g), np.std(hist_b)]) | |
| return "Structured" if color_std < 50 else "Random" | |
| # Main function to analyze randomness of an image | |
| def check_randomness(img_path): | |
| """Final randomness detection with improved texture and color analysis.""" | |
| img = Image.open(img_path).convert("RGB") | |
| img_np = np.array(img) | |
| texture_type = texture_analysis(img_np) | |
| gabor_variability = gabor_filter_analysis(img_np) | |
| color_randomness = color_distribution(img_np) | |
| randomness_score = gabor_variability * (1.5 if texture_type == "Random" else 0.7) | |
| randomness_level = "High" if randomness_score > 5.0 and color_randomness == "Random" else "Low" | |
| return f"Texture: {texture_type}, Gabor Variability: {gabor_variability:.2f}, Color Distribution: {color_randomness} → Randomness Level: {randomness_level}" | |
| # Gradio Interface with File Upload | |
| file_input = gr.File(label="Upload an Image", type="filepath", interactive=True) | |
| iface = gr.Interface(fn=check_randomness, inputs=file_input, outputs="text", live=True) | |
| iface.launch() |