File size: 1,321 Bytes
a4d3de8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
"""
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 check first
    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()