University of Southern CaliforniaUSC
USC ICT TwitterUSC ICT FacebookUSC ICT YouTube

Performance while running a thread from SBScript in python | General SmartBody Discussion | Forum

Avatar

Please consider registering
guest

sp_LogInOut Log In sp_Registration Register

Register | Lost password?
Advanced Search

— Forum Scope —




— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

sp_Feed Topic RSS sp_TopicIcon
Performance while running a thread from SBScript in python
January 8, 2016
7:59 am
Avatar
Member
Members
Forum Posts: 11
Member Since:
July 29, 2015
sp_UserOfflineSmall Offline

Hey!

 

I'm running the following code:

http://pastebin.com/4NSuUNAF

It shows an opencv camera display and listens for key presses, which then invoke recognize methods that assert gender or emotion.

and I have a huge problem with performance in the thread that I run from SBScript.start.

Has anyone run into this problem and found a solution?

I have read about python multithreading being uncontrollable due to GIL, but I'm not sure if that is the reason for this behavior or some limitations on the SBScript side. I thought about multiprocessing but haven't tried that yet (I'll be getting to it soon, and then I'll update this forum thread).

January 8, 2016
5:48 pm
Avatar
Admin
Forum Posts: 944
Member Since:
December 1, 2011
sp_UserOfflineSmall Offline

I would organize it a different way:

 

I would instead run your facial detection in another process, then send messages over ActiveMQ (the VHMSG protocol) to SmartBody, which can process the data in its own thread. This would eliminate the threading issue entirely, by using the messaging protocol as the means the synchronize the data.

 

Ari

January 9, 2016
1:46 pm
Avatar
Member
Members
Forum Posts: 11
Member Since:
July 29, 2015
sp_UserOfflineSmall Offline

Sounds a little bit overcomplicated, nevertheless I did try that and for some reason SmartBody does not want to consume my messages.

I send sb print 'test' with STOMP in python using stomp.py to the DEFAULT_SCOPE topic and nothing happens, it is clearly connected and the consumer exists: ELVISH_SCOPE = 'DEFAULT_SCOPE' AND MESSAGE_PREFIX = 'sb'.

Anyway I am completly new in the activemq topic, I don't even know if the message sent by STOMP can be consumed by OpenWire.

 

UPDATE: Nevermind, I'm stupid I see the Python API scene.vhmsg(string), I'll see if I can make a use of it.

UPDATE2: Right, I'm doubly stupid, I need to send messages outside of the SmartBody API. I subscribed to the 'DEFAULT_SCOPE' in stomp and got a message from scene.vhmsg(string) with headers {"MESSAGE_PREFIX":"sb", "ELVISH_SCOPE":"DEFAULT_SCOPE"} so I tried sending messages with such headers, and while they 'fit' the selectors they are always stuck inflight, and wont get dequeued. It might have something to do with the format I send the message as "sb print 'test'".encode(encoding='UTF-8',errors='strict') and from scene.vhmsg("sb print 'test'") I receive "sb print+'test'".

January 10, 2016
3:51 pm
Avatar
Member
Members
Forum Posts: 11
Member Since:
July 29, 2015
sp_UserOfflineSmall Offline

In the end I implemented a simple interface for vhmsg similar to VHMsgManager, and exposed it in python, and it sends messages properly.

January 29, 2016
4:15 pm
Avatar
Member
Members
Forum Posts: 30
Member Since:
April 27, 2015
sp_UserOfflineSmall Offline

Hi!

How did you do it? I would be really interested as I need to do something similar in my project.

Thank you very much

January 31, 2016
8:17 am
Avatar
Admin
Forum Posts: 944
Member Since:
December 1, 2011
sp_UserOfflineSmall Offline

You can send messages from your application in python to SmartBody via VHMSG. There is a realtime data manager (starting in r6226) that you can use by sending a message like this:

 

sb scene.getRealTimeManager().setData("position", "8 2 3")
sb scene.getRealTimeManager().setData("rotation", ".7 .2 .3 .5")

 

and the data can be accessed in a controller in real time in SmartBody:

 

class MyController (PythonController):
   def init(self, pawn):
      print "Controller initiliazlied..."
   def evaluate(self):

      # every time step, get the realtime manager and interpret the data
      realtimeManager = scene.getRealtimeManager()

     # get the position, interpreted as a vector

      vec = realtimeManager.getDataVec("position")

      # now you have an SrVec as a position, do something with it...

 

      # get the orientation, interpreted as a quaternion

      q = realtimeManager.getDataQuat("rotation")

      # now you have an SrQuat as a rotation, do something with it...

 

# instantiate this controller once for each character
myc = MyController()
# get the character
character = scene.getCharacter("mycharacter")
# run this controller in position 30 (or whereever the right place to run this controller is relative to the other controllers)
character.addController(30, myc)

 

So messages from your camera/sensor will be sent asynchronously. SmartBody on every frame will check for new messages in the message queue, then place them in the RealtimeManager, accessible for any controller (or other process) to access. Your controller will run every step and apply the data to your character. This way the sensor and SmartBody are not run in the same process, and could potentially use different processors/cores/whatever.

 

Ari

February 7, 2016
4:13 pm
Avatar
Member
Members
Forum Posts: 30
Member Since:
April 27, 2015
sp_UserOfflineSmall Offline

Hello

I was asking more on how to expose the VHMsgManager in python. I would like to send messages to SmartBody but I cannot using stomp.py. I am also trying to export SBVHMsgManager using pybind11 but I am failing at it. Could I write a more simple file in C++ to export to python?. Is there a way to send messages to SmartBody using only Python?

Thanks

February 8, 2016
11:16 am
Avatar
Admin
Forum Posts: 944
Member Since:
December 1, 2011
sp_UserOfflineSmall Offline

You can download Python bindings for VHMSG here:

 

http://smartbody.ict.usc.edu/s.....msg-py.zip

 

Ari

February 8, 2016
11:28 am
Avatar
Member
Members
Forum Posts: 30
Member Since:
April 27, 2015
sp_UserOfflineSmall Offline

Thank you very much, I really apreciate it. To use it in Linux I think I should compile the files in the vhmsg-py-src/src, am I right?

February 8, 2016
11:33 am
Avatar
Admin
Forum Posts: 944
Member Since:
December 1, 2011
sp_UserOfflineSmall Offline

yes

February 10, 2016
10:14 am
Avatar
Member
Members
Forum Posts: 30
Member Since:
April 27, 2015
sp_UserOfflineSmall Offline

Hello

How could I compile and link vhmsg in order to compile the vhmsg-py files?

Thank you

February 11, 2016
12:50 pm
Avatar
Admin
Forum Posts: 944
Member Since:
December 1, 2011
sp_UserOfflineSmall Offline

The source code for vhmsg is in the lib/vhmsg directory in the source repository. You should be able to build the vhmsg library via a SmartBody build, then reuse it for vhmsg.py.

March 17, 2017
11:15 pm
Avatar
Member
Members
Forum Posts: 8
Member Since:
August 3, 2014
sp_UserOfflineSmall Offline

In case somebody is still interested in using stomp.py to talk to SmartBody:

for it to work, you need to make sure the content length is not being transmitted ("content-length").

You can do this by calling

conn.auto_content_length = False

here's a complete script that works for me for sending a BML command:

import time
import sys

import stomp

conn = stomp.Connection()
conn.start()
conn.connect('admin', 'password', wait=True)

msg = "vrSpeak FuseCharacter+ALL+bml_25722044+%3c%3fxml+version%3d%221.0%22+%3f%3e%3cact%3e%3cbml%3e%3cface+au%3d%229%22+type%3d%22facs%22+side%3d%22BOTH%22+start%3d%220%22+end%3d%220.4%22+amount%3d%220.2%22%2f%3e%3cface+au%3d%2227%22+type%3d%22facs%22+side%3d%22BOTH%22+start%3d%220%22+end%3d%220.4%22+amount%3d%220.2%22%2f%3e%3c%2fbml%3e%3c%2fact%3e+"
headers = {}
headers['ELVISH_SCOPE'] = 'DEFAULT_SCOPE'
headers['MESSAGE_PREFIX'] = 'vrSpeak'
conn.auto_content_length = False
conn.send(body=msg, headers=headers, destination='/topic/DEFAULT_SCOPE')

time.sleep(1)
conn.disconnect()

Forum Timezone: America/Los_Angeles

Most Users Ever Online: 733

Currently Online: bonadio56, irmawy11
25 Guest(s)

Currently Browsing this Page:
2 Guest(s)

Member Stats:

Guest Posters: 61

Members: 11750

Moderators: 3

Admins: 4

Forum Stats:

Groups: 1

Forums: 5

Topics: 409

Posts: 2264