|
|
|
|
|
|
|
|
|
|
|
|
|
|
import { DETECTION_CONFIG } from '../config.js';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function loadImage(src) {
|
|
|
return new Promise((resolve, reject) => {
|
|
|
const img = new Image();
|
|
|
img.crossOrigin = 'anonymous';
|
|
|
img.onload = () => resolve(img);
|
|
|
img.onerror = reject;
|
|
|
img.src = src;
|
|
|
});
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export async function cropImage(img, x1, y1, x2, y2, padding = DETECTION_CONFIG.yolo.padding) {
|
|
|
const width = x2 - x1;
|
|
|
const height = y2 - y1;
|
|
|
const padX = width * padding;
|
|
|
const padY = height * padding;
|
|
|
|
|
|
x1 = Math.max(0, x1 - padX);
|
|
|
y1 = Math.max(0, y1 - padY);
|
|
|
x2 = Math.min(img.width, x2 + padX);
|
|
|
y2 = Math.min(img.height, y2 + padY);
|
|
|
|
|
|
const canvas = document.createElement('canvas');
|
|
|
canvas.width = x2 - x1;
|
|
|
canvas.height = y2 - y1;
|
|
|
const ctx = canvas.getContext('2d');
|
|
|
ctx.drawImage(img, x1, y1, x2 - x1, y2 - y1, 0, 0, x2 - x1, y2 - y1);
|
|
|
|
|
|
return canvas.toDataURL();
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function readFileAsDataURL(file) {
|
|
|
return new Promise((resolve, reject) => {
|
|
|
const reader = new FileReader();
|
|
|
reader.onload = (e) => resolve(e.target.result);
|
|
|
reader.onerror = reject;
|
|
|
reader.readAsDataURL(file);
|
|
|
});
|
|
|
}
|
|
|
|