File size: 2,195 Bytes
30e6c9e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""
edge_client.py — Lightweight Edge Client for PV Defect Classifier API
======================================================================
Simulates an edge device (e.g. Raspberry Pi, industrial controller) sending
a PV cell image to the cloud inference API and printing the result.

Usage:
    python edge_client.py --image path/to/cell.png
    python edge_client.py --image path/to/cell.png --host https://hakimi233-pv-classifier.hf.space
"""

import argparse
import sys
import time

import requests

DEFAULT_HOST = "https://hakimi233-pv-classifier.hf.space"


def classify(image_path: str, host: str) -> None:
    url = f"{host.rstrip('/')}/predict"

    with open(image_path, "rb") as f:
        files = {"file": (image_path, f, "image/jpeg")}
        print(f"[edge_client] Sending: {image_path}")
        print(f"[edge_client] Endpoint: {url}")

        t0 = time.time()
        response = requests.post(url, files=files, timeout=30)
        round_trip_ms = (time.time() - t0) * 1000

    if response.status_code != 200:
        print(f"[edge_client] ERROR: HTTP {response.status_code}")
        print(response.text)
        sys.exit(1)

    data = response.json()
    if "error" in data:
        print(f"[edge_client] API error: {data['error']}")
        sys.exit(1)

    print("\n========== Prediction Result ==========")
    print(f"  Prediction  : {data['prediction']}")
    print(f"  Confidence  : {data['confidence']}%")
    print(f"  Inference   : {data['latency_ms']} ms  (server-side)")
    print(f"  Round-trip  : {round_trip_ms:.1f} ms  (including network)")
    print(f"  Network OH  : {round_trip_ms - data['latency_ms']:.1f} ms")
    print("=======================================\n")

    for cls, pct in data.get("probabilities", {}).items():
        bar = "#" * int(pct / 5)
        print(f"  {cls:<12} {bar:<20} {pct}%")


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Edge client for PV Defect Classifier")
    parser.add_argument("--image", required=True, help="Path to PV cell image")
    parser.add_argument("--host", default=DEFAULT_HOST, help="API host URL")
    args = parser.parse_args()

    classify(args.image, args.host)