2019年12月20日金曜日

App Inventor version nb181 (December 19th, 2019)

MIT App Inventor is constantly upgrading, including minor improvements and bug fixes. I'd like to talk about two things that I've paid attention to in this release of "nb181". They are improved XML decoding and the addition of pressure sensor component.

XMLDecode Now Handles CDATA Sections
In XML, you may want to use markup symbols in regular strings. For example as in the following figure. However, XMLTextDecode in the XML Web component did not handle the "![CDATA [...]]" sections well. Therefore, these sections had to be removed as follows:


This issue was discussed by the community around 2015. As a countermeasure, Taifun [1] presented the above method. However, this version of App Inventor seems to have solved this problem.

Addition of Barometer
I developed several apps that use the barometric sensor built into the Android Phone and published them at the MIT App Inventor Summit 2014 [2]. At that time, App Inventor could not use the barometric pressure sensor, so I created a small Java program to get the barometric pressure value and called it with ActivityStarter.

An example of an Android application using a barometer [2]

Since then, I have used barometric pressure components [3] installed in AppyBuilder and Taifun's Extension [4]. However, with this version upgrade of App Inventor, you can use barometric pressure sensor as one of the standard sensors without using them. Great!

References

[1] Taifun's post on CDATA
https://groups.google.com/forum/#!topic/mitappinventortest/TZO5InFSIkU
[2] Fujio Yamamoto, “Rapid Prototyping Using App Inventor in Information and Computer Sciences Course”, Proc. of the MIT App Inventor Summit 2014, 3 pages, Cambridge USA, July 2014.
[3] PressureSensor by Hossein's Appy Builder
http://appybuilder.com (AppyBuilder is being integrated with Kodular)
[4] Taifun's Barometer Sensor Extension
https://puravidaapps.com/barometer.php

2019年12月11日水曜日

Finally arrived! Beautiful mountain photos that were automatically recognized!

In the previous article [1], I mentioned the recognition of photos from live cameras that shoot mountains. And I have developed an app that can automatically receive it by e-mail only when a photo with a clear view of the top is obtained. In order to recognize photos, I created a model in which many photos of the mountain were classified into four categories and trained. There, I used MIT's Personal Image Classifier and Google's Teachable Machine2 and incorporated the trained model into the App Inventor app.

However, in general, mountains are often covered with clouds. For example, at Rishiri Mountain in northern Hokkaido, you can see the middle but rarely see the top. So, I checked the weather forecast, and on a sunny day, I ran this app continuously from the early morning and waited for a notification email. During that time, of course, I was doing another job because it is fully automatic notification!


Finally, good luck came. It happened after 12:00 that day. As shown in Figure 1, the mountain photo was judged to be “clear enough to see the summit” and I received an automatic email to inform it. I was very happy. Sure, I got a great photo that looks good up to the top of the mountain.

References
[1] Using MIT's Personal Image Classifier with App Inventor
http://sparse-dense.blogspot.com/2019/12/using-mits-personal-image-classifier.html

2019年12月6日金曜日

Another example using JSON Decoding Extension

This article shows an example of using the JSON Decoding Extension I developed. Below are descriptions of this extension:

https://sparse-dense.blogspot.com/2019/01/a-json-decoding-extension-for-app.html
https://sparse-dense.blogspot.com/2019/01/a-json-decoding-extension-for-app_19.html

First of all, I would like to thank many people for their interest in this extension. In fact, these two articles have been accessed more than 1,500 times in total, but sometimes there are inquiries that they can't use it well. So here's another example that's simple but practical. It is a display of the weather forecast using OpenWeather ([1][2]). Here, the weather forecast of Sapporo City in Japan, up to five days in advance every three hours is displayed. An example of a JSON text as output from OpenWeather is shown in FIg.1. Some parts are folded in the figure, but if all are expanded, there are 1,341 lines.


In this JSON text, all the tag values shown in red frame are taken out and displayed on the smartphone as shown in FIg.2. In other words, the city name, date and time, temperature, and weather are displayed. In order to use OpenWeather in an application, it is necessary to obtain an API Key.


Here are the points of using the extension (FoYoJSON) to decode this JSON text. For example, to get the city name "Sapporo-shi", treat it like Fig.3. In addition, to retrieve all the weather forecast every three hours in order of time, write as Fig.4.



The complete source code (blocks) of this application is shown in Fig.5.



If you take a little more time, you can create a more practical application as shown in Fig. 6, where there are four cities registered and can be switched with the button.



Thanks
I found out that this Extension was introduced by CERCA [3]. Thank you for your correct understanding of my design intent and usage.

References

[1] to get OpenWeaterMap API guide:
https://openweathermap.org/guide
[2] to get City ID:
http://bulk.openweathermap.org/sample/city.list.json.gz
[3] CERCA NEL, Estrarre dati da JSON con una comoda estensione di appinventor, novembre 15, 2019
https://osmtreeathlon.blogspot.com/2019/11/estrarre-dati-da-json-con-una-comoda.html

2019年11月27日水曜日

利尻山の画像認識をTeachable Machine2とApp Inventorで行う

 前回の記事では、MIT App InventorからGoogleのTeachable Machine2を使って画像認識する例を書きました。今回、それを少し進めたアプリを作成しました。

 北海道の北部にある利尻山(島全体が利尻山になっている)の状況をリアルタイムに認識するスマホアプリを作成しました。まずは、どんなものかをご紹介します。色々な造り方はあるでしょうが、ここでは、App InventorとTeachable Machine2を使って、如何に簡単に、美しい(?)AIアプリを作るかに主眼があります。何かのご参考になれば幸いです。

 このアプリは、参考資料[1]に示した利尻町役場のライブカメラの画像を適宜引用して、その画像を学習させています。その学習はTeachable Machine2で行います。その学習済みモデルを使って、以下のアプリ(App Inventorによる)は動きます。

 Fig.1では、現時点の利尻山のライブカメラ画像を読み込み、その画像を認識させようとしています。


 画像の認識が終わった状態が、Fig.2です。この場合の画像認識は、Fig.4に示す4つのカテゴリ(ラベル)のどれに該当するかを言うことです。この例では、幸いにも"よく晴れて山頂までみえる”という認識結果でした。間違いありません!そして、このカテゴリに該当した場合に限り、予め登録してあるメールIDあてに、この画像を添付して自動メール送信します。右側の図は、メールが届いた場面です。滅多に、"よく晴れて山頂までみえる”場面に遭遇しないので、この自動メールは有り難いはずです!


 他の場面の認識結果も示しておきます。Fig.3は、"曇っているが中腹は見える"場合と、"夜になり真っ暗なのでおやすみ!”の認識例です。いずれも妥当です。


 学習には、Fig.4に示す4つのカテゴリを設けました。それぞれに十数枚の画像を集めて学習させました。なかでも、"Clear enough to se the top"(よく晴れていて山頂までみえる)場面になかなか出会うことができず、予想外に作業時間がかかってしまいました。


 今後も、このような、比較的簡単に作成できるが、ちょっと光る(?)アプリを心がけます。

参考資料
[1] ライブカメラが掲載されている利尻町役場のホームページ
http://www.town.rishiri.hokkaido.jp/rishiri/

2019年11月20日水曜日

MIT App InventorとGoogle Teachable Machine2を連携させる

【要旨】GoogleのAI環境Teachable Machine2を使うと、自分用に、音/画像/ポーズの学習と認識を非常に簡単に行えますが、それだけでは、初心者レベルでに留まってしまいます。一歩進めて、自分の作りたいアプリにこれを組み入れることができれば、世界はグンと広げられます。その糸口となる試みを、MIT AppInventorとの連携という形で行いました。
-----

 先の記事に書きましたように、Teachable Machine2によれば、PCやスマホのブラウザを使って容易に自分用に(ここが重要!)、音/画像/ポーズの学習と認識を行うことができます。子供にも大人にも、素晴らしいとAI環境と言えます。

 しかしながら、自分が解きたい問題、作成したいアプリにその機能を取り込めなければ、それ以上の発展がありません。私の場合、スマホのアプリをMIT App Inventorを使って作るのですが、ここに、このTeachable Machine2を組み込めないかと思い、情報を探しましたが見当たりません。それならば、と自分で以下のようにやってみました。

問題の設定(ライブカメラ画像のリアルタイム認識)
 今回は、富士山などの山岳を望むライブカメラの画像を取得して、AI(人工知能)に、「多分、富士吉田からみた富士山でしょう」とか、「多分、夕暮れの富士山(須走)でしょう」等と、リアルタイムに認識させるアプリを作ります。

学習用画像の収集と学習の実行
 ここでは、以下の四種(4ラベル)の画像を学習/認識させます。今後種類を増やして行きますが。
  • 富士山(富士吉田)
  • 利尻岳(沓形)
  • 昼間の富士山(須走)
  • 夕暮れの富士山(須走)
 収集した画像は、図1の通りです。各ラベル毎に、十数枚の画像しかなく少なすぎるのですが、とりあえずこれで進めます。これを学習させることは、先の記事のとおり容易にできますので、手順は省略します。学習済みモデルは、(デフォルトでは)Googleのサーバに置くか、または自分のPCに保存して利用することもできます。


学習済みモデルをMIT App Inventorから使う準備
 スマホ用のApp InventorにあるWebViewerというブロックは、実は強力なJavaScriptエンジンなのです。上記の学習済みモデルは、JavaScriptの環境があればどこでも動くTensorflow.jsで作られています。そのため、Googleのサーバにおいた学習済みモデルは、App Inventor側で実行できます。

 しかしながら、スマホにある画像をこの学習済みモデルに与える場合、有名なクロスドメイン問題が起こるため、うまく行きません。その解決法の一つは、学習済みのモデルを、スマホのWebサーバに置くことなのです。画像も当然、このサーバにコピーしておきます。今回はこれに従いました。図2にその様子を示します。


MIT App Inventorから学習済みモデルを動かして認識結果を得る
App Inventorから学習済みモデルを動かして認識を実行させます。図3(a)は、ライブカメラから取得した画像を、上で述べたローカルWebサーバに配置しています。図3(b)は、ローカルサーバで認識をさせ、その結果をApp Inventor側で取得しています。ここで、WebViewerのWebViewStringが受け渡しの役を担っています。


ライブカメラ画像のリアルタイム認識の実行例
 以上のとおりに作ったスマホアプリでのリアルタイム認識をやってみました。その実行例を図4に示します。学習用の画像も少なく、断定的なことは言えませんが、期待どおりの良好な結果となっています。



 以上、ご参考にしていただける点があれば幸いです。

【参考情報】MIT App Inventorのフォーラム
以下でも、App InventorとTeachable Machine2に関するディスカッションがあります。私も、ひと言述べています。
Artificial Intelligence with App Inventor
https://groups.google.com/forum/#!topic/mitappinventortest/f-Hwk586hyQ


2019年11月12日火曜日

スマホを音/画像/ポーズの識別器にする

 先日(2019年11月初旬)に発表されたGoogleのTeachable Machine 2.0 [1]はちょっと衝撃的でした。バージョン1.0は2017年に発表されていたのですが、今回のバージョン2.0は著しく進歩しています。”Teachable Machine 2.0 makes AI easier for everyone”のキャッチフレーズの通りだと感じます。

 このTeachable Machineの特徴は、ブラウザのもとで動くTensorflow.jsを使っており、学習も認識もローカルマシンできてしまうことです。学習は今のところPCでしかできないようですが、学習済みのモデルは、スマホへ組み込めるので、スマホでリアルタイムに、音/画像/ポーズの識別ができてしまいます!ここで特筆すべきは、コーディング不要であり、例えば以下のようなスマホ用の音識別器は、全体で15分くらいでできてしまうことです!

 ここでは、図1に示した5つの音源を扱うことにします。(画像やポーズ認識もこの後やってみます。)


 まず、ブラウザで、周辺雑音と5種類の音をスマホへ入力して(各々20秒程度)、図2の中央にあるTraining(学習)ボタンを押します。数十秒で学習が完了します。恐らく、転移学習モデルが背後にあるため、こんなに速く終わるのだと思います。次に、スマホで学習済モデルを使うために、これをexportします。


 スマホ側では、この学習済みモデルのURLをブラウザから開くだけです。すなわち、これだけで、スマホが、音の識別器になってしまいました。図3と図4には、スマホに新たにテスト用入力音を与えた場合のリアルタイム識別結果を示します。5種類とも、かなり高い確度で認識に成功しています。




 この学習モデルは、JavaScriptとJsonで示されているので、スマホ側で、認識結果を使った何らかのアプリケーションを作ることもできるはずです。その場合は、JavaScriptプログラミングの基礎知識などは必要になりますが。

参考資料
[1] Teachable Machine 2.0 makes AI easier for everyone
https://blog.google/technology/ai/teachable-machine/



2019年11月4日月曜日

Enjoy live cameras from across the world on your smartphone

This app has won the "2019 December's MIT APP INVENTOR OF THE MONTH" award. 
http://appinventor.mit.edu/explore/app-month-winners-2019

-----
The source code (blocks) is published below:
ai2.appinventor.mit.edu/?galleryId=6239670284976128

Revision: Added a function to automatically acquire images periodically (see Fig. 4).
-----

Many live cameras are installed in cities, mountains, rivers, etc. around the world. They are useful for knowing the current situation in a nostalgic place or where an acquaintance lives. Of course, you can simply enjoy the beautiful scenery. It takes a little time to find a suitable one among those live cameras. There are not so many live cameras that are of interest to each person, at most about ten. If that's the case, don't you think it's convenient to register them in advance on your smartphone so that you can see them right away! At that time, it would be nice to be able to display the previous camera image together with the current camera image. It's often useful to know that something has changed in a few hours.

First, find out several suitable live cameras. They can be video images or still images. Also, the update interval varies from several minutes to one hour. Here, select still images with an update interval of several minutes. MIT App Inventor is very suitable for the smartphone app to be created this time.

Prepare a text file with the location name and image URL as input data. From such a text file, create a simple database with TinyDB, and through that, let the Web component get images. The acquired image is displayed with the Image component together with the current time. The saved previous image is also displayed at the same time. In addition, we added a function to automatically delete images that were not saved so as not to occupy a large amount of storage.

This is a simple app, but you can use it to see the image of the desired live camera as soon as you think of it. You can save your favorite images by pressing the button. It's also a good idea to email screenshots of current and past images to someone else.

Fig.1 shows the case where Mt. Fuji (FujiYoshida) is selected from 10 registered live cameras ([1]-[10]). It is interesting to compare this morning's image with yesterday's image.


Another examples are shown in Fig.2. Images from two live cameras (Liverpool Airport and Liverpool Waterfront) installed in Liverpool. Time is shown in Japan time. It ’s also fun to compare day and night images.



Some live cameras have a telephoto function and a variable direction shooting function. These features are switched automatically and cannot be controlled by the user, but if you are lucky you can get even better images. For example, Fig. 3 shows Mt. Fuji taken with the telephoto function and the night view of Sapporo taken from different directions.



Now we have added a function to periodically acquire images from the current Live camera. As shown in Fig.4, you can specify the shooting interval and press the button to automatically update the screen. And automatic shooting stops by Long-Click on the button.


please note
If Error_908 is displayed when running this app, please allow access to External_Storage in your Android settings. Please allow it again (twice) if necessary.

References
(The live camera URL is not shown below because some live cameras do not allow direct links to images, except for personal use.)
[1] Neuburg an der Donau
[2] Mt. Fuji Subashiri
[3] Sapporo Oh-dori park
[4] Liverpool AirPort
[5] Tokyo Haneda AirPort
[6] Kushiro Japan
[7] London Big Ben
[8] Mt. Fuji Fujiyoshida
[9] Liverpool Waterfront
[10] Chikuma river Naganuma

2019年11月1日金曜日

河川水位情報を使い易くするスマホアプリの作成(2)

[本アプリの改訂版(機能見直し版)のソースプログラムはここに公開しています。]

 前回の記事は、河川水位情報をスマホから見やすくするアプリの作成でした。単なるブックマーク用アプリ、と言えなくもない単純なものですが、それでも使っているとすぐに不都合点が見つかります。ソフトウェアはいつもそうしたものですね。

前回アプリの不都合点:
  1. 神奈川県内の河川では、あるコード体系でライブカメラ画像にファイル名が付与されているので、プログラムでは、コードから画像ファイル名を復元できた。しかし、他の都道府県や地域の河川はそれぞれが異なる命名規則を持つ。
  2. 神奈川県の場合、標準的に3枚の画像(河川写真2枚と水位グラフ)を表示できたが、他県では、それらが揃っているとは限らないうえ、別のタイプの画像もある。
  3. プログラムの造りに少し不満があった。すなわち、複数の画像読み込み要求に応じて、それぞれに個別の受信イベント処理ブロックを作っていた。
今回の改良点:
  1. 元の長いファイル名(URL)をそのまま使い、TinyDBを使って(tag, value)の形式で情報を保持する。すなわち、tagを河川名(観測地点名)とし、valueは画像URLを要素とするリストとした。そして、tagのリストをListpickerに設定した。
  2. これにより、画像の枚数は河川あたり1枚〜3枚まで可変でも不都合がない。
  3. 複数の画像読み込み要求(Web.get)に対する、画像受信イベントを、ひとつのGeneric Blockで処理するように変更した。拡張性と柔軟性を向上させるため。これに関して、前回との比較を図1に示した。

 この改良版で、画像の種類やファイル名の命名規則が異なる全国各地のライブカメラを自由に登録できます。例えば、千曲川沿いの観測点や、北海道釧路川の観測点なども登録してみました。その使用例を図2に示します。


 海外の河川にも、ライブカメラはあります。かなり昔行ったことのあるドナウ川(Neuburg an der Donau)にしてみました。画像取得までに若干時間がかかりますが、少し遅れたリアルタイム画像(毎分更新される)を表示できます。でも、こうして海外河川も入れてしまうと、防災よりも観光となってしまいそうですが、ご容赦下さい。

  

 最後に、傑作画像?を図4に掲載します。ちょっと珍しい場面に遭遇!脚が8本ありますので(一般に昆虫の脚は6本なので)蜘蛛に違いありません。本記事の主題から少し外れていまいますが、図5をご覧下さい。




2019年10月30日水曜日

河川水位情報を使い易くするスマホアプリの作成(1)

 最近の台風や低気圧による河川の氾濫などで被害を受けられた方々にお見舞い申し上げます。今後の水害をできるだけ抑えるために、(これまでほとんど気にとめていなかった)河川水位情報等も活用できるとよい思います。

 神奈川県内にも多数の河川があり、リアルタイム河川カメラ画像や水位グラフなどが提供されています。例えば、主にPC向けに図1のようなWebページがあります。多彩な情報が掲載されていて、有効に活用できそうです。ただ、思いついた時に直ぐにスマホでこれを利用しようとすると、以下に述べるような使いにくい点もあります。


 スマホで見るうえで問題となるのは、以下の点です。
  • スマホ用に設計されていないので、文字などが小さすぎる。
  • 多数の河川が掲載されているので、関心のある河川へ行くまでに数ステップかかる。
  • 各自に関心がある河川は、自宅や親戚の家の近くなど、数個程度と思われるが、それを登録して使えない。(Webページのブックマークはできるが...)
  • 水位グラフを見るには、河川カメラとは別系統なので、さらに数ステップかかる。
そこで、スマホ用に、自分用にカスタマイズして使い易くするアプリを作成することにしました。結論から言いますと、MIT App Inventorを使って、図2のようなアプリを作成しました。ここでは河川観測点を6ヶ所登録してあります。図2左側図は、[河川名-観測点-リアルタイムカメラ画像情報-水位グラフ情報] がリストになっていて、どれかをクリックすると、直ちに右側へ切り替わり、その河川の写真と水位グラフがセットで表示されます。また、上部の河川名をクリックすると、再度左側へ戻り、別の河川を選択できます。


 この例の「境川:境川橋付近」の写真は穏やかですが、水位グラフは直近数時間わずかに上昇傾向にあることが分かります。このアプリのソースコード(ブロック)全体は図3のとおりです。かなりコンパクトに作成できていると思います。


 詳しいことはここには書けませんが、ポイントをご参考までに以下に示します。図4(a)は、水位観測点の登録です。図2左図に示した観測点情報をテキストファイルに入れてあり、それを読み込んで、ListPickerの要素にしています。画像やグラフのURLは少し複雑な名称(文字列)ですが、共通部も多いので、このテキストファイルには、各画像に固有のコード情報のみを与えています。


 図4(b)は、ListPickerのリストから、河川観測点を選択した場合に、その写真をgetして表示します。図3では、写真を2枚と水位グラフの合計3枚を表示しますが、以下の図では、簡単のため、最初の1枚の写真だけを表示します。なお、画像のURLには、常に最新の画像が割り当てられるようになっています。


 このスマホアプリは、画像のURLから画像を表示しているだけなのですが、最も使いやすいユーザーインタフェースにするにはどうするかを考えました。水位情報をデータベース(TinyDB, TinyWebDB, SQLiteなど)にすることなども考えましたが、結局、自分の関心のある数個の河川情報をテキストファイルにして、その内容をListPickerで選択するこの方式に落ち着きました。

 このようなある程度実用的と思われるスマホアプリを、短時間で作成できるMIT App Inventorの有用性を改めて感じる次第です。最後に、登録してある他の河川の状況も示して、この記事を終わります。