-
Notifications
You must be signed in to change notification settings - Fork 60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add translator package #461
Open
sktometometo
wants to merge
10
commits into
jsk-ros-pkg:master
Choose a base branch
from
sktometometo:PR/add-translator-package
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
80fa657
[ros_translator] add ros_translator package
sktometometo d4e9e52
[ros_translator] add translator
sktometometo 076c2ab
[ros_translator] add README.md
sktometometo acd8d8c
update README.md
sktometometo 7d6c6e8
[ros_translator] specify deep-translator version
sktometometo 0a83f3f
[ros_translator] update package metadata
sktometometo 50733ff
[ros_translator] avoid next
sktometometo 24a41b2
[jsk_3rdparty] add ros_translator to package.xml
sktometometo 9c19030
[ros_translator] drop indigo and kinetic
sktometometo 9081889
Merge branch 'master' into PR/add-translator-package
sktometometo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
cmake_minimum_required(VERSION 2.8.3) | ||
project(ros_translator) | ||
|
||
find_package(catkin REQUIRED COMPONENTS dynamic_reconfigure catkin_virtualenv) | ||
|
||
if(NOT (("$ENV{ROS_DISTRO}" STREQUAL "noetic") OR | ||
("$ENV{ROS_DISTRO}" STREQUAL "melodic"))) | ||
message(WARNINIG "ros_translator can only be built for melodic and noetic") | ||
return() | ||
endif() | ||
|
||
catkin_python_setup() | ||
|
||
catkin_generate_virtualenv( | ||
PYTHON_INTERPRETER python3 | ||
USE_SYSTEM_PACKAGE FALSE | ||
CHECK_VENV FALSE | ||
) | ||
|
||
|
||
generate_dynamic_reconfigure_options( | ||
cfg/ROSTranslator.cfg | ||
) | ||
|
||
catkin_package() | ||
|
||
catkin_install_python(PROGRAMS | ||
scripts/ros_translator_node.py | ||
scripts/demo_input_node.py | ||
scripts/demo_output_node.py | ||
DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION} | ||
) | ||
|
||
install(FILES requirements.txt | ||
DESTINATION ${CATKIN_PACKAGE_SHARE_DESTINATION}) |
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,52 @@ | ||
# ros_translator | ||
|
||
A ROS package for language translation. | ||
This package uses Python package [deep-translator](https://pypi.org/project/deep-translator) as a backend. | ||
|
||
## Demo | ||
|
||
```bash | ||
roslaunch ros_translator demo | ||
``` | ||
|
||
And then, please input any text to `xterm` prompt. You will get a translated text. | ||
|
||
```bash | ||
Input any text >沙羅双樹の花の色、盛者必衰の理をあらはす | ||
``` | ||
|
||
```bash | ||
[INFO] [WallTime: 1682560820.715069] [node:/demo_output_node] [func:DEMO.callback]: Translated text: The color of the flowers of the sal tree reveals the reason why prosperity must decline | ||
``` | ||
|
||
## `ros_translator_node.py` Interface | ||
|
||
### Subsriber | ||
|
||
* `~input_text` (`std_msgs/String`) | ||
|
||
Input text to be translated. | ||
|
||
### Publisher | ||
|
||
* `~output_text` (`std_msgs/String`) | ||
|
||
Output text translated. | ||
|
||
### Parameters | ||
|
||
* `~translator` (`String`, default: `google`) | ||
|
||
Backend translation web service | ||
|
||
* `~from_language` (`String`, default: `auto`) | ||
|
||
Input language | ||
|
||
* `~to_language` (`String`, default: `en`) | ||
|
||
Target language | ||
|
||
* `~api_key` (`String`, default: ``) | ||
|
||
API key for some backend web services |
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,13 @@ | ||
#!/usr/bin/env python | ||
PACKAGE = "ros_translator" | ||
|
||
from dynamic_reconfigure.parameter_generator_catkin import * | ||
|
||
gen = ParameterGenerator() | ||
|
||
gen.add('translator', str_t, 0, 'Backend Translator Service', 'google') | ||
gen.add('from_language', str_t, 0, 'Input language', 'auto') | ||
gen.add('to_language', str_t, 0, 'Output Language', 'en') | ||
gen.add('api_key', str_t, 0, 'api_key for translator', '') | ||
|
||
exit(gen.generate(PACKAGE, 'ros_translator', 'ROSTranslator')) |
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,15 @@ | ||
<launch> | ||
|
||
<node pkg="ros_translator" type="ros_translator_node.py" name="ros_translator" output="screen"> | ||
</node> | ||
|
||
<node pkg="ros_translator" type="demo_output_node.py" name="demo_output_node" output="screen"> | ||
<remap from="~text" to="/ros_translator/output_text"/> | ||
</node> | ||
|
||
<node pkg="ros_translator" type="demo_input_node.py" name="demo_input_node" output="screen" | ||
launch-prefix="xterm -e"> | ||
<remap from="~text" to="/ros_translator/input_text"/> | ||
</node> | ||
|
||
</launch> |
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,26 @@ | ||
<?xml version="1.0"?> | ||
<package format="2"> | ||
<name>ros_translator</name> | ||
<version>2.1.24</version> | ||
<description>The ros_translator package</description> | ||
|
||
<maintainer email="[email protected]">Koki Shinjo</maintainer> | ||
<maintainer email="[email protected]">Kei Okada</maintainer> | ||
<author email="[email protected]">Koki Shinjo</author> | ||
|
||
<license>BSD</license> | ||
|
||
<buildtool_depend>catkin</buildtool_depend> | ||
|
||
<depend>rospy</depend> | ||
|
||
<build_depend>dynamic_reconfigure</build_depend> | ||
<build_depend>catkin_virtualenv</build_depend> | ||
|
||
<exec_depend>std_msgs</exec_depend> | ||
<exec_depend>xterm</exec_depend> | ||
|
||
<export> | ||
<pip_requirements>requirements.txt</pip_requirements> | ||
</export> | ||
</package> |
Empty file.
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,89 @@ | ||
import rospy | ||
from std_msgs.msg import String | ||
from deep_translator import ( | ||
GoogleTranslator, | ||
MicrosoftTranslator, | ||
PonsTranslator, | ||
LingueeTranslator, | ||
MyMemoryTranslator, | ||
YandexTranslator, | ||
PapagoTranslator, | ||
#DeeplTranslator, | ||
#QcriTranslator, | ||
single_detection, | ||
batch_detection) | ||
from dynamic_reconfigure.server import Server | ||
from ros_translator.cfg import ROSTranslatorConfig | ||
|
||
engines = { | ||
'google': GoogleTranslator, | ||
'microsoft': MicrosoftTranslator, | ||
'pons': PonsTranslator, | ||
'linguee': LingueeTranslator, | ||
'mymemory': MyMemoryTranslator, | ||
'yandex': YandexTranslator, | ||
'papago': PapagoTranslator, | ||
#'deepl': DeeplTranslator, | ||
#'qcri': QcriTranslator, | ||
} | ||
|
||
|
||
class ROSTranslator(object): | ||
|
||
def __init__(self): | ||
|
||
self.from_language = rospy.get_param('~from_language', 'auto') | ||
self.to_language = rospy.get_param('~to_language', 'en') | ||
self.translator = rospy.get_param('~translator', 'google') | ||
self.api_key = rospy.get_param('~api_key', '') | ||
self.engine = None | ||
self.update_engine() | ||
|
||
self.param_srv = Server(ROSTranslatorConfig, self.param_callback) | ||
self.pub = rospy.Publisher('~output_text', String, queue_size=1) | ||
self.sub = rospy.Subscriber('~input_text', String, self.callback) | ||
|
||
def update_engine(self): | ||
|
||
if self.translator in engines: | ||
if self.translator == 'yandex': | ||
self.engine = engines[self.translator](self.api_key) | ||
elif self.translator == 'microsoft': | ||
self.engine = engines[self.translator](api_key=self.api_key, | ||
target=self.to_language) | ||
else: | ||
self.engine = engines[self.translator](source=self.from_language, | ||
target=self.to_language) | ||
else: | ||
rospy.logerr('Unknown translator backend: {}'.format(self.translator)) | ||
self.engine = None | ||
|
||
def translate(self, text): | ||
|
||
if self.engine is None: | ||
rospy.logwarn('Engine not loaded.') | ||
return '' | ||
|
||
if self.translator == 'yandex': | ||
return self.engine.translate(source=self.from_language, | ||
target=self.to_language, | ||
text=text) | ||
elif self.translator == 'microsoft': | ||
return self.engine.translate(text) | ||
else: | ||
return self.engine.translate(text) | ||
|
||
def callback(self, msg): | ||
|
||
output_msg = String() | ||
output_msg.data = self.translate(msg.data) | ||
self.pub.publish(output_msg) | ||
|
||
def param_callback(self, config, level): | ||
|
||
self.from_language = config['from_language'] | ||
self.to_language = config['to_language'] | ||
self.translator = config['translator'] | ||
self.api_key = config['api_key'] | ||
self.update_engine() | ||
return config |
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 @@ | ||
deep_translator==1.5.0 |
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,25 @@ | ||
#!/usr/bin/env python | ||
|
||
import rospy | ||
from std_msgs.msg import String | ||
|
||
|
||
class DEMO(object): | ||
|
||
def __init__(self): | ||
|
||
self.pub = rospy.Publisher('~text', String, queue_size=1) | ||
|
||
def prompt(self): | ||
|
||
while not rospy.is_shutdown(): | ||
|
||
text = input('Input any text >') | ||
msg = String(data=text) | ||
self.pub.publish(msg) | ||
|
||
|
||
if __name__ == '__main__': | ||
rospy.init_node('demo_input') | ||
node = DEMO() | ||
node.prompt() |
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,21 @@ | ||
#!/usr/bin/env python | ||
|
||
import rospy | ||
from std_msgs.msg import String | ||
|
||
|
||
class DEMO(object): | ||
|
||
def __init__(self): | ||
|
||
self.sub = rospy.Subscriber('~text', String, self.callback) | ||
|
||
def callback(self, msg): | ||
|
||
rospy.loginfo('Translated text: {}'.format(msg.data)) | ||
|
||
|
||
if __name__ == '__main__': | ||
rospy.init_node('demo_output') | ||
node = DEMO() | ||
rospy.spin() |
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,10 @@ | ||
#!/usr/bin/env python | ||
|
||
import rospy | ||
from ros_translator.core import ROSTranslator | ||
|
||
if __name__ == '__main__': | ||
|
||
rospy.init_node('ros_translator') | ||
node = ROSTranslator() | ||
rospy.spin() |
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,7 @@ | ||
from distutils.core import setup | ||
from catkin_pkg.python_setup import generate_distutils_setup | ||
|
||
d = generate_distutils_setup(packages=['ros_translator'], | ||
package_dir={'': 'python'}) | ||
|
||
setup(**d) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about adding actionlib server?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think actionlib server is not necessary since if we just want to translate some text in one node. It is enough to use
deep-translator
library directory.