我正在一个学校项目中使用MLX90621 16x4红外传感器阵列和带有Python的Raspberry Pi,以获取温度阵列数据并将其转换为热彩色覆盖层。该覆盖层可以使用Haarcascade对准人的前额,并且可以显示前额温度的读数。
我设法用python做了额头跟踪代码,并通过以下链接获取了覆盖相机图像的热数据: https://hackaday.io/project/6416-raspberry-pi-thermal-imaging
但是,我只能使它分开工作,我正在努力使两个代码一起工作。我对该主题进行了大量研究,但似乎只找到Arduino和C ++教程或与理论相关的文章。
我是编程和树莓派的新手。
这是使用Haarcascade和Pi Camera进行额头跟踪的代码:
from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import cv2
# initialize the camera and grab a reference to the raw camera capture
camera = PiCamera()
camera.resolution = (640, 480)
camera.framerate = 32
rawCapture = PiRGBArray(camera, size=(640, 480))
# allow the camera to warmup
time.sleep(0.1)
#Use the trained xml classifier for eyes detection in the video
eye_cascade = cv2.CascadeClassifier('/home/pi/Desktop/mlxd-master_terickson/testScripts/haarcascade_eye.xml')
#Use the trained xml classifier for faces detection in the video
face_cascade = cv2.CascadeClassifier('/home/pi/Desktop/mlxd-master_terickson/testScripts/haarcascade_frontalface_default.xml')
# capture frames from the camera
for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
# grab the raw NumPy array representing the image, then initialize the timestamp
# and occupied/unoccupied text
image = frame.array
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
font = cv2.FONT_HERSHEY_SIMPLEX
eyes = eye_cascade.detectMultiScale(gray, 1.3, 6) # To detect eyes in the video
for(ex,ey,ew,eh) in eyes:
cv2.rectangle(image, (ex,ey), (ex+ew,ey+eh), (0,255,0),2) # To draw rectangle boxes around the eyes
faces = face_cascade.detectMultiScale(gray, 1.3, 6)
for (x,y,w,h) in faces:
forehead_center = int(x+w/1), int(y+h/4) # Location of the forehead in face
cv2.rectangle(image,(x,y),(x+w,y+h),(255,0,0),2) # To draw rectangle boxes around the face
cv2.rectangle(image,(x,y),forehead_center,(255,255,0),2) # To draw rectangle boxes around the forehead of the face
cv2.putText(image,'x: {0} | y: {1}'.format(forehead_center[0], forehead_center[1]),(10,50), font, 1.2,(0,0,255),1) # To put the x and y cord of the forehead
# show the frame
cv2.imshow("Eye, Face and Forehead tracking", image)
key = cv2.waitKey(1) & 0xFF
# clear the stream in preparation for the next frame
rawCapture.truncate(0)
# if the `q` key was pressed, break from the loop
if cv2.waitKey(1) & 0xFF == ord('q'):
break
以下是将温度数组叠加到叠加层并使用Pi Camera在视频预览上显示的代码:
import picamera
import numpy as np
import subprocess
import skimage
from skimage import io, exposure, transform, img_as_float, img_as_ubyte
import matplotlib.pyplot as plt
from time import sleep
import cv2
# IR registration parameters
ROT = np.deg2rad(90)
SCALE = (36.2, 36.4)
OFFSET = (580, 170)
def getImage():
fn = r'/home/pi/tmp.jpg'
proc = subprocess.Popen('raspistill -o %s -w 640 -h 480 -n -t 3' % (fn),
shell=True, stderr=subprocess.STDOUT)
proc.wait()
im = io.imread(fn, as_grey=True)
im = exposure.equalize_hist(im)
return skimage.img_as_ubyte(im)
def get_overlay(fifo):
# get the whole FIFO
ir_raw = fifo.read()
# trim to 128 bytes
ir_trimmed = ir_raw[0:128]
# go all numpy on it
ir = np.frombuffer(ir_trimmed, np.uint16)
# set the array shape to the sensor shape (16x4)
ir = ir.reshape((16, 4))[::-1, ::-1]
ir = img_as_float(ir)
# stretch contrast on our heat map
p2, p98 = np.percentile(ir, (2, 98))
ir = exposure.rescale_intensity(ir, in_range=(p2, p98))
# increase even further? (optional)
ir = exposure.equalize_hist(ir)
# turn our array into pretty colors
cmap = plt.get_cmap('Spectral')
rgba_img = cmap(ir)
rgb_img = np.delete(rgba_img, 3, 2)
# align the IR array with the camera
tform = transform.AffineTransform(scale=SCALE, rotation=ROT, translation=OFFSET)
ir_aligned = transform.warp(rgb_img, tform.inverse, mode='constant', output_shape=im.shape)
# turn it back into a ubyte so it'll display on the preview overlay
ir_byte = img_as_ubyte(ir_aligned)
# return buffer
return np.getbuffer(ir_byte)
#GaussianBlur = cv2.GaussianBlur(camera, (20,20),0)
#medianFiltered = cv2.medianBlur(camera,5)
im = getImage()
with picamera.PiCamera() as camera:
camera.led = True
camera.resolution = (640, 480)
camera.framerate = 35
camera.start_preview()
# get the temperature array, and align with the image
fifo = open('/var/run/mlx90621.sock', 'r')
o = camera.add_overlay(get_overlay(fifo), layer=3, alpha=90)
# update loop
while True:
sleep(0.001)
o.update(get_overlay(fifo))
print('Error! Closing...')
camera.remove_overlay(o)
fifo.close()