ora editor
This commit is contained in:
@@ -132,6 +132,15 @@ function oraEditor() {
|
||||
polygonWidth: 2,
|
||||
polygonPreviewUrl: null,
|
||||
|
||||
// SAM rough mask state
|
||||
isSamMode: false,
|
||||
samIncludePoints: [],
|
||||
samExcludePoints: [],
|
||||
isSamGenerating: false,
|
||||
samMaskPath: null,
|
||||
samMaskUrl: null,
|
||||
useSamAsStart: true,
|
||||
|
||||
// Mask extraction
|
||||
maskSubject: '',
|
||||
usePolygonHint: true,
|
||||
@@ -301,6 +310,7 @@ function oraEditor() {
|
||||
this.entityName = '';
|
||||
this.maskSubject = '';
|
||||
this.clearPolygon();
|
||||
this.clearSamPoints();
|
||||
this.scale = 100;
|
||||
},
|
||||
|
||||
@@ -310,6 +320,11 @@ function oraEditor() {
|
||||
this.isDrawing = false;
|
||||
this.polygonPoints = [];
|
||||
this.polygonPreviewUrl = null;
|
||||
this.isSamMode = false;
|
||||
this.samIncludePoints = [];
|
||||
this.samExcludePoints = [];
|
||||
this.samMaskPath = null;
|
||||
this.samMaskUrl = null;
|
||||
const canvas = document.getElementById('polygonCanvas');
|
||||
if (canvas) {
|
||||
canvas.style.display = 'none';
|
||||
@@ -319,6 +334,8 @@ function oraEditor() {
|
||||
async cancelAddMode() {
|
||||
if (this.isDrawing) {
|
||||
this.clearPolygon();
|
||||
} else if (this.isSamMode) {
|
||||
this.isSamMode = false;
|
||||
} else {
|
||||
this.exitAddMode();
|
||||
}
|
||||
@@ -585,6 +602,118 @@ function oraEditor() {
|
||||
}
|
||||
},
|
||||
|
||||
// === SAM Rough Mask ===
|
||||
startSamMode() {
|
||||
console.log('[ORA EDITOR] Starting SAM mode');
|
||||
this.isSamMode = true;
|
||||
this.samIncludePoints = [];
|
||||
this.samExcludePoints = [];
|
||||
this.samMaskPath = null;
|
||||
this.samMaskUrl = null;
|
||||
},
|
||||
|
||||
clearSamPoints() {
|
||||
this.samIncludePoints = [];
|
||||
this.samExcludePoints = [];
|
||||
this.samMaskPath = null;
|
||||
this.samMaskUrl = null;
|
||||
},
|
||||
|
||||
handleSamClick(e) {
|
||||
if (!this.isSamMode) return;
|
||||
|
||||
const container = document.getElementById('imageContainer');
|
||||
if (!container) return;
|
||||
|
||||
const rect = container.getBoundingClientRect();
|
||||
let x = (e.clientX - rect.left) / rect.width;
|
||||
let y = (e.clientY - rect.top) / rect.height;
|
||||
|
||||
x = Math.max(0, Math.min(1, x));
|
||||
y = Math.max(0, Math.min(1, y));
|
||||
|
||||
this.samIncludePoints.push({ x, y });
|
||||
console.log('[SAM] Added include point:', x, y);
|
||||
},
|
||||
|
||||
handleSamRightClick(e) {
|
||||
if (!this.isSamMode) return;
|
||||
e.preventDefault();
|
||||
|
||||
const container = document.getElementById('imageContainer');
|
||||
if (!container) return;
|
||||
|
||||
const rect = container.getBoundingClientRect();
|
||||
let x = (e.clientX - rect.left) / rect.width;
|
||||
let y = (e.clientY - rect.top) / rect.height;
|
||||
|
||||
x = Math.max(0, Math.min(1, x));
|
||||
y = Math.max(0, Math.min(1, y));
|
||||
|
||||
this.samExcludePoints.push({ x, y });
|
||||
console.log('[SAM] Added exclude point:', x, y);
|
||||
},
|
||||
|
||||
removeSamPoint(type, idx) {
|
||||
if (type === 'include') {
|
||||
this.samIncludePoints.splice(idx, 1);
|
||||
} else {
|
||||
this.samExcludePoints.splice(idx, 1);
|
||||
}
|
||||
},
|
||||
|
||||
async generateSamMask() {
|
||||
if (this.samIncludePoints.length === 0 || !this.oraPath) return;
|
||||
|
||||
console.log('[ORA EDITOR] Generating SAM mask with', this.samIncludePoints.length, 'include points');
|
||||
this.isSamGenerating = true;
|
||||
|
||||
try {
|
||||
const response = await fetch('/api/sam/generate', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
ora_path: this.oraPath,
|
||||
include_points: this.samIncludePoints,
|
||||
exclude_points: this.samExcludePoints,
|
||||
comfy_url: this.comfyUrl
|
||||
})
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
console.log('[ORA EDITOR] SAM response:', data);
|
||||
|
||||
if (!data.success) throw new Error(data.error || 'Failed');
|
||||
|
||||
this.samMaskPath = data.mask_path;
|
||||
this.samMaskUrl = data.mask_url;
|
||||
this.isSamMode = false;
|
||||
} catch (e) {
|
||||
console.error('[ORA EDITOR] Error generating SAM mask:', e);
|
||||
this.lastError = e.message;
|
||||
} finally {
|
||||
this.isSamGenerating = false;
|
||||
}
|
||||
},
|
||||
|
||||
useSamMask() {
|
||||
if (this.samMaskPath) {
|
||||
this.tempMaskPath = this.samMaskPath;
|
||||
this.tempMaskUrl = this.samMaskUrl;
|
||||
this.tempMaskPaths = [this.samMaskPath];
|
||||
this.currentMaskIndex = 0;
|
||||
this.showMaskModal = true;
|
||||
}
|
||||
},
|
||||
|
||||
discardSamMask() {
|
||||
this.samMaskPath = null;
|
||||
this.samMaskUrl = null;
|
||||
this.samIncludePoints = [];
|
||||
this.samExcludePoints = [];
|
||||
},
|
||||
|
||||
// === Mask Extraction ===
|
||||
async extractMask() {
|
||||
if (!this.maskSubject.trim()) return;
|
||||
@@ -593,17 +722,24 @@ function oraEditor() {
|
||||
this.isExtracting = true;
|
||||
this.lastError = '';
|
||||
|
||||
const requestBody = {
|
||||
subject: this.maskSubject,
|
||||
use_polygon: this.usePolygonHint && this.polygonPoints.length >= 3,
|
||||
ora_path: this.oraPath,
|
||||
comfy_url: this.comfyUrl,
|
||||
count: this.maskCount
|
||||
};
|
||||
|
||||
if (this.useSamAsStart && this.samMaskPath) {
|
||||
requestBody.start_mask_path = this.samMaskPath;
|
||||
console.log('[ORA EDITOR] Using SAM mask as starting point:', this.samMaskPath);
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch('/api/mask/extract', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
subject: this.maskSubject,
|
||||
use_polygon: this.usePolygonHint && this.polygonPoints.length >= 3,
|
||||
ora_path: this.oraPath,
|
||||
comfy_url: this.comfyUrl,
|
||||
count: this.maskCount
|
||||
})
|
||||
body: JSON.stringify(requestBody)
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
Reference in New Issue
Block a user