|
|
""" |
|
|
Test script to verify error handling in the object detection functionality. |
|
|
""" |
|
|
|
|
|
from PIL import Image |
|
|
import pandas as pd |
|
|
import app |
|
|
from app import detect_objects, initialize_model |
|
|
|
|
|
def test_with_none_model(): |
|
|
"""Test detection when model is None.""" |
|
|
print("Testing with None model...") |
|
|
|
|
|
|
|
|
original_model = app.model |
|
|
app.model = None |
|
|
|
|
|
|
|
|
test_img = Image.new('RGB', (100, 100), 'white') |
|
|
|
|
|
|
|
|
annotated_img, detection_df = detect_objects(test_img) |
|
|
|
|
|
|
|
|
print(f"Returned image type: {type(annotated_img)}") |
|
|
print(f"Returned DataFrame shape: {detection_df.shape}") |
|
|
print(f"DataFrame columns: {list(detection_df.columns)}") |
|
|
print(f"DataFrame empty: {detection_df.empty}") |
|
|
|
|
|
|
|
|
app.model = original_model |
|
|
print("Model restored\n") |
|
|
|
|
|
def test_with_corrupted_image(): |
|
|
"""Test detection with edge case images.""" |
|
|
print("Testing with very small image...") |
|
|
|
|
|
|
|
|
tiny_img = Image.new('RGB', (1, 1), 'black') |
|
|
|
|
|
|
|
|
annotated_img, detection_df = detect_objects(tiny_img) |
|
|
|
|
|
print(f"Small image - Detections: {len(detection_df)}") |
|
|
expected_columns = ['class', 'confidence', 'x1', 'y1', 'x2', 'y2'] |
|
|
print(f"Small image - DataFrame columns: {list(detection_df.columns)}") |
|
|
print(f"Small image - DataFrame columns correct: {list(detection_df.columns) == expected_columns}") |
|
|
|
|
|
print("Testing with large image...") |
|
|
|
|
|
|
|
|
large_img = Image.new('RGB', (2000, 2000), 'blue') |
|
|
|
|
|
|
|
|
annotated_img, detection_df = detect_objects(large_img) |
|
|
|
|
|
print(f"Large image - Detections: {len(detection_df)}") |
|
|
print(f"Large image - DataFrame columns: {list(detection_df.columns)}") |
|
|
print(f"Large image - DataFrame columns correct: {list(detection_df.columns) == expected_columns}") |
|
|
|
|
|
def main(): |
|
|
print("Testing error handling scenarios...\n") |
|
|
|
|
|
|
|
|
if not initialize_model(): |
|
|
print("Failed to initialize model for testing") |
|
|
return |
|
|
|
|
|
test_with_none_model() |
|
|
test_with_corrupted_image() |
|
|
|
|
|
print("Error handling tests completed!") |
|
|
|
|
|
if __name__ == "__main__": |
|
|
main() |