Solution is very simple. If the thermal camera is stable then the mean of the pixel values may wander around a constant value. But when another element like gas appears the thermal image gets dark which makes the mean value of the pixel shift to another value which is significantly away from the previous value. So ayes theoram is used where initially a normal thermal image and a gas leaked thermal image is eing fed to the algo where it considers the mean values of those two as the reference.
import numpy as np
import cv2
print("Here please add the path of a previously saved image of how the view you are monitoring normally looks")
normal_img_path = input('Normal Image Path - ') #Here please add the path of a previously saved image of how the view you are monitoring normally looks
normal_img = cv2.imread(normal_img_path)
normal_img_mean = np.mean(normal_img)
normal_img_std = np.std(normal_img)
snd_normal = stats.norm(normal_img_mean, normal_img_std)
print("Here please add the path of a previously saved image of how the view you are monitoring filled with gas")
gas_img_path = input('Gas image path - ') #Here please add the path of a previously saved image of how the view you are monitoring filled with gas
gas_img = cv2.imread(gas_img_path)
gas_img_mean = np.mean(gas_img)
gas_img_std = np.std(gas_img)
snd_gas = stats.norm(gas_img_mean,gas_img_std)
x = np.linspace(normal_img_mean, gas_img_mean, 1000)
min_val =np.argmin(np.abs( snd_normal.pdf(x) - snd_gas.pdf(x) ))
boundary = x[min_val]
Then from the loop function we check the input images mean value coming from the thermal camera and compare it with above measured reference values.
#inside_loop
#input_from_sensor = get the sensor input here
sensor_mean = np.mean(input_from_sensor)
if(abs(sensor_mean-normal_img_mean)>abs(sensor_mean-gas_img_mean)):
print("Gas leak")
else:
print("No gas leak")