lost-sia
Version:
Single Image Annotation Tool
2 lines (1 loc) • 2.71 kB
JavaScript
function L(r,o,t,n){switch(t){case"bBox":const x=o.width*r.w,c=o.height*r.h,y=n.x+o.width*r.x-x/2,h=n.y+o.height*r.y-c/2;return[{x:y,y:h},{x:y+x,y:h},{x:y+x,y:h+c},{x:y,y:h+c}];case"point":return[{x:n.x+o.width*r.x,y:n.y+o.height*r.y}];case"line":case"polygon":return r.map(i=>({x:n.x+o.width*i.x,y:n.y+o.height*i.y}))}}function p(r,o,t,n={x:0,y:0}){const x=o.width-2*n.x,c=o.height-2*n.y;switch(t){case"bBox":const y=r.map(s=>s.x),h=r.map(s=>s.y),i=Math.min(...y)-n.x,M=Math.max(...y)-n.x,u=Math.min(...h)-n.y,d=Math.max(...h)-n.y,e=M-i,a=d-u,b=i+e/2,B=u+a/2;return{x:b/x,y:B/c,w:e/x,h:a/c};case"point":return{x:(r[0].x-n.x)/x,y:(r[0].y-n.y)/c};case"line":case"polygon":return r.map(s=>({x:(s.x-n.x)/x,y:(s.y-n.y)/c}))}}function P(r){const o=r.map(c=>c.x),t=r.map(c=>c.y),n={x:Math.min(...o),y:Math.min(...t)},x={x:Math.max(...o),y:Math.max(...t)};return[n,x]}function X(r,o,t,n){const x=p(r,o,t);switch(t){case"bBox":return Math.abs(x.w*x.h)*n.width*n.height;case"line":case"point":return;case"polygon":let c=0;if(x.length>2){let y=x.length-1;for(let h=0;h<x.length;h++)c=c+(x[y].x+x[h].x)*(x[y].y-x[h].y),y=h}return Math.abs(c/2)*n.width*n.height;default:return}}function Y(r,o,t){return r.map(n=>({x:n.x+o,y:n.y+t}))}function w(r,o){switch(o){case"bBox":return r;case"point":case"line":case"polygon":let t=0,n=0,x=1/0,c=1/0;return r.forEach(y=>{y.x>t&&(t=y.x),y.y>n&&(n=y.y),y.x<x&&(x=y.x),y.y<c&&(c=y.y)}),[{x,y:c},{x:t,y:c},{x,y:n},{x:t,y:n}]}}function l(r,o){let t;switch(o){case"point":return r[0];case"line":case"polygon":case"bBox":t=w(r,o);const n=t[1].x-t[0].x,x=t[3].y-t[0].y;return{x:t[0].x+n/2,y:t[0].y+x/2}}}function m(r){let o=1/0,t=[];return r.forEach(n=>{n.x<o?(o=n.x,t=[],t.push(n)):n.x===o&&t.push(n)}),t}function A(r){let o=1/0,t=[];return r.forEach(n=>{n.y<o?(o=n.y,t=[],t.push(n)):n.y===o&&t.push(n)}),t}function I(r,o,t){const n=o.width-2*t.x,x=o.height-2*t.y;return r.map(y=>{let h=y.x,i=y.y;return y.x<=t.x?h=t.x:y.x>n+t.x&&(h=n+t.x),y.y<t.y?i=t.y:y.y>x+t.y&&(i=x+t.y),{x:h,y:i}})}function E(r,o,t){return t=t*(Math.PI/180),r.map(x=>({x:Math.round(Math.cos(t)*(x.x-o.x)-Math.sin(t)*(x.y-o.y)+o.x),y:Math.round(Math.sin(t)*(x.x-o.x)+Math.cos(t)*(x.y-o.y)+o.y)}))}function D(r,o,t){const n=t.width/o.width,x=t.height/o.height;return r.map(c=>({x:parseInt(c.x*n),y:parseInt(c.y*x)}))}const H={toSia:L,toBackend:p,getMinMaxPoints:P,getArea:X,move:Y,getBox:w,getCenter:l,getMostLeftPoint:m,getTopPoint:A,correctAnnotation:I,rotateAnnotation:E,resizeAnnoData:D};export{I as correctAnnotation,H as default,X as getArea,w as getBox,l as getCenter,P as getMinMaxPoints,m as getMostLeftPoint,A as getTopPoint,Y as move,D as resizeAnnoData,E as rotateAnnotation,p as toBackend,L as toSia};