def run(self): print("MLXThread") colormap = [0] * self.COLORDEPTH for i in range(self.COLORDEPTH): colormap[i] = gradient(i, self.COLORDEPTH, self.heatmap) i2c = busio.I2C(board.SCL, board.SDA) mlx = adafruit_mlx90640.MLX90640(i2c) mlx.refresh_rate = adafruit_mlx90640.RefreshRate.REFRESH_8_HZ frame = [0] * 768 while True: # stamp = time.monotonic() try: mlx.getFrame(frame) except ValueError: print("ValueError") continue # these happen, no biggie - retry # print("Read 2 frames in %0.2f s" % (time.monotonic()-stamp)) pixels = [0] * 768 for i, pixel in enumerate(frame): coloridx = map_value(pixel, self.MINTEMP, self.MAXTEMP, 0, self.COLORDEPTH - 1) coloridx = int(constrain(coloridx, 0, self.COLORDEPTH - 1)) pixels[i] = colormap[coloridx] img = Image.new('RGB', (32, 24)) img.putdata(pixels) img = img.resize((640, 480), Image.BICUBIC) img = ImageQt(img) pixmap = QPixmap.fromImage(img) self.changeHeatmap.emit(pixmap)
def _setup_therm_cam(self): """Initialize the thermal camera""" # Setup camera self.i2c = busio.I2C(board.SCL, board.SDA, frequency=800000) # setup I2C self.mlx = adafruit_mlx90640.MLX90640( self.i2c) # begin MLX90640 with I2C comm self.mlx.refresh_rate = adafruit_mlx90640.RefreshRate.REFRESH_8_HZ # set refresh rate time.sleep(0.1)
def __init__(self): print('Creating thermal camera') self.ic2 = busio.I2C(board.SCL, board.SDA, frequency=400000) self.mlx = adafruit_mlx90640.MLX90640(self.ic2) self.mlx.refresh_rate = adafruit_mlx90640.RefreshRate.REFRESH_2_HZ self.msg = ("MLX addr detected on I2C", [hex(i) for i in self.mlx.serial_number]) self.frame = [0] * 768 self.centers = Settings.thermal_roi self.fov = Settings.thermal_fov self.regions = RegionInterest.Regions(width=32, height=24, fov=self.fov, centers=self.centers)
def takeTemp(): i2c = busio.I2C(board.SCL, board.SDA, frequency=400000) # setup I2C mlx = adafruit_mlx90640.MLX90640(i2c) # begin MLX90640 with I2C comm mlx.refresh_rate = adafruit_mlx90640.RefreshRate.REFRESH_2_HZ # set refresh rate frame = np.zeros( (24 * 32, )) # setup array for storing all 768 temperatures while True: try: mlx.getFrame(frame) # read MLX temperatures into frame var break except ValueError: continue # if error, just read again return (((9.0 / 5.0) * np.mean(frame)) + 32.00)
def __init__(self, i2c=None): super().__init__('rtf_mlx90640') if i2c is None: self.i2c = busio.I2C(board.SCL, board.SDA, frequency=400000) else: self.i2c = i2c self.camera = adafruit_mlx90640.MLX90640(i2c) self.camera.refresh_rate = adafruit_mlx90640.RefreshRate.REFRESH_2_HZ self.frame = [0] * 24 * 32 # replace with deque? self.timer = self.create_timer(1 / 2, self.callback) self.pub = self.create_publisher(ImageIR, 'image_ir', 10) self.msg = ImageIR() self.msg.header.frame_id = self.declare_parameter( 'frame_id', "base_imu_link").value self.msg.height = 24 # rows self.msg.width = 32 # cols
def __init__(self, i2c=None, visualise_on=False): self.quit_event = Event() logger.info("Initialising thermal camera...") if i2c is None: i2c = busio.I2C(board.SCL, board.SDA) mlx = adafruit_mlx90640.MLX90640(i2c) mlx.refresh_rate = adafruit_mlx90640.RefreshRate.REFRESH_32_HZ self.mlx = mlx self.file_queue = Queue(1) self.temperature = 0 self.thermal_history = deque([0] * 120) self.data = { "temperature": None, "thermal_history": None, }
import json import sys FRAME_SIZE = 768 FRAME_HEIGHT = 24 FRAME_WIDTH = 32 #temp threshold now in fahrenheit UPPER_TEMPERATURE_THRESHOLD = 85 LOWER_TEMPERATURE_THRESHOLD = 45 VARIANCE_CHANGE_THRESHOLD = 0.5 NOTIFICATION_INTERVAL = 60 I2C = busio.I2C(board.SCL, board.SDA, frequency=400000) thermalCamera = adafruit_mlx90640.MLX90640(I2C) thermalCamera.refresh_rate = adafruit_mlx90640.RefreshRate.REFRESH_0_5_HZ cameraFrame = [0] * FRAME_SIZE DEVICE_ID = " " DEVICE_NAME = " " SERVER_URL = "http://198.211.109.9:8000/SafeHomeDatabase/setTemp/" FIREBASE_URL = "https://fcm.googleapis.com/fcm/send" FIREBASE_API_KEY = 'AAAAEDDiUSU:APA91bFqIUEReFZhmk4SsGkIpIvh9Bz_TTb6s9-MuPjxj9QYwEaBY6BhfxnNVNJCYtE_pngp1bPsOUvQMICdK5LKwcXKcoPT-QAKXK9otinw4t13Q0FyEB1dE9DSzXx59fQSZWzG_o9m' header = { 'Content-Type': 'application/json', 'Authorization': 'key=' + FIREBASE_API_KEY }
# Add all the sub-group to the SuperGroup group.append(image_group) group.append(scale_group) group.append(min_label) group.append(max_label) # Add the SuperGroup to the Display board.DISPLAY.show(group) min_t = 20 # Initial minimum temperature range, before auto scale max_t = 37 # Initial maximum temperature range, before auto scale i2c = busio.I2C(board.SCL, board.SDA, frequency=800000) mlx = adafruit_mlx90640.MLX90640(i2c) print("MLX addr detected on I2C") print([hex(i) for i in mlx.serial_number]) # mlx.refresh_rate = adafruit_mlx90640.RefreshRate.REFRESH_2_HZ mlx.refresh_rate = adafruit_mlx90640.RefreshRate.REFRESH_4_HZ frame = [0] * 768 while True: stamp = time.monotonic() try: mlx.getFrame(frame) except ValueError: # these happen, no biggie - retry continue
import time import adafruit_mlx90640 import board import busio import sys import os import math import numpy as np # import pygame # from scipy.interpolate import griddata # from colour import Color #---------------------------Initializing the Setup of the IR MLX Camera--------------------------------------------------------------------- #MLX IR Camera Specifics Information and Setup i2c = busio.I2C(board.SCL, board.SDA, frequency=800000) mlx = adafruit_mlx90640.MLX90640(i2c) #initialize the mlx sensor print("MLX addr detected on I2C") print([hex(i) for i in mlx.serial_number]) mlx.refresh_rate = adafruit_mlx90640.RefreshRate.REFRESH_2_HZ #let the mlx sensor initialize time.sleep(1) temp_frame = [0]*768 #initializing the temperature frame print(mlx.getFrame(temp_frame)) #---------------------------Code for setting up the Thermal Camera GUI with PyGame--------------------------------------------------------------- # #Setting up the min/max values for the test of the cameras (for body heat temperature) # MINTEMP = 19. # MAXTEMP = 35.
import time, board, busio import numpy as np import adafruit_mlx90640 import matplotlib.pyplot as plt import tensorflow as tf import glob import cv2 from scipy import ndimage from PIL import Image from datetime import datetime i2c = busio.I2C(board.SCL, board.SDA, frequency=1000000) # setup I2C mlx_1 = adafruit_mlx90640.MLX90640( i2c, address=0x32) # begin MLX90640 with I2C comm mlx_1.refresh_rate = adafruit_mlx90640.RefreshRate.REFRESH_32_HZ # set refresh rate mlx_2 = adafruit_mlx90640.MLX90640(i2c, address=0x33) mlx_2.refresh_rate = adafruit_mlx90640.RefreshRate.REFRESH_32_HZ mlx_shape = (24, 32) # mlx90640 shape #model = tf.keras.models.load_model('//home/pi/models/people_detect') frame_1 = np.zeros(mlx_shape[0] * mlx_shape[1]) # 768 pts frame_2 = np.zeros(mlx_shape[0] * mlx_shape[1]) model = tf.keras.models.load_model('//home/pi/models/') cnt = 0 def predict(idx):
"""This example is for Raspberry Pi (Linux) only! It will not work on microcontrollers running CircuitPython!""" import math from PIL import Image import board import adafruit_mlx90640 FILENAME = "mlx.jpg" MINTEMP = 25.0 # low range of the sensor (deg C) MAXTEMP = 45.0 # high range of the sensor (deg C) COLORDEPTH = 1000 # how many color values we can have INTERPOLATE = 10 # scale factor for final image mlx = adafruit_mlx90640.MLX90640(board.I2C()) # the list of colors we can choose from heatmap = ( (0.0, (0, 0, 0)), (0.20, (0, 0, 0.5)), (0.40, (0, 0.5, 0)), (0.60, (0.5, 0, 0)), (0.80, (0.75, 0.75, 0)), (0.90, (1.0, 0.75, 0)), (1.00, (1.0, 1.0, 1.0)), ) colormap = [0] * COLORDEPTH
MLX90640_BUS_FREQUENCY = 800000 MLX90640_WIDTH = 24 MLX90640_HEIGHT = 32 MLX90640_INTERPOLATE_VALUE = 10 # PRE-DEFINE CONST VALUE NUM_OF_COLUMNS_CALCULATING = 50 NUM_OF_ROWS_CALCULATING = 50 WIDTH_SIZE_CALCULATING = 24 HEIGHT_SIZE_CALCULATING = 32 ROUNDING_NUMBER = 5 ################################# i2c = busio.I2C(board.SCL, board.SDA, frequency=MLX90640_BUS_FREQUENCY) # setup I2C mlx = adafruit_mlx90640.MLX90640(i2c) # begin MLX90640 with I2C comm mlx.refresh_rate = adafruit_mlx90640.RefreshRate.REFRESH_16_HZ # set refresh rate mlx_shape = (MLX90640_WIDTH, MLX90640_HEIGHT) # mlx90640 shape mlx_interp_val = MLX90640_INTERPOLATE_VALUE # interpolate # on each dimension mlx_interp_shape = (mlx_shape[0] * mlx_interp_val, mlx_shape[1] * mlx_interp_val) # new shape # We got number 768 pts because of 24 * 32 frame = np.zeros(mlx_shape[0] * mlx_shape[1]) # 768 pts # Getting connection from database conn = sqlite3.connect(DATABASE_LOCATION) if conn is None: print("Connecting to database failed. Please check the code") exit(0)
BASE_PATH = os.environ['PWD'] # count the frames in this scene frame_counter = 0 # array to host the calibration data eeData = [0] * 832 I2C_BUS = busio.I2C(board.SCL, board.SDA, frequency=400000) # low range of the sensor (this will be blue on the screen) MINTEMP = 20.0 # high range of the sensor (this will be red on the screen) MAXTEMP = 40.0 COLORDEPTH = 1024 SENSOR = adafruit_mlx90640.MLX90640(I2C_BUS) SENSOR.refresh_rate = adafruit_mlx90640.RefreshRate.REFRESH_16_HZ eeData = SENSOR.getEeData() # pylint: disable=invalid-slice-index POINTS = [(math.floor(ix / 8), (ix % 8)) for ix in range(0, 64)] GRID_X, GRID_Y = np.mgrid[0:7:32j, 0:7:32j] # pylint: enable=invalid-slice-index # sensor is an 8x8 grid so lets do a square HEIGHT = 240 WIDTH = 240 # the list of colors we can choose from
def thermal_cam(show_video=True, save_data=False): # initialize stuff here i2c = busio.I2C(board.SCL, board.SDA, frequency=800000) mlx = adafruit_mlx90640.MLX90640(i2c) print("MLX addr detected on I2C", [hex(i) for i in mlx.serial_number]) mlx.refresh_rate = adafruit_mlx90640.RefreshRate.REFRESH_32_HZ frame = [0] * 768 # thresholding parameters min_temp = 27 zero_temp = 25 # frame averaging mean_temp = 0 max_temp = 0 maxlen = 3 multiple_frames = deque(maxlen=maxlen) frame_number = 0 avg_weights = [i + 1 for i in range(0, maxlen, 1)] # create filename filename = datetime.datetime.now().strftime( "%Y-%m-%d_%H:%M:%S") + "_temperature.csv" while True: try: mlx.getFrame(frame) # make everything below zero_temp go to zero frame = [value if value > zero_temp else 0 for value in frame] # get frame in np.uint8, correct format and transpose frame2 = np.array(frame, np.uint8).reshape((32, 24), order="F").T # flip it (camera is twisted) # frame2 = np.rot90(frame2) frame2 = np.flip(frame2, 0) # append to deque and # resize so that we can see multiple_frames.append(imutils.resize(frame2, height=320)) # calculate average of stored frames big_frame = np.average(multiple_frames, axis=0, weights=avg_weights) # scale it to 0 to 1 scale max_temp = 38 scaled_big_frame = big_frame / max_temp # blur blurr = cv2.GaussianBlur(scaled_big_frame, (5, 5), 0) # the second element is the matrix, the first element is the threshold # that's why we need [1] at the end binary = cv2.threshold( blurr, min_temp / max_temp, # min 1, # max temp will be 1 because normalizerd cv2.THRESH_BINARY)[1] kernel = np.ones((11, 11), np.uint8) closed = cv2.morphologyEx(binary.copy(), cv2.MORPH_CLOSE, kernel, iterations=3) # find contours cv2.cvtColor(closed, cv2.COLOR_BGR2GRAY) cnts = cv2.findContours(closed.copy().astype(np.uint8), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) cnts = imutils.grab_contours(cnts) #print(len(cnts) > 0, end="\r") # if we detect more than one, sort them and get the biggest if (len(cnts) > 0): # get the one with max area cnts = sorted(cnts, key=cv2.contourArea, reverse=True)[0] # get area area = cv2.contourArea(cnts) # get pixel values and calculate mean pixel_values = access_pixel_intensity(img=blurr, contours=cnts) mean_temp = np.mean(pixel_values) * max_temp max_temp = np.max(pixel_values) * max_temp if (save_data): save_to_file(filename, area, mean_temp, max_temp) else: area = 0 mean_temp = 0 max_temp = 0 #print(area, end = "\r") cv2.drawContours(scaled_big_frame, cnts, -1, (0, 0, 0), 3) cv2.putText(scaled_big_frame, str(area), (10, 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2) cv2.putText(scaled_big_frame, str([np.round(mean_temp, 2), np.round(max_temp, 2)]), (10, 80), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2) if (show_video): # this WINDOW_NORMAL thing is needed for some reason on the pi... cv2.namedWindow("frame", cv2.WINDOW_NORMAL) cv2.imshow("frame", scaled_big_frame) cv2.namedWindow("closed", cv2.WINDOW_NORMAL) cv2.imshow("closed", closed) #cv2.namedWindow("binary", cv2.WINDOW_NORMAL) #cv2.imshow("binary", binary) k = cv2.waitKey(1) if (k == ord("q")): break except ValueError: # these happen, no biggie - retry continue except RuntimeError: # these happen quite a lot cv2.destroyAllWindows() cv2.destroyAllWindows()