# featureExtraction.py import cv2 import numpy as np from globalVars import HEIGHT, WIDTH def extract_edge_features(frame): """ Extract edge features using Canny edge detection. Args: - frame (ndarray): Image frame. Returns: - ndarray: Edge feature map. """ edges = cv2.Canny(frame, threshold1=100, threshold2=200) return edges.astype(np.float32) / 255.0 def extract_histogram_features(frame, bins=64): """ Extract histogram features from a frame. Args: - frame (ndarray): Image frame. - bins (int): Number of bins for the histogram. Returns: - ndarray: Normalized histogram feature vector. """ histogram, _ = np.histogram(frame.flatten(), bins=bins, range=[0, 255]) return histogram.astype(np.float32) / frame.size def preprocess_frame(frame): # Check frame dimensions and resize if necessary if frame.shape[:2] != (HEIGHT, WIDTH): frame = cv2.resize(frame, (WIDTH, HEIGHT), interpolation=cv2.INTER_NEAREST) # Extract features edge_feature = extract_edge_features(frame) histogram_feature = extract_histogram_features(frame) histogram_feature_image = np.full((HEIGHT, WIDTH), histogram_feature.mean()) # Convert histogram feature to image-like shape combined_feature = np.stack([edge_feature, histogram_feature_image], axis=-1) compressed_frame = frame / 255.0 # Assuming the frame is uint8, scale to [0, 1] return combined_feature, compressed_frame