69 lines
3.4 KiB
Python
69 lines
3.4 KiB
Python
"""Repeat one Laya vision question against the same captured image."""
|
|
import argparse
|
|
import datetime
|
|
import hashlib
|
|
import json
|
|
from pathlib import Path
|
|
import statistics
|
|
import time
|
|
import urllib.request
|
|
import uuid
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument("image", type=Path)
|
|
parser.add_argument("--count", type=int, default=5)
|
|
parser.add_argument("--endpoint", default="http://127.0.0.1:8000/v1/vision/decide")
|
|
parser.add_argument("--question", default="图中打开的棕色纸盒里面是否为空?只检查纸盒内部,不考虑盒子外的物体。空表示盒内没有任何物品。")
|
|
parser.add_argument("--output", type=Path, required=True)
|
|
args = parser.parse_args()
|
|
if args.count < 1:
|
|
parser.error("count must be positive")
|
|
image = args.image.read_bytes()
|
|
if not 0 < len(image) <= 10 * 1024 * 1024:
|
|
parser.error("image must be between 1 byte and 10MB")
|
|
questions = {"box_empty": {"type": "noul", "instructions": args.question}}
|
|
boundary = uuid.uuid4().hex
|
|
body = (
|
|
f'--{boundary}\r\nContent-Disposition: form-data; name="questions"\r\n\r\n'.encode()
|
|
+ json.dumps(questions, ensure_ascii=False).encode("utf-8")
|
|
+ f'\r\n--{boundary}\r\nContent-Disposition: form-data; name="file"; filename="capture.jpg"\r\nContent-Type: image/jpeg\r\n\r\n'.encode()
|
|
+ image
|
|
+ f"\r\n--{boundary}--\r\n".encode()
|
|
)
|
|
report = {
|
|
"created_at": datetime.datetime.now(datetime.timezone.utc).isoformat(),
|
|
"endpoint": args.endpoint, "image": str(args.image.resolve()),
|
|
"image_bytes": len(image), "image_sha256": hashlib.sha256(image).hexdigest(),
|
|
"questions": questions, "timing_scope": "HTTP request through complete response read; excludes capture and multipart preparation",
|
|
"runs": [],
|
|
}
|
|
args.output.parent.mkdir(parents=True, exist_ok=True)
|
|
for index in range(1, args.count + 1):
|
|
request = urllib.request.Request(args.endpoint, data=body, headers={"Content-Type": f"multipart/form-data; boundary={boundary}"})
|
|
started = time.perf_counter()
|
|
with urllib.request.urlopen(request, timeout=180) as response:
|
|
payload = response.read()
|
|
http_ms = (time.perf_counter() - started) * 1000
|
|
result = json.loads(payload)
|
|
answer = result["answers"]["box_empty"]
|
|
probability = answer["noul"]
|
|
run = {"run": index, "http_ms": round(http_ms, 2), "server_ms": result["latency_ms"],
|
|
"inference_ms": result["inference_ms"], "empty_probability": probability,
|
|
"prediction": "空" if probability >= .5 else "非空", "response": result}
|
|
report["runs"].append(run)
|
|
args.output.write_text(json.dumps(report, ensure_ascii=False, indent=2), encoding="utf-8")
|
|
print(json.dumps({key: value for key, value in run.items() if key != "response"}, ensure_ascii=False), flush=True)
|
|
report["summary"] = {
|
|
key: {"mean": round(statistics.mean(run[key] for run in report["runs"]), 2),
|
|
"min": min(run[key] for run in report["runs"]), "max": max(run[key] for run in report["runs"])}
|
|
for key in ("http_ms", "server_ms", "inference_ms")
|
|
}
|
|
args.output.write_text(json.dumps(report, ensure_ascii=False, indent=2), encoding="utf-8")
|
|
print(json.dumps(report["summary"], ensure_ascii=False), flush=True)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|