CoolFace
Apppublic

AtharvaRJ/few-shot-object-identification

sourceHugging Faceupdated 3mo agoView on Hugging Face
0likes
test_api.py62 linesDownload Raw Back to src
1"""2test_api.py3 4Quick manual test script for the running FastAPI service.5Run the API first (uvicorn api:app --reload --port 8000), then run this.6"""7 8import requests9from pathlib import Path10 11BASE_URL = "http://127.0.0.1:8000"12DATA_ROOT = Path("../data/caltech256/256_ObjectCategories")  # relative to src/13 14 15def add_class(name: str, description: str, folder: str, image_filenames: list[str]):16    folder_path = DATA_ROOT / folder17    files = [18        ("images", (fname, open(folder_path / fname, "rb"), "image/jpeg"))19        for fname in image_filenames20    ]21    data = {"name": name, "description": description}22    response = requests.post(f"{BASE_URL}/add_class", data=data, files=files)23    print(f"\n--- add_class('{name}') ---")24    print(response.status_code, response.json())25 26 27def predict(folder: str, image_filename: str):28    folder_path = DATA_ROOT / folder29    files = {"image": (image_filename, open(folder_path / image_filename, "rb"), "image/jpeg")}30    response = requests.post(f"{BASE_URL}/predict", files=files)31    print(f"\n--- predict('{image_filename}') ---")32    print(response.status_code, response.json())33 34 35def drift_report():36    response = requests.get(f"{BASE_URL}/drift_report")37    print(f"\n--- drift_report ---")38    print(response.status_code, response.json())39 40 41if __name__ == "__main__":42    # Register beer mug with 3 example images (pick filenames that exist in your folder)43    add_class(44        name="beer mug",45        description="a beer mug",46        folder="010.beer-mug",47        image_filenames=["010_0001.jpg", "010_0002.jpg", "010_0003.jpg"],48    )49 50    # Register coffee mug51    add_class(52        name="coffee mug",53        description="a coffee mug",54        folder="041.coffee-mug",55        image_filenames=["041_0001.jpg", "041_0002.jpg", "041_0003.jpg"],56    )57 58    # Test prediction on a held-out beer mug image (use a filename NOT in the list above)59    predict(folder="010.beer-mug", image_filename="010_0010.jpg")60 61    # Check drift report62    drift_report()