Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 77 additions & 25 deletions src/tc001v4.2.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
print('r t: Record and Stop')
print('p : Snapshot')
print('m : Cycle through ColorMaps')
print('u : Cycle through Temperature Units')
print('o : Cycle through image rotation angles')
print('h : Toggle HUD')

import cv2
Expand All @@ -43,22 +45,45 @@ def is_raspberrypi():

parser = argparse.ArgumentParser()
parser.add_argument("--device", type=int, default=0, help="Video Device number e.g. 0, use v4l2-ctl --list-devices")
parser.add_argument("--tempunits", default='C', help="Default temperature units to display <K|C|F>")
parser.add_argument("--rotate", type=int, default=0, help="Default rotation angle <0|90|180|270>")
args = parser.parse_args()

if args.device:
dev = args.device
else:
dev = 0


if args.tempunits:
if args.tempunits in set(['K', 'k', 'C', 'c', 'F', 'f']):
temp_units = args.tempunits.upper()
else:
raise ValueError('Temperature units must be one of K, C or F')
else:
temp_units = "C"

if args.rotate:
if args.rotate in set([0, 90, 180, 270]):
rotate = args.rotate
if rotate == 0:
rotate = None
elif rotate == 90:
rotate = cv2.ROTATE_90_CLOCKWISE
elif rotate == 180:
rotate = cv2.ROTATE_180
elif rotate == 270:
rotate = cv2.ROTATE_90_COUNTERCLOCKWISE
else:
raise ValueError('Rotation value must be one of 0, 90, 180, 270.')
else:
rotate = None

#init video
cap = cv2.VideoCapture('/dev/video'+str(dev), cv2.CAP_V4L)
#cap = cv2.VideoCapture(0)
#pull in the video but do NOT automatically convert to RGB, else it breaks the temperature data!
#https://stackoverflow.com/questions/63108721/opencv-setting-videocap-property-to-cap-prop-convert-rgb-generates-weird-boolean
if isPi == True:
cap.set(cv2.CAP_PROP_CONVERT_RGB, 0.0)
else:
cap.set(cv2.CAP_PROP_CONVERT_RGB, False)
cap.set(cv2.CAP_PROP_CONVERT_RGB, 0.0)

#256x192 General settings
width = 256 #Sensor width
Expand Down Expand Up @@ -91,7 +116,35 @@ def snapshot(heatmap):
snaptime = time.strftime("%H:%M:%S")
cv2.imwrite("TC001"+now+".png", heatmap)
return snaptime


def convert_temp(temp):
global temp_units
if temp_units == "K":
return str(round(temp/64,2))+' K'
elif temp_units == "C":
return str(round(((temp/64)-273.15),2))+' C'
elif temp_units == "F":
return str(round(((temp/64)-273.15)*1.8+32,2))+' F'

def change_temp_units():
global temp_units
if temp_units == "K":
temp_units = 'C'
elif temp_units == "C":
temp_units = 'F'
elif temp_units == 'F':
temp_units = 'K'

def change_rotation():
global rotate
if rotate is None:
rotate = cv2.ROTATE_90_CLOCKWISE
elif rotate == cv2.ROTATE_90_CLOCKWISE:
rotate = cv2.ROTATE_180
elif rotate == cv2.ROTATE_180:
rotate = cv2.ROTATE_90_COUNTERCLOCKWISE
elif rotate == cv2.ROTATE_90_COUNTERCLOCKWISE:
rotate = None

while(cap.isOpened()):
# Capture frame-by-frame
Expand All @@ -106,10 +159,7 @@ def snapshot(heatmap):
lo = thdata[96][128][1]
#print(hi,lo)
lo = lo*256
rawtemp = hi+lo
#print(rawtemp)
temp = (rawtemp/64)-273.15
temp = round(temp,2)
temp = hi+lo
#print(temp)
#break

Expand All @@ -121,8 +171,6 @@ def snapshot(heatmap):
himax = thdata[mcol][mrow][0]
lomax=lomax*256
maxtemp = himax+lomax
maxtemp = (maxtemp/64)-273.15
maxtemp = round(maxtemp,2)


#find the lowest temperature in the frame
Expand All @@ -133,18 +181,16 @@ def snapshot(heatmap):
himin = thdata[lcol][lrow][0]
lomin=lomin*256
mintemp = himin+lomin
mintemp = (mintemp/64)-273.15
mintemp = round(mintemp,2)

#find the average temperature in the frame
loavg = thdata[...,1].mean()
hiavg = thdata[...,0].mean()
loavg=loavg*256
avgtemp = loavg+hiavg
avgtemp = (avgtemp/64)-273.15
avgtemp = round(avgtemp,2)


# rotate image, if requested
if not rotate is None:
imdata = cv2.rotate(imdata, rotate)

# Convert the real image to RGB
bgr = cv2.cvtColor(imdata, cv2.COLOR_YUV2BGR_YUYV)
Expand Down Expand Up @@ -204,19 +250,19 @@ def snapshot(heatmap):
cv2.line(heatmap,(int(newWidth/2)+20,int(newHeight/2)),\
(int(newWidth/2)-20,int(newHeight/2)),(0,0,0),1) #hline
#show temp
cv2.putText(heatmap,str(temp)+' C', (int(newWidth/2)+10, int(newHeight/2)-10),\
cv2.putText(heatmap,convert_temp(temp), (int(newWidth/2)+10, int(newHeight/2)-10),\
cv2.FONT_HERSHEY_SIMPLEX, 0.45,(0, 0, 0), 2, cv2.LINE_AA)
cv2.putText(heatmap,str(temp)+' C', (int(newWidth/2)+10, int(newHeight/2)-10),\
cv2.putText(heatmap,convert_temp(temp), (int(newWidth/2)+10, int(newHeight/2)-10),\
cv2.FONT_HERSHEY_SIMPLEX, 0.45,(0, 255, 255), 1, cv2.LINE_AA)

if hud==True:
# display black box for our data
cv2.rectangle(heatmap, (0, 0),(160, 120), (0,0,0), -1)
# put text in the box
cv2.putText(heatmap,'Avg Temp: '+str(avgtemp)+' C', (10, 14),\
cv2.putText(heatmap,'Avg Temp: '+convert_temp(avgtemp), (10, 14),\
cv2.FONT_HERSHEY_SIMPLEX, 0.4,(0, 255, 255), 1, cv2.LINE_AA)

cv2.putText(heatmap,'Label Threshold: '+str(threshold)+' C', (10, 28),\
cv2.putText(heatmap,'Label Threshold: '+str(threshold), (10, 28),\
cv2.FONT_HERSHEY_SIMPLEX, 0.4,(0, 255, 255), 1, cv2.LINE_AA)

cv2.putText(heatmap,'Colormap: '+cmapText, (10, 42),\
Expand Down Expand Up @@ -247,18 +293,18 @@ def snapshot(heatmap):
if maxtemp > avgtemp+threshold:
cv2.circle(heatmap, (mrow*scale, mcol*scale), 5, (0,0,0), 2)
cv2.circle(heatmap, (mrow*scale, mcol*scale), 5, (0,0,255), -1)
cv2.putText(heatmap,str(maxtemp)+' C', ((mrow*scale)+10, (mcol*scale)+5),\
cv2.putText(heatmap,convert_temp(maxtemp), ((mrow*scale)+10, (mcol*scale)+5),\
cv2.FONT_HERSHEY_SIMPLEX, 0.45,(0,0,0), 2, cv2.LINE_AA)
cv2.putText(heatmap,str(maxtemp)+' C', ((mrow*scale)+10, (mcol*scale)+5),\
cv2.putText(heatmap,convert_temp(maxtemp), ((mrow*scale)+10, (mcol*scale)+5),\
cv2.FONT_HERSHEY_SIMPLEX, 0.45,(0, 255, 255), 1, cv2.LINE_AA)

#display floating min temp
if mintemp < avgtemp-threshold:
cv2.circle(heatmap, (lrow*scale, lcol*scale), 5, (0,0,0), 2)
cv2.circle(heatmap, (lrow*scale, lcol*scale), 5, (255,0,0), -1)
cv2.putText(heatmap,str(mintemp)+' C', ((lrow*scale)+10, (lcol*scale)+5),\
cv2.putText(heatmap,convert_temp(mintemp), ((lrow*scale)+10, (lcol*scale)+5),\
cv2.FONT_HERSHEY_SIMPLEX, 0.45,(0,0,0), 2, cv2.LINE_AA)
cv2.putText(heatmap,str(mintemp)+' C', ((lrow*scale)+10, (lcol*scale)+5),\
cv2.putText(heatmap,convert_temp(mintemp), ((lrow*scale)+10, (lcol*scale)+5),\
cv2.FONT_HERSHEY_SIMPLEX, 0.45,(0, 255, 255), 1, cv2.LINE_AA)

#display image
Expand Down Expand Up @@ -346,6 +392,12 @@ def snapshot(heatmap):
if keyPress == ord('p'): #f to finish reording
snaptime = snapshot(heatmap)

if keyPress == ord('u'): #u to toggle units for temperature display
change_temp_units()

if keyPress == ord('o'): #o to r(O)tate the image
change_rotation()

if keyPress == ord('q'):
break
capture.release()
Expand Down