[PYTHON] Try using ArUco on Raspberry Pi

Try using ArUco on Raspberry Pi

It's a little old technology, but you can see it in the self-position estimation of the robot system. I want to try AR markers I tried to recognize ArUco with the obtained Raspberry Pi.

What i did

It looks like this to see the execution result. (https://www.youtube.com/watch?v=aepFM_JsxbU) The outline of the marker and the ID and xyz axes are displayed. Because you can also get pitch roll yaw It can be used for various purposes. 2020-05-03-135817_1920x1080_scrot.png

environment ・ RasPi4 (It should work with RasPi3) ・ USB camera (Logitech) → Raspi cam is also available.

RasPi setup

First, set up according to the following article by "Karaage". OpenCV is required for recognition. It was very smooth. I am always grateful. -[How to build an image recognition environment with deep learning of Raspberry Pi 4 from zero to 1 hour] (https://karaage.hatenadiary.jp/entry/rpi4-dl-setup)

Install additional packages As mentioned in the article, I put everything in once. ・ [Setup for reinforcement learning of Raspberry Pi 4] (https://note.com/npaka/n/n034c8ee6e5cc)

Now you are ready.

Procedure for recognizing AR markers

Marker generation → See the link below for generation. ・ (Camera calibration) → If you want to move it for the time being, put it off. ・ Marker recognition ・ Posture estimation of AR marker with python

Implementation

・ You should be able to use it by copying.

ARdetect.py



#!/usr/bin/env python
# -*- coding: utf-8 -*

import numpy as np
import cv2
from cv2 import aruco

def main():
    cap = cv2.VideoCapture(1) #Change the value depending on the camera used
    #Marker size
    marker_length = 0.056 # [m]
    #Marker dictionary selection
    dictionary = aruco.getPredefinedDictionary(aruco.DICT_4X4_50)

    #camera_matrix = np.load("mtx.npy")
    #distortion_coeff = np.load("dist.npy")
    #If you have calibrated the camera, use the above.
    camera_matrix = np.array( [[1.42068235e+03,0.00000000e+00,9.49208512e+02],
    [0.00000000e+00,1.37416685e+03,5.39622051e+02],
    [0.00000000e+00,0.00000000e+00,1.00000000e+00]] )
    distortion_coeff = np.array( [1.69926613e-01,-7.40003491e-01,-7.45655262e-03,-1.79442353e-03, 2.46650225e+00] )

    while True:
        ret, img = cap.read()
        corners, ids, rejectedImgPoints = aruco.detectMarkers(img, dictionary)
        aruco.drawDetectedMarkers(img, corners, ids, (0,255,255))

        if len(corners) > 0:
            #Process by marker
            for i, corner in enumerate(corners):

                rvec, tvec, _ = aruco.estimatePoseSingleMarkers(corner, marker_length, camera_matrix, distortion_coeff)

                #Remove unnecessary axes
                tvec = np.squeeze(tvec)
                rvec = np.squeeze(rvec)
                #Convert from rotation vector to rodorigues
                rvec_matrix = cv2.Rodrigues(rvec)
                rvec_matrix = rvec_matrix[0] #Extracted from rodorigues
                #Transpose of translation vector
                transpose_tvec = tvec[np.newaxis, :].T
                #Synthetic
                proj_matrix = np.hstack((rvec_matrix, transpose_tvec))
                #Conversion to Euler angles
                euler_angle = cv2.decomposeProjectionMatrix(proj_matrix)[6] # [deg]

                print("ID : " + str(ids[i]))
 
                #Visualization
                draw_pole_length = marker_length/2 #Real length[m]
                aruco.drawAxis(img, camera_matrix, distortion_coeff, rvec, tvec, draw_pole_length)
        
        cv2.imshow('drawDetectedMarkers', img)
        if cv2.waitKey(10) & 0xFF == ord('q'):
            break
    cv2.destroyAllWindows()

if __name__ == '__main__':
    main()




Marker (for testing)

Please use it when it is troublesome to generate.

1.png

Recommended Posts

Try using ArUco on Raspberry Pi
Try using a QR code on a Raspberry Pi
Try using the temperature sensor (LM75B) on the Raspberry Pi.
Detect "brightness" using python on Raspberry Pi 3!
Run servomotor on Raspberry Pi 3 using python
Detect temperature using python on Raspberry Pi 3!
pigpio on Raspberry pi
Cython on Raspberry Pi
Detect magnet switches using python on Raspberry Pi 3!
Sound the buzzer using python on Raspberry Pi 3!
Indoor monitoring using Raspberry Pi
Try using OpenCV on Windows
Introduced pyenv on Raspberry Pi
Use NeoPixel on Raspberry Pi
Install OpenCV4 on Raspberry Pi 3
Install TensorFlow 1.15.0 on Raspberry Pi
Build an OpenCV4 environment on Raspberry Pi using Poetry
Try debugging Python on Raspberry Pi with Visual Studio.
Output to "7-segment LED" using python on Raspberry Pi 3!
Testing uart communication on Raspberry Pi
Try using Pillow on iPython (Part 1)
USB over ethernet using Raspberry pi
raspberry pi 4 centos7 install on docker
Cross-compiling for Raspberry Pi Zero on Debian-Try using shared libraries
Install ghoto2 on Raspberry Pi (memo)
Try using Pillow on iPython (Part 2)
Detect "temperature (using A / D converter)" using python on Raspberry Pi 3!
Access google spreadsheet using python on raspberry pi (for myself)
Install PyCall on Raspberry PI and try using GPIO's library for Python from Ruby
Try L Chika with raspberry pi
OpenCV installation procedure on Raspberry Pi
Try using Pillow on iPython (Part 3)
Try moving 3 servos with Raspberry Pi
Power on / off Raspberry pi on Arduino
Detect switch status on Raspberry Pi 3
Install OpenMedia Vault 5 on Raspberry Pi 4
Using a webcam with Raspberry Pi
L Chika on Raspberry Pi C #
Build wxPython on Ubuntu 20.04 on raspberry pi 4
Control the motor with a motor driver using python on Raspberry Pi 3!
Weighing instrument using raspberry pi and hx711 (GUI display on Tkinter)
"Honwaka Notification Lamp" on Raspberry Pi Part 2
Try using Bash on Windows 10 2 (TensorFlow installation)
USB boot on Raspberry Pi 4 Model B
"Honwaka Notification Lamp" on Raspberry Pi Part 1
Enable UART + serial communication on Raspberry Pi
Make Raspberry Pi speak Japanese using OpenJtalk
Adafruit Python BluefruitLE works on Raspberry Pi.
Accelerate Deep Learning on Raspberry Pi 4 CPU
Set swap space on Ubuntu on Raspberry Pi
Programming normally with Node-RED programming on Raspberry Pi 3
Use the Grove sensor on the Raspberry Pi
Install 64-bit OS (bate) on Raspberry Pi
Install docker-compose on 64-bit Raspberry Pi OS
Try Object detection with Raspberry Pi 4 + Coral
"Honwaka Notification Lamp" on Raspberry Pi Part 3
Working with sensors on Mathematica on Raspberry Pi
Build OpenCV-Python environment on Raspberry Pi B +
Mount Windows shared folder on Raspberry Pi
Matrix multiplication on Raspberry Pi GPU (Part 2)
How to install NumPy on Raspberry Pi