Files
gobot/board-vision/cnn_model/util.py
2024-09-10 15:04:45 +02:00

42 lines
1.5 KiB (Stored with Git LFS)
Python

import cv2
from cv2.typing import MatLike
import numpy as np
POOL_SIZES = 10
POOL_KERNAL = np.ones((POOL_SIZES,POOL_SIZES),np.float32)/(POOL_SIZES*POOL_SIZES)
WINDOW_WIDTH = 21
def process_frame(frame: MatLike, thresh: float) -> tuple[MatLike, MatLike, list[tuple[int, int]], list[tuple[int, int]], list[tuple[int, int]]]:
frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
frame_hls = cv2.cvtColor(frame, cv2.COLOR_BGR2HLS)
H_filter_mask = cv2.inRange(frame_hls[:,:,0], 100, 140)
L_filter_mask = cv2.inRange(frame_hls[:,:,1], 20, 210)
S_filter_mask = cv2.inRange(frame_hls[:,:,2], 200, 255)
mask = cv2.bitwise_and(S_filter_mask, H_filter_mask)
mask = cv2.bitwise_and(mask, L_filter_mask)
frame_grey_filtered = cv2.bitwise_and(frame_gray, mask)
frame_convolution_mask = cv2.inRange(cv2.filter2D(frame_grey_filtered, -1, POOL_KERNAL), 1, 255)
WIDTH = frame_convolution_mask.shape[1]
HEIGHT = frame_convolution_mask.shape[0]
pos_windows = []
neg_windows = []
neutral_windows = []
for x in range(0, WIDTH - WINDOW_WIDTH, 5):
for y in range(0, HEIGHT - WINDOW_WIDTH, 5):
score = np.sum(frame_convolution_mask[y:y+WINDOW_WIDTH, x:x+WINDOW_WIDTH])
if score > 255*(WINDOW_WIDTH*WINDOW_WIDTH*thresh):
pos_windows += [(x, y)]
elif score < 1:
neg_windows += [(x, y)]
else:
neutral_windows += [(x, y)]
return frame_gray, frame_convolution_mask, pos_windows, neg_windows, neutral_windows