annotator_rule_schema.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. from __future__ import annotations
  2. from collections import OrderedDict
  3. from copy import deepcopy
  4. from typing import Any
  5. RULE_TYPE_SCHEMAS: OrderedDict[str, dict[str, Any]] = OrderedDict(
  6. {
  7. "image": {
  8. "type": "image",
  9. "label": "RuleImage",
  10. "capabilities": {
  11. "supports_test": True,
  12. "supports_crop": True,
  13. "supports_image_preview": True,
  14. "shared_roi_back": False,
  15. },
  16. "fields": [
  17. {"key": "itemName", "label": "itemName", "control": "text", "default": "new"},
  18. {
  19. "key": "imageName",
  20. "label": "imageName",
  21. "control": "text",
  22. "default": "",
  23. "auto_from_item_name": True,
  24. },
  25. {
  26. "key": "method",
  27. "label": "method",
  28. "control": "select",
  29. "default": "Template matching",
  30. "options": ["Template matching", "Sift Flann"],
  31. },
  32. {
  33. "key": "threshold",
  34. "label": "threshold",
  35. "control": "number",
  36. "default": 0.8,
  37. "step": 0.01,
  38. "min": 0,
  39. "max": 1,
  40. "integer": False,
  41. },
  42. {"key": "description", "label": "description", "control": "textarea", "default": "", "full": True},
  43. ],
  44. },
  45. "ocr": {
  46. "type": "ocr",
  47. "label": "RuleOcr",
  48. "capabilities": {
  49. "supports_test": True,
  50. "supports_crop": False,
  51. "supports_image_preview": False,
  52. "shared_roi_back": False,
  53. },
  54. "fields": [
  55. {"key": "itemName", "label": "itemName", "control": "text", "default": "new"},
  56. {
  57. "key": "mode",
  58. "label": "mode",
  59. "control": "select",
  60. "default": "Single",
  61. "options": ["Single", "Full", "Digit", "DigitCounter", "Duration", "Quantity"],
  62. },
  63. {"key": "method", "label": "method", "control": "text", "default": "Default"},
  64. {"key": "keyword", "label": "keyword", "control": "text", "default": ""},
  65. {"key": "description", "label": "description", "control": "textarea", "default": "", "full": True},
  66. ],
  67. },
  68. "click": {
  69. "type": "click",
  70. "label": "RuleClick",
  71. "capabilities": {
  72. "supports_test": False,
  73. "supports_crop": False,
  74. "supports_image_preview": False,
  75. "shared_roi_back": False,
  76. },
  77. "fields": [
  78. {"key": "itemName", "label": "itemName", "control": "text", "default": "new"},
  79. {"key": "description", "label": "description", "control": "textarea", "default": "", "full": True},
  80. ],
  81. },
  82. "swipe": {
  83. "type": "swipe",
  84. "label": "RuleSwipe",
  85. "capabilities": {
  86. "supports_test": False,
  87. "supports_crop": False,
  88. "supports_image_preview": False,
  89. "shared_roi_back": False,
  90. },
  91. "fields": [
  92. {"key": "itemName", "label": "itemName", "control": "text", "default": "new"},
  93. {
  94. "key": "mode",
  95. "label": "mode",
  96. "control": "select",
  97. "default": "default",
  98. "options": ["default", "vector"],
  99. },
  100. {"key": "description", "label": "description", "control": "textarea", "default": "", "full": True},
  101. ],
  102. },
  103. "long_click": {
  104. "type": "long_click",
  105. "label": "RuleLongClick",
  106. "capabilities": {
  107. "supports_test": False,
  108. "supports_crop": False,
  109. "supports_image_preview": False,
  110. "shared_roi_back": False,
  111. },
  112. "fields": [
  113. {"key": "itemName", "label": "itemName", "control": "text", "default": "new"},
  114. {
  115. "key": "duration",
  116. "label": "duration(ms)",
  117. "control": "number",
  118. "default": 1000,
  119. "min": 1,
  120. "step": 1,
  121. "integer": True,
  122. },
  123. {"key": "description", "label": "description", "control": "textarea", "default": "", "full": True},
  124. ],
  125. },
  126. "list": {
  127. "type": "list",
  128. "label": "RuleList",
  129. "capabilities": {
  130. "supports_test": True,
  131. "supports_crop": True,
  132. "supports_image_preview": True,
  133. "shared_roi_back": True,
  134. },
  135. "fields": [
  136. {"key": "itemName", "label": "itemName", "control": "text", "default": "new"},
  137. ],
  138. "meta_fields": [
  139. {"key": "name", "label": "list.name", "control": "text", "default": "list_name"},
  140. {
  141. "key": "direction",
  142. "label": "list.direction",
  143. "control": "select",
  144. "default": "vertical",
  145. "options": ["vertical", "horizontal"],
  146. },
  147. {
  148. "key": "type",
  149. "label": "list.type",
  150. "control": "select",
  151. "default": "image",
  152. "options": ["image", "ocr"],
  153. },
  154. {"key": "description", "label": "list.description", "control": "text", "default": ""},
  155. ],
  156. },
  157. }
  158. )
  159. ROI_DEFAULT = "0,0,100,100"
  160. def get_rule_types() -> list[str]:
  161. return list(RULE_TYPE_SCHEMAS.keys())
  162. def get_rule_schema(rule_type: str) -> dict[str, Any]:
  163. schema = RULE_TYPE_SCHEMAS.get(str(rule_type or "").strip())
  164. if not schema:
  165. schema = RULE_TYPE_SCHEMAS["image"]
  166. return deepcopy(schema)
  167. def get_rule_schemas() -> list[dict[str, Any]]:
  168. return [get_rule_schema(rule_type) for rule_type in get_rule_types()]
  169. def get_schema_payload() -> dict[str, Any]:
  170. return {
  171. "rule_types": get_rule_types(),
  172. "schemas": {rule_type: get_rule_schema(rule_type) for rule_type in get_rule_types()},
  173. }
  174. def _field_defaults(fields: list[dict[str, Any]]) -> dict[str, Any]:
  175. return {field["key"]: deepcopy(field.get("default")) for field in fields}
  176. def default_rule(rule_type: str) -> dict[str, Any]:
  177. schema = get_rule_schema(rule_type)
  178. rule = _field_defaults(schema.get("fields", []))
  179. rule["roiFront"] = ROI_DEFAULT
  180. if rule_type != "list":
  181. rule["roiBack"] = ROI_DEFAULT
  182. return rule
  183. def default_list_meta() -> dict[str, Any]:
  184. schema = get_rule_schema("list")
  185. meta = _field_defaults(schema.get("meta_fields", []))
  186. meta["roiBack"] = ROI_DEFAULT
  187. return meta
  188. def merge_rule_with_defaults(rule_type: str, rule: dict[str, Any] | None) -> dict[str, Any]:
  189. merged = default_rule(rule_type)
  190. if isinstance(rule, dict):
  191. merged.update(rule)
  192. if rule_type == "list":
  193. merged.pop("description", None)
  194. return merged
  195. def merge_list_meta_with_defaults(meta: dict[str, Any] | None) -> dict[str, Any]:
  196. merged = default_list_meta()
  197. if isinstance(meta, dict):
  198. merged.update(meta)
  199. return merged
  200. def field_default(rule_type: str, key: str, fallback: Any = None) -> Any:
  201. schema = get_rule_schema(rule_type)
  202. fields = list(schema.get("fields", [])) + list(schema.get("meta_fields", []))
  203. for field in fields:
  204. if field.get("key") == key:
  205. return deepcopy(field.get("default"))
  206. return fallback
  207. def field_options(rule_type: str, key: str) -> list[str]:
  208. schema = get_rule_schema(rule_type)
  209. fields = list(schema.get("fields", [])) + list(schema.get("meta_fields", []))
  210. for field in fields:
  211. if field.get("key") == key:
  212. return list(field.get("options", []))
  213. return []