AlgoParamsCard.vue 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. <template>
  2. <div
  3. class="algo-params-card"
  4. :class="{ 'algo-params-card-active': markedParamCardIds.includes(props.id) }"
  5. >
  6. <el-form
  7. ref="ruleFormRef"
  8. :model="algoParams"
  9. :inline="true"
  10. :rules="rules"
  11. label-width="120px"
  12. >
  13. <div v-for="item in paramItems">
  14. <el-form-item v-if="item.type === 'label'" label="检测对象:" prop="label">
  15. <el-select v-model="algoParams.label" style="width: 186px" @change="handleLabelChange">
  16. <el-option
  17. v-for="item in getLabelList()"
  18. :key="item.value"
  19. :label="item.label"
  20. :value="item.value"
  21. :disabled="item.disabled"
  22. />
  23. </el-select>
  24. </el-form-item>
  25. <el-form-item
  26. v-if="item.type === 'criticalCount'"
  27. :label="labelNameMap[item.label] + '检测数量:'"
  28. :prop="item.prop"
  29. required
  30. >
  31. <ElInputNumber
  32. v-model="algoParams[item.prop]"
  33. controls-position="right"
  34. :min="0"
  35. :step="1"
  36. style="width: 186px; margin-right: 5px"
  37. :disabled="!algoParams.label"
  38. placeholder="请输入检测数量"
  39. />
  40. </el-form-item>
  41. <el-form-item
  42. v-if="item.type === 'confidence'"
  43. :label="labelNameMap[item.label] + '置信度:'"
  44. :prop="item.prop"
  45. required
  46. >
  47. <div style="display: flex">
  48. <el-slider
  49. v-model="algoParams[item.prop]"
  50. :min="25"
  51. :max="98"
  52. :step="1"
  53. style="width: 128px; margin-right: 6px"
  54. :disabled="!algoParams.label"
  55. />
  56. <ElInputNumber
  57. v-model="algoParams[item.prop]"
  58. controls-position="right"
  59. :min="0"
  60. :step="1"
  61. style="width: 88px; margin-right: 5px"
  62. :disabled="!algoParams.label"
  63. />
  64. <span>%</span>
  65. </div>
  66. </el-form-item>
  67. <el-form-item
  68. v-if="item.type === 'minArea'"
  69. :label="labelNameMap[item.label] + '最小检测面积:'"
  70. required
  71. >
  72. <el-form-item :prop="item.label ? item.label + '.' + 'min_width' : 'min_width'">
  73. <ElInputNumber
  74. v-model="algoParams[item.label ? item.label + '.' + 'min_width' : 'min_width']"
  75. controls-position="right"
  76. :min="32"
  77. :step="1"
  78. style="width: 88px; margin-right: 5px"
  79. :disabled="!algoParams.label"
  80. />
  81. <span>px</span>
  82. </el-form-item>
  83. <el-form-item
  84. :prop="item.label ? item.label + '.' + 'min_height' : 'min_height'"
  85. style="margin-left: 17px"
  86. >
  87. <ElInputNumber
  88. v-model="algoParams[item.label ? item.label + '.' + 'min_height' : 'min_height']"
  89. controls-position="right"
  90. :min="32"
  91. :step="1"
  92. style="width: 88px; margin-right: 5px"
  93. :disabled="!algoParams.label"
  94. />
  95. <span>px</span>
  96. </el-form-item>
  97. </el-form-item>
  98. </div>
  99. </el-form>
  100. <div class="paramOptIcons">
  101. <!-- <el-icon v-if="isEdit" size="16px" @click="handleSaveParam(ruleFormRef)">
  102. <SaveOutline />
  103. </el-icon>
  104. <el-icon v-else size="16px" @click="isEdit = true"><Edit /></el-icon> -->
  105. <el-icon size="16px" @click="deleteParam(props.id)"><Delete /></el-icon>
  106. </div>
  107. </div>
  108. </template>
  109. <script setup lang="ts">
  110. import { computed, ref, watch } from 'vue';
  111. import useCameraAlgoStore, { AlgoParamMetaItem } from '../../store/useCameraAlgoStore';
  112. import { storeToRefs } from 'pinia';
  113. import { ElInputNumber, ElMessage, FormInstance } from 'element-plus';
  114. import { Delete, Edit } from '@element-plus/icons-vue';
  115. import { SaveOutline } from '@vicons/ionicons5';
  116. import { labelNameMap } from './types';
  117. import { getCriticalCounts } from './utils';
  118. const props = defineProps<{
  119. id: string;
  120. }>();
  121. const cameraAlgoStore = useCameraAlgoStore();
  122. const { selectedAlgoDetail, metaObjList, markedParamCardIds } = storeToRefs(cameraAlgoStore);
  123. const { deleteParam } = cameraAlgoStore;
  124. const ruleFormRef = ref<FormInstance>();
  125. //是否在编辑
  126. const isEdit = ref(false);
  127. const getLabelList = () => {
  128. return metaObjList.value.map((item) => {
  129. const entry = selectedAlgoDetail.value.metaValues.find(
  130. (val) => val.label === item.label && item.label !== algoParams.value.label,
  131. );
  132. return {
  133. label: labelNameMap[item.label],
  134. value: item.label,
  135. disabled: entry ? true : false,
  136. };
  137. });
  138. };
  139. const handleLabelChange = () => {
  140. markedParamCardIds.value = selectedAlgoDetail.value.metaValues
  141. .filter((item) => !item.label)
  142. .map((item) => item.id);
  143. };
  144. const algoParams = computed(() => {
  145. const countList = getCriticalCounts(selectedAlgoDetail.value.extra);
  146. const index = selectedAlgoDetail.value.metaValues.findIndex((item) => item.id === props.id);
  147. if (index < 0) {
  148. return {} as AlgoParamMetaItem;
  149. } else {
  150. const param = selectedAlgoDetail.value.metaValues[index];
  151. param.criticalCount = countList && countList.length > index ? countList[index] : 0;
  152. return param;
  153. }
  154. });
  155. const paramItems = ref([
  156. { label: '', type: 'label', prop: 'label' },
  157. { label: '', type: 'criticalCount', prop: 'criticalCount' },
  158. { label: '', type: 'confidence', prop: 'confidence' },
  159. { label: '', type: 'minArea', prop: '' },
  160. ]);
  161. watch(
  162. () => algoParams.value.label,
  163. (val) => {
  164. if (!val) return;
  165. const meta = metaObjList.value.find((item) => item.label === val);
  166. algoParams.value.confidence = meta.confidence * 100;
  167. algoParams.value['min_width'] = meta['min_width'];
  168. algoParams.value['min_height'] = meta['min_height'];
  169. const nexts = meta.nextObjs;
  170. if (nexts) {
  171. for (let i = 0; i < nexts.length; i++) {
  172. const item = nexts[i];
  173. paramItems.value.push({
  174. label: item.label,
  175. type: 'confidence',
  176. prop: `${item.label}.confidence`,
  177. });
  178. algoParams.value[`${item.label}.confidence`] = item.confidence * 100;
  179. paramItems.value.push({
  180. label: item.label,
  181. type: 'minArea',
  182. prop: '',
  183. });
  184. algoParams.value[item.label + '.' + 'min_width'] = item['min_width'];
  185. algoParams.value[item.label + '.' + 'min_height'] = item['min_height'];
  186. }
  187. }
  188. },
  189. {
  190. immediate: true,
  191. },
  192. );
  193. const rules = computed(() => {
  194. const rule = {} as any;
  195. paramItems.value.forEach((param) => {
  196. if (param.type === 'label') {
  197. rule.label = [
  198. {
  199. required: true,
  200. message: '请选择检测对象',
  201. trigger: 'blur',
  202. },
  203. ];
  204. }
  205. if (param.type === 'criticalCount') {
  206. rule[`${param.label}${param.label ? '.' : ''}criticalCount`] = [
  207. {
  208. required: true,
  209. message: '请输入检测数量',
  210. trigger: 'blur',
  211. },
  212. { validator: integerJudge, trigger: 'blur' },
  213. ];
  214. }
  215. if (param.type === 'confidence') {
  216. rule[`${param.label}${param.label ? '.' : ''}confidence`] = [
  217. {
  218. required: true,
  219. message: '请输入检测置信度',
  220. trigger: 'blur',
  221. },
  222. { validator: integerJudge, trigger: 'blur' },
  223. ];
  224. }
  225. if (param.type === 'minArea') {
  226. rule[param.label ? param.label + '.' + 'min_width' : 'min_width'] = [
  227. {
  228. required: true,
  229. message: '请输入最小宽度',
  230. trigger: 'blur',
  231. },
  232. { validator: integerJudge, trigger: 'blur' },
  233. ];
  234. rule[param.label ? param.label + '.' + 'min_height' : 'min_height'] = [
  235. {
  236. required: true,
  237. message: '请输入最小高度',
  238. trigger: 'blur',
  239. },
  240. { validator: integerJudge, trigger: 'blur' },
  241. ];
  242. }
  243. });
  244. return rule;
  245. });
  246. const integerJudge = (rule, value, callback) => {
  247. // 整数校验逻辑
  248. const pattern = /^-?\d+$/;
  249. if (pattern.test(value)) {
  250. callback(); // 校验通过
  251. } else {
  252. callback(new Error('请输入一个整数'));
  253. }
  254. };
  255. const handleSaveParam = async (formEl: FormInstance | undefined) => {
  256. if (!formEl) return;
  257. await formEl.validate((valid, fields) => {
  258. if (valid) {
  259. isEdit.value = false;
  260. } else {
  261. ElMessage.error('保存失败,请检查填写');
  262. }
  263. });
  264. };
  265. const checkValid = async () => {
  266. if (!ruleFormRef.value) return false;
  267. let isValid = true;
  268. await ruleFormRef.value.validate((valid, fields) => {
  269. if (valid) {
  270. } else {
  271. isValid = false;
  272. }
  273. });
  274. return isValid;
  275. };
  276. const setEditable = (value: boolean) => {
  277. isEdit.value = value;
  278. };
  279. defineExpose({
  280. checkValid,
  281. });
  282. </script>
  283. <style scoped lang="scss">
  284. .algo-params-card {
  285. position: relative;
  286. width: 400px;
  287. background: #0000000a;
  288. border-radius: 5px;
  289. border: 1px solid #e8ecf2;
  290. padding: 10px 0 0 10px;
  291. margin: 0 5px 5px 5px;
  292. &-active {
  293. border: 1px solid #ff0000ab;
  294. }
  295. }
  296. .input-box {
  297. width: 80px;
  298. }
  299. .paramOptIcons {
  300. position: absolute;
  301. top: 6px;
  302. right: 6px;
  303. display: flex;
  304. justify-content: space-between;
  305. color: #00000026;
  306. cursor: pointer;
  307. }
  308. :deep(.el-form--inline .el-form-item) {
  309. margin-bottom: 10px;
  310. margin-right: 10px;
  311. }
  312. :deep(.el-form-item__label) {
  313. line-height: 16px;
  314. font-size: 14px;
  315. }
  316. :deep(.el-form--inline .el-form-item) {
  317. margin-bottom: 16px;
  318. }
  319. </style>