358 lines
14 KiB
JavaScript
358 lines
14 KiB
JavaScript
var _ALGORITHM_VERSION;
|
||
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
||
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
||
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
||
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
|
||
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
||
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
||
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
|
||
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
|
||
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
|
||
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
||
function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
|
||
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
|
||
import { cloneDeep, get, isNull } from 'lodash-es';
|
||
import { dataURLToBlob } from "../file";
|
||
var proto = {
|
||
Common: {
|
||
AlgorithmVersion: {
|
||
VERSION_REID_HEAD_ATTR: '形体',
|
||
VERSION_FACE: '人脸',
|
||
VERSION_NON_MOTOR_VEHICLE: '非机动车'
|
||
}
|
||
}
|
||
};
|
||
export var ALGORITHM_VERSION = (_ALGORITHM_VERSION = {}, _defineProperty(_ALGORITHM_VERSION, '7', '形体'), _defineProperty(_ALGORITHM_VERSION, '4', '人脸'), _defineProperty(_ALGORITHM_VERSION, '6', '非机动车'), _ALGORITHM_VERSION);
|
||
export var algorithmVersions = _toConsumableArray(Object.keys(ALGORITHM_VERSION));
|
||
export var getBikeExtendRect = function getBikeExtendRect(rect, maxW) {
|
||
var newRect = _objectSpread({}, rect);
|
||
//向上扩大一倍
|
||
var oldY = cloneDeep(rect.y);
|
||
newRect.y = newRect.y - newRect.h < 0 ? 0 : newRect.y - newRect.h;
|
||
newRect.h += oldY - newRect.y;
|
||
var newX = Math.round(newRect.x - newRect.w * 0.15);
|
||
if (newX < 0) {
|
||
newX = 0;
|
||
}
|
||
var newW = newRect.x - newX + newRect.w + Math.round(newRect.w * 0.15);
|
||
if (newX + newW > maxW) {
|
||
newW = maxW - newX;
|
||
}
|
||
newRect.x = newX;
|
||
newRect.w = newW;
|
||
return newRect;
|
||
};
|
||
export var getOtherExtendRect = function getOtherExtendRect(srcRect, maxW, maxH, type) {
|
||
var wExtendRadio = 0.25;
|
||
var upExtendRadio = 0.25;
|
||
var downExtendRadio = 0.25;
|
||
var fixPersonExtend = true;
|
||
var nx = 0;
|
||
var nw = 0;
|
||
nx = srcRect.x - Math.round(srcRect.w * wExtendRadio);
|
||
if (nx < 0) {
|
||
nx = 0;
|
||
}
|
||
nw = srcRect.x - nx + srcRect.w + Math.round(srcRect.w * wExtendRadio);
|
||
if (nx + nw > maxW) {
|
||
nw = maxW - nx;
|
||
}
|
||
var ny = 0;
|
||
var nh = 0;
|
||
ny = srcRect.y - Math.round(upExtendRadio * srcRect.h);
|
||
if (ny < 0) {
|
||
ny = 0;
|
||
}
|
||
nh = srcRect.y - ny + srcRect.h + Math.round(srcRect.h * downExtendRadio);
|
||
if (ny + nh > maxH) {
|
||
nh = maxH - ny;
|
||
}
|
||
var newRect = {
|
||
x: nx,
|
||
y: ny,
|
||
w: nw,
|
||
h: nh
|
||
};
|
||
if ((type === proto.Common.AlgorithmVersion.VERSION_REID_HEAD_ATTR || type === proto.Common.AlgorithmVersion.VERSION_FACE) && fixPersonExtend) {
|
||
var fixW = Math.round(nh * 0.75);
|
||
if (nw < fixW) {
|
||
// 应该扩展宽度
|
||
var newX = nx + Math.round(nw / 2.0 - 0.5 * fixW);
|
||
if (newX < 0) {
|
||
newX = 0;
|
||
}
|
||
var newW = fixW;
|
||
if (newW + newX > maxW) {
|
||
newW = maxW - newX;
|
||
}
|
||
newRect = {
|
||
x: newX,
|
||
y: ny,
|
||
w: newW,
|
||
h: nh
|
||
};
|
||
} else if (nw > fixW) {
|
||
// 应该扩展高度
|
||
var fixH = Math.round(nw * 1.333);
|
||
var newY = ny + Math.round(nh / 2.0 - 0.5 * fixH);
|
||
if (newY < 0) {
|
||
newY = 0;
|
||
}
|
||
var newH = fixH;
|
||
if (newY + newH > maxH) {
|
||
newH = maxH - newY;
|
||
}
|
||
newRect = {
|
||
x: nx,
|
||
y: newY,
|
||
w: nw,
|
||
h: newH
|
||
};
|
||
}
|
||
}
|
||
return newRect;
|
||
};
|
||
export var getNormalization = function getNormalization(srcRect, maxW, maxH) {
|
||
var newRect = _objectSpread({}, srcRect);
|
||
newRect.x = srcRect.x / maxW;
|
||
newRect.y = srcRect.y / maxH;
|
||
newRect.w = srcRect.w / maxW;
|
||
newRect.h = srcRect.h / maxH;
|
||
if (newRect.x + newRect.w > 1) {
|
||
newRect.w = 1 - newRect.x;
|
||
}
|
||
if (newRect.y + newRect.h > 1) {
|
||
newRect.h = 1 - newRect.y;
|
||
}
|
||
return newRect;
|
||
};
|
||
//传入od框 穿出 od扩展框
|
||
export var getExtendRect = function getExtendRect(normalizationRect, imgW, imgH, type) {
|
||
var rect = {
|
||
x: normalizationRect.x * imgW,
|
||
y: normalizationRect.y * imgH,
|
||
w: normalizationRect.w * imgW,
|
||
h: normalizationRect.h * imgH
|
||
};
|
||
var newRect;
|
||
if (type === proto.Common.AlgorithmVersion.VERSION_NON_MOTOR_VEHICLE) {
|
||
newRect = getBikeExtendRect(rect, imgW);
|
||
} else {
|
||
newRect = getOtherExtendRect(rect, imgW, imgH, type);
|
||
}
|
||
newRect = getNormalization(newRect, imgW, imgH);
|
||
return newRect;
|
||
};
|
||
export var getTransformRect = function getTransformRect(image, transform, rect) {
|
||
var canvasRect = {
|
||
x: rect.x,
|
||
y: rect.y,
|
||
x2: rect.x + rect.w,
|
||
y2: rect.h + rect.y
|
||
};
|
||
//1.转成缩放前的坐标
|
||
var translateX = transform.translateX,
|
||
translateY = transform.translateY,
|
||
scale = transform.scale,
|
||
rotate = transform.rotate;
|
||
var originAxisRect = {
|
||
x: (canvasRect.x - translateX) / scale,
|
||
y: (canvasRect.y - translateY) / scale,
|
||
x2: (canvasRect.x2 - translateX) / scale,
|
||
y2: (canvasRect.y2 - translateY) / scale
|
||
};
|
||
//2.转成图片坐标
|
||
//不考虑旋转 图片原点就是坐标原点
|
||
var imgAxisRect = originAxisRect;
|
||
//但是旋转90度后图片不在坐标原点 加上这部分diff
|
||
if (rotate % 180 !== 0) {
|
||
//90度调整偏移量
|
||
var offsetX = -(image.height - image.width) / 2;
|
||
var offsetY = -(image.width - image.height) / 2;
|
||
imgAxisRect = {
|
||
x: originAxisRect.x - offsetX,
|
||
y: originAxisRect.y - offsetY,
|
||
x2: originAxisRect.x2 - offsetX,
|
||
y2: originAxisRect.y2 - offsetY
|
||
};
|
||
}
|
||
//3.限制框不要超出图片
|
||
var imgW = image.width;
|
||
var imgH = image.height;
|
||
if (rotate % 180 !== 0) {
|
||
var _ref = [imgH, imgW];
|
||
imgW = _ref[0];
|
||
imgH = _ref[1];
|
||
}
|
||
imgAxisRect.x = Math.min(imgW, Math.max(imgAxisRect.x, 0));
|
||
imgAxisRect.y = Math.min(imgH, Math.max(imgAxisRect.y, 0));
|
||
imgAxisRect.x2 = Math.min(imgW, Math.max(imgAxisRect.x2, 0));
|
||
imgAxisRect.y2 = Math.min(imgH, Math.max(imgAxisRect.y2, 0));
|
||
//获取归一化坐标
|
||
var endRect = {
|
||
x: imgAxisRect.x2 > imgAxisRect.x ? imgAxisRect.x : imgAxisRect.x2,
|
||
y: imgAxisRect.y2 > imgAxisRect.y ? imgAxisRect.y : imgAxisRect.y2,
|
||
w: Math.abs(imgAxisRect.x2 - imgAxisRect.x),
|
||
h: Math.abs(imgAxisRect.y2 - imgAxisRect.y)
|
||
};
|
||
return getNormalization(endRect, imgW, imgH);
|
||
};
|
||
|
||
//旋转图片后转成file 对象
|
||
export var getRotateImg = function getRotateImg(image, rotate) {
|
||
var _commonCanvas$parentN;
|
||
var imgW = image.width;
|
||
var imgH = image.height;
|
||
if (rotate % 180 !== 0) {
|
||
var _ref2 = [imgH, imgW];
|
||
imgW = _ref2[0];
|
||
imgH = _ref2[1];
|
||
}
|
||
var commonCanvas = document.createElement('canvas');
|
||
commonCanvas.width = imgW;
|
||
commonCanvas.height = imgH;
|
||
commonCanvas.style.display = 'none';
|
||
document.body.appendChild(commonCanvas);
|
||
var commonCtx = commonCanvas.getContext('2d');
|
||
//移动到图片中心 旋转
|
||
|
||
commonCtx === null || commonCtx === void 0 || commonCtx.save();
|
||
if (rotate % 180 !== 0) {
|
||
//90度调整偏移量
|
||
commonCtx === null || commonCtx === void 0 || commonCtx.translate((image.height - image.width) / 2, (image.width - image.height) / 2);
|
||
}
|
||
commonCtx === null || commonCtx === void 0 || commonCtx.translate(image.width / 2, image.height / 2);
|
||
commonCtx === null || commonCtx === void 0 || commonCtx.rotate(rotate / 180 * Math.PI);
|
||
commonCtx === null || commonCtx === void 0 || commonCtx.translate(-image.width / 2, -image.height / 2);
|
||
commonCtx === null || commonCtx === void 0 || commonCtx.drawImage(image, 0, 0);
|
||
commonCtx === null || commonCtx === void 0 || commonCtx.restore();
|
||
var dataUrl = commonCanvas.toDataURL('image/jpeg');
|
||
var blobData = dataURLToBlob(dataUrl);
|
||
var file = new window.File([blobData], "".concat(new Date().getTime()), {
|
||
type: 'image/jpeg'
|
||
});
|
||
(_commonCanvas$parentN = commonCanvas.parentNode) === null || _commonCanvas$parentN === void 0 || _commonCanvas$parentN.removeChild(commonCanvas);
|
||
return file;
|
||
};
|
||
|
||
/**
|
||
* 格式化工具
|
||
* @param originData
|
||
* @returns
|
||
*/
|
||
export var getOdRect = function getOdRect(originData) {
|
||
var data = get(originData, 'objects', []).filter(function (v) {
|
||
return !isNull(get(v, 'infoOnSource.bboxInFrame.bboxRatio'));
|
||
}).map(function (v, index) {
|
||
// objectId==0 特征没有提取到过滤1掉
|
||
var rect = get(v, 'infoOnSource.bboxInFrame.bboxRatio');
|
||
var extendBox = get(v, 'infoOnSource.bboxInFrame.extendBoxRatio');
|
||
var frameTimestamp = get(v, 'timestamp'); //时间戳创建档案的时候需要
|
||
var qualityScore = get(v, 'qualityScore');
|
||
var algorithmVersion = get(v, 'objectType') === 'OBJECT_TYPE_PEDESTRAIN' ? 'VERSION_REID_HEAD_ATTR' : get(v, 'objectType') === 'OBJECT_TYPE_FACE' ? 'VERSION_FACE' : 'VERSION_REID_HEAD_ATTR';
|
||
var featureData = get(v, 'feature', []).filter(function (v) {
|
||
return v.type === 'FEATURE_TYPE_BYTE';
|
||
});
|
||
var objectRectIndex = algorithmVersion === 'VERSION_FACE' ? 0 : 1;
|
||
var objectType = get(v, 'objectType');
|
||
var objectId = get(v, 'objectIndex.objectId');
|
||
var sourceObjectId = get(v, 'sourceObjectId');
|
||
return {
|
||
x: rect.x,
|
||
y: rect.y,
|
||
w: rect.w,
|
||
h: rect.h,
|
||
// faceCorrectImage: faceCorrectImage,
|
||
id: index,
|
||
qualityScore: qualityScore,
|
||
algorithmVersion: algorithmVersion,
|
||
featureData: get(featureData, '0.featureByte'),
|
||
objectRectIndex: objectRectIndex,
|
||
objectType: objectType,
|
||
objectId: objectId,
|
||
frameTimestamp: frameTimestamp,
|
||
sourceObjectId: sourceObjectId,
|
||
extendBox: extendBox
|
||
};
|
||
});
|
||
if (data.length > 0) {
|
||
data = data.filter(function (v) {
|
||
return v.objectId !== '0';
|
||
});
|
||
} else {
|
||
throw new Error('empty');
|
||
}
|
||
return data;
|
||
};
|
||
|
||
//档案库od
|
||
export var getOdRectV2 = function getOdRectV2(originData) {
|
||
var resp = originData.odv2Result[0];
|
||
var subObjects = []; //形体
|
||
var data = get(resp, 'objects', []).filter(function (v) {
|
||
return !isNull(get(v, 'subObjects[0].infoOnSource.bboxInFrame.bboxRatio'));
|
||
}).map(function (v, index) {
|
||
var rect = get(v, 'infoOnSource.bboxInFrame.bboxRatio');
|
||
var qualityScore = get(v, 'qualityScore');
|
||
var algorithmVersion = get(v, 'objectType');
|
||
var featrueData = get(v, 'feature', []).filter(function (v) {
|
||
return v.name === 'feature-body' || v.name === 'feature-face';
|
||
});
|
||
var objectRectIndex = algorithmVersion === 'OBJECT_TYPE_FACE' ? 0 : 1;
|
||
var objectType = get(v, 'objectType');
|
||
var objectId = get(v, 'objectIndex.objectId');
|
||
//如果存在subObjects的数组不为null表示形体里面带人脸,人脸的od框也要显示出来
|
||
if (get(v, 'subObjects', []).length) {
|
||
get(v, 'subObjects', []).forEach(function (e) {
|
||
var rect = get(e, 'infoOnSource.bboxInFrame.bboxRatio');
|
||
var qualityScore = get(e, 'qualityScore');
|
||
var algorithmVersion = get(e, 'objectType');
|
||
var featrueData = get(e, 'feature', []).filter(function (v) {
|
||
return v.name === 'feature-body' || v.name === 'feature-face';
|
||
});
|
||
var objectRectIndex = algorithmVersion === 'OBJECT_TYPE_FACE' ? 0 : 1;
|
||
var objectType = get(e, 'objectType');
|
||
var objectId = get(e, 'objectIndex.objectId');
|
||
subObjects.push({
|
||
x: rect.x,
|
||
y: rect.y,
|
||
w: rect.w,
|
||
h: rect.h,
|
||
id: index,
|
||
qualityScore: qualityScore,
|
||
algorithmVersion: algorithmVersion,
|
||
featrueData: featrueData.length ? featrueData[0].featureByte : '',
|
||
objectRectIndex: objectRectIndex,
|
||
objectType: objectType,
|
||
objectId: objectId
|
||
});
|
||
});
|
||
}
|
||
return {
|
||
x: rect.x,
|
||
y: rect.y,
|
||
w: rect.w,
|
||
h: rect.h,
|
||
id: index,
|
||
qualityScore: qualityScore,
|
||
algorithmVersion: algorithmVersion,
|
||
featrueData: featrueData[0].featureByte,
|
||
objectRectIndex: objectRectIndex,
|
||
objectType: objectType,
|
||
objectId: objectId
|
||
};
|
||
});
|
||
var brr = data.concat(subObjects).map(function (v, vs) {
|
||
if (String(v.id)) {
|
||
v.id = vs;
|
||
}
|
||
return v;
|
||
});
|
||
if (brr.length > 0) {
|
||
console.log(brr, 'data111');
|
||
} else {
|
||
throw new Error('empty');
|
||
}
|
||
console.log(brr);
|
||
return brr;
|
||
}; |