File size: 2,027 Bytes
c1ed328
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import cv2
import numpy as np


def preprocess(video_path, resize=(224, 224), max_frames=50):
    video_path = video_path.decode("utf-8") if isinstance(video_path, bytes) else video_path
    cap = cv2.VideoCapture(video_path)

    if not cap.isOpened():
        print("Error: Could not open video.")
        return None, None

    total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
    fps = cap.get(cv2.CAP_PROP_FPS)
    interval = max(int(fps // (fps / 7)), 1)  # Adjust interval based on fps
    print(f"Total frames: {total_frames}, FPS: {fps}, Interval: {interval}")

    start_frame, end_frame = 0, total_frames
    frames = []
    display_frames = []
    empty_frame = np.zeros((resize[1], resize[0], 3))
    cap.set(cv2.CAP_PROP_POS_FRAMES, start_frame)

    while len(frames) < max_frames and cap.get(cv2.CAP_PROP_POS_FRAMES) <= end_frame:
        ret, frame = cap.read()
        if not ret:
            print(f"Error: Could not read frame at position {cap.get(cv2.CAP_PROP_POS_FRAMES)}")
            break

        if (cap.get(cv2.CAP_PROP_POS_FRAMES) - 1 - start_frame) % interval == 0:
            original_frame = frame.copy()
            frame_display = cv2.resize(original_frame, resize)
            # frame_display = cv2.cvtColor(frame_display, cv2.COLOR_BGR2RGB)
            frame = cv2.resize(frame, resize)
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)  # Convert from BGR to RGB
            frame = frame.astype(np.float32) / 255.0
            frames.append(frame)
            display_frames.append(frame_display)
            print(f"Extracted frame {len(frames)} at position {cap.get(cv2.CAP_PROP_POS_FRAMES)}")

    cap.release()

    if len(frames) < max_frames:
        i = 0
        while len(frames) < max_frames:
            frames.append(frames[i])
            display_frames.append(display_frames[i])
            i += 1

    frames_to_prediction = np.expand_dims(frames, axis=0)
    return np.array(frames_to_prediction, dtype=np.float32), np.array(display_frames, dtype=np.float32)