270 lines
11 KiB
JavaScript
270 lines
11 KiB
JavaScript
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(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
|
|
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
import { cloneDeep, dataURLToBlob, get, isNull } from "@zhst/func";
|
|
var proto = {
|
|
Common: {
|
|
AlgorithmVersion: {
|
|
VERSION_REID_HEAD_ATTR: '形体',
|
|
VERSION_FACE: '人脸',
|
|
VERSION_NON_MOTOR_VEHICLE: '非机动车'
|
|
}
|
|
}
|
|
};
|
|
export var ALGORITHM_VERSION = _defineProperty(_defineProperty(_defineProperty({}, '7', '形体'), '4', '人脸'), '6', '非机动车');
|
|
export var algorithmVersions = _toConsumableArray(Object.keys(ALGORITHM_VERSION));
|
|
/**
|
|
* 格式化工具
|
|
* @param originData
|
|
* @returns
|
|
*/
|
|
export var getOdRect = function getOdRect(originData) {
|
|
var data = get(originData, 'objects', []).filter(function (v) {
|
|
return !isNull(get(v, 'bboxRatio')) || get(v, 'objectIndex.objectId') !== '0';
|
|
}).map(function (v, index) {
|
|
var rect = get(v, 'bboxRatio');
|
|
var extendBox = get(v, '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 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,
|
|
id: index,
|
|
qualityScore: qualityScore,
|
|
algorithmVersion: algorithmVersion,
|
|
objectRectIndex: objectRectIndex,
|
|
objectType: objectType,
|
|
objectId: objectId,
|
|
frameTimestamp: frameTimestamp,
|
|
sourceObjectId: sourceObjectId,
|
|
extendBox: extendBox
|
|
};
|
|
});
|
|
return data;
|
|
};
|
|
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 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 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;
|
|
}; |