Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
File size: 16,635 Bytes
95a4e14 42642e4 95a4e14 4c069c7 95a4e14 4c069c7 95a4e14 42642e4 95a4e14 4c069c7 95a4e14 4c069c7 2e12a66 95a4e14 4c069c7 95a4e14 4c069c7 95a4e14 4c069c7 95a4e14 4c069c7 95a4e14 4c069c7 42642e4 4c069c7 95a4e14 4c069c7 95a4e14 4c069c7 95a4e14 4c069c7 95a4e14 2e12a66 95a4e14 4c069c7 95a4e14 2e12a66 95a4e14 42642e4 2e12a66 95a4e14 4c069c7 95a4e14 2e12a66 4c069c7 95a4e14 2e12a66 95a4e14 2e12a66 95a4e14 4c069c7 2e12a66 4c069c7 2e12a66 95a4e14 4c069c7 95a4e14 2e12a66 4c069c7 95a4e14 2e12a66 95a4e14 2e12a66 95a4e14 4c069c7 95a4e14 4c069c7 95a4e14 4c069c7 95a4e14 4c069c7 95a4e14 4c069c7 95a4e14 4c069c7 95a4e14 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 |
import { ImageSegmenter, FilesetResolver } from "@mediapipe/tasks-vision"
import { actionman } from "../fonts";
interface BoundingBox {
top: number;
left: number;
width: number;
height: number;
}
/**
* Injects speech bubbles into the background of an image.
* @param params - The parameters for injecting speech bubbles.
* @returns A Promise that resolves to a base64-encoded string of the modified image.
*/
export async function injectSpeechBubbleInTheBackground(params: {
inputImageInBase64: string;
text?: string;
shape?: "oval" | "rectangular" | "cloud" | "thought";
line?: "handdrawn" | "straight" | "bubble" | "chaotic";
font?: string;
debug?: boolean;
}): Promise<string> {
const {
inputImageInBase64,
text,
shape = "oval",
line = "handdrawn",
font = actionman.style.fontFamily,
debug = false,
} = params;
if (!text) {
return inputImageInBase64;
}
const image = await loadImage(inputImageInBase64);
const canvas = document.createElement('canvas');
canvas.width = image.width;
canvas.height = image.height;
const ctx = canvas.getContext('2d')!;
ctx.drawImage(image, 0, 0);
const vision = await FilesetResolver.forVisionTasks(
"https://cdn.jsdelivr.net/npm/@mediapipe/tasks-vision@latest/wasm"
);
const imageSegmenter = await ImageSegmenter.createFromOptions(vision, {
baseOptions: {
modelAssetPath: "https://storage.googleapis.com/mediapipe-models/image_segmenter/deeplab_v3/float32/1/deeplab_v3.tflite",
delegate: "GPU"
},
outputCategoryMask: true,
outputConfidenceMasks: false
});
const segmentationResult = imageSegmenter.segment(image);
let characterBoundingBox: BoundingBox | null = null;
if (segmentationResult.categoryMask) {
const mask = segmentationResult.categoryMask.getAsUint8Array();
characterBoundingBox = findCharacterBoundingBox(mask, image.width, image.height);
console.log(segmentationResult)
if (debug) {
drawSegmentationMask(ctx, mask, image.width, image.height);
}
}
const bubbles = splitTextIntoBubbles(text);
const bubbleLocations = calculateBubbleLocations(bubbles.length, image.width, image.height, characterBoundingBox);
bubbles.forEach((bubbleText, index) => {
const bubbleLocation = bubbleLocations[index];
drawSpeechBubble(ctx, bubbleLocation, bubbleText, shape, line, font, characterBoundingBox, image.width, image.height);
});
return canvas.toDataURL('image/png');
}
function loadImage(base64: string): Promise<HTMLImageElement> {
return new Promise((resolve, reject) => {
const img = new Image();
img.onload = () => resolve(img);
img.onerror = reject;
img.src = base64;
});
}
function findCharacterBoundingBox(mask: Uint8Array, width: number, height: number): BoundingBox {
let minX = width, minY = height, maxX = 0, maxY = 0;
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
const index = y * width + x;
if (mask[index] > 0) {
minX = Math.min(minX, x);
minY = Math.min(minY, y);
maxX = Math.max(maxX, x);
maxY = Math.max(maxY, y);
}
}
}
return {
top: minY,
left: minX,
width: maxX - minX,
height: maxY - minY
};
}
function analyzeSegmentationMask(mask: Uint8Array, width: number, height: number): string[] {
const categories = new Set<number>();
for (let i = 0; i < mask.length; i++) {
if (mask[i] > 0) {
categories.add(mask[i]);
}
}
return Array.from(categories).map(c => `unknown-${c}`);
}
function splitTextIntoBubbles(text: string): string[] {
// Define a regular expression pattern
const pattern = /(?:[A-Z][a-z]*\.\s*)*(?:[^.!?\s]+[^.!?]*[.!?]+)|\S+/g;
const matches = text.match(pattern) || [text];
return matches.map(sentence => sentence.trim());
}
function calculateBubbleLocations(
bubbleCount: number,
imageWidth: number,
imageHeight: number,
characterBoundingBox: BoundingBox | null
): { x: number, y: number }[] {
const locations: { x: number, y: number }[] = [];
const padding = 50;
const availableWidth = imageWidth - padding * 2;
const availableHeight = imageHeight - padding * 2;
const maxAttempts = 50;
for (let i = 0; i < bubbleCount; i++) {
let x, y;
let attempts = 0;
do {
x = Math.random() * availableWidth + padding;
y = (i / bubbleCount) * availableHeight + padding;
attempts++;
if (attempts >= maxAttempts) {
console.warn(`Could not find non-overlapping position for bubble ${i} after ${maxAttempts} attempts.`);
break;
}
} while (characterBoundingBox && isOverlapping({ x, y }, characterBoundingBox));
locations.push({ x, y });
}
return locations;
}
function isOverlapping(point: { x: number, y: number }, box: BoundingBox): boolean {
return point.x >= box.left && point.x <= box.left + box.width &&
point.y >= box.top && point.y <= box.top + box.height;
}
function drawSegmentationMask(ctx: CanvasRenderingContext2D, mask: Uint8Array, width: number, height: number) {
const imageData = ctx.getImageData(0, 0, width, height);
const data = imageData.data;
for (let i = 0; i < mask.length; i++) {
const category = mask[i];
if (category > 0) {
// Use a different color for each category
const color = getCategoryColor(category);
data[i * 4] = color[0];
data[i * 4 + 1] = color[1];
data[i * 4 + 2] = color[2];
data[i * 4 + 3] = 128; // 50% opacity
}
}
ctx.putImageData(imageData, 0, 0);
}
function getCategoryColor(category: number): [number, number, number] {
// Generate a pseudo-random color based on the category
const hue = (category * 137) % 360;
return hslToRgb(hue / 360, 1, 0.5);
}
function hslToRgb(h: number, s: number, l: number): [number, number, number] {
let r, g, b;
if (s === 0) {
r = g = b = l;
} else {
const hue2rgb = (p: number, q: number, t: number) => {
if (t < 0) t += 1;
if (t > 1) t -= 1;
if (t < 1/6) return p + (q - p) * 6 * t;
if (t < 1/2) return q;
if (t < 2/3) return p + (q - p) * (2/3 - t) * 6;
return p;
};
const q = l < 0.5 ? l * (1 + s) : l + s - l * s;
const p = 2 * l - q;
r = hue2rgb(p, q, h + 1/3);
g = hue2rgb(p, q, h);
b = hue2rgb(p, q, h - 1/3);
}
return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)];
}
function drawSpeechBubble(
ctx: CanvasRenderingContext2D,
location: { x: number; y: number },
text: string,
shape: "oval" | "rectangular" | "cloud" | "thought",
line: "handdrawn" | "straight" | "bubble" | "chaotic",
font: string,
characterBoundingBox: BoundingBox | null,
imageWidth: number,
imageHeight: number,
safetyMargin: number = 0.1 // Default safety margin is 10%
) {
const padding = 24;
const borderPadding = Math.max(10, Math.min(imageWidth, imageHeight) * safetyMargin);
const fontSize = 20;
ctx.font = `${fontSize}px ${font}`;
// Adjust maximum width to account for border padding
const maxBubbleWidth = imageWidth - 2 * borderPadding;
const wrappedText = wrapText(ctx, text, maxBubbleWidth - padding * 2, fontSize);
const textDimensions = measureTextDimensions(ctx, wrappedText, fontSize);
// Adjust bubble size based on text content
const finalWidth = Math.min(Math.max(textDimensions.width + padding * 2, 100), maxBubbleWidth);
const finalHeight = Math.min(Math.max(textDimensions.height + padding * 2, 50), imageHeight - 2 * borderPadding);
const bubbleLocation = adjustBubbleLocation(location, finalWidth, finalHeight, characterBoundingBox, imageWidth, imageHeight, borderPadding);
let tailTarget = null;
if (characterBoundingBox) {
tailTarget = {
x: characterBoundingBox.left + characterBoundingBox.width / 2,
y: characterBoundingBox.top + characterBoundingBox.height * 0.3
};
}
// Draw the main bubble
ctx.fillStyle = 'white';
ctx.strokeStyle = 'black';
ctx.lineWidth = 2;
ctx.beginPath();
drawBubbleShape(ctx, shape, bubbleLocation, finalWidth, finalHeight, tailTarget);
ctx.fill();
ctx.stroke();
// Draw the tail
if (tailTarget) {
drawTail(ctx, bubbleLocation, finalWidth, finalHeight, tailTarget, shape);
}
// Draw a white oval to blend the tail with the bubble
ctx.fillStyle = 'white';
ctx.beginPath();
drawBubbleShape(ctx, shape, bubbleLocation, finalWidth, finalHeight, null);
ctx.fill();
// Draw the text
ctx.fillStyle = 'black';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
drawFormattedText(ctx, wrappedText, bubbleLocation.x, bubbleLocation.y, finalWidth - padding * 2, fontSize);
}
function drawTail(
ctx: CanvasRenderingContext2D,
bubbleLocation: { x: number; y: number },
bubbleWidth: number,
bubbleHeight: number,
tailTarget: { x: number; y: number },
shape: string
) {
const bubbleCenterX = bubbleLocation.x;
const bubbleCenterY = bubbleLocation.y;
const tailBaseWidth = 40;
// Calculate the distance from bubble center to tail target
const deltaX = tailTarget.x - bubbleCenterX;
const deltaY = tailTarget.y - bubbleCenterY;
const distance = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
// Set the tail length to 30% of the distance
const tailLength = distance * 0.3;
// Calculate the tail end point
const tailEndX = bubbleCenterX + (deltaX / distance) * tailLength;
const tailEndY = bubbleCenterY + (deltaY / distance) * tailLength;
// Calculate the angle of the tail
const angle = Math.atan2(deltaY, deltaX);
// Calculate the base points of the tail
const perpAngle = angle + Math.PI / 2;
const basePoint1 = {
x: bubbleCenterX + Math.cos(perpAngle) * tailBaseWidth / 2,
y: bubbleCenterY + Math.sin(perpAngle) * tailBaseWidth / 2
};
const basePoint2 = {
x: bubbleCenterX - Math.cos(perpAngle) * tailBaseWidth / 2,
y: bubbleCenterY - Math.sin(perpAngle) * tailBaseWidth / 2
};
// Calculate control points for the Bézier curves
const controlPointDistance = tailLength * 0.3;
const controlPoint1 = {
x: basePoint1.x + Math.cos(angle) * controlPointDistance,
y: basePoint1.y + Math.sin(angle) * controlPointDistance
};
const controlPoint2 = {
x: basePoint2.x + Math.cos(angle) * controlPointDistance,
y: basePoint2.y + Math.sin(angle) * controlPointDistance
};
// Draw the tail
ctx.beginPath();
ctx.moveTo(basePoint1.x, basePoint1.y);
ctx.quadraticCurveTo(controlPoint1.x, controlPoint1.y, tailEndX, tailEndY);
ctx.quadraticCurveTo(controlPoint2.x, controlPoint2.y, basePoint2.x, basePoint2.y);
ctx.closePath();
// Fill and stroke the tail
ctx.fillStyle = 'white';
ctx.fill();
ctx.strokeStyle = 'black';
ctx.stroke();
}
function adjustBubbleLocation(
location: { x: number; y: number },
width: number,
height: number,
characterBoundingBox: BoundingBox | null,
imageWidth: number,
imageHeight: number,
borderPadding: number
): { x: number; y: number } {
let adjustedX = location.x;
let adjustedY = location.y;
// Ensure the bubble doesn't overlap with the character
if (characterBoundingBox) {
if (
adjustedX > characterBoundingBox.left &&
adjustedX < characterBoundingBox.left + characterBoundingBox.width
) {
adjustedX = characterBoundingBox.left > imageWidth / 2
? characterBoundingBox.left - width / 2 - 10
: characterBoundingBox.left + characterBoundingBox.width + width / 2 + 10;
}
}
// Ensure the bubble (including text) is fully visible
adjustedX = Math.max(width / 2 + borderPadding, Math.min(imageWidth - width / 2 - borderPadding, adjustedX));
adjustedY = Math.max(height / 2 + borderPadding, Math.min(imageHeight - height / 2 - borderPadding, adjustedY));
return { x: adjustedX, y: adjustedY };
}
function drawBubbleShape(
ctx: CanvasRenderingContext2D,
shape: "oval" | "rectangular" | "cloud" | "thought",
bubbleLocation: { x: number, y: number },
width: number,
height: number,
tailTarget: { x: number, y: number } | null
) {
switch (shape) {
case "oval":
drawOvalBubble(ctx, bubbleLocation, width, height);
break;
case "rectangular":
drawRectangularBubble(ctx, bubbleLocation, width, height);
break;
case "cloud":
drawCloudBubble(ctx, bubbleLocation, width, height);
break;
case "thought":
drawThoughtBubble(ctx, bubbleLocation, width, height);
break;
}
}
function drawOvalBubble(ctx: CanvasRenderingContext2D, location: { x: number, y: number }, width: number, height: number) {
ctx.beginPath();
ctx.ellipse(location.x, location.y, width / 2, height / 2, 0, 0, 2 * Math.PI);
ctx.closePath();
}
function drawRectangularBubble(ctx: CanvasRenderingContext2D, location: { x: number, y: number }, width: number, height: number) {
const radius = 20;
ctx.beginPath();
ctx.moveTo(location.x - width / 2 + radius, location.y - height / 2);
ctx.lineTo(location.x + width / 2 - radius, location.y - height / 2);
ctx.quadraticCurveTo(location.x + width / 2, location.y - height / 2, location.x + width / 2, location.y - height / 2 + radius);
ctx.lineTo(location.x + width / 2, location.y + height / 2 - radius);
ctx.quadraticCurveTo(location.x + width / 2, location.y + height / 2, location.x + width / 2 - radius, location.y + height / 2);
ctx.lineTo(location.x - width / 2 + radius, location.y + height / 2);
ctx.quadraticCurveTo(location.x - width / 2, location.y + height / 2, location.x - width / 2, location.y + height / 2 - radius);
ctx.lineTo(location.x - width / 2, location.y - height / 2 + radius);
ctx.quadraticCurveTo(location.x - width / 2, location.y - height / 2, location.x - width / 2 + radius, location.y - height / 2);
ctx.closePath();
}
function drawCloudBubble(ctx: CanvasRenderingContext2D, location: { x: number, y: number }, width: number, height: number) {
const numBumps = Math.floor(width / 40);
const bumpRadius = width / (numBumps * 2);
ctx.beginPath();
ctx.moveTo(location.x - width / 2 + bumpRadius, location.y);
// Top
for (let i = 0; i < numBumps; i++) {
const x = location.x - width / 2 + (i * 2 + 1) * bumpRadius;
const y = location.y - height / 2;
ctx.quadraticCurveTo(x, y - bumpRadius / 2, x + bumpRadius, y);
}
// Right
for (let i = 0; i < numBumps / 2; i++) {
const x = location.x + width / 2;
const y = location.y - height / 2 + (i * 2 + 1) * bumpRadius * 2;
ctx.quadraticCurveTo(x + bumpRadius / 2, y, x, y + bumpRadius * 2);
}
// Bottom
for (let i = numBumps; i > 0; i--) {
const x = location.x - width / 2 + (i * 2 - 1) * bumpRadius;
const y = location.y + height / 2;
ctx.quadraticCurveTo(x, y + bumpRadius / 2, x - bumpRadius, y);
}
// Left
for (let i = numBumps / 2; i > 0; i--) {
const x = location.x - width / 2;
const y = location.y - height / 2 + (i * 2 - 1) * bumpRadius * 2;
ctx.quadraticCurveTo(x - bumpRadius / 2, y, x, y - bumpRadius * 2);
}
ctx.closePath();
}
function drawThoughtBubble(ctx: CanvasRenderingContext2D, location: { x: number, y: number }, width: number, height: number) {
drawCloudBubble(ctx, location, width, height);
// The tail for thought bubbles is handled in the drawTail function
}
function wrapText(ctx: CanvasRenderingContext2D, text: string, maxWidth: number, lineHeight: number): string[] {
const words = text.split(' ');
const lines: string[] = [];
let currentLine = '';
for (const word of words) {
const testLine = currentLine + (currentLine ? ' ' : '') + word;
const metrics = ctx.measureText(testLine);
if (metrics.width > maxWidth) {
lines.push(currentLine);
currentLine = word;
} else {
currentLine = testLine;
}
}
if (currentLine) {
lines.push(currentLine);
}
return lines;
}
function measureTextDimensions(ctx: CanvasRenderingContext2D, lines: string[], lineHeight: number): { width: number, height: number } {
let maxWidth = 0;
const height = lineHeight * lines.length;
for (const line of lines) {
const metrics = ctx.measureText(line);
maxWidth = Math.max(maxWidth, metrics.width);
}
return { width: maxWidth, height };
}
function drawFormattedText(ctx: CanvasRenderingContext2D, lines: string[], x: number, y: number, maxWidth: number, lineHeight: number) {
const totalHeight = lineHeight * lines.length;
let startY = y - totalHeight / 2 + lineHeight / 2;
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
const lineY = startY + i * lineHeight;
ctx.fillText(line, x, lineY, maxWidth);
}
} |