|
|
|
|
|
""" |
|
|
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 |
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
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): |
|
|
|
|
|
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") |
|
|
|
|
|
|
|
|
if frames: |
|
|
frame = frames[0] |
|
|
print(f"β Frame size: {frame.size}") |
|
|
print(f"β Frame mode: {frame.mode}") |
|
|
|
|
|
|
|
|
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") |
|
|
|
|
|
|
|
|
model_loaded, _ = test_model_loading() |
|
|
if not model_loaded: |
|
|
print("β Model loading failed - cannot continue tests") |
|
|
return |
|
|
|
|
|
|
|
|
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") |
|
|
|
|
|
|
|
|
if test_frame_reading(test_video_path): |
|
|
print("β Frame reading test passed") |
|
|
else: |
|
|
print("β Frame reading test failed") |
|
|
return |
|
|
|
|
|
|
|
|
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() |
|
|
|