How to get bounding box with text using Tesseract4Android? - tesseract

I am using 'cz.adaptech.tesseract4android:tesseract4android:4.3.0' in my Android project.
Is it possible to get bounding box with text data like in example below?
(32, 24, 60, 17) Maitre
(100, 24, 82, 19) corbeau,
(191, 28, 29, 13) sur
(227, 28, 22, 12) un
(257, 24, 50, 17) arbre
(315, 24, 70, 21) perché,
(79, 49, 58, 17) Tenait
Official sample shows how to get plain text only, not boxes with text inside:
TessBaseAPI tess = getTessBaseAPI(path, context);
String text = tess.getUTF8Text();

To get bounding box with text use next code:
TessBaseAPI tess = new TessBaseAPI();
// Given path must contain subdirectory `tessdata` where are `*.traineddata` language files
String dataPath = context.getExternalFilesDir(null).getPath() + "/OCRme/";
// Initialize API for specified language (can be called multiple times during Tesseract lifetime)
if (!tess.init(dataPath, "eng", TessBaseAPI.OEM_TESSERACT_LSTM_COMBINED)) {
throw new IOException("Error initializing Tesseract (wrong data path or language)");
}
// Specify image and then recognize it and get result (can be called multiple times during Tesseract lifetime)
tess.setImage(bitmap);
tess.setPageSegMode(TessBaseAPI.PageSegMode.PSM_AUTO_OSD);
tess.getUTF8Text();
ResultIterator resultIterator = tess.getResultIterator();
List < Rect > boxes = new ArrayList < > ();
List < String > texts = new ArrayList < > ();
while (resultIterator.next(TessBaseAPI.PageIteratorLevel.RIL_WORD)) {
Rect rect = resultIterator.getBoundingRect(TessBaseAPI.PageIteratorLevel.RIL_WORD);
String text = resultIterator.getUTF8Text(TessBaseAPI.PageIteratorLevel.RIL_WORD);
boxes.add(rect);
texts.add(text);
}

Related

Is there any way to download file converted by webcontent_converter package?

I have some pdf content which i retrieved from the API and i converted it using webcontent_converter package. But when i try to download it, it is throwing error or not downloading.
here is the code:
var dir = await getApplicationDocumentsDirectory();
var savedPath = "${dir.path}/$title.pdf";
var result = await WebcontentConverter.contentToPDF(
content: content,
savedPath: savedPath,
format: PaperFormat.a4,
margins:
PdfMargins.px(top: 55, bottom: 55, right: 55, left: 55),
);
I can convert the above result into file by :
File file = File(result!)
but how can i download the file?

pyglet label not showing on screen on draw() with OpenAI gym render

I'm using a wrapper of OpenAI gym to create a graphical representation of a problem. For that, I'm drawing a circle with the given wrapper and then try to add some text.
My issue is, that only the circle shows, but the text label does not appear. Any suggestions on how I can make the text visible would be highly appreciated. Below a minimal example, that show the problem:
import pyglet
from gym.envs.classic_control import rendering
screen_width = 600
screen_height = 400
table_radius = 200
viewer = rendering.Viewer(screen_width, screen_height + 20)
table = rendering.make_circle(radius=table_radius, filled=False)
table_trans = rendering.Transform()
table.add_attr(table_trans)
table_trans.set_translation(screen_width / 2, screen_height / 2)
viewer.add_geom(table)
text = 'This is a test but it is not visible'
label = pyglet.text.Label(text, font_size=36,
x=10, y=10, anchor_x='left', anchor_y='bottom',
color=(255, 123, 255, 255))
label.draw()
viewer.render(return_rgb_array=False)
input()
What worked for me is to manually implement the render function and insert the label after the tranform.disable()
## Content of the render function
glClearColor(1, 1, 1, 1)
self.viewer.window.clear()
self.viewer.window.switch_to()
self.viewer.window.dispatch_events()
self.viewer.transform.enable()
for geom in self.viewer.geoms:
geom.render()
for geom in self.viewer.onetime_geoms:
geom.render()
self.viewer.transform.disable()
# Text
label = pyglet.text.Label(
"Hello, world",
font_name="Times New Roman",
font_size=36,
x=100,
y=100,
anchor_x="center",
anchor_y="center",
color=(255, 0, 0, 255),
)
label.draw()
arr = None
...
please try this code:
import pyglet
from gym.envs.classic_control import rendering
class DrawText:
def __init__(self, label:pyglet.text.Label):
self.label=label
def render(self):
self.label.draw()
screen_width = 600
screen_height = 400
table_radius = 200
viewer = rendering.Viewer(screen_width, screen_height + 20)
table = rendering.make_circle(radius=table_radius, filled=False)
table_trans = rendering.Transform()
table.add_attr(table_trans)
table_trans.set_translation(screen_width / 2, screen_height / 2)
viewer.add_geom(table)
text = 'This is a test but it is not visible'
label = pyglet.text.Label(text, font_size=36,
x=10, y=10, anchor_x='left', anchor_y='bottom',
color=(255, 123, 255, 255))
label.draw()
viewer.add_geom(DrawText(label))
viewer.render(return_rgb_array=False)
input()

PDFKit annotation saving text annotation

I have a 100 page PDF document that my app opens up. In my document there are various textfield that I have added to allow the user to add comments. I have tried various ways to save the text in the textboxes to allow the user to return tp editing/marking up the document, but I am without luck. How can I save the text in the PDFAnotoation TextBox as an annotated string to allow the user to pickup where they left off when editing?
Here is my original:
var textFieldMultiline21 = PDFAnnotation()
let textFieldMultilineBounds21 = CGRect(x: 27, y: 58+2, width: 339, height: 508)
textFieldMultiline21 = PDFAnnotation(bounds: textFieldMultilineBounds21, forType: PDFAnnotationSubtype(rawValue: PDFAnnotationSubtype.widget.rawValue), withProperties: nil)
textFieldMultiline21.widgetFieldType = PDFAnnotationWidgetSubtype(rawValue: PDFAnnotationWidgetSubtype.text.rawValue)
textFieldMultiline21.backgroundColor = UIColor.clear
textFieldMultiline21.font = UIFont.systemFont(ofSize: 18)
textFieldMultiline21.isMultiline = true
page.addAnnotation(textFieldMultiline21)
Here I say attempt in saving the text inside the text boxes:
document.page(at: 21)?.annotations[0].contents = "Sample text"
textFieldMultiline21.caption = "sampletext"
Somewhat of a simple answer that was hiding in plain sight
Reading Text
pdfAnnotationTextField.value(forAnnotationKey: .widgetValue)
returns a string value with attributes
Setting Text
pdfAnnotationTextField.setValue("Enter Text", forAnnotationKey: .widgetValue)

using an if statement inside button

I am currently stuck on a bit of program, I'm sure there is a way to do it but just can't think of it, so here it goes. so I am using eclipse window viewer and I made a GUI to how I want and I got a Ccombo with a few options to select from, and what I want to do is when a button is clicked I want the program to be able to see what they selected and then depending on what they selected have a different outcome put into another text field
OptionsOptimizer = new Shell();
OptionsOptimizer.setSize(450, 340);
OptionsOptimizer.setText("Options Optimizer");
Label lblSpread = new Label(OptionsOptimizer, SWT.NONE);
lblSpread.setAlignment(SWT.CENTER);
lblSpread.setBounds(10, 10, 213, 15);
lblSpread.setText("Type Of Spread");
CCombo combo = new CCombo(OptionsOptimizer, SWT.BORDER);
combo.setItems(new String[] {"Put Credit Spread", "Short Naked Put", "Put Broken Wing Butterfly", "Custom Naked Put", "Call Debit Spread", "Call Calander", "Call Ratio Backspread", "Put Diagonal", "Short Straddle", "Short Strangle", "Short Iron Condor", "Short Iron Butterfle"});
combo.setBounds(10, 31, 213, 21);
Button Enter = new Button(OptionsOptimizer, SWT.NONE);
Enter.addMouseListener(new MouseAdapter() {
public void mouseDoubleClick(MouseEvent e) {
double profit = Double.parseDouble(Profit.getText());
double margin = Double.parseDouble(Margin.getText());
double roi = profit/margin;
double roundroi = Math.round(roi*10000)/100.0;
ROI.setText("Your return on investment is " + roundroi + "%");
double percent = Double.parseDouble(ITM.getText());
double OTM = ((100 - percent)/100);
int day = Integer.parseInt(Days.getText());
double roc = (roi/day)*OTM;
double roundroc = Math.round(roc*10000)/100.0;
ROC.setText("Your return on capital per day is " + roundroc + "%");
double annual = roc*365*.4;
double roundannual = Math.round(annual*10000)/100.0;
Annual.setText("Your annual return is " + roundannual + "% if you invested 40% of your total equity");
Point spread = combo.getSelection();
if(spread.toString() == "Put Credit Spread")
{
Volitility.setText("A fall in implied volitility will help your position");
}
});
Enter.setBounds(159, 108, 75, 25);
Enter.setText("Evaluate");
}
I can see one problem with your code: Your are comparing the text of the Button using ==, but you should be using spread.toString().equals("Put Credit Spread").
See How do I compare strings in Java? for more information.
As a bit of advice: CCombo has a getSelectionIndex function that returns a integer value of the selected item beginning at 0, or -1 if no item is selected. Using this function would have the benefit of beeing able to change the Item text without needing to change the logic aswell.

XMLWorkerHelper to convert a long html into pdf produces only first page

I am using following code to convert a long html and want the pdf to span multiple A4 pages depending on the length of the source html. But it only produces first pdf page for first part of the html text that fits the A4 page. Using itextsharp 5.4.0
Source HTML is something like this
<html><body><table><tr><td>
<p> Para 1 -lorem ipsum ..... </p>
......
<p> Para 100 - loren ipsum .... </p>
</td></tr></table></body></html>
C# code is
Document pdfDocument = new Document(PageSize.A4, 70, 55, 40, 25);
string sFileName = "output.pdf";
PdfWriter wri = PdfWriter.GetInstance(pdfDocument, new FileStream(sFilePath, FileMode.Create));
PdfWriter.GetInstance(pdfDocument, HttpContext.Current.Response.OutputStream);
pdfDocument.Open();
StreamReader fw = new StreamReader("D:\\sample\\avfs.html");
string htmlText = fw.ReadToEnd();
fw.Close();
XMLWorkerHelper.GetInstance().ParseXHtml(wri, pdfDocument, new StringReader(htmlText));
pdfDocument.Close();
Use
Document pdfDocument = new Document(**PageSize.LETTER**, 70, 55, 40, 25);
instead of
Document pdfDocument = new Document(**PageSize.A4**, 70, 55, 40, 25);