Skip to content

Commit a04dc76

Browse files
committed
refactor(shared): move annotateRects to shared/img, remove unused bufferFromBase64
- Move annotateRects from evaluation/tests/util.ts to shared/src/img/box-select.ts - Export annotateRects from @midscene/shared/img - Remove unused bufferFromBase64 function from info.ts - Remove unused imports in box-select.ts
1 parent 5972b0a commit a04dc76

4 files changed

Lines changed: 33 additions & 38 deletions

File tree

‎packages/evaluation/tests/util.ts‎

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
22
import path from 'node:path';
33
import type { PlanningAIResponse, Rect } from '@midscene/core';
4-
import { NodeType } from '@midscene/shared/constants';
54
import {
6-
compositeElementInfoImg,
5+
annotateRects,
76
imageInfoOfBase64,
87
localImg2Base64,
98
} from '@midscene/shared/img';
109
import { WebPageContextParser } from '@midscene/web';
1110

11+
export { annotateRects };
12+
1213
export const repeatTime = 1;
1314

1415
export type TestCase = {
@@ -220,26 +221,3 @@ export async function buildContext(pageName: string) {
220221
const context = await WebPageContextParser(fakePage as any, {});
221222
return context;
222223
}
223-
224-
export async function annotateRects(
225-
imgBase64: string,
226-
rects: Rect[],
227-
prompt?: string,
228-
) {
229-
const markedImage = await compositeElementInfoImg({
230-
inputImgBase64: imgBase64,
231-
elementsPositionInfo: rects.map((rect, index) => {
232-
return {
233-
id: `rect-${index}`,
234-
rect,
235-
indexId: index + 1,
236-
attributes: { nodeType: NodeType.CONTAINER },
237-
content: '',
238-
center: [rect.left + rect.width / 2, rect.top + rect.height / 2],
239-
};
240-
}),
241-
annotationPadding: 0,
242-
prompt,
243-
});
244-
return markedImage;
245-
}

‎packages/shared/src/img/box-select.ts‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import assert from 'node:assert';
22
import type { PhotonImage as PhotonImageType } from '@silvia-odwyer/photon-node';
3+
import { NodeType } from '../constants';
34
import type { BaseElement, Rect } from '../types';
45
import getPhoton from './get-photon';
5-
import { bufferFromBase64, imageInfoOfBase64 } from './info';
66
import { photonFromBase64, photonToBase64 } from './transform';
77

88
// Simple 5x7 bitmap font for digits 0-9
@@ -548,3 +548,26 @@ export const processImageElementInfo = async (options: {
548548
compositeElementInfoImgWithoutTextBase64,
549549
};
550550
};
551+
552+
export async function annotateRects(
553+
imgBase64: string,
554+
rects: Rect[],
555+
prompt?: string,
556+
) {
557+
const markedImage = await compositeElementInfoImg({
558+
inputImgBase64: imgBase64,
559+
elementsPositionInfo: rects.map((rect, index) => {
560+
return {
561+
id: `rect-${index}`,
562+
rect,
563+
indexId: index + 1,
564+
attributes: { nodeType: NodeType.CONTAINER },
565+
content: '',
566+
center: [rect.left + rect.width / 2, rect.top + rect.height / 2],
567+
};
568+
}),
569+
annotationPadding: 0,
570+
prompt,
571+
});
572+
return markedImage;
573+
}

‎packages/shared/src/img/index.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
export {
2-
imageInfoOfBase64,
3-
bufferFromBase64,
4-
isValidPNGImageBuffer,
5-
} from './info';
1+
export { imageInfoOfBase64, isValidPNGImageBuffer } from './info';
62
export {
73
resizeAndConvertImgBuffer,
84
resizeImgBase64,
@@ -19,5 +15,9 @@ export {
1915
parseBase64,
2016
createImgBase64ByFormat,
2117
} from './transform';
22-
export { processImageElementInfo, compositeElementInfoImg } from './box-select';
18+
export {
19+
processImageElementInfo,
20+
compositeElementInfoImg,
21+
annotateRects,
22+
} from './box-select';
2323
export { drawBoxOnImage, savePositionImg } from './draw-box';

‎packages/shared/src/img/info.ts‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import assert from 'node:assert';
2-
import { Buffer } from 'node:buffer';
32
import type { Size } from '../types';
43
import getPhoton from './get-photon';
54

@@ -25,11 +24,6 @@ export async function imageInfoOfBase64(
2524
return { width, height };
2625
}
2726

28-
export async function bufferFromBase64(imageBase64: string): Promise<Buffer> {
29-
const base64Data = imageBase64.replace(/^data:image\/\w+;base64,/, '');
30-
return Buffer.from(base64Data, 'base64');
31-
}
32-
3327
/**
3428
* Check if the Buffer is a valid PNG image
3529
* @param buffer The Buffer to check

0 commit comments

Comments
 (0)