63 lines
1.6 KiB
Python
63 lines
1.6 KiB
Python
import numpy as np
|
|
from typing import List
|
|
import cv2
|
|
|
|
class DiffSumHeatmap:
|
|
def __init__(self, capture):
|
|
self.diffs: List[np.array] = []
|
|
self.heatmap = np.array([])
|
|
self.cap = capture
|
|
|
|
# Fill up with first frame
|
|
ret, frame = self.cap.read()
|
|
self.lastframe = frame
|
|
self.lastsum = self.to_floats(self.to_grayscale(frame))
|
|
|
|
@staticmethod
|
|
def to_grayscale(frame):
|
|
return cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
|
|
|
@staticmethod
|
|
def to_floats(frame):
|
|
return cv2.normalize(frame, None, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_32F)
|
|
|
|
@staticmethod
|
|
def float_to_gray(frame):
|
|
return cv2.normalize(frame, None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8UC1)
|
|
|
|
@staticmethod
|
|
def gray_to_heat(frame):
|
|
return cv2.applyColorMap(frame, cv2.COLORMAP_JET)
|
|
|
|
def update(self):
|
|
ret, frame = self.cap.read()
|
|
self.diffs.append(
|
|
self.to_grayscale(
|
|
cv2.absdiff(self.lastframe, frame)
|
|
)
|
|
)
|
|
self.lastsum += self.diffs[-1]
|
|
self.heatmap = self.gray_to_heat(
|
|
self.float_to_gray(
|
|
self.to_floats(self.lastsum)
|
|
)
|
|
)
|
|
self.lastframe = frame
|
|
|
|
cap = cv2.VideoCapture(0)
|
|
diffsum = DiffSumHeatmap(cap)
|
|
|
|
# Load diffsum up with first
|
|
|
|
while(True):
|
|
# Update heatmap
|
|
diffsum.update()
|
|
|
|
# Display the resulting frame
|
|
cv2.imshow('frame', diffsum.heatmap)
|
|
if cv2.waitKey(1) & 0xFF == ord('q'):
|
|
break
|
|
|
|
# When everything done, release the capture
|
|
diffsum.cap.release()
|
|
cv2.destroyAllWindows()\ |