I have a Python program that detects hand coordinates in a video stream.
I’m trying to send those coordinates continuously to my Slicer Python extension so I can display the hand in Slicer.
I think I’m half-way there. I can update the hand model in Slicer by repeatedly pressing a button. This is what I currently have:
The python program continuously sends data using from multiprocessing.connection import Client
, it looks more or less like this:with Client(('localhost', 5038)) as c:
msg = np.reshape(all_fings_world[:, 0:3], (63, 1)).tobytes()
c.send(msg)
In Slicer, in my extension I created a button that when clicked executes code that looks more or less like this:try:
self.serv = Listener(('', 5038))
client = self.serv.accept()
self.message = client.recv()
self.update_hand()
except Exception as e:
print(e)
What I’m missing is to not have to keep clicking this button to update the hand in Slicer. Is there a way to make Slicer execute some code in a loop in the ‘background’ so that Slicer can still be used? I tried creating a thread to run this code, but this didn’t do anything. And just putting everything in a while True
freezes Slicer.
You can use OpenIGTLink to send and receive the points:
In your python program, you can use the python implementation of OpenIGTLink (GitHub - lassoan/pyigtl: Python implementation of OpenIGTLink ) to send and on the Slicer side you can use the OpenIGTLinkIF module to receive the data.
OpenIGTLink natively supports streaming points. You can find the protocol specifications here: OpenIGTLink/point.md at release-3.0 · openigtlink/OpenIGTLink · GitHub
2 Likes
pieper
(Steve Pieper (Isomics, Inc.))
June 20, 2022, 7:56pm
3
OpenIGTLink is a good option. Another option would be to use the new WebServer to control the scene. It is also integrated in the Slicer event loop so it handles updates asynchronously.
Or if you want to stick with something lower level like you are currently doing you could use a QSocketNotifier
like is done in the WebServer code so that you get a Qt signal when the socket has data ready to read.
1 Like
muratmaga
(Murat Maga)
June 21, 2022, 1:55am
4
Is this in latest stable? I am not seeing a module called webserver.
WebServer was integrated into Slicer on May 11th or about a week after the Slicer 5.0.2 release. Therefore it is only available in Slicer Preview builds since then.
committed 01:57AM - 12 May 22 UTC
* ENH: add WebServer module
Code imported from Steve Pieper's developments.
…
https://github.com/pieper/SlicerWeb
* DOC: add WebServer documentation page
* DOC: add note about web server access
* ENH: Improve WebServer module
- Moved out request handlers to separate files.
- Allow registration of custom request handlers.
- Iterate through registered request handlers and let that one process the request that returned the highest confidence.
- Save default handler and logging options in application settings.
- Start/stop the server when a start/stop button is pressed (not start when opening the WebServer module GUI).
- Updated Slicer icon.
- Updated module icon (API icon from Google Material Icon Set)
- Added `sampleData` command to allow easy testing and demoing of the API
- Added `gui` command to switch between full application/viewers only, switch viewer layout
- Added `screenshot` command to get screenshot of the application main window
* DOC: Update webserver.md
* ENH: web server fixes
* add entry on demo page to reset view to four up with controls
* change "got it" messages to more meaningful feedback
* add extra tooltip hints on usage
* fix logic of local connection button
* DOC: add web server security suggestion
* STYLE: fix codespell findings
https://github.com/Slicer/Slicer/actions/runs/2254569488
* ENH: add exception handler for web requests
This puts the stack trace in the log and prevents
any bad request handlers from leaving the network connection
dangling.
* BUG: fix dicomweb in Web Server module
A newer version of pydicom is required to fix a bug
in json serialization of a pydicom multivalue class.
Also fix a warning from bad uri syntax.
* BUG: fix DICOMweb endpoint for OHIF
* Implements study level query and metadata.
* Adds offset and limits to study listing
* Adds documentation on using with OHIF
Co-authored-by: Andras Lasso <lasso@queensu.ca>
1 Like