Inference
safety.InferenceHandler
학습한 Object Detection/Classification 모델을 사용하여 검사를 하기 위한 InferenceHandler class 입니다.
Usage
demo/inference.py 참고
build(config)
classmethod
InferenceHandler class의 instance를 생성합니다.
Parameters:
Returns:
| Name | Type | Description |
|---|---|---|
InferenceHandler |
build가 완료된 safety.InferenceHandler class의 instance를 반환합니다. |
Note
- 각 테스크 모델의 경로(
{task}_checkpoint_path)가 None인 경우 해당 버전의 기본 모델을 사용합니다. - default_alarm_options, default_object_options는 API에서 기본적으로 비활성화합니다. (즉,
enabled가 False입니다.) - Normal 모델은 당장 필요하지 않아 제거하였습니다.
infer(images, camera_ids, timestamps)
현재 설정된 inference 옵션을 바탕으로 inference를 수행합니다.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
images |
list[ndarray]
|
inference를 수행할 이미지들의 list (shape: [H, W, C]) |
required |
camera_ids |
list[int]
|
images의 각 image에 대한 camera_id list |
required |
timestamps |
list[int]
|
images의 각 image에 대한 timestamp list (단위: 밀리초) |
required |
Returns:
warmup()
get_default_inference_option(model, key)
Model별 Postprocess 옵션의 기본 설정 값을 반환합니다.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model |
str
|
"safety", "detection", "classification", "depth", "match" 중 하나. |
required |
key |
str
|
옵션 key |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
옵션 value |
사용 가능한 key와 value 목록
model: safety
key: 가능한 key는 다음과 같습니다.
{
"outputs.time": bool, # 인퍼런스 시 각 요소에 걸린 시간을 반환할지 여부. (default False)
"params.mlc_filter_thresholds": dict[str, float], # MLC 필터의 각 클래스별 threshold 값. 각 값은 [0, 1] 범위의 실수. (default {"person": 0.9, "fire": 0.5, "smoke": 0.5})
}
model: detection
key: DET 모델 결과 후처리 파라미터. 가능한 key는 다음과 같습니다.
{
"params.object_score_threshold": list[int], # 각 클래스의 score threshold 값. 예측 box의 score가 threshold보다 작은 경우 필터링 됩니다.
# 각 값은 [0, 100] 범위의 정수. (default [30, 10, ... , 10])
"params.object_area_threshold": list[int], # 각 클래스의 area threshold 값. 예측 box의 면적이 threshold보다 작은 경우 필터링 됩니다.
# 각 값은 0 이상의 정수. (default [0, ... , 0])
"params.max_num_of_detected_objects": list[int], # 각 클래스의 최대 예측 박스 개수. 예측 box의 개수가 이 값을 넘을 경우 score가 낮은 순으로 제거됩니다.
# 각 값은 -1 이상의 정수이며, 값이 -1인 경우 개수 필터링을 적용하지 않습니다. (default [30, -1, ... , -1])
}
model: classification
key: CLS 모델 결과 후처리 파라미터. 가능한 key는 다음과 같습니다.
{
"params.additional_scores": list[int | float], # scores_including_add를 계산할 때, scores_original에 더해지는 값입니다. 각 값은 [0, 100] 범위의 실수. (default [0.0, ... , 0.0])
}
model: depth
key: DEPTH 모델 결과 후처리 파라미터. 가능한 key는 다음과 같습니다.
model: match
key: MATCH 모델 결과 후처리 파라미터. 가능한 key는 다음과 같습니다.
get_inference_option(model, key)
Model에 따른 현재 설정된 postprocess 옵션 값을 읽습니다.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model |
str
|
"safety", "detection", "classification", "depth", "match" 중 하나. |
required |
key |
str
|
옵션 key |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Any |
Any
|
옵션 value |
Keys
get_default_inference_option과 동일합니다.
set_inference_option(model, key, value)
Model에 따른 Postprocess 옵션을 설정합니다.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model |
str
|
"safety", "detection", "classification", "depth", "match" 중 하나. |
required |
key |
str
|
설정하고자 하는 옵션 key 입니다. |
required |
value |
Any
|
설정하고자 하는 옵션 value 입니다. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
None |
None
|
None |
Keys
get_default_inference_option과 동일합니다.
register_camera(camera_id)
검사할 camera_id를 등록합니다.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
camera_id |
int
|
등록할 camera_id |
required |
Returns:
| Name | Type | Description |
|---|---|---|
None |
None
|
None |
Raises:
| Type | Description |
|---|---|
InvalidCameraIdError
|
camera_id가 int가 아닌 경우 발생하는 에러 |
AlreadyRegisteredCameraIdError
|
이미 등록된 camera_id를 등록하려고 할 때 발생하는 에러 |
release_camera(camera_id)
등록된 camera_id를 해제합니다.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
camera_id |
int
|
해제할 camera_id |
required |
Returns:
| Name | Type | Description |
|---|---|---|
None |
None
|
None |
Raises:
| Type | Description |
|---|---|
UnregisteredCameraIdError
|
등록되지 않은 camera_id를 해제하려고 할 때 발생하는 에러 |
get_registered_camera_ids()
등록된 camera_id 리스트를 반환합니다.
Returns:
| Type | Description |
|---|---|
list[int]
|
list[int]: 등록된 camera_id 리스트 |
get_default_tracker_option(camera_id)
기본 tracker option을 반환합니다.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
camera_id |
int
|
tracker option을 반환할 camera_id (사용하지 않음, 단순히 다른 API와의 일관성을 위해 존재) |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Usage
Note: camera_id는 사용하지 않으며, 단순히 다른 API와의 일관성을 위해 존재합니다.
get_tracker_option(camera_id)
등록된 camera_id에 대한 tracker option을 반환합니다.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
camera_id |
int
|
tracker option을 반환할 camera_id |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
dict[str, Any]: tracker option. 형태는 |
Raises:
| Type | Description |
|---|---|
UnregisteredCameraIdError
|
등록되지 않은 camera_id에 대한 정보를 요청할 때 발생하는 에러 |
set_tracker_option(camera_id, tracker_option)
등록된 camera_id에 대한 tracker option을 설정합니다.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
camera_id |
int
|
tracker option을 설정할 camera_id |
required |
tracker_option |
dict[str, Any]
|
tracker option. 형태는 |
required |
Returns:
| Name | Type | Description |
|---|---|---|
None |
None
|
None |
Raises:
| Type | Description |
|---|---|
InvalidTrackerOptionError
|
tracker_option가 올바르지 않을 때 발생하는 에러 |
UnregisteredCameraIdError
|
등록되지 않은 camera_id에 대해 설정할 때 발생하는 에러 |
get_default_grid_option(camera_id)
기본 grid option을 반환합니다.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
camera_id |
int
|
grid option을 반환할 camera_id (사용하지 않음, 단순히 다른 API와의 일관성을 위해 존재) |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Usage
Note: camera_id는 사용하지 않으며, 단순히 다른 API와의 일관성을 위해 존재합니다.
get_grid_option(camera_id)
등록된 camera_id에 대한 grid option을 반환합니다.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
camera_id |
int
|
grid option을 반환할 camera_id |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
dict[str, Any]: grid option. 형태는 |
Raises:
| Type | Description |
|---|---|
UnregisteredCameraIdError
|
등록되지 않은 camera_id에 대한 정보를 요청할 때 발생하는 에러 |
set_grid_option(camera_id, grid_option)
등록된 camera_id에 대한 grid option을 설정합니다.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
camera_id |
int
|
grid option을 설정할 camera_id |
required |
grid_option |
dict[str, Any]
|
grid option. 형태는 |
required |
Returns:
| Name | Type | Description |
|---|---|---|
None |
None
|
None |
Raises:
| Type | Description |
|---|---|
InvalidGridOptionError
|
grid_option이 올바르지 않을 때 발생하는 에러 |
UnregisteredCameraIdError
|
등록되지 않은 camera_id에 대해 설정할 때 발생하는 에러 |
get_reference_image(camera_id)
등록된 camera_id에 대한 reference image를 반환합니다.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
camera_id |
int
|
reference image를 반환할 camera_id |
required |
Returns:
| Type | Description |
|---|---|
ndarray | None
|
np.ndarray | None: reference image (shape: [H, W, C]). None인 경우 reference image가 설정되지 않은 상태입니다. |
Raises:
| Type | Description |
|---|---|
UnregisteredCameraIdError
|
등록되지 않은 camera_id에 대한 정보를 요청할 때 발생하는 에러 |
set_reference_image(camera_id, reference_image)
등록된 camera_id에 대한 reference image를 설정합니다.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
camera_id |
int
|
reference image를 설정할 camera_id |
required |
reference_image |
ndarray
|
reference image
형태는 |
required |
Returns:
| Name | Type | Description |
|---|---|---|
None |
None
|
None |
Raises:
| Type | Description |
|---|---|
InvalidReferenceImageError
|
reference_image가 올바르지 않을 때 발생하는 에러 |
UnregisteredCameraIdError
|
등록되지 않은 camera_id에 대해 설정할 때 발생하는 에러 |
Usage
get_monitoring_area(camera_id)
등록된 camera_id에 대한 monitoring area를 반환합니다.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
camera_id |
int
|
monitoring area를 반환할 camera_id |
required |
Returns:
| Type | Description |
|---|---|
dict[str, list[list[list[int]]]]
|
Raises: UnregisteredCameraIdError: 등록되지 않은 camera_id에 대한 정보를 요청할 때 발생하는 에러
set_monitoring_area(camera_id, monitoring_area)
등록된 camera_id에 대한 monitoring area를 설정합니다.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
camera_id |
int
|
monitoring area를 설정할 camera_id |
required |
monitoring_area |
dict[str, list[list[list[int]]]]
|
dictionary of monitoring area polygon list
형태는 |
required |
Returns:
| Name | Type | Description |
|---|---|---|
None |
None
|
None |
Raises:
| Type | Description |
|---|---|
InvalidMonitoringAreaError
|
monitoring_area가 올바르지 않을 때 발생하는 에러 |
UnregisteredCameraIdError
|
등록되지 않은 camera_id에 대해 설정할 때 발생하는 에러 |
Usage
area = {"includes": [[[0, 0], [100, 0], [100, 100], [0, 100]]], "excludes": []}
err, msg, _ = handler.set_monitoring_area(camera_id=0, monitoring_area=area)
err, msg, area = handler.get_monitoring_area(camera_id=0)
print(area)
# {"includes": [[[0, 0], [100, 0], [100, 100], [0, 100]]], "excludes": []} # 설정한 값이 반환됩니다.
get_default_object_options(camera_id)
등록된 camera_id에 대한 default object options를 반환합니다.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
camera_id |
int
|
default object options를 반환할 camera_id |
required |
Returns:
Raises:
| Type | Description |
|---|---|
UnregisteredCameraIdError
|
등록되지 않은 camera_id에 대한 정보를 요청할 때 발생하는 에러 |
get_object_options(camera_id)
등록된 camera_id에 대한 object options를 반환합니다.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
camera_id |
int
|
object options를 반환할 camera_id |
required |
Returns:
| Type | Description |
|---|---|
dict[str, dict]
|
dict[str, dict[str, Any]]: object options. 형태는 |
Raises:
| Type | Description |
|---|---|
UnregisteredCameraIdError
|
등록되지 않은 camera_id에 대한 정보를 요청할 때 발생하는 에러 |
set_object_options(camera_id, object_options)
등록된 camera_id에 대한 object options를 설정합니다.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
camera_id |
int
|
object options를 설정할 camera_id |
required |
object_options |
dict[str, dict]]
|
object options. 형태는 |
required |
Returns:
| Name | Type | Description |
|---|---|---|
None |
None
|
None |
Raises:
| Type | Description |
|---|---|
InvalidObjectOptionsError
|
object_options가 올바르지 않을 때 발생하는 에러 |
UnregisteredCameraIdError
|
등록되지 않은 camera_id에 대해 설정할 때 발생하는 에러 |
Usage
options = handler.get_object_options(camera_id=0)
options["person"]["enabled"] = True
options["person"]["score_threshold"] = 50
handler.set_object_options(camera_id=0, object_options=options)
options = handler.get_object_options(camera_id=0)
print(options)
# {"person": {"enabled": True, "area_threshold": 900.0, "score_threshold": 50}, ...} # 설정한 값이 반환됩니다.
get_default_event_options(camera_id)
등록된 camera_id에 대한 default event options를 반환합니다.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
camera_id |
int
|
default event options를 반환할 camera_id |
required |
Returns:
get_event_options(camera_id)
등록된 camera_id에 대한 event options를 반환합니다.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
camera_id |
int
|
event options를 반환할 camera_id |
required |
Returns:
| Type | Description |
|---|---|
dict[str, dict]
|
dict[str, dict[str, Any]]: event options. 형태는 |
Raises:
| Type | Description |
|---|---|
UnregisteredCameraIdError
|
등록되지 않은 camera_id에 대한 정보를 요청할 때 발생하는 에러 |
set_event_options(camera_id, event_options)
등록된 camera_id에 대한 event options를 설정합니다.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
camera_id |
int
|
event options를 설정할 camera_id |
required |
event_options |
dict[str, dict]]
|
event options. 형태는 |
required |
Returns:
| Name | Type | Description |
|---|---|---|
None |
None
|
None |
Raises:
| Type | Description |
|---|---|
InvalidEventOptionsError
|
event_options가 올바르지 않을 때 발생하는 에러 |
UnregisteredCameraIdError
|
등록되지 않은 camera_id에 대해 설정할 때 발생하는 에러 |
Usage
get_default_alarm_options(camera_id)
등록된 camera_id에 대한 default alarm options를 반환합니다.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
camera_id |
int
|
default alarm options를 반환할 camera_id |
required |
Returns:
Note
start_time==end_time인 경우, 항상 알람이 활성화 됩니다.
Raises:
| Type | Description |
|---|---|
UnregisteredCameraIdError
|
등록되지 않은 camera_id에 대한 정보를 요청할 때 발생하는 에러 |
get_alarm_options(camera_id)
등록된 camera_id에 대한 alarm options를 반환합니다.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
camera_id |
int
|
alarm options를 반환할 camera_id |
required |
Returns:
| Type | Description |
|---|---|
dict[str, dict]
|
dict[str, dict]: alarm options. 형태는 |
Raises:
| Type | Description |
|---|---|
UnregisteredCameraIdError
|
등록되지 않은 camera_id에 대한 정보를 요청할 때 발생하는 에러 |
set_alarm_options(camera_id, alarm_options)
등록된 camera_id에 대한 alarm options를 설정합니다.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
camera_id |
int
|
alarm options를 설정할 camera_id |
required |
alarm_options |
dict[str, dict]]
|
alarm options. 형태는 |
required |
Returns:
| Name | Type | Description |
|---|---|---|
None |
None
|
None |
Raises:
| Type | Description |
|---|---|
InvalidAlarmOptionsError
|
alarm_options가 올바르지 않을 때 발생하는 에러 |
UnregisteredCameraIdError
|
등록되지 않은 camera_id에 대해 설정할 때 발생하는 에러 |
Usage
options = handler.get_alarm_options(camera_id=0)
options["fire_detect"]["enabled"] = True
options["fire_detect"]["start_time"] = 8 * 3600
options["fire_detect"]["end_time"] = 18 * 3600
handler.set_alarm_options(camera_id=0, alarm_options=options)
options = handler.get_alarm_options(camera_id=0)
print(options)
# {"fire_detect": {"enabled": True, "start_time": 28800, "end_time": 64800, ...}, ...} # 설정한 값이 반환됩니다.
get_depth_fitting_preview(image, surface_points)
이미지에 depth fitting preview를 적용합니다.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image |
ndarray
|
depth fitting preview를 적용할 이미지, shape: (H, W, C) |
required |
surface_points |
list[list[int]]
|
depth fitting을 위한 reference surface points. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
np.ndarray: depth fitting preview가 적용된 이미지 |
Raises:
| Type | Description |
|---|---|
InsufficientSurfacePointsError
|
surface_points의 개수가 3개 미만인 경우 발생하는 에러 |