| """ |
| Simple test client for the Cattle Breed Classifier API. |
| |
| Usage: |
| python test_client.py path/to/image.jpg |
| python test_client.py path/to/image.jpg --url http://localhost:8000 |
| """ |
|
|
| import sys |
| import argparse |
| import requests |
|
|
|
|
| def main(): |
| parser = argparse.ArgumentParser(description="Test the Cattle Breed Classifier API") |
| parser.add_argument("image_path", help="Path to an image file") |
| parser.add_argument("--url", default="http://localhost:8000", help="Base URL of the API") |
| args = parser.parse_args() |
|
|
| |
| health_resp = requests.get(f"{args.url}/health") |
| print("Health:", health_resp.json()) |
|
|
| with open(args.image_path, "rb") as f: |
| files = {"file": (args.image_path, f, "image/jpeg")} |
| resp = requests.post(f"{args.url}/predict", files=files) |
|
|
| if resp.status_code != 200: |
| print(f"Error {resp.status_code}: {resp.text}") |
| sys.exit(1) |
|
|
| result = resp.json() |
| print(f"\nPredicted label: {result['predicted_label']}") |
| print(f"Confidence: {result['confidence']:.4f}\n") |
|
|
| print("Top 5 probabilities:") |
| sorted_probs = sorted(result["probabilities"].items(), key=lambda x: x[1], reverse=True) |
| for label, prob in sorted_probs[:5]: |
| print(f" {label:25s} {prob:.4f}") |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|