|
|
|
|
|
""" |
|
|
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: |
|
|
|
|
|
arr = np.array([1, 2, 3, 4, 5]) |
|
|
print(f"β Array creation works: {arr}") |
|
|
|
|
|
|
|
|
result = arr * 2 |
|
|
print(f"β Array operations work: {result}") |
|
|
|
|
|
|
|
|
float_arr = np.array([[1, 2], [3, 4]], dtype=np.float32) |
|
|
print(f"β Float32 arrays work: {float_arr}") |
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
test_image = Image.new('RGB', (224, 224), color='red') |
|
|
print(f"β PIL Image created: {test_image}") |
|
|
|
|
|
|
|
|
frame_array = np.array(test_image, dtype=np.float32) / 255.0 |
|
|
print(f"β PIL to numpy conversion works, shape: {frame_array.shape}") |
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
np_array = np.array([[[1, 2], [3, 4]]], dtype=np.float32) |
|
|
print(f"β Numpy array created: shape {np_array.shape}") |
|
|
|
|
|
|
|
|
tensor = torch.from_numpy(np_array) |
|
|
print(f"β Torch tensor from numpy: shape {tensor.shape}") |
|
|
|
|
|
|
|
|
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") |
|
|
|
|
|
|
|
|
print(f"Python version: {sys.version}") |
|
|
print(f"Python executable: {sys.executable}\n") |
|
|
|
|
|
|
|
|
print("1. Checking numpy import...") |
|
|
np = check_numpy_import() |
|
|
print() |
|
|
|
|
|
|
|
|
print("2. Checking basic numpy operations...") |
|
|
basic_ok = check_numpy_basic_operations(np) |
|
|
print() |
|
|
|
|
|
|
|
|
print("3. Checking PIL-numpy integration...") |
|
|
pil_ok = check_numpy_with_pil() |
|
|
print() |
|
|
|
|
|
|
|
|
print("4. Checking PyTorch-numpy integration...") |
|
|
torch_ok = check_torch_numpy_integration() |
|
|
print() |
|
|
|
|
|
|
|
|
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.") |
|
|
|
|
|
|
|
|
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() |
|
|
|