Skip to content

add display test harness #91597

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions scripts/pylib/display-twister-harness/camera_shield/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
==============
Display capture Twister harness
==============


Configuration example
---------------------

```yaml: config.yaml
case_config:
device_id: 0 # Try different camera indices
res_x: 1280 # x reslution
res_y: 720 # y resolution
fps: 30 # analysis frame pre-second
run_time: 20 # Run for 20 seconds
tests:
timeout: 30 # second wait for prompt string
prompt: "screen starts" # prompt to show the test start
expect: ["sample.display.shield"]
plugins:
- name: signature
module: plugins.signature_plugin
class: VideoSignaturePlugin
status: "enable"
config:
operations: "compare" # operation ('generate', 'compare')
metadata:
name: "sample.display.shield" # finger-print stored metadata
platform: "frdm_mcxn947"
directory: "./fingerprints" # fingerprints directory to compare with not used in generate mode
duration: 100 # number of frames to check
method: "combined" #Signature method ('phash', 'dhash', 'histogram', 'combined')
threshold: 0.65
phash_weight: 0.35
dhash_weight: 0.25
histogram_weight: 0.2
edge_ratio_weight: 0.1
gradient_hist_weight: 0.1
```

example zephyr display tests
----------------------------

1. Setup camera to capture display content

- UVC compatible camera with at least 2 megapixels (such as 1080p)
- a light-blocking black curtain
- a PC host where camera connect to
- DUT connect to same PC host for flashing and console uart

2. Generate video fingerprints

- build and flash the known to work firmware to DUT
e.g.
```
west build -b frdm_mcxn947/mcxn947/cpu0 tests/drivers/display/display_check
west flash
```

- clone code
```bash
git clone https://github.com/hakehuang/camera_shield
```

- set the signature capture mode as below in config.yaml
```yaml
- name: signature
module: .plugins.signature_plugin
class: VideoSignaturePlugin
status: "enable"
config:
operations: "generate" # operation ('generate', 'compare')
metadata:
name: "test.display.shield" # finger-print stored metadata
platform: "frdm_mcxn947"
directory: "./fingerprints" # fingerprints directory to compare with not used in generate mode
```

- Run generate fingerprints program
```bash
python -m camera_shield.main --config camera_shield/config.yaml
```

video fingerprint for given screen shots will be recorded in directory './fingerprints' by default

- set environment variable to "DISPLAY_TEST_DIR"

```bash
DISPLAY_TEST_DIR=~/camera_shield/
```

3. Run test
```bash
# export the fingerprints path
export DISPLAY_TEST_DIR=<your path with fingerprints subfolder inside>

# map file settings
# ensure your map file has the required fixture
# in below example you need have "fixture_display"

# Run detection program
scripts/twister --device-testing --hardware-map ~/frdm_mcxn947/map.yaml -T tests/drivers/display/display_check/

```

Notes
-----

1. when generating the fingerprints, they will stored in "name" as defined in "metadata" from ``config.yaml`` .
2. the DUT case will match the name with captured one.
3. you can put mutliply fingerprints in one folder, it will increase compare time,
but will help to check other defects.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Copyright 2025 NXP
#
# SPDX-License-Identifier: Apache-2.0
22 changes: 22 additions & 0 deletions scripts/pylib/display-twister-harness/camera_shield/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
case_config: {device_id: 0, fps: 30, res_y: 720, res_x: 1280, run_time: 20}
plugins:
- class: VideoSignaturePlugin
config:
dhash_weight: 0.25
directory: ${DISPLAY_TEST_DIR}/./fingerprints
duration: 100
edge_ratio_weight: 0.1
gradient_hist_weight: 0.1
histogram_weight: 0.2
metadata: {name: test.display.shield, platform: frdm_mcxn947}
method: combined
operations: compare
phash_weight: 0.35
threshold: 0.65
module: .plugins.signature_plugin
name: signature
status: enable
tests:
expect: [sample.display.shield]
prompt: screen starts
timeout: 30
120 changes: 120 additions & 0 deletions scripts/pylib/display-twister-harness/camera_shield/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
# Copyright (c) 2025 NXP
#
# SPDX-License-Identifier: Apache-2.0

import importlib
import io
import os
import sys
import time
from string import Template

import cv2
import yaml

from camera_shield.uvc_core.camera_controller import UVCCamera
from camera_shield.uvc_core.plugin_base import PluginManager

sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')


class Application:
def __init__(self, config_path="config.yaml"):
def resolve_env_vars(yaml_dict):
"""Process yaml with Template strings for safer environment variable resolution."""
if isinstance(yaml_dict, dict):
return {k: resolve_env_vars(v) for k, v in yaml_dict.items()}
elif isinstance(yaml_dict, list):
return [resolve_env_vars(i) for i in yaml_dict]
elif isinstance(yaml_dict, str):
# Create a template and substitute environment variables
template = Template(yaml_dict)
return template.safe_substitute(os.environ)
else:
return yaml_dict

self.active_plugins = {} # Initialize empty plugin dictionary
with open(config_path, encoding="utf-8-sig") as f:
config = yaml.safe_load(f)
self.config = resolve_env_vars(config)

os.environ["DISPLAY"] = ":0"

self.case_config = {
"device_id": 0,
"res_x": 1280,
"res_y": 720,
"fps": 30,
"run_time": 20,
}

if "case_config" in self.config:
self.case_config["device_id"] = self.config["case_config"].get("device_id", 0)
self.case_config["res_x"] = self.config["case_config"].get("res_x", 1280)
self.case_config["res_y"] = self.config["case_config"].get("res_y", 720)
self.case_config["fps"] = self.config["case_config"].get("fps", 30)
self.case_config["run_time"] = self.config["case_config"].get("run_time", 30)

self.camera = UVCCamera(self.case_config)
self.plugin_manager = PluginManager()
self.load_plugins()
self.results = []

def load_plugins(self):
for plugin_cfg in self.config["plugins"]:
if plugin_cfg.get("status", "disable") == "disable":
continue
module = importlib.import_module(plugin_cfg["module"], package=__package__)
plugin_class = getattr(module, plugin_cfg["class"])
self.active_plugins[plugin_cfg["name"]] = plugin_class(
plugin_cfg["name"], plugin_cfg.get("config", {})
)
self.plugin_manager.register_plugin(plugin_cfg["name"], plugin_class)

def handle_results(self, results, frame):
for name, plugin in self.active_plugins.items():
if name in results:
plugin.handle_results(results[name], frame)

def shutdown(self):
self.camera.release()
for plugin in self.active_plugins.values():
self.results += plugin.shutdown()

def run(self):
try:
start_time = time.time()
self.camera.initialize()
for name, plugin in self.active_plugins.items(): # noqa: B007
plugin.initialize()
while True:
ret, frame = self.camera.get_frame()
if not ret:
continue

# Maintain OpenCV event loop
if cv2.waitKey(1) == 27: # ESC key
break

results = {}
for name, plugin in self.active_plugins.items():
results[name] = plugin.process_frame(frame)

self.handle_results(results, frame)
self.camera.show_frame(frame)
frame_delay = 1 / self.case_config["fps"]
if time.time() - start_time > self.case_config["run_time"]:
break
time.sleep(frame_delay)

except KeyboardInterrupt:
print("quit by key input\n")
finally:
self.shutdown()

return self.results


if __name__ == "__main__":
app = Application()
app.run()
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Copyright 2025 NXP
#
# SPDX-License-Identifier: Apache-2.0
Loading
Loading