Tried AprilTag samples useful for industrial applications.

産業用Raspberry Pi「PL-R4」にカメラを繋ぎ、PiLink株式会社で用意したサンプルプログラムを使い、印刷したAprilTagを検出させてみました。
AprilTagの仕組みは、カメラによる位置情報や姿勢を高精度に検出する単純なものです。一見シンプルな仕組みに見えますが、実際には奥深い設計思想と多彩な用途があります。そのため、すべてをご紹介することはできません。

「AprilTagってなに?」、「自社の現場で取り入れられないかな?」とお考えの方に向けて、試した限りではありますが、基本部分に沿った内容でご紹介します。

What is AprilTag?

AprilTagは、2011年頃に米国ミシガン大学の研究チーム(主にEdwin Olson氏の研究グループ)によって開発されました。
名前の「April」は、「4月」の意味ですが、”4月にプロジェクトを始めたから”といった軽い理由で名付けられたとされています。
つまり、名称自体には深い意味はありません。

AprilTagは、一見するとQRコードに似ています。二次元バーコードの種類という意味でも似ています。
どうやらQRコードよりも小さいデータ(4ビットから12ビット)を扱うために設計され、検出の精度が高いことや、計算が速くリアルタイム制であること、遠くからの検出も可能な点が特徴的です。
暗かったり、低コントラストでも読み取ることが可能です。

サンプルプログラムを使うと分かるのですが、AprilTagをカメラで捉えると、カメラに対する正確な3D位置をリアルタイムで計算し表示できます。
かなりの精度だと思いました。
3Dボックスを表示させるわけですけど、Raspberry Piでもかなりスムーズです。
3Dボックスを表示させるわけですけど、Raspberry Piでもかなりスムーズです。

The lower left image is quite close to the camera, but it can be recognized if all the tags are within the camera.
In the lower right image, we made the detection very dark, but you can see that it is properly recognized.

QR codes are rather specialized in the amount of information, while AprilTag is specialized only in location and posture information.
This is why it has been used in robotics.

その意味でも、ミドルウェアのROSを介して利用されるのが一般的です。
ただ、AprilTagのライブラリはC言語で書かれているため、簡単な画面・操作インターフェイスであれば、C++やPythonのスタンドアロンで手軽に利用できます。
今回のようにテスト目的であれば、Pythonプログラムを使ったRaspberry Pi でも手軽に扱うことができます。
産業用Raspberry Piであれば各現場に導入できますね。

A type of TagFamily

There are different types of AprilTag, called TagFamily. Each has a different specification.
The reason is that they are used differently depending on the conditions under which they are used.

Examples of different elements in each TagFamily

  • Bits (data capacity)
  • error-correcting capability
  • Tag size (number of cells)
  • Number of tags (number of tags in the same family)

You will need to choose based on usage conditions, such as whether speed of tag detection is important, whether accuracy is a priority, or whether the tags to be printed need to be small.

今回使用したのはtag36h11で、一般的な種類です。
処理速度も中程度なので、先ずはこのtag36h11がテストにはうってつけです。

TagFamily(タグファミリー)の種類と特徴

Family NamefeatureNumber of tag IDsGuideline for use
tag36h11Generally the most commonly used. High error correction capability and easy to use.587Wide range of experiments, robot control, AR, SLAM, etc.
TAG25H9Compact and suitable for high-speed processing. Error correction is rather low.35When you want to use small markers
TAG16H5Very small. Almost no error correction.30Recommended only when processing speed is a priority and distance is close.
tagCircle21h7Circular design for easy visual identification121For special applications (not usually recommended)
tagStandard41h12Very robust. High error correction capability, but also large in size.211Industrial applications requiring high reliability, etc.

Four tags recommended for broadly different purposes

useRecommended Family
General development and testingTAG36H11 (General)
Small devices and small printTAG25H9 (size-oriented)
Educational use and simple demonstrationTAG16H5 (speed-oriented)
Factory and long distance readingstagStandard41h12 (for robustness)

This does not apply to some applications, as the distance between the camera and the object is also relevant. The following classification is easy to understand.

カメラのキャリブレーション

カメラを繋いだ後、早速試してみたいところですが、最初にカメラのキャリブレーションをします。これもAprilTagの特徴の1つで、この機能により正確な3D位置を計算できます。
今回のPiLink株式会社で用意したプログラムでは、以下のコマンドで実行させました。

python /home/pi/Projects/live_calibration.pu --calibration_name calibration

キャリブレーション用にチェッカーフラッグをカメラで捉えると、線と点が表示され、ステータス文字の色も赤から緑になるので分かりやすいです。

この状態でEキーでキャプチャーできます。
0/20と表示されているように20回のキャプチャーが必要です。角度や上下左右の向きを変えながら20回Eキーを押しましょう。
20回が終了すると自動的にプログラムは終了します。
calibration.npzファイルが作成されていれば完了です。

*Calibration is completed when actually shipped.

AprilTagの検出テスト手順

使用するタグ(tag36h11)のPDFファイルを以下からダウンロードして印刷します。段ボール紙などに貼り付けると安定して検出できます。
https://github.com/shengwen-tw/apriltag/blob/master/tag36h11.pdf

Next, start the program prepared with the command
python3 /home/pi/pi_apriltag/estimation_final.py --source 0 --tag-size 16.0 --calibration_file /home/pi/pi_apriltag/calibration.npz --draw --port 12345

The two things to change are tag-size and port.
tag-size is the length of one side of the actual printed tag. Here we set it to 16 cm. If you do not specify the actual tag size, the distance and orientation may not match reality.
port is the TCP port number. If you are using the robot for communication with the client, specify the port number to be used for that communication. Here it is number 12345.

After executing the command, another window will open and when you hold the printed AprilTag in front of the camera, a 3D box will appear with a yellow line.

You can see the changing values in real time in the screen.
Try tilting it or moving it left or right.

This 3D box is displayed to facilitate intuitive understanding of the tag’s position and orientation.
This is a supplementary indication for visual clarity and is not directly related to the object to which the tag is attached.
It is easier to see how they are leaning than the numbers, for example.

The meanings of the numbers displayed on the screen are as follows

X = position in left/right direction
Y = position in up/down direction
Z = position in front/back direction (= distance from camera)
Roll = rotation around X axis (tilt left/right)
Pitch = rotation around Y axis (tilt up/down)
Yaw = rotation around Z axis (horizontal rotation around front/back axis)

カメラ視点

       y (上下)
        ↑
        |
        |     
        +------→ x (左右)
       /
      /
     z (前後) ← カメラの「向いている方向」

As the printed tag is moved in various ways, the 3D box display appears without any delay, and the values change as actual measurements.
A line is displayed from the center point, capturing the center of the tag as if pulling a string. It is simply interesting.

AprilTagの識別機能と設計思想

AprilTag is completely different from QR Code in that it only has a unique ID for each tag; QR Code would rather have data. (URL, name, phone number, etc.)
In other words, it is simply a marker that can only have one ID (number). It is also a visual marker that displays a 3D box.

It can be said that an ID can instantly identify which of several IDs it is, or it can identify all of several IDs.
The data associated with the ID is held externally, so the detection (reading) speed is fast.

外部にもたせるデータを既存システムと捉えれば、後付けする仕組みとして検討できるのではないでしょうか。

データ構成と誤り訂正(ハミング距離)の仕組み

少し複雑ではありますが、知っておくと理解が深まるbit(ビット)のお話を解説しておきます。
ビットは2進数1桁も意味します。

先程のtag36h11を例にすると、36という数字はデータビットが36bit(2進数の36桁分)を表しています。
h11は ハミング距離11bit を表しています。

ハミング距離とは、2つのビット列の「異なるビットの数」 を意味し、h11だと11個のbit(2進数の11桁分)が異なるIDを使っていることになります。

少ない桁数の方が分かりやすいですね。6bitで考えてみます。

例:2つのビット列6個(6bit)で表したハミング距離

101010 ↔ 101110 → ハミング距離 = 1
101010 ↔ 010101 → ハミング距離 = 6

ハミング距離が1だとすると、1ビット(1列)しか異ならないIDのため、万が一ノイズなどでタグが壊れて読まれてしまうと、同じIDと誤認識してしまう恐れが出てきます。確かに、101010101110は似てますよね。

仮に1bitだけ間違えても、似たようなビット列がなければ誤認識しない仕組みです。
ハミング距離が6bitだと、全桁が異なるため、仮にタグがすべて読み取れなくても推測できます。
ハミング距離が11bitもあれば、確かに間違えなさそうですよね。

一般的なグループに入るtag36h11は、データビット数が多いため持てるIDの数も587個と多く、ハミング距離が11bitもあるので誤認識しにくいタグです。テストや簡易的な物に利用するのは理解できます。

データビット数が小さければ、検出の速度が上がると同時に、タグ自体も小さな印刷で済みます。その分だけ誤り訂正が少ないため、あまり速く移動する対象物や、カメラより遠い対象物だと検出できないかもしれません。

タグはどれが優れているというより、用途によって選ぶことになります。

産業利用の活用ポイントと可能性

By recognizing the physically installed AprilTag with a camera, accurate 3D position (X, Y, Z) and orientation (rotation) information is known.
For example, it could be used to align an arm-type work robot to grasp a part, or to identify the location of an inspection target based on ID information.

The black-and-white pattern of the tags has the advantage that they are not easily affected by changes in brightness or the background of the object.
Even if the AprilTag is shadowed or slightly smudged, it has characteristics that make it easy to recognize.

It can also recognize multiple tags at once, enabling management and tracking of multiple objects.

Objects such as those flowing on a conveyor belt are particularly suited for this feature, since they can be detected in real time at high speed as long as they are within the range of the camera. It can also be used to detect abnormalities in objects.

The advantage is that it is free and lightweight, making it easy to incorporate into a wide range of libraries.
Furthermore, the ability to select the size and type of tag according to the application also expands the scope of use.

  1. Real-time acquisition of highly accurate position and orientation information
  2. Recognition performance not easily affected by the environment
  3. Simultaneous recognition of multiple tags is possible
  4. Suitable for tracking and identifying moving targets
  5. Lightweight, open source implementation available (BSD)
  6. Flexibility in tag families.

Specifically, it may be used for picking with robotic machines, process work in assembly, and quality inspection of products and components.
The wide frontage that can accommodate both fixed and dynamic objects is attractive.

I felt that the greatest features of the AprilTag were its simplicity and high-speed detection, with only a marker (ID) and no information in the tag itself.
Incorporating it into existing systems would be useful in a variety of industrial settings.

Reference:
AprilTag ( https://april.eecs.umich.edu/software/apriltag )


記事寄稿:ラズパイダ

非エンジニアでも楽しく扱えるRaspberry Pi 情報サイト raspida.com を運営。ラズベリーパイに長年触れた経験をもとに、ラズベリーパイを知る人にも、これから始めたいと興味を持つ人にも参考になる情報・トピックを数多く発信。PiLinkのサイトへは産業用ラズベリーパイについて技術ブログ記事を寄稿。