Remote control / inspect existing GTK application - widget

How can I non-interactively access the widget tree of an existing GTK application? I'd like to query the coordinates of a widget, and if possible send events (hover, click, key press, …).
Using GTKInspector, I can do browse the widget tree interactively and see the size and coordinates of a widget. I'd like to access this info using a command-line tool, or using some GTK API in any language.
# Steps to do this interactively, I'd like a non-interactive version.
sudo apt install libgtk-3-dev
gsettings set org.gtk.Settings.Debug enable-inspector-keybinding true
GTK_DEBUG=interactive mate-calc
# Then go to `MathWindow → GtkBox → GtkMenuBar`
# click on the (i) icon to show the details view
# `allocation` indicates the `width×height+x+y`, here it's `312×25+0+0`
Solutions I've considered:
Use xdotool to control the GTKInspector automatically (expect poor performance + the need to have an extra GTKInspector window on the side).
Use dbus, but d-feet does not show the widget tree and I suspect the things it shows about interface are about OOP or the d-bus interface, not about some user interface.
Back in the good old days, we could do similar things with dcop for QT applications, but of course it won't work for GTK.

dogtail uses assistive technologies to access an application's widget tree. Tested on Xubuntu 20.04.
$ sudo apt install python3-dogtail
$ sniff # explore using a GUI, it will enable assistive technologies for you.
dogtail-example.py:
#!/usr/bin/env python3
from dogtail.tree import *
app = root.application('mate-calc')
menuBar = app.findChildren(predicate.GenericPredicate(roleName="menu bar"))[0]
print('On-screen coordinates: ', menuBar.position)
print('Size: ', menuBar.size)
# to preview the nodes available in the menu bar, use menuBar.dump()
Output:
$ python3 dogtail-example.py
On-screen coordinates: (529, 431)
Size: (312, 25)
There are a few examples in /usr/share/doc/python3-dogtail/examples/ but the actual "doc" is in the docstrings of /usr/lib/python3/dist-packages/dogtail/tree.py and other files in that directory.

Related

RF Module CC1101 configuration

Nowadays I would like to use CC1101 RF Module to send small data to another RF Module. I have problem with configuration process. In datasheet of this device, there is a note that to use SmartRF Studio to configure it. This program does not detect any device, but I connect it by Nucleo Board to the computer(propably there is a problem). In SmartRF Studio tutorial is a note that to use CC Debugger or another SmartRF Evaluation Board if I want have access to configure process, but is it necessary? Maybe there is another way to configure this module?
Thanks for you answers.
If you chose "Open RF device in offline mode" in the start screen of SmartRF studio, you can configure the RF parameters without any device connected, and then enable "Register View" via the checkbox on the configuration pane. The registers can be exported in various forms, e.g. as C header files or XML
You need a evaluation board to get it working with SmartRF but you can just use SmartRF to export the regeister settings and use them with a MCU using an SPI interface. To export the register settings go to "Sub-1 GHz" tab and double click on "CC1101" box, then just configure the "RF Parameters" section and click on "Register Export". You can chose the format you're exporting on "Template" tab (or create your own template) and finally export the settings as shown here.

How do I segment a document using Tesseract then output the resulting bounding boxes and labels

I'm trying to get Tesseract to output a file with labelled bounding boxes that result from page segmentation (pre OCR). I know it must be capable of doing this 'out of the box' because of the results shown at the ICDAR competitions where contestants had to segment and various documents (academic paper here). Here's an example from that paper illustrating what I want to create:
I have built the latest version of tesseract using brew, brew install tesseract --HEAD, and have been trying to edit config files located in /usr/local/Cellar/tesseract/HEAD/share/tessdata/configs/ to output labelled boxes. The output received using hocr as the config, i.e.
tesseract infile.tiff outfile_stem -l eng -psm 1 hocr
gives a bounding box for everything and has some labelling in class tags e.g.
<p class='ocr_par' dir='ltr' id='par_5_82' title="bbox 2194 4490 3842 4589">
<span class='ocr_line' id='line_5_142' ...
but I can't visualise this. Is there a standard tool to visualize hOCR files, or is the facility to create an output file with bounding boxes built into Tesseract?
The current head version details:
tesseract 3.04.00
leptonica-1.71
libjpeg 8d : libpng 1.6.16 : libtiff 4.0.3 : zlib 1.2.5
Edit
I'm really looking to achieve this using the command line tool (as in examples above). #nguyenq has pointed me to the API reference, unfortunately I have no c++ experience. If the only solution is to use the API, please can you provide a quick python example?
Success. Many thanks to the people at the Pattern Recognition and Image Analysis Research Lab (PRImA) for producing tools to handle this. You can obtain them freely on their website or github.
Below I give the full solution for a Mac running 10.10 and using the homebrew package manager. I use wine to run windows executables.
Overview
Download tools: Tesseract OCR to Page (TPT) and Page Viewer (PVT)
Use the TPT to run tesseract on your document and convert the HOCR xml to a PAGE xml
Use the PVT to view the original image with the PAGE xml information overlaid
Code
brew install wine # takes a little while >10m
brew install gs # only for generating a tif example. Not required, you can use Preview
brew install wget # only for downloading example paper. Not required, you can do so manually!
cd ~/Downloads
wget -O paper.pdf "http://www.prima.cse.salford.ac.uk/www/assets/papers/ICDAR2013_Antonacopoulos_HNLA2013.pdf"
# This command can be ommitted and you can do the conversion to tiff with Preview
gs \
-o paper-%d.tif \
-sDEVICE=tiff24nc \
-r300x300 \
paper.pdf
cd ~/Downloads
# ttptool is the location you downloaded the Tesseract to PAGE tool to
ttptool="/Users/Me/Project/tools/TesseractToPAGE 1.3"
# sudo chmod 777 "$ttptool/bin/PRImA_Tesseract-1-3-78.exe"
touch "$ttptool/log.txt"
wine "$ttptool/bin/PRImA_Tesseract-1-3-78.exe" \
-inp-img "$dl/Downloads/paper-3.tif" \
-out-xml "$dl/Downloads/paper-3-tool.xml" \
-rec-mode layout>>log.txt
# pvtool is the location you downloaded the PAGE Viewer tool to
pvtool="/Users/Me/Project/tools/PAGEViewerMacOS_1.1/JPageViewer 1.1 (Mac OS, 64 bit)"
cd "$pvtool"
dl=~
java -XstartOnFirstThread -jar JPageViewer.jar "$dl/Downloads/paper-3-tool.xml" "$dl/Downloads/paper-3.tif"
Results
Document with overlays (rollover to see text and type)
Overlays alone (use GUI buttons to toggle)
Appendix
You can run tesseract yourself and use another tool to convert its output to PAGE format. I was unable to get this to work but I'm sure you'll be fine!
# Note that the pvtool does take as input HOCR xml but it ignores the region type
brew install tesseract --devel # installs v 3.03 at time of writing
tesseract ~/Downloads/paper-3.tif ~/Downloads/paper-3 hocr
mv paper-3.hocr paper-3.xml # The page viewer will only open XML files
java -XstartOnFirstThread -jar JPageViewer.jar "$dl/Downloads/paper-3.xml"
At this point you need to use the PAGE Converter Java Tool to convert the HOCR xml into a PAGE xml. It should go a little something like this:
pctool="/Users/Me/Project/tools/JPageConverter 1.0"
java -jar "$pctool/PageConverter.jar" -source-xml paper-3.xml -target-xml paper-3-hocrconvert.xml -convert-to LATEST
Unfortunately, I kept getting null pointers.
Could not convert to target XML schema format.
java.lang.NullPointerException
at org.primaresearch.dla.page.converter.PageConverter.run(PageConverter.java:126)
at org.primaresearch.dla.page.converter.PageConverter.main(PageConverter.java:65)
Could not save target PAGE XML file: paper-3-hocrconvert.xml
java.lang.NullPointerException
at org.primaresearch.dla.page.io.xml.XmlInputOutput.writePage(XmlInputOutput.java:144)
at org.primaresearch.dla.page.converter.PageConverter.run(PageConverter.java:135)
at org.primaresearch.dla.page.converter.PageConverter.main(PageConverter.java:65)
You can use its API to obtain the bounding boxes at various levels (character/word/line/para) -- see API Example. You have to draw the labels yourself.
If you are python familiar, you can directly use tesserocr library which is a nice python wrapper around the C++ API. Here is a code snippet to draw polygons at block level using PIL:
from PIL import Image, ImageDraw
from tesserocr import PyTessBaseAPI, RIL, iterate_level, PSM
img = Image.open(filename)
results = []
with PyTessBaseAPI() as api:
api.SetImage(img)
api.SetPageSegMode(PSM.AUTO_ONLY)
iterator = api.AnalyseLayout()
for w in iterate_level(iterator, RIL.BLOCK):
if w is not None:
results.append((w.BlockType(), w.BlockPolygon()))
print('Found {} block elements.'.format(len(results)))
draw = ImageDraw.Draw(img)
for block_type, poly in results:
# you can define a color per block type (see tesserocr.PT for block types list)
draw.line(poly + [poly[0]], fill=(0, 255, 0), width=2)
With Tesseract 4.0.0, a command like tesseract source/dir/myimage.tiff target/directory/basefilename hocr will create a basefilename.hocr file with block-, paragraph-, line-, and word-level bounding boxes for the OCR'ed text. Even the command without the hocr config creates a text file with newlines between block-level text, but the hocr format is more explicit.
More config options here: https://github.com/tesseract-ocr/tesseract/tree/master/tessdata/configs
Shortcut
It is also possible to open HOCR files directly with the PageViewer tool. The file extension has to be .xml, however.
The HOCR individual character step is now available in Tesseract since 4.1.
Once the installation check, use :
tesseract {image file} {output name} -c tessedit_create_hocr=1 -c hocr_char_boxes=1

Disable Chrome address bar autosearch for dev

Developing a web app locally and I just can't get Chrome to actually go to this address, because auto search always kicks in (http://0.0.0.0:5000/api works though, for example).
Is there a way to overwrite this behaviour or force Chrome to try a http request first, before anything else?
I am aware I can just curl it or whatever to see the response...
Go to chrome://omnibox/ and check [x] Prevent inline autocomplete
The answer to this has to be in chrome://chrome-urls
you should see something like the following:
The "full" set of settings is on the chrome://chrome-urls . Also chrome://flags is worth to check. As a side by enabling some experimental features from chrome://flags you can greatly enhance your browsers HTML5 support which can be checked at HTML 5 support .
else:
Clear browser historythen go to settings and under Privacy --> uncheck [] Use a prediction service to help complete searches and URLs typed in the address bar .
Another idea is to examine carefully if using linux the output of the following command for parameters:
ps -aux | grep google-chrome-stable
in my case the output tells me a lot about what parameters are used at launch by default:
/opt/google/chrome/chrome --type=renderer --disable-layer-squashing --enable-transition-compositing --enable-deferred-image-decoding --enable-display-list-2d-canvas --enable-distance-field-text --enable-encrypted-media --enable-experimental-canvas-features --enable-experimental-web-platform-features --enable-lcd-text --enable-one-copy --enable-overlay-scrollbar --enable-renderer-mojo-channel --enable-smooth-scrolling --enable-viewport-meta --enable-webgl-draft-extensions --enable-web-midi --enable-zero-copy --max-tiles-for-interest-area=512 --enable-plugin-power-saver --lang=en-US --force-fieldtrials=AutoReloadExperiment/FlagEnabled/AutoReloadVisibleOnlyExperiment/FlagEnabled/ChromeSuggestions/Default/DomRel-Enable/enable/EnhancedBookmarks/Default/ExtensionContentVerification/Enforce/ExtensionInstallVerification/None/GCM/Enabled/MaterialDesignNTP/Enabled_forced/OmniboxBundledExperimentV1/StandardR4/PasswordGeneration/Disabled/PrerenderFromOmnibox/OmniboxPrerenderEnabled/QUIC/FlagEnabled/SafeBrowsingIncidentReportingService/Default/SettingsEnforcement/no_enforcement/UMA-Dynamic-Binary-Uniformity-Trial/default/UMA-Population-Restrict/normal/UMA-Uniformity-Trial-1-Percent/group_09/UMA-Uniformity-Trial-10-Percent/group_02/UMA-Uniformity-Trial-100-Percent/group_01/UMA-Uniformity-Trial-20-Percent/group_04/UMA-Uniformity-Trial-5-Percent/group_16/UMA-Uniformity-Trial-50-Percent/group_01/UwSInterstitialStatus/OnButInvisible/VoiceTrigger/Install/WebRTC-IPv6Default/Default/ --enable-crash-reporter=9F2AFD26-85F1-40CB-991F-0980EF2C4D14 --enable-offline-auto-reload --enable-offline-auto-reload-visible-only --enable-offline-load-stale-cache --enable-app-window-controls --enable-embedded-extension-options --enable-experimental-extension-apis --enable-scripts-require-action --enable-nacl --enable-nacl-debug --enable-streamlined-hosted-apps --enable-web-based-signin --javascript-harmony --out-of-process-pdf --enable-delegated-renderer --enable-impl-side-painting --num-raster-threads=4 --enable-gpu-rasterization --channel=5035.27.2136067136
Even yet another idea is maybe to write a small widget using python's tkinter and the webrowser modules, it could even get its input from the clipboard.
this command could be a work around solution too:
python -m webbrowser -t "http://ip.ip.ip.ip:portport/file/"
Another option yet is to use The Omnibox API and embed a custom omnibox in a simple webpage. Here are the omnibox api ready made samples.
You can avoid this by adding a "/" at the end of the URL http://0.0.0.0:5000/api/
or
You can try to add a null search engine with a URL of http://%s and null keyword.
Go to the search engine settings:
Open Settings.
Click Manage search engines
At the bottom of the Other search engines section add a new search engine.
Using #Juan Buhagiar answer as a starting point, I added your URL as the URL of a default search engine:
Other search engines
| MyAPI | 0.0.0.0 | http://0.0.0.0:5000/api/venues/show/45/20/cafes?rubbish=%s |
That just worked. The only drawback is that you get a redundant query to your request:
http://0.0.0.0:5000/api/venues/show/45/20/cafes?rubbish=0.0.0.0%3A5000%2Fapi%2Fvenues%2Fshow%2F45%2F20%2Fcafes%2F
instead of plain:
http://0.0.0.0:5000/api/venues/show/45/20/cafes
So as long as it does not conflict with your own GET, you can just ignore it.
Simplest way - Add a bookmark with your desired address. This time it will not goto search. This may not be useful if you have to change variables regularly, which was not my case.
This is helpful if you do not want it to disable omnibar search completely since it is quite a useful feature.

Gnome 3 displays two icons for same app in dock

I have created my own .desktop file for an application (sublimetext) in gnome 3 (fedora 16) I have succeeded in that:
[Desktop Entry]
Version=1.0
Name=Sublime Text 2
Comment=Text Editor
Terminal=false
Exec=sublime
Icon=/home/asher/apps/SublimeText2/Icon.png
Type=Application
Categories=TextEditor;Tools;
X-Ayatana-Desktop-Shortcuts=NewWindow
[NewWindow Shortcut Group]
Name=New Window
Exec=sublime
I want to add it to my favorites so it's locked to the sidebar. I did that, as you can see in the first screenshot, where no windows are open. The problem is, when I then open the program, another icon displays in the dockbar. The second icon is where the windows for the program are listed. The windows are consolidated to one icon, as you can see in the third screenshoot with two windows open (only two icons, not three).
Is there a way to make the favorited icon be the same as the icon that shows when the app launches? Does it have to be done in the program, or is this something I can do through a gnome 3 config file?
there's nothing wrong with your system.
The duplicated launcher icons explained:
The different icons are different commandline options. Some context applications with call the associated *.desktop icon. The exec option of the icon will depend on how the application is called.
Some of the Icons you show in your image may be obvious because of the difference in the way they are named. You can see the difference in the way the app is called by right clicking and clicking on properties to see other differences.
Some of the *.desktop files have a %U argument, used so the application will accept arguments.
Some of the Launchers are different commands that are called differently and are named differently often by a symbolic link.
Some exampes from the list in you image are:
Name: Online Accounts
Command: unity-control-center credentials
Name: Online Accounts
Command: Online account credentials and settings
Name: Personal File Sharing
Command: gnome-file-share-properties
Name: Rhythmbox
Command: rhythmbox %U
Name: Rhythmbox
Command rhythmbox-client --select-source %U
Source: link

Is there a way to convert Trac Wiki pages to HTML?

I see the suggestion of using Mylyn WikiText to convert wiki pages to html from this question except I'm not sure if its what I'm looking for from reading the front page of the site alone. I'll look into it further. Though I would prefer it being a Trac plug-in so I could initiate the conversion from within the wiki options but all the plugins at Trac-Hacks export single pages only whereas I want to dump all formatted pages in one go.
So is there an existing Trac plug-in or stand-alone application that'll meet my requirements? If not where would you point me to start looking at implementing that functionality myself?
You may find some useful information in the comments for this ticket on trac-hacks. One user reports using the wget utility to create a mirror copy of the wiki as if it was a normal website. Another user reports using the XmlRpc plugin to extract HTML versions of any given wiki page, but this method would probably require you to create a script to interface with the plugin. The poster didn't provide any example code, unfortunately, but the XmlRpc Plugin page includes a decent amount of documentation and samples to get you started.
If you have access to a command line on the server hosting Trac, you can use the trac-admin command like:
trac-admin /path/to/trac wiki export <wiki page name>
to retrieve a plain-text version of the specified wiki page. You would then have to parse the wiki syntax to HTML, but there are tools available to do that.
For our purposes, we wanted to export each of the wiki pages individually without the header/footer and other instance-specific content. For this purpose, the XML-RPC interface was a good fit. Here's the Python 3.6+ script I created for exporting the whole of the wiki into HTML files in the current directory. Note that this technique doesn't rewrite any hyperlinks, so they will resolve absolutely to the site.
import os
import xmlrpc.client
import getpass
import urllib.parse
def add_auth(url):
host = urllib.parse.urlparse(url).netloc
realm = os.environ.get('TRAC_REALM', host)
username = getpass.getuser()
try:
import keyring
password = keyring.get_password(realm, username)
except Exception:
password = getpass.getpass(f"password for {username}#{realm}: ")
if password:
url = url.replace('://', f'://{username}:{password}#')
return url
def main():
trac_url = add_auth(os.environ['TRAC_URL'])
rpc_url = urllib.parse.urljoin(trac_url, 'login/xmlrpc')
trac = xmlrpc.client.ServerProxy(rpc_url)
for page in trac.wiki.getAllPages():
filename = f'{page}.html'.lstrip('/')
dir = os.path.dirname(filename)
dir and os.makedirs(dir, exist_ok=True)
with open(filename, 'w') as f:
doc = trac.wiki.getPageHTML(page)
f.write(doc)
__name__ == '__main__' and main()
This script requires only Python 3.6, so download and save to a export-wiki.py file, then set the TRAC_URL environment variable and invoke the script. For example on Unix:
$ TRAC_URL=http://mytrac.mydomain.com python3.6 export-wiki.py
It will prompt for a password. If no password is required, just hit enter to bypass. If a different username is needed, also set the USER environment variable. Keyring support is also available but can be disregarded.