본문으로 바로가기
728x90
  • 파일명: CollectedData_[Experimenter].csv
  • 파일위치: ./labeled-data/[Video_Title]/CollectedData_[Experimenter].csv

1. CSV format

 참고: https://github.com/DeepLabCut/DeepLabCut/blob/master/examples/Reaching-Mackenzie-2018-08-30/labeled-data/reachingvideo1/CollectedData_Mackenzie.csv

scorer [experimenter] [experimenter] [experimenter] [experimenter] ...
bodyparts part1 part1 part2 part2 ...
coords x y x y ...
[image1_path]          
[image2_path]          
...          

2. CSV 파일 변환

import json
import csv
import glob
import os

experimenter = 'cklee'	# 실험자
key_points = ['nose', 'forehead', 'mouth-corner', 'mouth-center', 'neck']

def getImageFileName(_frame, _timestamp, _format = 'jpg'):
    return 'frame_{0}_timestamp_{1}.{2}'.format(_frame, _timestamp, _format)

def generateCsvFileDlcFormatted(dir_path):
    dir_name = os.path.basename(os.path.dirname(dir_path))
    json_file_name = dir_name + '.json'
    csv_file_name = 'CollectedData_' + experimenter + '.csv'
    csv_headers = ['scorer', 'bodyparts', 'coords']
    csv_keys = [experimenter, key_points, ['x', 'y']]    

    try: 
        # create csv file
        csv_file = open(os.path.join(dir_path, csv_file_name), 'w')        

        # write headers & keys
        len_key1 = len(csv_keys[1])
        len_key2 = len(csv_keys[2])
        for idx, header in enumerate(csv_headers):
            csv_file.write(header)    
            if (idx == 0) :     # write 1st row(scorer)
                for i in range(len_key1*len_key2):
                    csv_file.write(',' + experimenter)
            elif (idx == 1) :   # write 2nd row(bodyparts)
                for key in csv_keys[idx]:
                    for i in range(len_key2):
                        csv_file.write(',' + key)
            elif (idx == 2) :   # write 3rd row(coords)
                for i in range(len_key1):
                    for key in csv_keys[idx]:
                        csv_file.write(',' + key)
            csv_file.write('\n')
           
        # create json file        
        json_file = open(os.path.join(dir_path, '..', json_file_name), 'r')
        json_dic = json.load(json_file)
        
        # write values from json        
        for item in json_dic['annotations']:
            img_path = os.path.join('labeled-data', dir_name, getImageFileName(item['frame_number'], item['timestamp']))
            csv_file.write(img_path)
            for key, value in sorted(item['keypoints'].items(), key=lambda item: int(item[0])):
                if value is None:            
                    csv_file.write(',')
                    csv_file.write(',')
                else:
                    csv_file.write(',' + str(value['x']))
                    csv_file.write(',' + str(value['y']))
            csv_file.write('\n')

        print('[OK] ' + dir_name)        
    except:
        print('[ERR]' + dir_name)          
    finally:
        csv_file.close()
        json_file.close()

'''
main()
'''
folder_list = glob.glob(os.getcwd() + '/labeled-data/*/')

for folder in folder_list:          
    generateCsvFileDlcFormatted(folder)

3. 참고

Using labeled data in DeepLabCut that was annotated elsewhere

 

GitHub - DeepLabCut/DeepLabCut: Official implementation of DeepLabCut: Markerless pose estimation of user-defined features with

Official implementation of DeepLabCut: Markerless pose estimation of user-defined features with deep learning for all animals incl. humans - GitHub - DeepLabCut/DeepLabCut: Official implementation ...

github.com

 

728x90

'AI > PoseEstimation' 카테고리의 다른 글

[DeepLabCut] 테스트 ResNet50 1030000  (0) 2021.11.22
[DeepLabCut] Quick guide to Python  (0) 2021.08.03
[DeepLabCut] 처리 과정(process flow)  (0) 2021.08.03
[DeepLabCut] 설치 및 실행  (4) 2021.08.03
[DeepLabCut] 개요  (6) 2021.08.03