LoginSignup
0
0

More than 3 years have passed since last update.

DonkeyCar 教師データを可視化(旧バージョン2.5.8)

Last updated at Posted at 2019-02-18

目的

コースを手動で走らせて生成したjpeg画像にスロットルとステアリングのデータを表示する.
緑線の縦の長さがスロットル,左右の長さがステアリングの値.

準備

ホストPCにopencvが入ってなければインストールする.
(donkey)$ pip install opencv-python

教師データを加工

加工したいtubフォルダのあるフォルダで以下のプログラムを実行する.

TubVisualize.py
#!/usr/bin/env python3
"""
Usage:
    TubVisualize.py [--src <src>] [--dst <dst>]

Options:
    -h --help
    --src <src> sorce directory [default: ./tub].
    --dst <dst> distination directory [default: ./tub2].
"""

import os
from docopt import docopt
import json
import cv2

if __name__ == '__main__':
    args = docopt(__doc__)

    if args['--src']:
        src = args['--src']
    else:
        src = './tub'

    if args['--dst']:
        dst = args['--dst']
    else:
        dst = './tub2'

    print(src)
    print(dst)

    if not os.path.isdir(dst):
        os.mkdir(dst)

    lineColor = (0,255,0)
    lineThickness = 2
    textOrg = (0,120)
    textFontFace = cv2.FONT_HERSHEY_SIMPLEX
    textFontScale = 1
    textColor = (255,255,255)
    textThickness = 1

    cv2.namedWindow('DonkeyCam', cv2.WINDOW_NORMAL)

    num = 0
    while num < 100000:

        if os.path.exists(src+'/record_'+str(num)+'.json'):
            if os.path.exists(src+'/'+str(num)+'_cam-image_array_.jpg'):

                fp = open(src+'/record_'+str(num)+'.json','r')
                json_data = json.load(fp)
                fp.close()

                fp = open(dst+'/record_'+str(num)+'.json','w')
                json.dump(json_data, fp)
                fp.close()

                throttle = json_data["user/throttle"]
                angle = json_data["user/angle"]
                timestamp = json_data["timestamp"]

                img = cv2.imread(src+'/'+str(num)+'_cam-image_array_.jpg')
                cv2.line(img,(int(round(160/2+160/2*angle)),int(round(120-120*throttle))),(int(160/2),120),lineColor,lineThickness)
                cv2.putText(img, str(num),textOrg,textFontFace,textFontScale,textColor,textThickness)
                cv2.imwrite(dst+'/'+str(num)+'_cam-image_array_.jpg',img)

                cv2.imshow('DonkeyCam',img)
                cv2.waitKey(1)

        num += 1

    if os.path.exists(src+'/meta.json'):
        fp = open(src+'/meta.json','r')
        meta_json = json.load(fp)
        fp.close()

        fp = open(dst+'/meta.json','w')
        json.dump(meta_json, fp)
        fp.close()

    cv2.destroyAllWindows()

(donkey)$ python TubVisualize.py
tub2フォルダが出来るのでそれを makemovie する.

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