working
This commit is contained in:
parent
f4512bba99
commit
db43239b3d
5 changed files with 311 additions and 197 deletions
|
@ -9,51 +9,21 @@ os.environ['TF_CPP_MIN_LOG_LEVEL'] = '1'
|
|||
import tensorflow as tf
|
||||
from tensorflow.keras import backend as K
|
||||
|
||||
from globalVars import HEIGHT, NUM_PRESET_SPEEDS, WIDTH
|
||||
from globalVars import HEIGHT, LOGGER, NUM_PRESET_SPEEDS, WIDTH
|
||||
|
||||
def scale_crf(crf):
|
||||
return crf / 51
|
||||
|
||||
|
||||
def scale_speed_preset(speed):
|
||||
return speed / NUM_PRESET_SPEEDS
|
||||
|
||||
|
||||
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 with 3 channels.
|
||||
|
||||
Args:
|
||||
- frame (ndarray): Image frame with shape (height, width, 3).
|
||||
- bins (int): Number of bins for the histogram.
|
||||
|
||||
Returns:
|
||||
- ndarray: Normalized histogram feature vector.
|
||||
"""
|
||||
feature_vector = []
|
||||
for channel in range(3):
|
||||
histogram, _ = np.histogram(frame[:,:,channel].flatten(), bins=bins, range=[0, 255])
|
||||
normalized_histogram = histogram.astype(np.float32) / frame[:,:,channel].size
|
||||
feature_vector.extend(normalized_histogram)
|
||||
|
||||
return np.array(feature_vector)
|
||||
|
||||
|
||||
def psnr(y_true, y_pred):
|
||||
#LOGGER.info(f"[psnr function] y_true: {y_true.shape}, y_pred: {y_pred.shape}")
|
||||
max_pixel = 1.0
|
||||
return 10.0 * K.log((max_pixel ** 2) / (K.mean(K.square(y_pred - y_true)))) / K.log(10.0)
|
||||
mse = K.mean(K.square(y_pred - y_true))
|
||||
return 20.0 * K.log(max_pixel / K.sqrt(mse)) / K.log(10.0)
|
||||
|
||||
|
||||
def ssim(y_true, y_pred):
|
||||
|
@ -64,14 +34,41 @@ def combined(y_true, y_pred):
|
|||
return (psnr(y_true, y_pred) + ssim(y_true, y_pred)) / 2
|
||||
|
||||
|
||||
def preprocess_frame(frame, resize=True):
|
||||
#Preprocesses a single frame, cropping it if needed
|
||||
def combined_loss(y_true, y_pred):
|
||||
return -combined(y_true, y_pred) # The goal is to maximize the combined value
|
||||
|
||||
|
||||
def detect_noise(image, threshold=15):
|
||||
# Convert to grayscale if it's a color image
|
||||
if len(image.shape) == 3:
|
||||
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
||||
|
||||
# Compute the standard deviation
|
||||
std_dev = np.std(image)
|
||||
|
||||
# If the standard deviation is higher than a threshold, it might be considered noisy
|
||||
return std_dev > threshold
|
||||
|
||||
|
||||
def frame_difference(frame1, frame2):
|
||||
# Ensure both frames are of the same size and type
|
||||
if frame1.shape != frame2.shape:
|
||||
raise ValueError("Frames must have the same dimensions and number of channels")
|
||||
|
||||
# Calculate the absolute difference between the frames
|
||||
difference = cv2.absdiff(frame1, frame2)
|
||||
|
||||
return difference
|
||||
|
||||
|
||||
def preprocess_frame(frame, resize=True, scale=True):
|
||||
|
||||
# Check frame dimensions and resize if necessary
|
||||
if resize and frame.shape[:2] != (HEIGHT, WIDTH):
|
||||
frame = cv2.resize(frame, (WIDTH, HEIGHT), interpolation=cv2.INTER_LINEAR)
|
||||
|
||||
if scale:
|
||||
# Scale frame to [0, 1]
|
||||
compressed_frame = frame / 255.0
|
||||
frame = frame / 255.0
|
||||
|
||||
return compressed_frame
|
||||
return frame
|
||||
|
|
Reference in a new issue