Skip to content

Commit 9d32d7c

Browse files
authored
:chore: Get rid of duplicate utils (#515)
1 parent 159e7a6 commit 9d32d7c

File tree

5 files changed

+9
-37
lines changed

5 files changed

+9
-37
lines changed

web_ui/packages/smart-tools/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,8 @@
22
// LIMITED EDGE SOFTWARE DISTRIBUTION LICENSE
33

44
export { default as OpenCVLoader } from './src/utils/opencv-loader';
5+
56
export { Watershed } from './src/watershed/watershed';
67
export { type WatershedInstance, type WatershedPolygon } from './src/watershed/interfaces';
8+
9+
export { formatContourToPoints, approximateShape } from './src/utils/utils';

web_ui/src/pages/annotator/tools/segment-anything-tool/model/post-processing.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// Copyright (C) 2022-2025 Intel Corporation
22
// LIMITED EDGE SOFTWARE DISTRIBUTION LICENSE
33

4+
import { approximateShape } from '@geti/smart-tools';
45
import type cv from 'OpenCVTypes';
56

67
import { Circle, Point, Polygon, Rect, RotatedRect, Shape } from '../../../../../core/annotations/shapes.interface';
78
import { ShapeType } from '../../../../../core/annotations/shapetype.enum';
8-
import { approximateShape } from '../../../../../webworkers/utils';
99
import type { SegmentAnythingResult } from './segment-anything-result';
1010

1111
interface PostProcessorConfig {

web_ui/src/webworkers/grabcut.worker.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33

44
// Dependencies get bundled into the worker
55

6-
import { OpenCVLoader } from '@geti/smart-tools';
6+
import { approximateShape, OpenCVLoader } from '@geti/smart-tools';
77
import { expose } from 'comlink';
88
import type OpenCVTypes from 'OpenCVTypes';
99

1010
import { Point, Polygon, Rect } from '../core/annotations/shapes.interface';
1111
import { ShapeType } from '../core/annotations/shapetype.enum';
1212
import { GrabcutToolType } from '../pages/annotator/tools/grabcut-tool/grabcut-tool.enums';
1313
import { GrabcutData } from '../pages/annotator/tools/grabcut-tool/grabcut-tool.interface';
14-
import { approximateShape, getMatFromPoints, getPointsFromMat } from './utils';
14+
import { getMatFromPoints, getPointsFromMat } from './utils';
1515

1616
declare const self: DedicatedWorkerGlobalScope;
1717

web_ui/src/webworkers/ritm.worker.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
// Dependencies get bundled into the worker
55

6-
import { OpenCVLoader } from '@geti/smart-tools';
6+
import { approximateShape, OpenCVLoader } from '@geti/smart-tools';
77
import { expose } from 'comlink';
88
import ndarray from 'ndarray';
99
import ops from 'ndarray-ops';
@@ -21,7 +21,7 @@ import {
2121
} from '../pages/annotator/tools/ritm-tool/ritm-tool.interface';
2222
import { isPolygonValid } from '../pages/annotator/tools/utils';
2323
import { sessionParams } from '../pages/annotator/tools/wasm-utils';
24-
import { approximateShape, concatFloat32Arrays, loadSource, stackPlanes } from './utils';
24+
import { concatFloat32Arrays, loadSource, stackPlanes } from './utils';
2525

2626
declare const self: DedicatedWorkerGlobalScope;
2727

web_ui/src/webworkers/utils.ts

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,12 @@
11
// Copyright (C) 2022-2025 Intel Corporation
22
// LIMITED EDGE SOFTWARE DISTRIBUTION LICENSE
33

4+
import { approximateShape } from '@geti/smart-tools';
45
import axios from 'axios';
56
import type OpenCVTypes from 'OpenCVTypes';
67

78
import { Point } from '../core/annotations/shapes.interface';
89

9-
export const formatContourToPoints = (
10-
mask: OpenCVTypes.Mat,
11-
contour: OpenCVTypes.Mat,
12-
width: number,
13-
height: number
14-
): Point[] => {
15-
const points: Point[] = [];
16-
17-
if (!contour?.rows) {
18-
return points;
19-
}
20-
21-
for (let row = 0; row < contour.rows; row++) {
22-
points.push({
23-
x: (contour.intAt(row, 0) / mask.cols) * width,
24-
y: (contour.intAt(row, 1) / mask.rows) * height,
25-
});
26-
}
27-
28-
return points;
29-
};
30-
3110
export const isPointOutsideOfBounds = (limit: OpenCVTypes.Rect, point: OpenCVTypes.Point | Point): boolean =>
3211
point.x <= limit.x || point.x >= limit.width || point.y <= limit.y || point.y >= limit.height;
3312

@@ -42,16 +21,6 @@ export const optimizePolygonAndCV = (CV: OpenCVTypes.cv, points: Point[], isClos
4221
return newPoints;
4322
};
4423

45-
//It approximates a contour shape to another shape with less number of vertices
46-
export const approximateShape = (CV: OpenCVTypes.cv, contour: OpenCVTypes.Mat, isClose = true): OpenCVTypes.Mat => {
47-
const epsilon = 1.0;
48-
const newContour = new CV.Mat();
49-
50-
CV.approxPolyDP(contour, newContour, epsilon, isClose);
51-
52-
return newContour;
53-
};
54-
5524
export const getMatFromPoints = (CV: OpenCVTypes.cv, points: Point[], offset = { x: 0, y: 0 }): OpenCVTypes.Mat => {
5625
const pointsMat = new CV.Mat(points.length, 1, CV.CV_32SC2);
5726

0 commit comments

Comments
 (0)