Skip to content

Commit 5323d21

Browse files
committed
DM: added thermal cam example with interpolation and pygame
1 parent da0554d commit 5323d21

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

examples/thermal_cam.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
from Adafruit_AMG88xx import Adafruit_AMG88xx
2+
import pygame
3+
import os
4+
import math
5+
import time
6+
7+
import numpy as np
8+
from scipy.interpolate import griddata
9+
10+
from colour import Color
11+
12+
#low range of the sensor (this will be blue on the screen)
13+
MINTEMP = 26
14+
15+
#high range of the sensor (this will be red on the screen)
16+
MAXTEMP = 32
17+
18+
#how many color values we can have
19+
COLORDEPTH = 1024
20+
21+
os.putenv('SDL_FBDEV', '/dev/fb1')
22+
pygame.init()
23+
24+
#initialize the sensor
25+
sensor = Adafruit_AMG88xx(address=0x68)
26+
27+
points = [(math.floor(ix / 8), (ix % 8)) for ix in range(0, 64)]
28+
grid_x, grid_y = np.mgrid[0:7:32j, 0:7:32j]
29+
30+
#sensor is an 8x8 grid so lets do a square
31+
height = 240
32+
width = 240
33+
34+
#the list of colors we can choose from
35+
blue = Color("indigo")
36+
colors = list(blue.range_to(Color("red"), COLORDEPTH))
37+
38+
#create the array of colors
39+
colors = [(int(c.red * 255), int(c.green * 255), int(c.blue * 255)) for c in colors]
40+
41+
displayPixelWidth = width / 30
42+
displayPixelHeight = height / 30
43+
44+
lcd = pygame.display.set_mode((width, height))
45+
46+
lcd.fill((255,0,0))
47+
48+
pygame.display.update()
49+
pygame.mouse.set_visible(False)
50+
51+
lcd.fill((0,0,0))
52+
pygame.display.update()
53+
54+
#some utility functions
55+
def constrain(val, min_val, max_val):
56+
return min(max_val, max(min_val, val))
57+
58+
def map(x, in_min, in_max, out_min, out_max):
59+
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
60+
61+
#let the sensor initialize
62+
time.sleep(.1)
63+
64+
while(1):
65+
66+
#read the pixels
67+
pixels = sensor.readPixels()
68+
pixels = [map(p, MINTEMP, MAXTEMP, 0, COLORDEPTH - 1) for p in pixels]
69+
70+
#perdorm interpolation
71+
bicubic = griddata(points, pixels, (grid_x, grid_y), method='cubic')
72+
73+
#draw everything
74+
for ix, row in enumerate(bicubic):
75+
for jx, pixel in enumerate(row):
76+
pygame.draw.rect(lcd, colors[constrain(int(pixel), 0, COLORDEPTH- 1)], (displayPixelHeight * ix, displayPixelWidth * jx, displayPixelHeight, displayPixelWidth))
77+
78+
pygame.display.update()

0 commit comments

Comments
 (0)