Skip to content

Commit

Permalink
Added GUI test and video test. (#419)
Browse files Browse the repository at this point in the history
  • Loading branch information
ATATC committed Oct 2, 2024
1 parent eeda7f1 commit 5594aaf
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 2 deletions.
7 changes: 5 additions & 2 deletions leads_vec/__entry__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
from sys import exit as _exit, version as _version
from warnings import filterwarnings as _filterwarnings

from leads import L as _L
from leads import L as _L, register_controller as _register_controller, Controller as _Controller, \
MAIN_CONTROLLER as _MAIN_CONTROLLER
from leads_gui.system import get_system_kernel as _get_system_kernel
from leads_vec.run import run

Expand All @@ -22,7 +23,7 @@ def __entry__() -> None:
parser = _ArgumentParser(prog="LEADS VeC", description="Lightweight Embedded Assisted Driving System VeC",
epilog="Project Neura: https://projectneura.org\n"
"GitHub: https://github.com/ProjectNeura/LEADS")
parser.add_argument("action", choices=("info", "replay", "run"))
parser.add_argument("action", choices=("info", "replay", "benchmark", "run"))
parser.add_argument("-c", "--config", default=None, help="specify a configuration file")
parser.add_argument("-d", "--devices", default=f"{MODULE_PATH}/devices.py", help="specify a devices module")
parser.add_argument("-m", "--main", default=f"{MODULE_PATH}/cli.py", help="specify a main module")
Expand Down Expand Up @@ -60,6 +61,8 @@ def __entry__() -> None:
args.emu = False
_L.debug("Replay mode enabled")
case "benchmark":
_register_controller(_MAIN_CONTROLLER, _Controller())
args.devices = f"{MODULE_PATH}/benchmark.py"
args.main = f"{MODULE_PATH}/benchmark.py"
_L.debug("Benchmark mode enabled")
_exit(run(parse_path(args.config), parse_path(args.devices), parse_path(args.main), args.register,
Expand Down
88 changes: 88 additions & 0 deletions leads_vec/benchmark.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,90 @@
from base64 import b64encode
from io import BytesIO
from time import time
from typing import Callable

from PIL.Image import open
from customtkinter import CTkLabel, DoubleVar
from cv2 import VideoCapture, imencode, IMWRITE_JPEG_QUALITY

from leads import L
from leads_gui import RuntimeData, Window, ContextManager, Speedometer


def video_tester(container: Callable[[], None]) -> float:
start = time()
sum_delay = 0
i = 0
while True:
if (t := time()) - start > 10:
return sum_delay / i
container()
sum_delay += time() - t
i += 1


def video_test() -> dict[str, float]:
r = {}
vc = VideoCapture(1)
if not vc.isOpened():
L.error("No camera available")
return r

def test1() -> None:
vc.read()

def test2() -> None:
_, frame = vc.read()
imencode(".jpg", frame, (IMWRITE_JPEG_QUALITY, 90))[1].tobytes()

def test3() -> None:
_, frame = vc.read()
im = imencode(".jpg", frame, (IMWRITE_JPEG_QUALITY, 90))[1].tobytes()
b64encode(im)

def test4() -> None:
_, frame = vc.read()
im = imencode(".jpg", frame, (IMWRITE_JPEG_QUALITY, 90))[1].tobytes()
open(BytesIO(im))

r["video capture"] = video_tester(test1) * 1000
r["video capture and encoding"] = video_tester(test2) * 1000
r["video capture and Base64 encoding"] = video_tester(test3) * 1000
r["video capture and PIL"] = video_tester(test4) * 1000
return r


class Callbacks(object):
def __init__(self) -> None:
self.t: float = time()
self.speed: DoubleVar | None = None

def on_refresh(self, window: Window) -> None:
self.speed.set((d := time() - self.t) * 20)
if d > 10:
window.kill()


def main() -> int:
report = {}
L.info("GUI test starting, this takes about 10 seconds")
rd = RuntimeData()
callbacks = Callbacks()
w = Window(800, 256, 30, rd, callbacks.on_refresh, "Benchmark", no_title_bar=False)
callbacks.speed = DoubleVar(w.root())
uim = ContextManager(w)
uim.layout([[CTkLabel(w.root(), text="Benchmark Ongoing", height=240),
Speedometer(w.root(), height=240, variable=callbacks.speed)]])
uim.show()
L.info("GUI test complete")
L.info("Video test starting, this takes about 40 seconds")
report["frame rate"] = w.frame_rate()
report["net delay"] = w.net_delay() * 1000
report.update(video_test())
L.info("Video test complete")
for k, v in report.items():
L.info(f"{k}: {v:.3f}")
return 0


_: None = None

0 comments on commit 5594aaf

Please sign in to comment.