[PYTHON] [IOS] First Pyto

Introduction

I think Pythonista is the most famous Python environment for iOS. Personally Pythonista

I was caught in that point and did not buy it. One day, an application called "Pyto" was introduced in Nikkei software, and when I searched for it, the following points were more attractive than Pythonista, so I bought it.

This article explains how to use Pyto and its basic functions.

environment

iPad pro11(2018)64GB (iOS13.3.1) Pyto(11.4)

How to use

Startup screen / file selection screen

119ADF6B-1BF9-460A-830F-FF8E719FE5D6.jpeg

When you start the app for the first time, the screen for selecting a file will be displayed. Open the file in any location or create a new one. In addition, the "..." button on this screen (the button indicated by the pink arrow in the figure) ~~ (Added on 2020/4/22 (v11.8.3): changed to a bulleted icon) is an app or It becomes the menu and setting of the editor. 050F0333-FE89-4B2B-8D57-940304DBE305.jpeg

Menu / Settings

96D890E7-A6CE-4219-A927-CA7E488E89BE.png D33381E6-15EB-4CBF-89F3-177E98441FF7.png

The menu is as shown above. Here you can set the editor / appearance, use the interactive mode, and add a new library with PyPi (additional packages are limited to pure python).

Interactive mode (REPL)

AEE1ED64-E070-40EB-A179-586CEEA2DFFC.png Interactive mode. Enter the code in the text box below. The graph of matplotlib can be displayed even in the interactive mode.

Run module (python -m) DF58FB17-6DC9-479F-AC54-545CEF6F2273.png

You can run the .py file. I'm running pip on the screen above, but I couldn't see the package information in pip show. I also ran pip list to see which packages were installed, but only the packages I installed later on PyPi, which I'll discuss later, were displayed.

PyPi 61F51E40-6823-4EBA-9221-CF994A14CF4F.png 166D96AF-234F-40A3-8422-24A9C92A295A.png 988088C5-1672-4DA2-837D-5AB3196EB792.png

You can use PyPi to check the version of the installed library or add a new library. However, it seems that only pure python can be added. I was able to install seaborn, but not tensorflow and pyaudio.

B703C6B8-29F2-4436-9BC7-51AC1CF816A7.png In the figure above, pair plot is executed using the installed seaborn. When using matplotlib or seaborn with Pyto, if you do not write plt.clf () at the end, the graphs will overlap when executed multiple times.

Sample code

ADC66C5B-9A2C-4FFF-A238-03AF43963979.png Contains sample code using OpenCV, sklearn, GUI modules, etc.

Loaded modules 51B7203E-521E-40C4-A3A7-0EEAE7466E13.png

The modules that have been loaded are displayed. You can also view the source code for that module. I'm not sure what it's used for.

Settings 5AC0364F-4ACE-40E1-BBE4-718DBF50E06A.png B3694FC6-E2D0-4C16-A222-27F8C0CB2469.png

This is the setting screen of the editor. It corresponds to the soft tab (space tab). You can also choose from several types of editor appearances and customize them.

Editor screen

CBAE04E3-7B21-4232-B16A-10F3A32B7E56.png

This is the editor screen. There is a gear mark at the bottom right, but this is not an app setting. You can register the file being edited in Siri and change the current directory. The lower left ... is an editor action that seems to be able to do something with the file. The bookmark icon next to it is the Pyto document. It describes how to use Pyto packages (GUI tools, etc.). This document is published on the Internet and can be found at here.

The buttons at the top are "Return to file selection screen", "Search / Replace", "Debug (pdb)", and "Execute" in order from the four rectangles on the left.

You can also end the input when using the external keyboard by pressing a button such as "V" at the bottom right of the screen. This makes it easier to move the cursor to the browser when used with the browser in split view.

add-to-siri, arguments, current directory 2C364699-02CD-4BBC-ABE0-479A590C348B.png You can set add-to-siri, arguments, and current directory. In arguments, you can set the arguments to be passed when the program is executed. You can change the current directory with current directory. If you change it, a warning will be displayed, but you can execute it without any problem even after changing the current directory. The directory selected here has edit permission, and you can browse and change files. If you cannot read a local file, you may be able to read it by selecting a folder here.

(Added on 2020/4/22 (v11.8.3): The design has changed) FAFC6C25-426A-4384-B1B9-F754CDC3FCD8.png

Editor actions DCC4FE4E-7D14-4810-9EB5-F4006FA0F774.png Editor actions. 2to3 and black are available by default.

document

8C27C7A1-ADC6-4429-925A-68C007B37B62.png The document. It is the same as the published here. Click the button like "V" on the upper right to switch to split view.

8C6BDE7F-5BE0-4C97-B1B8-7DAB200FFF4C.png I put the document in split view.

Complement

78DD5921-502D-4D33-ABA5-C517DFA0B87C.png Complementation is possible. Tap to select, or use the Tab key to select from candidates and press Enter to confirm.

Folder lock

41FCA36E-CC70-4EF8-8D81-534D1EDD94D8.jpeg The folder used for the first time cannot be referenced or changed in the initial state. In this case, you can refer to or change it by pressing the lock mark at the bottom right and selecting the current directory.

keyboard

06720EAA-27AA-4718-B3D8-B035BC0B16CA.png

The software keyboard is the default for iOS. Pyto does not provide extended keys other than the Tab key. Therefore, writing a program on the iPhone is a little difficult.

You can use an external keyboard (Bluetooth keyboard, etc.) without difficulty. You can also use shortcut keys such as comment out, reverse indent, and execute, which is reasonably comfortable. However, it was not possible to make a batch comment out with multiple lines selected.

bonus

Since OpenCV includes the dnn module, I tried running Mobilenet. The source code was borrowed from http://asukiaaa.blogspot.com/2018/03/opencvdnnpythonmobilenet.html and rewritten a little for Pyto. The camera is the outside camera of the iPad. There was no problem with the operation, and the frame rate was at a level that did not feel stiff. To be honest, I was surprised because I didn't expect deep learning to work on iOS apps. 7F93B6F4-F29B-4BCC-A1E9-071D864BE627.png

[Updated on April 22, 2020] How to use Mobilenet

MobileNet trained data is included at here

I used. If you have iOS13 or later, you can unzip it, so download it in zip format.

Next, extract the above files to any folder. I,

python_code (appropriate folder)
├── MobileNet-Caffe-master
│   ├── mobilenet_v2.caffemodel
│   ├── mobilenet_v2_deploy.prototxt
│   └── synset.txt
├── mobilenet.py (executable file)

It was made. Permission may be required in some locations.

The source code used is specified as a file with command line arguments. Arguments can be specified in pyto, but I wrote it directly in the program as follows.

import os
from cv2 import dnn

modelfolder = "./MobileNet-Caffe-master"  #Folder containing trained data
net = dnn.readNetFromCaffe(os.path.join(modelfolder, "mobilenet_v2_deploy.prototxt"), os.path.join(modelfolder, "mobilenet_v2.caffemodel"))
with open(os.path.join(modelfolder, "synset.txt"), "r") as f:
    classNames = f.readlines()
showPreview = True

After that, I think that it will work if you rewrite the following points.

--Use cv2.autorotate (rotates the image according to the orientation of the device) --Do not write cv2.destroyAllWindows and cv2.waitKey --Comment out all print functions

#How to write autorotate
import cv2

device = 0
cap = cv2.VideoCapture(device)
while cap.isOpened():
    ret, frame = cap.read()
    if not ret:
        continue
    frame = cv2.autorotate(frame, device)  #Add this part
    frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

Other

--Universal app (compatible with both iPhone and iPad) --A debugger (pdb) can be used --csv, loading images was fine locally --The graph overlap of matplotlib can be solved by plt.clf () (mentioned above). ――Can Pyto's GUI module be written in almost the same way as pythonista? -~~ syntax highlighting is a little strange ("bonus" lines 18 and 20 etc.) fixed in ~~ v11.5 --Wrapped lines are treated as new lines (cannot be automatically indented or can be commented out even though they are in the middle of the line. Bug?) --The closing parenthesis ")" is automatically added, but if you write it yourself, it will be duplicated (it will not be put out of the parenthesis like vscode) --Not as strong as pythonista in GUI (Parts cannot be placed with GUI creation tool)

If you focus on GUI application creation, Pythonista, if you focus on processing, Pyto. Some libraries are in Pythonista but not in Pyto, so it's a good idea to compare the built-in libraries in the documentation.

Pyto is likely to continue to develop, so I felt that it was a very promising app.

Link

Pyto official website: https://pyto.app/ Pyto documentation: https://pyto.readthedocs.io/en/latest/

Recommended Posts

[IOS] First Pyto
First Flask
First draft
First python-review-
First gdb