File size: 4,645 Bytes
6f1c297
3d6dd72
6f1c297
3d6dd72
6f1c297
 
 
 
 
 
 
 
 
 
 
f574881
6f1c297
 
 
f574881
 
 
 
 
 
 
 
6f1c297
 
 
 
 
 
 
 
 
f574881
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6f1c297
f574881
 
 
 
6f1c297
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3d6dd72
6f1c297
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import { pack, unpack } from 'msgpackr';
import * as zmq from 'zeromq';
import { config, PredictorType } from '../config.js';
import type { PageLayoutDetection } from '../types/layout.js';

interface ZeroResponse {
	code: number;
	msg: string;
	data?: any;
}

export class ZeroClient {
	private socket: zmq.Request;
	private address: string;
	private connected = false;
	private pending = Promise.resolve();

	constructor(address: string) {
		this.address = address;
		this.socket = this.createSocket();
	}

	private createSocket(): zmq.Request {
		const socket = new zmq.Request();
		socket.receiveTimeout = 300000;
		socket.sendTimeout = 15000;
		return socket;
	}

	async connect(): Promise<void> {
		if (this.connected) return;
		await this.socket.connect(this.address);
		this.connected = true;
	}

	async request<T = any>(method: string, args?: any[], kwargs?: Record<string, any>): Promise<T> {
		const run = async () => {
			if (!this.connected) {
				await this.connect();
			}

			const msg: any = { method };
			if (args) msg.args = args;
			if (kwargs) msg.kwargs = kwargs;

			try {
				const packed = pack(msg);
				await this.socket.send(packed);
				const [response] = await this.socket.receive();
				const result = unpack(response) as ZeroResponse;

				if (result.code === 0) {
					return result.data as T;
				}

				throw new Error(result.msg || 'Predictor request failed');
			} catch (error) {
				await this.reset();
				throw error;
			}
		};

		const result = this.pending.then(run, run);
		this.pending = result.then(
			() => undefined,
			() => undefined
		);
		return result;
	}

	private async reset(): Promise<void> {
		this.socket.close();
		this.socket = this.createSocket();
		this.connected = false;
	}

	async close(): Promise<void> {
		if (this.connected) {
			this.socket.close();
			this.connected = false;
		}
	}
}

// Predictor client pool
const clients = new Map<PredictorType, ZeroClient>();

export function getPredictor(type: PredictorType): ZeroClient {
	if (!clients.has(type)) {
		const address = config.predictors[type];
		clients.set(type, new ZeroClient(address));
	}
	return clients.get(type)!;
}

export async function closeAllPredictors(): Promise<void> {
	for (const client of clients.values()) {
		await client.close();
	}
	clients.clear();
}

// High-level predictor functions
export interface LayoutResult {
	detection: PageLayoutDetection;
	theta: number;
	interval: number;
	sourceSize?: {
		width: number;
		height: number;
	};
}

export interface GaugeResult {
	image: Buffer;
}

export interface MaskResult {
	image: Buffer;
}

export interface SemanticResult {
	clusters: any[];
}

export interface LocResult {
	boxes: number[][];
	scores: number[];
}

export interface OcrResult {
	texts: string[];
	scores: number[];
}

// Layout: predictDetection([buffer])
export async function predictLayout(imageData: Buffer): Promise<LayoutResult> {
	const client = getPredictor('layout');
	const results = await client.request<LayoutResult[]>('predictDetection', [[imageData]]);
	return results[0];
}

// Gauge: predict([buffer], by_buffer=true)
export async function predictGauge(imageData: Buffer): Promise<GaugeResult> {
	const client = getPredictor('gauge');
	const results = await client.request<GaugeResult[]>('predict', [[imageData]], { by_buffer: true });
	return results[0];
}

// Mask: predict([buffer], by_buffer=true)
export async function predictMask(imageData: Buffer): Promise<MaskResult> {
	const client = getPredictor('mask');
	const results = await client.request<MaskResult[]>('predict', [[imageData]], { by_buffer: true });
	return results[0];
}

// Semantic: predict([buffer])
export async function predictSemantic(imageData: Buffer): Promise<SemanticResult> {
	const client = getPredictor('semantic');
	const results = await client.request<SemanticResult[]>('predict', [[imageData]]);
	return results[0];
}

// Loc (text localization): predict([buffer])
export async function predictLoc(imageData: Buffer): Promise<LocResult> {
	const client = getPredictor('loc');
	const results = await client.request<LocResult[]>('predict', [[imageData]]);
	return results[0];
}

// OCR: predict(buffers=[buffer], location=[...])
export async function predictOcr(imageData: Buffer, locations: any[]): Promise<OcrResult> {
	const client = getPredictor('ocr');
	return client.request<OcrResult>('predict', [], { buffers: [imageData], location: locations });
}

// Brackets: predict(buffers=[buffer])
export async function predictBrackets(imageData: Buffer): Promise<any> {
	const client = getPredictor('brackets');
	return client.request('predict', [], { buffers: [imageData] });
}