78 lines
2.1 KiB
JavaScript
78 lines
2.1 KiB
JavaScript
import { api } from "../../scripts/api.js";
|
|
|
|
/**
|
|
* Segment data for an image.
|
|
*/
|
|
export class Segments extends EventTarget {
|
|
|
|
constructor(segments) {
|
|
super();
|
|
|
|
this.sizes = [];
|
|
this.bboxes = [];
|
|
this.label = [];
|
|
this.labels = [];
|
|
this.masks = [];
|
|
this.unwanted = [];
|
|
|
|
for (const seg of segments) {
|
|
this.sizes.push(seg.size);
|
|
this.bboxes.push(seg.bbox);
|
|
this.label.push(seg.label || 'unknown');
|
|
this.labels.push(seg.labels || []);
|
|
this.masks.push(seg.mask);
|
|
this.unwanted.push(seg.unwanted);
|
|
}
|
|
}
|
|
|
|
// Get the URL for the image at index.
|
|
getMaskUrl (segn) {
|
|
const url = this.masks[segn];
|
|
if (url) {
|
|
return api.apiURL(`/view?filename=${encodeURIComponent(url.filename)}&type=${url.type ?? "input"}&subfolder=${url.subfolder ?? ""}&r=${Math.random()}`);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
// Get the resulting array of labels for segments.
|
|
getResult () {
|
|
return this.label.map((label, index) => {
|
|
return this.unwanted[index] ? 'unwanted' : label;
|
|
});
|
|
}
|
|
|
|
nextLabel(segn, offset = 1) {
|
|
const labels = this.labels[segn];
|
|
|
|
// Get the index of the next label in the list of options.
|
|
let index = labels.indexOf(this.label[segn]);
|
|
index = (index === -1) ? 0 : (index + offset) % labels.length;
|
|
if (index < 0) {
|
|
index = labels.length - 1;
|
|
}
|
|
this.label[segn] = labels[index];
|
|
this.unwanted[segn] = false;
|
|
|
|
this.dispatchEvent(new CustomEvent('segment-change', {
|
|
detail: {
|
|
segn,
|
|
label: this.label[segn],
|
|
unwanted: this.unwanted[segn]
|
|
}
|
|
}));
|
|
}
|
|
|
|
toggleUnwanted(segn) {
|
|
this.unwanted[segn] = !this.unwanted[segn];
|
|
|
|
this.dispatchEvent(new CustomEvent('segment-change', {
|
|
detail: {
|
|
segn,
|
|
label: this.label[segn],
|
|
unwanted: this.unwanted[segn]
|
|
}
|
|
}));
|
|
}
|
|
|
|
}
|