File size: 5,134 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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
#!/usr/bin/env python3
"""
Diagnostic script to check numpy installation and functionality.
This helps troubleshoot the "Numpy is not available" error.
"""
import sys
import traceback
def check_numpy_import():
"""Check if numpy can be imported."""
try:
import numpy as np
print(f"β Numpy imported successfully")
print(f"β Numpy version: {np.__version__}")
return np
except ImportError as e:
print(f"β Failed to import numpy: {e}")
return None
except Exception as e:
print(f"β Unexpected error importing numpy: {e}")
traceback.print_exc()
return None
def check_numpy_basic_operations(np):
"""Test basic numpy operations."""
if np is None:
return False
try:
# Test array creation
arr = np.array([1, 2, 3, 4, 5])
print(f"β Array creation works: {arr}")
# Test array operations
result = arr * 2
print(f"β Array operations work: {result}")
# Test float32 arrays (used in the video processing)
float_arr = np.array([[1, 2], [3, 4]], dtype=np.float32)
print(f"β Float32 arrays work: {float_arr}")
# Test stack operation (used in video processing)
stacked = np.stack([float_arr, float_arr], axis=0)
print(f"β Stack operation works, shape: {stacked.shape}")
return True
except Exception as e:
print(f"β Numpy basic operations failed: {e}")
traceback.print_exc()
return False
def check_numpy_with_pil():
"""Test numpy integration with PIL (used in video processing)."""
try:
import numpy as np
from PIL import Image
# Create a test image
test_image = Image.new('RGB', (224, 224), color='red')
print(f"β PIL Image created: {test_image}")
# Convert to numpy array (this is what fails in video processing)
frame_array = np.array(test_image, dtype=np.float32) / 255.0
print(f"β PIL to numpy conversion works, shape: {frame_array.shape}")
# Test the exact operation from the video processing code
frame_arrays = [frame_array, frame_array, frame_array]
video_array = np.stack(frame_arrays, axis=0)
print(f"β Video array stacking works, shape: {video_array.shape}")
return True
except ImportError as e:
print(f"β Missing dependency: {e}")
return False
except Exception as e:
print(f"β PIL-numpy integration failed: {e}")
traceback.print_exc()
return False
def check_torch_numpy_integration():
"""Test numpy integration with PyTorch."""
try:
import numpy as np
import torch
# Create numpy array
np_array = np.array([[[1, 2], [3, 4]]], dtype=np.float32)
print(f"β Numpy array created: shape {np_array.shape}")
# Convert to PyTorch tensor
tensor = torch.from_numpy(np_array)
print(f"β Torch tensor from numpy: shape {tensor.shape}")
# Test permute operation (used in video processing)
permuted = tensor.permute(2, 0, 1)
print(f"β Tensor permute works: shape {permuted.shape}")
return True
except ImportError as e:
print(f"β Missing dependency: {e}")
return False
except Exception as e:
print(f"β PyTorch-numpy integration failed: {e}")
traceback.print_exc()
return False
def main():
"""Run all diagnostic checks."""
print("=== Numpy Diagnostic Check ===\n")
# Check Python version
print(f"Python version: {sys.version}")
print(f"Python executable: {sys.executable}\n")
# Check numpy import
print("1. Checking numpy import...")
np = check_numpy_import()
print()
# Check basic operations
print("2. Checking basic numpy operations...")
basic_ok = check_numpy_basic_operations(np)
print()
# Check PIL integration
print("3. Checking PIL-numpy integration...")
pil_ok = check_numpy_with_pil()
print()
# Check PyTorch integration
print("4. Checking PyTorch-numpy integration...")
torch_ok = check_torch_numpy_integration()
print()
# Summary
print("=== Summary ===")
if np is not None and basic_ok and pil_ok and torch_ok:
print("β All checks passed! Numpy should work correctly.")
else:
print("β Some checks failed. This may explain the 'Numpy is not available' error.")
# Provide troubleshooting suggestions
print("\n=== Troubleshooting Suggestions ===")
if np is None:
print("- Reinstall numpy: pip install --force-reinstall numpy")
if not basic_ok:
print("- Numpy installation may be corrupted")
if not pil_ok:
print("- Check PIL/Pillow installation: pip install --upgrade Pillow")
if not torch_ok:
print("- Check PyTorch installation: pip install --upgrade torch")
print("- Try recreating your virtual environment")
print("- Check for conflicting package versions")
if __name__ == "__main__":
main()
|