Skip to content

Commit

Permalink
speed
Browse files Browse the repository at this point in the history
  • Loading branch information
Dor-sketch committed Jun 26, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 8c1f86e commit 8c82cb8
Showing 2 changed files with 20 additions and 43 deletions.
8 changes: 3 additions & 5 deletions docs/index.html
Original file line number Diff line number Diff line change
@@ -262,23 +262,21 @@

const img = new Image();
img.onload = function () {
const MAX_WIDTH = 500;
const MAX_WIDTH = 400;
let targetWidth = img.width;
let targetHeight = img.height;

if (img.width > MAX_WIDTH) {
const scale = MAX_WIDTH / img.width;
targetWidth = MAX_WIDTH; // Scale width to MAX_WIDTH
targetHeight = img.height * scale; // Scale height to maintain aspect ratio
}


canvas.width = targetWidth;
canvas.height = targetHeight;
ctx.drawImage(img, 0, 0, targetWidth, targetHeight);

// Binarize and enhance the image
let imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
imageData = binarizeImage(imageData);
ctx.putImageData(imageData, 0, 0);

const processedImageSrc = canvas.toDataURL();
@@ -295,7 +293,7 @@
const data = imageData.data;
for (let i = 0; i < data.length; i += 4) {
const avg = data[i] * 0.3 + data[i + 1] * 0.59 + data[i + 2] * 0.11; // Grayscale
const binarized = avg <= 90 ? 0 : 255;
const binarized = avg <= 150 ? 0 : 255;
data[i] = binarized;
data[i + 1] = binarized;
data[i + 2] = binarized;
55 changes: 17 additions & 38 deletions docs/worker.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,18 @@
// Global storage for debug images
const debugImages = [];

importScripts('https://cdn.jsdelivr.net/npm/tesseract.js@4/dist/tesseract.min.js');

self.onmessage = async function(e) {
const { width, height, imageData, row, col } = e.data;
const result = await extractSudokuNumber(width, height, imageData, row, col);
const result = await extractSudokuNumber(width, height, imageData);
self.postMessage({ result });
};

async function extractSudokuNumber(cellWidth, cellHeight, imageData, row, col) {
async function extractSudokuNumber(cellWidth, cellHeight, imageData) {
const cellImageData = new ImageData(new Uint8ClampedArray(imageData), cellWidth, cellHeight);

// Check for black pixels in the center of the cell
if (!hasBlackPixelsInSampledLines(cellImageData)) {
console.log('No black pixels found in cell at row:', row, 'col:', col);
if (!hasBlackPixelsInCenter(cellImageData)) {
return '.'; // Return '.' immediately if no black pixels are found
}

// Save the cell image data for debugging
debugImages.push({ row, col, imageData: cellImageData });

// Convert ImageData to Blob as Tesseract.js can work with Blob for recognition
const blob = await new Promise(resolve => {
const canvas = new OffscreenCanvas(cellWidth, cellHeight);
@@ -40,37 +32,24 @@ async function extractSudokuNumber(cellWidth, cellHeight, imageData, row, col) {

const digit = text.trim();
const validDigit = digit.length === 1 && '123456789'.includes(digit) ? digit : '.';
console.log('Extracted Digit: ', validDigit, 'at row:', row, 'col:', col);
console.log('Extracted Digit:', validDigit);
return validDigit;
}

// Existing functions remain unchanged...
function hasBlackPixelsInSampledLines(imageData) {
function hasBlackPixelsInCenter(imageData) {
const { width, height, data } = imageData;

// Define lines to sample: 25% and 75% for both horizontal and vertical
const rowsToCheck = [Math.floor(height * 0.25), Math.floor(height * 0.75)];
const colsToCheck = [Math.floor(width * 0.25), Math.floor(width * 0.75)];

// Check horizontal lines
for (let y of rowsToCheck) {
for (let x = 0; x < width; x++) {
if (isBlackPixel(data, y, x, width)) return true;
const centerX = Math.floor(width / 2);
const centerY = Math.floor(height / 2);
const radius = Math.floor(Math.min(width, height) / 4); // Check a central square of the cell

for (let y = centerY - radius; y <= centerY + radius; y++) {
for (let x = centerX - radius; x <= centerX + radius; x++) {
const index = (y * width + x) * 4;
if (data[index] === 0 && data[index + 1] === 0 && data[index + 2] === 0 && data[index + 3] === 255) {
// Found a black pixel (RGBA)
return true;
}
}
}

// Check vertical lines
for (let x of colsToCheck) {
for (let y = 0; y < height; y++) {
if (isBlackPixel(data, y, x, width)) return true;
}
}

return false; // No black pixels found in sampled lines
}

// Helper function to check if a pixel is black
function isBlackPixel(data, y, x, width) {
const index = (y * width + x) * 4;
return data[index] === 0 && data[index + 1] === 0 && data[index + 2] === 0 && data[index + 3] === 255;
return false; // No black pixels found
}

0 comments on commit 8c82cb8

Please sign in to comment.