File size: 4,633 Bytes
eb09c29 |
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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
#!/usr/bin/env python3
"""
Test script to verify the video processing fix works correctly.
This script tests the predict_actions function with different scenarios.
"""
import sys
import tempfile
from pathlib import Path
import logging
# Configure logging to see debug output
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
try:
from predict import predict_actions, _read_video_frames, load_model
print("β Successfully imported predict functions")
except ImportError as e:
print(f"β Failed to import predict functions: {e}")
sys.exit(1)
def create_test_video(output_path: Path, duration: int = 2, fps: int = 10):
"""Create a simple test video using OpenCV."""
try:
import cv2
import numpy as np
except ImportError:
print("OpenCV not available for creating test video")
return False
# Create a simple test video with moving rectangle
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(str(output_path), fourcc, fps, (224, 224))
total_frames = duration * fps
for i in range(total_frames):
# Create frame with moving rectangle
frame = np.zeros((224, 224, 3), dtype=np.uint8)
x_pos = int(50 + 100 * (i / total_frames))
cv2.rectangle(frame, (x_pos, 50), (x_pos + 50, 150), (0, 255, 0), -1)
out.write(frame)
out.release()
return True
def test_frame_reading(video_path: Path):
"""Test frame reading functionality."""
print(f"\n--- Testing frame reading from {video_path.name} ---")
try:
frames = _read_video_frames(video_path, num_frames=8)
print(f"β Successfully read {len(frames)} frames")
# Check frame properties
if frames:
frame = frames[0]
print(f"β Frame size: {frame.size}")
print(f"β Frame mode: {frame.mode}")
# Check all frames have same size
sizes = [f.size for f in frames]
if len(set(sizes)) == 1:
print("β All frames have consistent size")
else:
print(f"β Inconsistent frame sizes: {set(sizes)}")
return True
except Exception as e:
print(f"β Frame reading failed: {e}")
return False
def test_model_loading():
"""Test model loading functionality."""
print("\n--- Testing model loading ---")
try:
processor, model, device = load_model()
print(f"β Successfully loaded model on device: {device}")
print(f"β Model config num_frames: {getattr(model.config, 'num_frames', 'Not specified')}")
return True, (processor, model, device)
except Exception as e:
print(f"β Model loading failed: {e}")
return False, (None, None, None)
def test_prediction(video_path: Path):
"""Test full prediction pipeline."""
print(f"\n--- Testing prediction on {video_path.name} ---")
try:
predictions = predict_actions(str(video_path), top_k=3)
print(f"β Successfully got {len(predictions)} predictions")
for i, (label, score) in enumerate(predictions, 1):
print(f" {i}. {label}: {score:.4f} ({score*100:.2f}%)")
return True
except Exception as e:
print(f"β Prediction failed: {e}")
import traceback
traceback.print_exc()
return False
def main():
print("π§ͺ Starting Video Action Recognition Test Suite")
# Test 1: Model loading
model_loaded, _ = test_model_loading()
if not model_loaded:
print("β Model loading failed - cannot continue tests")
return
# Test 2: Create test video
with tempfile.TemporaryDirectory() as tmp_dir:
test_video_path = Path(tmp_dir) / "test_video.mp4"
print(f"\n--- Creating test video at {test_video_path} ---")
if create_test_video(test_video_path):
print("β Test video created successfully")
# Test 3: Frame reading
if test_frame_reading(test_video_path):
print("β Frame reading test passed")
else:
print("β Frame reading test failed")
return
# Test 4: Full prediction
if test_prediction(test_video_path):
print("β
All tests passed! The fix is working correctly.")
else:
print("β Prediction test failed")
else:
print("β Could not create test video, skipping video-based tests")
print("π‘ Try testing with an existing video file")
if __name__ == "__main__":
main()
|