[PYTHON] Save and retrieve files with Pepper

[ALMemory](http://qiita.com/Atelier-Akihabara/items/4162192129f366da1240#almemory%E3%82%92%E4%BB%8B%] when saving or retrieving any information in Pepper E3% 81% 97% E3% 81% 9F% E3% 82% A4% E3% 83% 99% E3% 83% B3% E3% 83% 88% E5% 80% A4% E3% 81% AE% E5% 8F% 96% E5% BE% 97) is the royal road, but when you think about saving a slightly larger text with an application and extracting it with a development PC, the file is more convenient. There is also.

As a process in such a case, I made a box to write out a text file and tried to extract the file from Pepper to the local environment, so I made a note.

Text file export box

The box is located in the web-boxes directory at https://github.com/yacchin1205/pepper-web-boxes.

Clone or Download ZIP, Load Box Library [http://qiita.com/Atelier-Akihabara/items/db907ed319b8af0984db#%E3%83%9C%E3%83%83%E3%82% AF% E3% 82% B9% E3% 83% A9% E3% 82% A4% E3% 83% 96% E3% 83% A9% E3% 83% AA% E3% 81% AE% E8% AA% AD% You can use it by loading the box library according to the procedure of E3% 81% BF% E8% BE% BC% E3% 81% BF).

The created box is in the ʻIO` folder of the box library and looks like the following. Is there something like a box document generation tool somewhere ...

IO> Write Text box

Write the text to a file.

write-text-box.png

onStart input

Enter the text you want to export. When this input fires, it writes the string entered in the file specified by the File path parameter described below.

onStopped output

Output when the export is successful.

onFailure output

When writing fails, a character string indicating the content of the error is output.

File path parameter

Specify the path of the text file to read. It is created based on the current directory where the behavior is running.

Encoding parameter

Specifies the character encoding of the text file to read. The default is ʻutf8`. Please refer to http://docs.python.jp/2.7/library/codecs.html for what kind of value can be specified.

I'm addicted to: handling unicode in python boxes

It's not this Write Text box, but I fell in love with the Unicode trap with the code below in the Read Text box I made with it.

with codecs.open(self.getParameter("File path"), "r", self.getParameter("Encoding")) as f:
    text = f.read()
self.onStopped(text.encode("utf8"))

I have specified character string as the type of onStopped, but if I give the unicode type character string obtained byread ()as an argument, None is passed to the next input. It seems to end up. I wrote some test code, but when I say String in Choregraphe's documentation, it seems that only Python's str type is considered and unicode type cannot be processed. For the time being, I avoided it by giving it as a byte array with ʻencode ("utf8") `, but this Choregraphe behavior does not match the idea of Python strings, and it is very cool in that it may cause confusion. I feel like I'm not.

For Japanese people, it's better to clarify this, so I asked in the Aldebaran Community (String in Choregraphe's python box. -choregraphes-python-box-2651)) However, the answer is that unicode type is not supported now ... Um.

test

For the time being, a simple test is written in the repository tests / test-io-text /. For example, test at the level of whether the content obtained in the Read Text box is as expected as shown below. For example, the Read Text test looks like this.

read-text-test.png

For the idea, refer to Pepper Box Library Test Consideration for reference.

Extract files to local environment

Some about how to retrieve files saved in these boxes.

Use Choregraphe

For Pepper's file system, you can upload and download files with Choregraphe. [Transfer files to advanced menu](http://qiita.com/tonosamart/items/fe3948a45ccc97ddbbab#choregraphe%E3%81%A7%E3%81%A4%E3%81%AA%E3%81%84% E3% 81% A7% E3% 81% BF% E3% 82% 8B% EF% BC% 92), so it is the royal road to use it.

File download from Pepper using HTTP

When thinking about automation, I sometimes want to retrieve files even in an environment without Choregraphe. Looking at the documentation NAOqi Developer guide> Programming> Connecting Choregraphe / your robot> Connection Management, it seems that you can assume that FTP is working, but I dared to try using HTTP.

[If you create a directory called html in the project, you can see it from the tablet](http://qiita.com/Atelier-Akihabara/items/716e88b306db12751c9b#%E7%B0%A1%E5%8D%98%E3%81%AAhtml% E3% 82% 92% E3% 82% BF% E3% 83% 96% E3% 83% AC% E3% 83% 83% E3% 83% 88% E3% 81% AB% E8% A1% A8% E7% A4% BA), but in this state you can see the contents of this html directory by doing http: // (Pepper's name.local. Or IP address) /apps/.lastUploadedChoregrapheBehavior/. Maybe it's the same idea as putting the public_html directory in your home directory on your web server.

So why not generate the files you want to make available for download in the html directory? I created the samples / dump-info project in the repository. Here, all the contents of ALMemory and all the contents of Preferences are dumped, saved in a text file, and downloaded from an external PC.

dump-info-flow.png

The points are the following two points.

  1. The File path parameter given to the Write Text box is set by the output value of the original Get HTML File box (also placed in web-boxes). In the Get HTML File box, the output file path is generated based on the absolute path of the html directory in your application.
  2. In html / index.html, a link to the specified file is provided in the Get HTML File box.

After running this application, open a URL like http: // Pepper's IP / apps / .lastUploadedChoregrapheBehavior / on a PC connected to the same LAN as Pepper. Then you can check the index.html of the project from your PC. Here, you can download the file (1.) exported in the Write Text box by clicking the link to the file (2.).

Like this, if you want to create some file in the project and pull it out from the PC, you should create the file in the html directory of the project. Let's use this for a while ...

Recommended Posts

Save and retrieve files with Pepper
Edit and save read-only files with vim
Read and write csv files with numpy
Reading and writing JSON files with Python
Face detection from multiple image files with openCV, cut out and save
Drag and drop local files with Selenium (Python)
Download and import files with Splunk external python
Describe ec2 with boto3 and retrieve the value
Reading and writing fits files with Python (memo)
Rename and move files (directories) with mv command
Upload files with Django
Save memory with `` __slots__``
I tried to read and save automatically with VOICEROID2 2
I tried to automatically read and save with VOICEROID2
Automatically save .py and .html files in Jupyter notebook.
Read and write files with Slackbot ~ Bot development with Python ~
Point Cloud with Pepper
Linux: files and directories
Save lists, dictionaries and tuples to external files python
With and without WSGI
kobitonote.py --Synchronize and save items edited with Kobito to Evernote
Visualize cavity flow with matplotlib and save as gif animation
Sample of HTTP GET and JSON parsing with python of pepper
Upload and delete files to Google Cloud Storages with django-storage
With me, cp, and Subprocess
Programming with Python and Tkinter
Encryption and decryption with Python
Linux (about files and directories)
Python and hardware-Using RS232C with Python-
About LINUX files and processes
Save images with web scraping
Reading and writing csv files
Sorting image files with Python (2)
Sort huge files with python
Sorting image files with Python (3)
Debug with PEPPER python interpreter
Sorting image files with Python
Save tweet data with Django
Transfer files with teraterm [Note]
Integrate PDF files with Python
Reading .txt files with Python
Super-resolution with SRGAN and ESRGAN
group_by with sqlalchemy and sum
python with pyenv and venv
Handle JSON files with Matlab
With me, NER and Flair
Works with Python and R
Operate Jupyter with REST API to extract and save Python code
Let's create a tic-tac-toe AI with Pylearn 2-Save and load models-
[AWS] Search and acquire necessary data from S3 files with S3 Select
How to import CSV and TSV files into SQLite with Python
Correspondence analysis of sentences with COTOHA API and save to file
Take an image with Pepper and display it on your tablet
[Python3] Save the mean and covariance matrix in json with pandas
Get OCTA simulation conditions from a file and save with pandas
Operate Firefox with Selenium from python and save the screen capture