LoginSignup
0
0

More than 1 year has passed since last update.

#2 データ収集(ハンドジェスチャーでドローンを操作する)

Last updated at Posted at 2023-04-24

作成するシステムのためのデータ収集器を作成していきます。

必要な環境
・Pythonを実行する環境(本記事ではAnacondaで実行する)
・OpenCV

1.Anacondaで仮想環境の作成

仮想環境の作成

Anaconda Powershell Prompt
conda create -n 仮想環境の名前

仮想環境の起動

Anaconda Powershell Prompt
conda activate 仮想環境の名前

2.OpenCVなどのインストール(自分はjupyter labのインストールも行った)

インストールができたかを確認するためにAnaconda Powershell PromptでPythonの対話モードを起動し、import cv2とcv2.__versionと入力してもらうとインストールの有無とOpenCVのバージョンの確認ができます。Import cv2の時点でエラーを吐く場合はインストールができていないので再度インストールを試みてください。

OpenCVのインストール

Anaconda Powershell Prompt
conda install opencv

3.コードの作成

今回のデータ収集器はデータセットを作成する為のものです。そのため、データセットの作成がしやすいように作成していこうと思います。

ファイル名の頭文字2つを分類のクラス番号とし「-」を挟んでファイル番号にしていこうと思います。「-」を入れるのはデータセットの読み込みの時にあると便利な為です。
フォルダ構成は以下の予定です。

データセット構成.jpg

上記のフォルダ構成をもとに以下のコードを作成しました。
収集するクラス毎に起動して収集するようにしています。

import cv2
import os
import glob

cap = cv2.VideoCapture(0)

#分類のクラスとして使用する
input_class = int(input('Input only integer : '))

while True:
    ret,frame = cap.read()
    
    cv2.imshow("CAMERA",frame)
    
    input_key = cv2.waitKey(1) & 0xff
    
    # ESCで終了
    if input_key == 27: 
        break 
        
    #画像の保存
    if input_key == ord('s'):
        
        #input_classの名前のフォルダが存在するか確認
        class_path = './' + str(input_class)
        
        
        if os.path.isdir(class_path) == True: #存在した場合
            all_file = glob.glob(class_path + '/*.jpg')
            file_num  = len(all_file) + 1 #フォルダ内のファイル数の取得
            
        else:
            os.mkdir(class_path)
            file_num = 1
            
        
        print(file_num)
        save_path = './' + str(input_class) + '/' + str(input_class) + '-' + str(file_num).zfill(4) + '.jpg'
        size = (64,48)
        resize_frame = cv2.resize(frame,size)
        cv2.imwrite(save_path,resize_frame)
        
        

cv2.destroyWindow("CAMERA")  
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