Spaces:
Sleeping
Sleeping
File size: 3,938 Bytes
2b7aae2 | 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 | import ZeroClient, { Logger } from './ZeroClient';
import * as starry from '../../src/starry';
import PyProcessor from './PyProcessor';
import { destructPromise } from './async-queue';
import { getPort } from 'portfinder';
import util from 'util';
import { Options } from 'python-shell';
const getPortPromise = util.promisify(getPort);
export interface LayoutResult {
detection: starry.PageLayout;
theta: number;
interval: number;
sourceSize?: {
width: number;
height: number;
};
}
export interface PredictorInterface {
layout: (streams: Buffer[]) => LayoutResult[];
layout$reinforce: (streams: Buffer[], baseLayouts: LayoutResult[]) => LayoutResult[];
gauge: (streams: Buffer[]) => {
image: Buffer;
}[];
mask: (streams: Buffer[]) => {
image: Buffer;
}[];
semantic: (streams: Buffer[]) => any[];
textLoc: (streams: Buffer[]) => any[];
textOcr: (params: { buffers: Buffer[]; location: any[] }) => any[];
brackets: (params: { buffers: Buffer[] }) => any[];
topo: (params: { clusters: starry.EventCluster[] }) => any[];
gaugeRenderer: (params: [Buffer, Buffer, number]) => { buffer: Buffer; size: { width: number; height: number } };
jianpu: (params: { buffers: Buffer[] }) => any[];
// [source: Buffer, gauge: Buffer, baseY: number]
}
type PredictorType = keyof PredictorInterface;
export type PyClientsConstructOptions = Partial<Record<PredictorType, Options | string>>;
export class PyClients {
clients = new Map<string, Promise<ZeroClient>>();
constructor(public readonly options: PyClientsConstructOptions, public readonly logger: Logger = console) {}
async getClient(type: PredictorType) {
if (this.clients.has(type)) {
return this.clients.get(type);
}
const [promise, resolve, reject] = destructPromise<ZeroClient>();
const opt = this.options[type];
if (!opt) {
throw new Error(`no config for client \`${type}\` found`);
}
try {
if (typeof opt === 'string') {
const client = new ZeroClient();
client.bind(opt);
resolve(client);
} else {
const { scriptPath, ...option } = opt;
const client = new PyProcessor(scriptPath, option, this.logger);
await client.bind(`${await getPortPromise()}`);
resolve(client);
}
this.logger.info(`PyClients: ${type} started`);
} catch (err) {
this.logger.error(`PyClients: ${type} start fail: ${JSON.stringify(err)}`);
reject(err);
}
this.clients.set(type, promise);
return promise;
}
async checkHost(type: PredictorType): Promise<string> {
const client = await this.getClient(type);
return client.request('checkHost');
}
async warmup() {
const opts = Object.keys(this.options) as PredictorType[];
await Promise.all(opts.map((type) => this.getClient(type)));
}
/**
* 模型预测
* @param type layout | mask | gauge | semantic
* @param args
*/
async predictScoreImages<T extends PredictorType>(type: T, ...args: Parameters<PredictorInterface[T]>): Promise<ReturnType<PredictorInterface[T]>> {
const clientType = type.split('$')[0] as PredictorType;
const client = await this.getClient(clientType);
let res = null;
this.logger.info(`[predictor]: ${type} py start..`);
const start = Date.now();
switch (type) {
case 'layout':
res = await client.request('predictDetection', args);
break;
case 'layout$reinforce':
res = await client.request('predictReinforce', args);
break;
case 'gauge':
case 'mask':
res = await client.request('predict', args, { by_buffer: true });
break;
case 'semantic':
case 'textLoc':
res = await client.request('predict', args);
break;
case 'textOcr':
case 'brackets':
case 'topo':
case 'gaugeRenderer':
case 'jianpu':
res = await client.request('predict', ...args);
break;
default:
this.logger.error(`[predictor]: no predictor ${type}`);
}
this.logger.info(`[predictor]: ${type} py duration: ${Date.now() - start}ms`);
return res;
}
}
|