ShubhangiAg commited on
Commit
cbcb8cb
·
verified ·
1 Parent(s): 90a5113

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -17
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 randomness of an uploaded image
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  def check_randomness(img_path):
9
- """Analyzes image randomness based on entropy, noise, and edge variation."""
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
- # Calculate histogram & entropy
16
- hist = cv2.calcHist([gray_img], [0], None, [256], [0, 256])
17
- hist = hist / hist.sum()
18
- img_entropy = entropy(hist.flatten())
19
 
20
- # Compute noise level using FFT
21
- fft_transform = np.fft.fft2(gray_img)
22
- noise_level = np.mean(np.abs(fft_transform))
23
 
24
- # Edge detection for randomness
25
- edges = cv2.Canny(gray_img, 100, 200)
26
- edge_variability = np.mean(edges)
27
 
28
- # Define randomness threshold
29
- randomness_score = img_entropy + (noise_level / 5000) + (edge_variability / 255)
30
- randomness_level = "High" if randomness_score > 5.5 else "Low"
31
 
32
- return f"Image Entropy: {img_entropy:.2f}, Noise Level: {noise_level:.2f}, Edge Variability: {edge_variability:.2f} → Randomness Level: {randomness_level}"
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)