-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rewrite how OCR is performed and results are returned. I found out ab…
…out the method Tesseract#getWords which returns both text and bounding boxes at the same time. My previous method was erroneously calling methods to calculate those independently of each other, so it was performing OCR twice; this should theoretically halve the execution time. I also simplified the convoluted mess of transforming results between arrays and lists. Grid2D is now OCRArray2D and is the universal container for OCR results. It contains OCRArrayNodes which hold the array position, value, and bounding box of each OCR word.
- Loading branch information
Showing
6 changed files
with
246 additions
and
288 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
146 changes: 146 additions & 0 deletions
146
src/main/java/com/github/hawkpath/cyberpunk_breach_protocol_solver/OCRArray2D.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
package com.github.hawkpath.cyberpunk_breach_protocol_solver; | ||
|
||
import java.awt.Rectangle; | ||
import java.util.ArrayList; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
|
||
class OCRArrayNode { | ||
public int x; | ||
public int y; | ||
public Integer value; | ||
public Rectangle boundingBox; | ||
|
||
public OCRArrayNode(int x, int y, Integer value, Rectangle boundingBox) { | ||
this.x = x; | ||
this.y = y; | ||
this.value = value; | ||
this.boundingBox = boundingBox; | ||
} | ||
|
||
public boolean equals(int other) { | ||
return value.equals(other); | ||
} | ||
|
||
public boolean equals(OCRArrayNode other) { | ||
return value.equals(other.value); | ||
} | ||
|
||
public String toString() { | ||
return String.format("<OCRArrayNode %02X (%d, %d)>", value, x, y); | ||
} | ||
} | ||
|
||
public class OCRArray2D implements Iterable<List<OCRArrayNode>> { | ||
|
||
private List<List<OCRArrayNode>> rows; | ||
private List<OCRArrayNode> lastRow; | ||
private Boolean isGrid = true; | ||
|
||
public OCRArray2D() { | ||
rows = new ArrayList<>(); | ||
} | ||
|
||
public Iterator<List<OCRArrayNode>> iterator() { | ||
return rows.iterator(); | ||
} | ||
|
||
public String toString() { | ||
StringBuilder sb = new StringBuilder(); | ||
for (int y=0; y<rows.size(); y++) { | ||
if (y != 0) | ||
sb.append("\n"); | ||
|
||
for (OCRArrayNode cell : getRow(y)) { | ||
sb.append(String.format("%02X ", cell.value)); | ||
} | ||
} | ||
return sb.toString(); | ||
} | ||
|
||
public void addRow() { | ||
lastRow = new ArrayList<>(); | ||
rows.add(lastRow); | ||
} | ||
|
||
public void add(Integer value, Rectangle boundingBox) { | ||
makeFieldsDirty(); | ||
lastRow.add(new OCRArrayNode(lastRow.size(), rows.size() - 1, value, boundingBox)); | ||
} | ||
|
||
private void makeFieldsDirty() { | ||
isGrid = null; | ||
} | ||
|
||
public OCRArrayNode get(int x, int y) throws IndexOutOfBoundsException { | ||
return rows.get(y).get(x); | ||
} | ||
|
||
public List<OCRArrayNode> getRow(int y) throws IndexOutOfBoundsException { | ||
return rows.get(y); | ||
} | ||
|
||
public boolean isGrid() { | ||
if (isGrid != null) | ||
return isGrid; | ||
|
||
if (rows.size() <= 1) { | ||
isGrid = true; | ||
return isGrid; | ||
} | ||
|
||
int width = rows.get(0).size(); | ||
for (int i=1; i<rows.size(); i++) { | ||
if (rows.get(i).size() != width) { | ||
isGrid = false; | ||
return isGrid; | ||
} | ||
} | ||
|
||
isGrid = true; | ||
return isGrid; | ||
} | ||
|
||
public int getWidth() { | ||
return isGrid() ? lastRow.size() : -1; | ||
} | ||
|
||
public int getHeight() { | ||
return rows.size(); | ||
} | ||
|
||
public OCRArrayNode findInRow(int row, OCRArrayNode nodeWithValue, int start) { | ||
return findInRow(row, nodeWithValue.value, start); | ||
} | ||
|
||
public OCRArrayNode findInRow(int row, int value, int start) { | ||
if (!isGrid()) | ||
return null; | ||
|
||
for (int i=start; i<getWidth(); i++) { | ||
OCRArrayNode node = get(i, row); | ||
if (node.equals(value)) | ||
return node; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public OCRArrayNode findInColumn(int col, OCRArrayNode nodeWithValue, int start) { | ||
return findInColumn(col, nodeWithValue.value, start); | ||
} | ||
|
||
public OCRArrayNode findInColumn(int col, int value, int start) { | ||
if (!isGrid()) | ||
return null; | ||
|
||
for (int i=start; i<getHeight(); i++) { | ||
OCRArrayNode node = get(col, i); | ||
if (node.equals(value)) | ||
return node; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
} |
Oops, something went wrong.