File size: 2,690 Bytes
a37d0db | 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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | """
POST a video (or image) to /api/detect.
Usage (FastAPI running):
python test_detect_api.py
python test_detect_api.py --video "d:\\path\\to\\file.mp4"
"""
import argparse
import glob
import os
import sys
import requests
import config
def main():
parser = argparse.ArgumentParser(description="Test POST /api/detect with video or image")
parser.add_argument("--video", default=None, help="Path to MP4/video file")
parser.add_argument("--image", default=None, help="Path to a single crop image (legacy)")
parser.add_argument("--camera-id", default=None)
parser.add_argument(
"--endpoint",
default=getattr(config, "API_ENDPOINT", "http://localhost:8000/api/detect"),
)
parser.add_argument("--max-triggers", type=int, default=5, help="Limit vehicles for test")
args = parser.parse_args()
camera_id = args.camera_id or getattr(config, "CAMERA_ID", "")
if not camera_id:
print("CAMERA_ID empty. Run: python register_camera.py --write-config")
sys.exit(1)
path = args.video
content_type = "video/mp4"
if not path:
if args.image:
path = args.image
content_type = "image/jpeg"
else:
path = getattr(config, "CAMERA_URL", None)
if path and os.path.isfile(str(path)) and str(path).lower().endswith(
(".mp4", ".avi", ".mov", ".mkv")
):
content_type = "video/mp4"
else:
candidates = sorted(glob.glob(os.path.join(config.OUTPUT_DIR, "plate_*.jpg")))
if candidates:
path = candidates[-1]
content_type = "image/jpeg"
else:
print("Provide --video or set CAMERA_URL to an mp4 in config.py")
sys.exit(1)
if not os.path.isfile(path):
print(f"File not found: {path}")
sys.exit(1)
print(f"POST {args.endpoint}")
print(f" file : {path}")
print(f" type : {content_type}")
print(f" camera_id : {camera_id}")
print(f" max_triggers: {args.max_triggers}")
with open(path, "rb") as f:
resp = requests.post(
args.endpoint,
files={"file": (os.path.basename(path), f, content_type)},
data={"camera_id": camera_id, "max_triggers": str(args.max_triggers)},
timeout=getattr(config, "API_TIMEOUT", 600),
)
print(f"\nStatus: {resp.status_code}")
print(resp.text[:5000])
sys.exit(0 if resp.status_code == 200 else 1)
if __name__ == "__main__":
main()
|