Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,32 +4,40 @@ import cv2
|
|
| 4 |
from scipy.stats import entropy
|
| 5 |
from PIL import Image
|
| 6 |
|
| 7 |
-
# Function to check
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
def check_randomness(img_path):
|
| 9 |
-
"""
|
| 10 |
|
| 11 |
-
# Load image from path
|
| 12 |
img = Image.open(img_path).convert("RGB")
|
| 13 |
gray_img = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2GRAY)
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
|
| 17 |
-
hist = hist / hist.sum()
|
| 18 |
-
img_entropy = entropy(hist.flatten())
|
| 19 |
|
| 20 |
-
# Compute noise level
|
| 21 |
-
|
| 22 |
-
noise_level = np.mean(np.abs(fft_transform))
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
|
| 26 |
-
edge_variability = np.mean(edges)
|
| 27 |
|
| 28 |
-
#
|
| 29 |
-
randomness_score =
|
| 30 |
-
randomness_level = "High" if randomness_score > 5.5 else "Low"
|
| 31 |
|
| 32 |
-
return f"
|
| 33 |
|
| 34 |
# Gradio Interface with File Upload
|
| 35 |
file_input = gr.File(label="Upload an Image", type="filepath", interactive=True)
|
|
|
|
| 4 |
from scipy.stats import entropy
|
| 5 |
from PIL import Image
|
| 6 |
|
| 7 |
+
# Function to check structured vs. random textures
|
| 8 |
+
def structured_texture_check(img):
|
| 9 |
+
"""Distinguish structured textures (fruits, objects) from random noise."""
|
| 10 |
+
blur_score = cv2.Laplacian(img, cv2.CV_64F).var() # Blur detection
|
| 11 |
+
return "Structured" if blur_score < 300 else "Random"
|
| 12 |
+
|
| 13 |
+
# Function to adjust noise filtering using FFT
|
| 14 |
+
def adjusted_noise_level(img):
|
| 15 |
+
"""Filter out structured details from randomness calculation."""
|
| 16 |
+
fft_transform = np.fft.fft2(img)
|
| 17 |
+
fft_filtered = np.where(np.abs(fft_transform) < 500, 0, fft_transform) # Removes weak signals
|
| 18 |
+
return np.mean(np.abs(fft_filtered))
|
| 19 |
+
|
| 20 |
+
# Main function to check randomness of an uploaded image
|
| 21 |
def check_randomness(img_path):
|
| 22 |
+
"""Final randomness detection with improved filtering."""
|
| 23 |
|
|
|
|
| 24 |
img = Image.open(img_path).convert("RGB")
|
| 25 |
gray_img = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2GRAY)
|
| 26 |
|
| 27 |
+
# Compute entropy
|
| 28 |
+
entropy_score = entropy(cv2.calcHist([gray_img], [0], None, [256], [0, 256]).flatten())
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
# Compute adjusted noise level
|
| 31 |
+
noise_level = adjusted_noise_level(gray_img)
|
|
|
|
| 32 |
|
| 33 |
+
# Determine structured vs. random texture
|
| 34 |
+
texture_type = structured_texture_check(gray_img)
|
|
|
|
| 35 |
|
| 36 |
+
# Final randomness classification
|
| 37 |
+
randomness_score = entropy_score + (noise_level / 5000)
|
| 38 |
+
randomness_level = "High" if randomness_score > 5.5 and texture_type == "Random" else "Low"
|
| 39 |
|
| 40 |
+
return f"Entropy: {entropy_score:.2f}, Noise: {noise_level:.2f}, Texture: {texture_type} → Randomness Level: {randomness_level}"
|
| 41 |
|
| 42 |
# Gradio Interface with File Upload
|
| 43 |
file_input = gr.File(label="Upload an Image", type="filepath", interactive=True)
|