File size: 2,034 Bytes
368b41d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
"""
src/utils.py

Helper utilities for loading images/videos and basic I/O.
"""
import os, io
import numpy as np
from PIL import Image

# Try to import cv2 for video loading
try:
    import cv2
except Exception as e:
    cv2 = None

def load_image_as_array(path, target_size=(224,224)):
    """
    Load image file and return numpy array resized to target_size.
    """
    img = Image.open(path).convert("RGB")
    img = img.resize(target_size)
    arr = np.asarray(img, dtype=np.uint8)
    return arr

def save_numpy_as_image(arr, out_path):
    img = Image.fromarray(arr.astype('uint8'))
    img.save(out_path)
    return out_path

def load_video_as_frames(path, max_frames=20, target_size=(224, 224)):
    """
    Load video file and return frames as numpy array.
    
    Args:
        path: Path to video file
        max_frames: Maximum number of frames to extract (0 = all frames)
        target_size: Target size for frames (default: (224, 224))
    
    Returns:
        numpy array of frames with shape (num_frames, H, W, 3)
    """
    if cv2 is None:
        raise RuntimeError("OpenCV (cv2) not available. Install opencv-python to use load_video_as_frames.")
    
    from src.model import load_video
    return load_video(path, max_frames=max_frames, resize=target_size)

def is_video_file(file_path):
    """
    Check if file is a video file based on extension.
    
    Args:
        file_path: Path to file
    
    Returns:
        True if file is a video, False otherwise
    """
    video_extensions = ['.mp4', '.avi', '.mov', '.mkv', '.flv', '.wmv', '.webm']
    return any(file_path.lower().endswith(ext) for ext in video_extensions)

def is_image_file(file_path):
    """
    Check if file is an image file based on extension.
    
    Args:
        file_path: Path to file
    
    Returns:
        True if file is an image, False otherwise
    """
    image_extensions = ['.jpg', '.jpeg', '.png', '.bmp', '.gif', '.tiff', '.webp']
    return any(file_path.lower().endswith(ext) for ext in image_extensions)