MCPcopy
hub / github.com/tensorflow/tfjs-models / CocoSsdTFJS

Class CocoSsdTFJS

tasks/src/tasks/object_detection/cocossd_tfjs.ts:106–148  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

104 * @doc {heading: 'Object Detection', subheading: 'Models'}
105 */
106export 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
150export const cocoSsdTfjsLoader = new CocoSsdTFJSLoader();

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected