File size: 2,358 Bytes
fe4f2fe
 
574ce44
 
fe4f2fe
78ae2d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90a5113
78ae2d1
90a5113
 
78ae2d1
574ce44
78ae2d1
 
 
574ce44
78ae2d1
 
574ce44
78ae2d1
574ce44
90a5113
 
 
 
574ce44
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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()