Skip to content

Commit a20791a

Browse files
Copilotcubap
andauthored
Fix Auto Parse TypeError from invalid OpenCV.js column data (#370)
* Initial plan * Fix Auto Parse TypeError by adding defensive validation to detectColumns Added validation checks to prevent "Cannot create property on number" error: - Validate columns array elements before destructuring - Add safe handling for malformed column data - Replace destructuring assignment with explicit variable assignments - Add validation in reduce operation to handle edge cases Co-authored-by: cubap <1119165+cubap@users.noreply.github.com> * Add number type validation to column detection Improve robustness by validating that column array elements are numbers before performing arithmetic operations. This prevents potential runtime errors from non-numeric values. Co-authored-by: cubap <1119165+cubap@users.noreply.github.com> * Refactor: Extract isValidColumn helper function Reduce code duplication by extracting column validation logic into a reusable helper function. This improves maintainability and makes the code more readable. Co-authored-by: cubap <1119165+cubap@users.noreply.github.com> * Add JSDoc documentation to isValidColumn helper Document the validation helper function with JSDoc comments for better code maintainability and developer experience. Co-authored-by: cubap <1119165+cubap@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: cubap <1119165+cubap@users.noreply.github.com>
1 parent c75ee70 commit a20791a

1 file changed

Lines changed: 40 additions & 9 deletions

File tree

components/annotorious-annotator/detect-lines.js

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ export function removeOutlierLines(lines, toleranceRatio = 1.5) {
2121
return lines.filter((_, i) => validIndices.has(i))
2222
}
2323

24+
/**
25+
* Validates that a column element is a properly formatted array with numeric start and end values.
26+
* @param {*} col - The column element to validate
27+
* @returns {boolean} True if the column is an array with at least 2 numeric elements
28+
*/
29+
function isValidColumn(col) {
30+
return Array.isArray(col) && col.length >= 2 &&
31+
typeof col[0] === 'number' && typeof col[1] === 'number'
32+
}
33+
2434
export function detectColumns(binary, minColumnWidth = 50, spaceThreshold = 0.1) {
2535
const verticalProj = new cv.Mat()
2636
cv.reduce(binary, verticalProj, 0, cv.REDUCE_SUM, cv.CV_32S)
@@ -50,19 +60,40 @@ export function detectColumns(binary, minColumnWidth = 50, spaceThreshold = 0.1)
5060

5161
if (columns.length > 1) {
5262
const merged = []
53-
let [curStart, curEnd] = columns[0]
54-
for (const [s, e] of columns.slice(1)) {
55-
if (s - curEnd < 3) curEnd = e
56-
else {
57-
merged.push([curStart, curEnd])
58-
[curStart, curEnd] = [s, e]
63+
// Ensure columns[0] is a valid array before destructuring
64+
if (!isValidColumn(columns[0])) {
65+
console.warn("Invalid column format detected, using default column")
66+
columns = [[0, binary.cols]]
67+
} else {
68+
let curStart = columns[0][0]
69+
let curEnd = columns[0][1]
70+
for (const col of columns.slice(1)) {
71+
// Validate each column element
72+
if (!isValidColumn(col)) {
73+
console.warn("Skipping invalid column element:", col)
74+
continue
75+
}
76+
const [s, e] = col
77+
if (s - curEnd < 3) curEnd = e
78+
else {
79+
merged.push([curStart, curEnd])
80+
curStart = s
81+
curEnd = e
82+
}
5983
}
84+
merged.push([curStart, curEnd])
85+
columns = merged
6086
}
61-
merged.push([curStart, curEnd])
62-
columns = merged
6387
}
6488

65-
const totalWidth = columns.reduce((sum, [s, e]) => sum + (e - s), 0)
89+
// Safely calculate total width with validation
90+
const totalWidth = columns.reduce((sum, col) => {
91+
if (isValidColumn(col)) {
92+
return sum + (col[1] - col[0])
93+
}
94+
return sum
95+
}, 0)
96+
6697
if (columns.length === 0 || totalWidth > 0.85 * binary.cols) {
6798
columns = [[0, binary.cols]]
6899
}

0 commit comments

Comments
 (0)