Skip to content
Merged
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
124 changes: 124 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,124 @@
==============
Display capture Twister harness
==============


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

.. code-block:: console

case_config:
device_id: 0 # Try different camera indices
res_x: 1280 # x resolution
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: ["tests.drivers.display.check.shield"]
plugins:
- name: signature
module: plugins.signature_plugin
class: VideoSignaturePlugin
status: "enable"
config:
operations: "compare" # operation ('generate', 'compare')
metadata:
name: "tests.drivers.display.check.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 connected to the same PC host for flashing and serial console

2. Generate video fingerprints

- build and flash the known-to-work display app 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
```


- follow the instructions in the repo's README.
- 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: "tests.drivers.display.check.shield" # finger-print stored metadata
platform: "frdm_mcxn947"
directory: "./fingerprints" # fingerprints directory to compare with not used in generate mode
```

- Run generate fingerprints program outside the camera_shield folder

Note:
On Ubuntu 24.04, you may need to do ```export QT_QPA_PLATFORM=xcb``` to resolve below error

```bash
qt.qpa.plugin: Could not find the Qt platform plugin "wayland" in "~/camera_shield/.ven/lib/python3.12/site-packages/cv2/qt/plugins"
```

```bash
python -m camera_shield.main --config camera_shield/config.yaml
```

video fingerprint for captured screenshots 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=<path to "fingerprints" parent-folder>

# Twister hardware map file settings:
# Ensure your map file has the required fixture
# in the example below, you need to have "fixture_display"

# Ensure you have installed the required Python packages for tests in scripts/requirements-run-test.txt

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

```

Notes
-----

1. When generating the fingerprints, they will be stored in folder "name" as defined in "metadata" from ``config.yaml`` .
2. The DUT testcase name shall match the value in the metadata 'name' field of the captured fingerprint's config.
3. You can put multiple 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: tests.drivers.display.check.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: [tests.drivers.display.check.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", 20)

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