<jupyter_start><jupyter_text>Remote Sensing Passive vs. ActiveTo have your deep learning models perform properly in noisy environments as an essential step it is critical to understand what is the noise grade in the target environment. This will allow your system to decide how to behave according to the existing noise.In this notebook I describe how to grade the effects in the environment using cameras (as passive sensors). However, the technique is applicable to activate sensor data as well.  Next, I will show how to use this information to manage the deep learning models on a device in real-time more effectively to keep the object detection/classification accuracy as high as possible.In this notebook you will see the following:      Use numpy built-in FFT to transform images to frequency domain.   Use numpy built-in fftShift to shift the low/high frequency components.   use numpy built-in inverse FFT to reconstruct the shifted images.  Observe the difference and grade the noise in the environment accordingly.<jupyter_code>import matplotlib.pyplot as plt
import numpy as np
import numpy as np
import argparse
import imutils
import cv2

# use the following test images to understand the process better  
#orig = cv2.imread('./img/original/0001.jpg')
orig = cv2.imread('./img/augmented/0009.jpg')
image = cv2.cvtColor(orig, cv2.COLOR_BGR2GRAY)

# display the original input image
(fig, ax) = plt.subplots(1, 1, )
ax.imshow(image, cmap="gray")
ax.set_title("Input")
ax.set_xticks([])
ax.set_yticks([])
plt.show()    
(h, w) = image.shape
(cX, cY) = (int(w / 2.0), int(h / 2.0))

# Using numpy built-in FFT to transform the image to frequency domain
# and use fftshift to shift zero frequency components to the center  
fft = np.fft.fft2(image)
fftShift = np.fft.fftshift(fft)   

magnitude = 20 * np.log(np.abs(fftShift))
mean = np.mean(magnitude)
print("mean: {}".format(mean))

(fig, ax) = plt.subplots(1, 1, )
ax.imshow(magnitude, cmap="gray")
ax.set_title("Magnitude Spectrum")
ax.set_xticks([])
ax.set_yticks([])
plt.show()    
# Reconstruct the transformed image
size = 60
thresh=0
# zero out the center to remove lower frequencies
# next use inverse fft to reconstruct the image
fftShift[cY - size:cY + size, cX - size:cX + size] = 0
fftShift = np.fft.ifftshift(fftShift)
recon = np.fft.ifft2(fftShift)

magnitude = 20 * np.log(np.abs(recon))
mean = np.mean(magnitude)
print("mean: {}".format(mean))


(fig, ax) = plt.subplots(1, 1, )
ax.imshow(magnitude, cmap="gray")
ax.set_title("reonstructed")
ax.set_xticks([])
ax.set_yticks([])
plt.show()    
# annotate image based on w/ fog or w/o fog
foggy = mean <= thresh
image = np.dstack([gray] * 3)

fog_grade = {range(thresh - 10, thresh): '0',
             range(thresh - 20,thresh - 10): '1',
             range(thresh - 30, thresh - 20): '2',
             range(thresh - 100, thresh - 30): '3'
            }

color = (0, 0, 255) if foggy else (0, 255, 0)
            
            
if foggy:
    text = "w Fog (grade: {})"
    for key in fog_grade:
        if int(mean) in key:
            break
    text = text.format(fog_grade[key])
else:
    text = "w/o Fog ({:.4f})"
    text = text.format(mean)
    
    
cv2.putText(image, text, (10, 25), cv2.FONT_HERSHEY_SIMPLEX, 0.7,color, 2)
print("[INFO] {}".format(text))
# Plot the annotated output image 
(fig, ax) = plt.subplots(1, 1, )
ax.imshow(image, cmap="gray")
ax.set_title("Output")
ax.set_xticks([])
ax.set_yticks([])
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12051649928263988, 'fraction_numerical': 0.0218077474892396, 'mean_word_length': 3.8082758620689656, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '225414', 'license': 'MIT', 'n_tokens_mistral': 1243, 'n_tokens_neox': 1172, 'n_words': 401, 'path': 'Passive_Remote_Sensing.ipynb', 'repo_name': 'Mahdi89/image-quality-grading', 'size': '3485'}