File size: 1,627 Bytes
9210bde
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import {OBJECT_DETECTION_MODEL_ID} from '../../shared/clusterModels';
import type {ObjectDetector} from '../detection/ObjectDetector';
import type {DetectionResult} from '../detection/workerMessages';
import {AbstractModelTaskHandler} from './AbstractModelTaskHandler';
import type {ClusterTaskHostContext, HostTaskMessage} from './types';

export class DetectTaskHandler extends AbstractModelTaskHandler {
  readonly kind = 'detect' as const;

  constructor(
    host: ClusterTaskHostContext,
    private readonly getDetector: () => ObjectDetector | null,
  ) {
    super(host);
  }

  async process(task: HostTaskMessage): Promise<void> {
    const detector = this.getDetector();
    if (this.host.getSelectedModelId() !== OBJECT_DETECTION_MODEL_ID || !detector) {
      await this.completeTask(
        task.id,
        this.taskError(
          'This host is not running the object detection model. Re-register with "Object detection" selected.',
        ),
      );
      return;
    }

    await this.withVideoFrame(task, async (frame) => {
      const results = await detector.detect(frame, {threshold: task.threshold ?? 0.5});
      return {
        status: 'done',
        kind: 'detect',
        result: DetectTaskHandler.serializeDetectionResults(results),
      };
    });
  }

  private static serializeDetectionResults(results: DetectionResult[]): unknown[] {
    return results.map((result) => ({
      label: result.label,
      score: result.score,
      box: {
        xmin: result.box.xmin,
        ymin: result.box.ymin,
        xmax: result.box.xmax,
        ymax: result.box.ymax,
      },
    }));
  }
}