first commit

This commit is contained in:
Bart Moyaers
2019-11-06 14:41:57 +01:00
commit 6aa165edb8
5 changed files with 231 additions and 0 deletions

63
live_heatmap_diff_test.py Normal file
View File

@@ -0,0 +1,63 @@
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()\