| 104 | * @doc {heading: 'Object Detection', subheading: 'Models'} |
| 105 | */ |
| 106 | export class CocoSsdTFJS extends ObjectDetector<CocoSsdTFJSInferenceOptions> { |
| 107 | constructor( |
| 108 | private cocoSsdModel?: cocoSsd.ObjectDetection, |
| 109 | private loadingOptions?: CocoSsdTFJSLoadingOptions) { |
| 110 | super(); |
| 111 | } |
| 112 | |
| 113 | async predict( |
| 114 | img: ImageData|HTMLImageElement|HTMLCanvasElement|HTMLVideoElement, |
| 115 | infereceOptions?: CocoSsdTFJSInferenceOptions): |
| 116 | Promise<ObjectDetectionResult> { |
| 117 | if (!this.cocoSsdModel) { |
| 118 | throw new Error('source model is not loaded'); |
| 119 | } |
| 120 | await ensureTFJSBackend(this.loadingOptions); |
| 121 | const cocoSsdResults = await this.cocoSsdModel.detect( |
| 122 | img, infereceOptions ? infereceOptions.maxNumBoxes : undefined, |
| 123 | infereceOptions ? infereceOptions.minScore : undefined); |
| 124 | const objects: DetectedObject[] = cocoSsdResults.map(result => { |
| 125 | return { |
| 126 | boundingBox: { |
| 127 | originX: result.bbox[0], |
| 128 | originY: result.bbox[1], |
| 129 | width: result.bbox[2], |
| 130 | height: result.bbox[3], |
| 131 | }, |
| 132 | className: result.class, |
| 133 | score: result.score, |
| 134 | }; |
| 135 | }); |
| 136 | const finalResult: ObjectDetectionResult = { |
| 137 | objects, |
| 138 | }; |
| 139 | return finalResult; |
| 140 | } |
| 141 | |
| 142 | cleanUp() { |
| 143 | if (!this.cocoSsdModel) { |
| 144 | throw new Error('source model is not loaded'); |
| 145 | } |
| 146 | return this.cocoSsdModel.dispose(); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | export const cocoSsdTfjsLoader = new CocoSsdTFJSLoader(); |
nothing calls this directly
no outgoing calls
no test coverage detected