Video-Summary/Application/HeatMap.py

30 lines
1009 B
Python
Raw Normal View History

2022-01-09 11:25:22 +00:00
import numpy as np
from matplotlib import pyplot as plt
2022-09-11 09:25:36 +00:00
from PIL import Image
2022-01-09 19:25:44 +00:00
2022-01-09 11:25:22 +00:00
class HeatMap:
2022-09-11 09:25:36 +00:00
def __init__(self, x, y, contours, resize_factor=1):
self.image_bw = np.zeros(shape=[y, x, 3], dtype=np.float64)
self._resize_factor = resize_factor
self._create_image(contours)
2022-01-09 11:25:22 +00:00
2022-09-11 09:25:36 +00:00
def _create_image(self, contours):
2022-01-09 11:25:22 +00:00
for contour in contours:
for x, y, w, h in contour:
2022-01-09 19:25:44 +00:00
x, y, w, h = (
2022-09-11 09:25:36 +00:00
x * self._resize_factor,
y * self._resize_factor,
w * self._resize_factor,
h * self._resize_factor,
2022-01-09 19:25:44 +00:00
)
2022-09-11 09:25:36 +00:00
self.image_bw[int(y) : int(y + h), int(x) : int(x + w)] += 1
2022-01-09 11:25:22 +00:00
2022-09-11 09:25:36 +00:00
self.image_bw = np.nan_to_num(self.image_bw / self.image_bw.sum(axis=1)[:, np.newaxis], 0)
2022-01-09 11:25:22 +00:00
2022-09-11 09:25:36 +00:00
def show_image(self):
plt.imshow(self.image_bw * 255)
2022-01-09 11:25:22 +00:00
plt.show()
2022-09-11 09:25:36 +00:00
def save__image(self, path):
im = Image.fromarray(self.image_bw * 255)
im.save(path)