|
| 1 | +import requests |
| 2 | + |
| 3 | +# API endpoint URL |
| 4 | +api_endpoint = "http://localhost:8000/detect/" |
| 5 | + |
| 6 | +# Test images folder |
| 7 | +test_images_folder = "test_images/" |
| 8 | + |
| 9 | +# Dictionary to store test status |
| 10 | +test_status = {} |
| 11 | + |
| 12 | +# Function to send a POST request to the detection API and log the test status |
| 13 | +def test_object_detection(image_path, label=None, test_number=0): |
| 14 | + # Prepare request payload |
| 15 | + files = {"image": open(image_path, "rb")} |
| 16 | + api_endpoint_with_label = f"{api_endpoint}?label={label}" if label else api_endpoint |
| 17 | + |
| 18 | + # Send POST request |
| 19 | + response = requests.post(api_endpoint_with_label, files=files) |
| 20 | + |
| 21 | + # Check response status |
| 22 | + if response.status_code == 200: |
| 23 | + # Successful response |
| 24 | + result = response.json() |
| 25 | + print("Test Image:", image_path) |
| 26 | + print("Detection Results:", result) |
| 27 | + |
| 28 | + # If no label specified |
| 29 | + if label is None: |
| 30 | + test_status[f"Test {test_number}"] = "Success" |
| 31 | + print("Success!") |
| 32 | + # If objects are detected with the specified label |
| 33 | + elif result["count"] > 0 and result["objects"][0]["label"] == label: |
| 34 | + test_status[f"Test {test_number}"] = "Success" |
| 35 | + print("Success!") |
| 36 | + else: |
| 37 | + # If the test fails |
| 38 | + test_status[f"Test {test_number}"] = "Failure" |
| 39 | + print("Failure!!!") |
| 40 | + else: |
| 41 | + # If the API request fails |
| 42 | + print("API request failed:", response.text) |
| 43 | + |
| 44 | +# Test scenario 1: Detect objects without specifying a label |
| 45 | +test_object_detection(test_images_folder + "test_image1_bus_people.jpg", label=None, test_number=1) |
| 46 | + |
| 47 | +# Test scenario 2: Detect only "bird" objects |
| 48 | +test_object_detection(test_images_folder + "test_image2_bird.jpg", label="bird", test_number=2) |
| 49 | + |
| 50 | +# Test scenario 3: Detect only "dog" objects |
| 51 | +test_object_detection(test_images_folder + "test_image3_dog.jpg", label="dog", test_number=3) |
| 52 | + |
| 53 | +# Print test status |
| 54 | +print("Test Status:", test_status) |
| 55 | + |
| 56 | + |
0 commit comments