| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295 |
- import dayjs, { Dayjs } from 'dayjs';
- import { uid } from 'uid';
- import NP from 'number-precision';
- import { isEqual } from 'lodash-es';
- import { TimeRangeItem, TimePeriodItem } from '@/modules/algo/algo-params-edit/types';
- import { ALGO_ENABLED_STATUS, CameraAlgoItem, FENCE_ENBALED_STATUS } from '@/api/camera/camera-preview';
- import safeParse from '@/utils/safeParse';
- import { QueryAlgoInfoRes } from '@/api/camera/camera-config';
- // export const createDefaultTime = (): TimeRangeItem => {
- // return { id: uid(), value: [dayjs(), dayjs().add(1, 'hour')] as [Dayjs, Dayjs] };
- // };
- export const createDefaultTime = (): TimePeriodItem => {
- return {
- id: uid(),
- startDay: null,
- endDay: null,
- timeRangeList: [{ id: uid(), startTime: '', endTime: '' }],
- };
- };
- export enum FrequencyEnum {
- second = 1,
- miniute = 60,
- hour = 3600,
- }
- export const frequencyOptions = [
- { label: '秒', value: FrequencyEnum.second },
- { label: '分钟', value: FrequencyEnum.miniute },
- { label: '小时', value: FrequencyEnum.hour },
- ];
- export interface DetectionJSON {
- detectionNum: number;
- detectionUnit: FrequencyEnum;
- }
- /** 根据后端返回的时间(单位是秒),拆分成单位和数值 */
- export const getDetectionJSON = (time: number | undefined | null): DetectionJSON => {
- if (time && time > 0) {
- for (let i = frequencyOptions.length - 1; i >= 0; i--) {
- const unit = frequencyOptions[i].value;
- if (time >= unit) {
- return { detectionNum: NP.divide(time, unit), detectionUnit: unit };
- }
- }
- }
- return { detectionNum: 5, detectionUnit: FrequencyEnum.miniute };
- };
- export const getDetectionTimeJSON = (time?: string): TimeRangeItem[] | null => {
- if (!time) return null;
- const timeArr = time.split(';');
- const nowStr = dayjs().format('YYYY-MM-DD');
- const timeStrArr = timeArr
- .map((x) => {
- const [startDate, endDate] = x.split('-');
- return [dayjs(`${nowStr} ${startDate}`), dayjs(`${nowStr} ${endDate}`)] as [Dayjs, Dayjs];
- })
- .map((x) => {
- return { id: uid(), value: x };
- });
- return timeStrArr;
- };
- export const getInferParam = (extra: string | undefined | null) => {
- if (!extra) return {};
- const extraObj = safeParse(extra);
- const params = extraObj?.inferParams;
- if (!params || (params && params.length == 0)) return {};
- return params[0];
- };
- export const getMetaValues = (extra: string | undefined | null) => {
- if (!extra) return [];
- const extraObj = safeParse(extra);
- const params = extraObj?.inferParams;
- if (!params || (params && params.length == 0)) return [];
- const metaObjs = params[0]?.metaObjs;
- if (!metaObjs || (metaObjs && metaObjs.length == 0)) return [];
- const metaArr = metaObjs.map((item: any) => {
- const val = {
- id: uid(),
- label: item.label,
- confidence: Number((item.confidence * 100).toFixed(0)),
- min_width: item['min_width'],
- min_height: item['min_height'],
- } as any;
- item.nextObjs.forEach((next) => {
- val[`${next.label}.confidence`] = Number((next.confidence * 100).toFixed(0));
- val[next.label + '.' + 'min_width'] = next['min_width'];
- val[next.label + '.' + 'min_height'] = next['min_height'];
- });
- return val;
- });
- return metaArr;
- };
- export const getDetectionTime = (time: string | undefined | null) => {
- if (!time) return [];
- const timeList = safeParse(time);
- if (!timeList || (timeList && timeList.length === 0)) {
- return [];
- }
- return timeList;
- };
- export const getInferCode = (extra: string | undefined | null) => {
- if (!extra) return '';
- const extraObj = safeParse(extra);
- return extraObj?.inferCode || '';
- };
- export const getAlgoType = (extra: string | undefined | null) => {
- if (!extra) return 0;
- const extraObj = safeParse(extra);
- const infers = extraObj?.inferParams;
- if (!infers || infers.length === 0) return 0;
- return infers[0]?.algoType || 0;
- };
- export const getCriticalCounts = (extra: string | undefined | null) => {
- if (!extra) return [];
- const extraObj = safeParse(extra);
- const infers = extraObj?.inferParams;
- if (!infers || infers.length === 0) return [];
- return infers[0]?.criticalCounts || [];
- };
- export const getExtraCommonInfo = (detail: CameraAlgoItem | undefined | null) => {
- if (!detail) return {};
- let extraValue = getCommonInfoByExtra(detail.extra);
- if (isEqual(extraValue, {})) {
- extraValue = getCommonInfoByExtra(detail.algoInfo?.extra);
- }
- return extraValue;
- };
- interface CommonInfo {
- regionJudge?: number;
- judge?: number;
- eventDurationMinMs?: number;
- eventDurationMinFrames?: number;
- eventAlarmIntervalMs?: number;
- eventAlarmIntervalFrames?: number;
- timeWindow?: number;
- }
- // 根据算法的extra字符串转化为JSON信息
- const getCommonInfoByExtra = (extra: string | undefined | null): CommonInfo => {
- if (!extra) return {};
- const extraObj = safeParse(extra);
- const params = extraObj?.inferParams;
- if (!params || (params && params.length == 0)) return {};
- const regionJudge = params[0]?.regionJudge;
- const judge = params[0]?.judge;
- const eventDurationMinMs = params[0]?.eventDurationMinMs;
- const eventDurationMinFrames = params[0]?.eventDurationMinFrames;
- const eventAlarmIntervalMs = params[0]?.eventAlarmIntervalMs;
- const eventAlarmIntervalFrames = params[0]?.eventAlarmIntervalFrames;
- const timeWindow = params[0]?.timeWindow;
- const ret = {} as CommonInfo;
- if (regionJudge || regionJudge == 0) {
- ret.regionJudge = regionJudge;
- }
- if (judge || judge == 0) {
- ret.judge = judge;
- }
- if (eventDurationMinMs || eventDurationMinMs == 0) {
- ret.eventDurationMinMs = eventDurationMinMs;
- }
- if (eventDurationMinFrames || eventDurationMinFrames == 0) {
- ret.eventDurationMinFrames = eventDurationMinFrames;
- }
- if (eventAlarmIntervalMs || eventAlarmIntervalMs == 0) {
- ret.eventAlarmIntervalMs = eventAlarmIntervalMs;
- }
- if (eventAlarmIntervalFrames || eventAlarmIntervalFrames == 0) {
- ret.eventAlarmIntervalFrames = eventAlarmIntervalFrames;
- }
- if (timeWindow) {
- ret.timeWindow = timeWindow;
- }
- return ret;
- };
- export const getTimeCompletion = (time: TimePeriodItem) => {
- if (!time.startDay || !time.endDay) {
- return false;
- }
- time.timeRangeList.forEach((item) => {
- if (!item.startTime || !item.endTime) {
- return false;
- }
- });
- return true;
- };
- /** 生成算法相关的提交参数 */
- export const createAlgoSubmitParams = (param, initialAlgoDetail) => {
- const inferParams = getInferParam(initialAlgoDetail.extra);
- inferParams.metaObjs = param.metaObjs;
- inferParams.regionJudge = param.regionJudge;
- inferParams.criticalCounts = param.criticalCounts;
- inferParams.judge = param.judge;
- inferParams.eventDurationMinMs = param.eventDurationMinMs;
- inferParams.eventDurationMinFrames = param.eventDurationMinFrames;
- inferParams.eventAlarmIntervalMs = param.eventAlarmIntervalMs;
- inferParams.eventAlarmIntervalFrames = param.eventAlarmIntervalFrames;
- inferParams.algoCode = initialAlgoDetail.algoInfo.code;
- inferParams.algoType = getAlgoType(initialAlgoDetail.algoInfo.extra);
- if (param.timeWindow) {
- inferParams.timeWindow = param.timeWindow;
- }
- const extraValue = {
- inferCode: param.inferCode,
- inferParams: [inferParams],
- } as any;
- const newParam = {
- algoId: initialAlgoDetail.algoId,
- detectionFrequency: param.detectionFrequency,
- detectionTime: param.detectionTime,
- extra: JSON.stringify(extraValue),
- };
- return newParam;
- };
- /** 相机关联的算法转化数据结构 */
- export const cameraAlgoToJSON = (detail: CameraAlgoItem) => {
- const enableCard = detail?.status === ALGO_ENABLED_STATUS.enabled ? true : false;
- const electronicFenceBool = detail?.electronicFence === FENCE_ENBALED_STATUS.enabled ? true : false;
- // const timeRangeArr = getDetectionTimeJSON(detail?.detectionTime) || [];
- const timeRangeArr = getDetectionTime(detail?.detectionTime) || [];
- const metaValues = getMetaValues(detail?.extra) || [];
- const commonInfo = getExtraCommonInfo(detail);
- return {
- ...detail,
- algoId: detail.algoId, // 兼容算法默认参数
- algoInfo: detail, // 兼容算法默认参数
- inferCode: getInferCode(detail?.extra),
- enableCardBool: enableCard,
- electronicFenceBool,
- timeRangeArr,
- metaValues,
- ...commonInfoToJSON(commonInfo),
- };
- };
- /** 算法自身数据结构转化json */
- export const algoMetaToJSON = (detail: QueryAlgoInfoRes) => {
- const enableCard = detail?.status === ALGO_ENABLED_STATUS.enabled ? true : false;
- const metaValues = getMetaValues(detail?.extra) || [];
- const commonInfo = getCommonInfoByExtra(detail.extra);
- return {
- ...detail,
- algoId: detail.id, // 兼容算法默认参数
- algoInfo: detail, // 兼容算法默认参数
- inferCode: getInferCode(detail?.extra),
- enableCardBool: enableCard,
- electronicFenceBool: false,
- timeRangeArr: [],
- metaValues,
- ...commonInfoToJSON(commonInfo),
- };
- };
- const commonInfoToJSON = (commonInfo) => {
- return {
- regionJudge: commonInfo.regionJudge || 0,
- judge: commonInfo.judge || commonInfo.judge == 0 ? commonInfo.judge : 1,
- eventDurationMinMs:
- commonInfo.eventDurationMinMs || commonInfo.eventDurationMinMs == 0 ? commonInfo.eventDurationMinMs : 1,
- eventDurationMinFrames:
- commonInfo.eventDurationMinFrames || commonInfo.eventDurationMinFrames == 0
- ? commonInfo.eventDurationMinFrames
- : 1,
- eventAlarmIntervalMs:
- commonInfo.eventAlarmIntervalMs || commonInfo.eventAlarmIntervalMs == 0 ? commonInfo.eventAlarmIntervalMs : 1,
- eventAlarmIntervalFrames:
- commonInfo.eventAlarmIntervalFrames || commonInfo.eventAlarmIntervalFrames == 0
- ? commonInfo.eventAlarmIntervalFrames
- : 1,
- timeWindow: commonInfo.timeWindow,
- };
- };
|