-
Notifications
You must be signed in to change notification settings - Fork 234
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1064 from luxonis/develop
DepthAI SDK 1.12.0
- Loading branch information
Showing
33 changed files
with
603 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
depthai_sdk/examples/CameraComponent/camera_control_with_nn.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from depthai_sdk import OakCamera | ||
|
||
with OakCamera() as oak: | ||
color = oak.create_camera('color') | ||
face_det = oak.create_nn('face-detection-retail-0004', color) | ||
# Control the camera's exposure/focus based on the (largest) detected face | ||
color.control_with_nn(face_det, auto_focus=True, auto_exposure=True, debug=False) | ||
|
||
oak.visualize(face_det, fps=True) | ||
oak.start(blocking=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 5 additions & 3 deletions
8
depthai_sdk/sdk_tests/components/stereo/test_stereo_component.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,38 @@ | ||
import os | ||
import subprocess | ||
import sys | ||
import time | ||
from pathlib import Path | ||
|
||
import cv2 | ||
import pytest | ||
|
||
EXAMPLES_DIR = Path(__file__).parent.parent.parent / "examples" | ||
EXAMPLES_DIR = Path(__file__).parents[1] / 'examples' | ||
|
||
# Create a temporary directory for the tests | ||
Path('/tmp/depthai_sdk_tests').mkdir(exist_ok=True) | ||
os.chdir('/tmp/depthai_sdk_tests') | ||
|
||
def test_examples(): | ||
python_executable = Path(sys.executable) | ||
for example in EXAMPLES_DIR.rglob("**/*.py"): | ||
print(f"Running example: {example.name}") | ||
|
||
result = subprocess.Popen(f"{python_executable} {example}", stdout=subprocess.PIPE, stderr=subprocess.PIPE, | ||
env={"DISPLAY": ""}, shell=True) | ||
|
||
time.sleep(5) | ||
result.kill() | ||
time.sleep(5) | ||
print('Stderr: ', result.stderr.read().decode()) | ||
|
||
# if result.returncode and result.returncode != 0: | ||
# assert False, f"{example} raised an exception: {result.stderr}" | ||
|
||
cv2.destroyAllWindows() | ||
@pytest.mark.parametrize('example', list(EXAMPLES_DIR.rglob("**/*.py"))) | ||
def test_examples(example): | ||
print(f"Running {example}") | ||
python_executable = Path(sys.executable) | ||
result = subprocess.Popen(f"{python_executable} {example}", | ||
stdout=subprocess.PIPE, | ||
stderr=subprocess.PIPE, | ||
env={ | ||
'DISPLAY': '', | ||
'PYTHONPATH': f'{os.environ["PYTHONPATH"]}:{EXAMPLES_DIR.parent}' | ||
}, | ||
shell=True) | ||
|
||
time.sleep(5) | ||
result.kill() | ||
time.sleep(5) | ||
print('Stderr: ', result.stderr.read().decode()) | ||
|
||
if result.returncode and result.returncode != 0: | ||
assert False, f"{example} raised an exception: {result.stderr}" | ||
|
||
cv2.destroyAllWindows() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
depthai_sdk/src/depthai_sdk/components/control_camera_with_nn.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import depthai as dai | ||
from depthai_sdk.classes.enum import ResizeMode | ||
from typing import Union, Tuple | ||
from depthai_sdk.components.camera_helper import get_resolution_size | ||
from pathlib import Path | ||
from string import Template | ||
|
||
def control_camera_with_nn( | ||
pipeline: dai.Pipeline, | ||
camera_control: dai.Node.Input, | ||
nn_output: dai.Node.Output, | ||
resize_mode: ResizeMode, | ||
resolution: Union[dai.ColorCameraProperties.SensorResolution, dai.MonoCameraProperties.SensorResolution], | ||
nn_size: Tuple[int, int], | ||
af: bool, | ||
ae: bool, | ||
debug: bool | ||
): | ||
sensor_resolution = get_resolution_size(resolution) | ||
# width / height (old ar) | ||
sensor_ar = sensor_resolution[0] / sensor_resolution[1] | ||
# NN ar (new ar) | ||
nn_ar = nn_size[0] / nn_size[1] | ||
|
||
if resize_mode == ResizeMode.LETTERBOX: | ||
padding = (sensor_ar - nn_ar) / 2 | ||
if padding > 0: | ||
init = f"xmin = 0; ymin = {-padding}; xmax = 1; ymax = {1 + padding}" | ||
else: | ||
init = f"xmin = {padding}; ymin = 0; xmax = {1 - padding}; ymax = 1" | ||
elif resize_mode in [ResizeMode.CROP, ResizeMode.FULL_CROP]: | ||
cropping = (1 - (nn_ar / sensor_ar)) / 2 | ||
if cropping < 0: | ||
init = f"xmin = 0; ymin = {-cropping}; xmax = 1; ymax = {1 + cropping}" | ||
else: | ||
init = f"xmin = {cropping}; ymin = 0; xmax = {1 - cropping}; ymax = 1" | ||
else: # Stretch | ||
init = f"xmin=0; ymin=0; xmax=1; ymax=1" | ||
|
||
|
||
resize_str = f"new_xmin=xmin+width*det.xmin; new_ymin=ymin+height*det.ymin; new_xmax=xmin+width*det.xmax; new_ymax=ymin+height*det.ymax;" | ||
denormalize = f"startx=int(new_xmin*{sensor_resolution[0]}); starty=int(new_ymin*{sensor_resolution[1]}); new_width=int((new_xmax-new_xmin)*{sensor_resolution[0]}); new_height=int((new_ymax-new_ymin)*{sensor_resolution[1]});" | ||
control_str = '' | ||
if ae: | ||
control_str += f"control.setAutoExposureRegion(startx, starty, new_width, new_height);" | ||
if af: | ||
control_str += f"control.setAutoFocusRegion(startx, starty, new_width, new_height);" | ||
|
||
|
||
script_node = pipeline.create(dai.node.Script) | ||
script_node.setProcessor(dai.ProcessorType.LEON_CSS) # More stable | ||
|
||
with open(Path(__file__).parent / 'template_control_cam_with_nn.py', 'r') as file: | ||
code = Template(file.read()).substitute( | ||
DEBUG='' if debug else '#', | ||
INIT=init, | ||
RESIZE=resize_str, | ||
DENORMALIZE=denormalize, | ||
CONTROL=control_str | ||
) | ||
script_node.setScript(code) | ||
|
||
if debug: | ||
print(code) | ||
|
||
# Node linking: | ||
# NN output -> Script -> Camera input | ||
nn_output.link(script_node.inputs['detections']) | ||
script_node.outputs['control'].link(camera_control) |
Oops, something went wrong.