0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Apriltagと工事写真

Posted at

Apriltagを使用した画像編集自動化スクリプト

この記事では、Pythonを使用してApriltagを検出し、画像を自動編集するスクリプトを解説します。主にPILpandaspupil_apriltagsライブラリを活用しており、建築現場での写真管理の効率化に役立つ内容です。あらかじめapriltagと対応する写真をリストにしておいて写真内にapriltagを検出したら対応する工事看板を挿入するだけです。

スクリプト概要

このスクリプトは以下の流れで処理を行います:

  1. 画像フォルダの選択
  2. 物件名の取得
  3. Apriltagの検出
  4. 対応する画像の貼り付け
  5. 編集済み画像の保存

必要なライブラリ

以下のライブラリを使用しています。事前にインストールしてください。

pip install pillow numpy pandas pupil_apriltags

コード解説

1. 画像フォルダの選択

import tkinter as tk
from tkinter import filedialog

root = tk.Tk()
root.withdraw()
initial_dir = r"Z:\(06)photo"
directory = filedialog.askdirectory(title="Select Image Folder", initialdir=initial_dir)
root.destroy()
print("選択されたフォルダ:", directory)

tkinterを使用してGUIでフォルダを選択します。

2. 物件名の取得

bukken_name = ""
current_dir = directory
while True:
    parent_dir = os.path.dirname(current_dir)
    if parent_dir == current_dir:
        break
    folder_name = os.path.basename(current_dir)
    if " " in folder_name:
        bukken_name = folder_name.split(" ")[0]
        break
    current_dir = parent_dir
bukken_name += "様邸"
print("物件名:", bukken_name)

フォルダ構造から物件名を抽出します。

3. Apriltagの検出

from pupil_apriltags import Detector

detector = Detector(families='tag36h11')

for image_file in image_files:
    img_pil = Image.open(os.path.join(directory, image_file))
    img_np = np.array(img_pil)
    gray = Image.fromarray(np.uint8(img_np)).convert('L')
    gray_np = np.array(gray)
    detections = detector.detect(gray_np)

pupil_apriltagsを使用してApriltagを検出します。

4. 対応する画像の貼り付け

for detection in detections:
    for n in range(len(id_list)):
        if str(id_list[n]) == str(detection.tag_id):
            sc_image_path = os.path.join(sc_directory, f"{sc_image_file_list[n]}.jpg")
            break

    if os.path.exists(sc_image_path):
        sc_img = Image.open(sc_image_path)
        # 画像編集処理(リサイズ、テキスト追加など)
        img_pil.paste(sc_img_resized, paste_position)

検出されたIDに対応する画像を貼り付けます。

5. 編集済み画像の保存

output_path = os.path.join(output_directory, f"edited_{image_file}")
img_pil.save(output_path)

編集済みの画像を保存します。

使用例

  1. スクリプトを実行します。
  2. 画像フォルダを選択します。
  3. 処理が完了すると、編集済み画像がkokubanフォルダに保存されます。

注意点

  • Apriltagが検出できなかった画像はfailed_images.txtに記録されます。
  • フォルダ構造や画像ファイル名の規則に依存している部分があるため、環境に合わせて調整が必要な場合があります。

この記事が役立った場合は「LGTM」をお願いします! 😊

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?