Skip to content

Instantly share code, notes, and snippets.

@chizuchizu
Created September 19, 2020 12:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chizuchizu/8439a75ed50e3510a165e525f2857a21 to your computer and use it in GitHub Desktop.
Save chizuchizu/8439a75ed50e3510a165e525f2857a21 to your computer and use it in GitHub Desktop.
import cv2
import time
import busio
import board
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import adafruit_amg88xx
i2c_bus = busio.I2C(board.SCL, board.SDA)
sensor = adafruit_amg88xx.AMG88XX(i2c_bus)
time.sleep(.1)
filepath = "vtest.avi"
# cap = cv2.VideoCapture(filepath)
# Webカメラを使うときはこちら
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 480)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
avg = None
gain = 10
offset_x= 0.2
offset_green = 0.6
def vconcat_resize_min(im_list, interpolation=cv2.INTER_CUBIC):
w_min = min(im.shape[1] for im in im_list)
im_list_resize = [cv2.resize(im, (w_min, int(im.shape[0] * w_min / im.shape[1])), interpolation=interpolation)
for im in im_list]
return cv2.vconcat(im_list_resize)
def sigmoid(x, gain=1, offset_x=0):
return ((np.tanh(((x+offset_x)*gain)/2)+1)/2)
def colorBarRGB(x):
x = (x * 2) - 1
red = sigmoid(x, gain, -1*offset_x)
blue = 1-sigmoid(x, gain, offset_x)
green = sigmoid(x, gain, offset_green) + (1-sigmoid(x,gain,-1*offset_green))
green = green - 1.0
return (blue * 1024,green * 1024,red * 1024)
count = 0
min_deg = max_deg = 0
while True:
pixels = np.array(sensor.pixels)
# 1フレームずつ取得する。
ret, frame = cap.read()
if not ret:
break
# グレースケールに変換
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# 比較用のフレームを取得する
if avg is None:
avg = gray.copy().astype("float")
continue
# 現在のフレームと移動平均との差を計算
cv2.accumulateWeighted(gray, avg, 0.95)
frameDelta = cv2.absdiff(gray, cv2.convertScaleAbs(avg))
# デルタ画像を閾値処理を行う
thresh = cv2.threshold(frameDelta, 3, 255, cv2.THRESH_BINARY)[1]
# 画像の閾値に輪郭線を入れる
contours, hierarchy = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
frame = cv2.drawContours(frame, contours, -1, (0, 255, 0), 3)
# 結果を出力
deg = pixels.max()
# min_deg = min(deg, min_deg)
# max_deg = max(deg, max_deg)
min_deg = 25
max_deg = 40
if count % 5 == 0:
nor_deg = (deg - min_deg) / (max_deg - min_deg)
colors = colorBarRGB(nor_deg)
# print(colors)
new_img = np.zeros(shape=(200,480,3),dtype="uint8")
new_img[:, :] = colors
# print(frame.shape)
# print(frame.dtype)
# print(new_img.shape)
frame = vconcat_resize_min([frame, new_img])
# frame = cv2.vconcat([frame, new_img])
w, h = np.unravel_index(np.argmax(pixels), pixels.shape)
# frame = cv2.drawMarker(frame, (w * 60 + 50, h * 60 + 50), color=(255, 0, 0), markerType=cv2.MARKER_CROSS, markerSize=20, thickness=6)
frame = cv2.putText(frame, f"{'{:.2f}'.format(deg)} deg C", (50, 600), cv2.FONT_HERSHEY_SIMPLEX, 2.0, (0, 0, 0), thickness=5)
cv2.imshow("Frame", frame)
key = cv2.waitKey(30)
# print(cap.get(cv2.CAP_PROP_FPS))
# print(w, h)
count += 1
if key == 27:
break
cap.release()
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment