#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Jul 4 12:32:53 2017
@author: wroscoe
"""
import os
import sys
import time
import json
import datetime
import random
import tarfile
import numpy as np
import pandas as pd
from PIL import Image
from donkeycar import util
from ..log import get_logger
logger = get_logger(__name__)
class Tub(object):
"""
A datastore to store sensor data in a key, value format.
Accepts str, int, float, image_array, image, and array data types.
For example:
#Create a tub to store speed values.
>>> path = '~/mydonkey/test_tub'
>>> inputs = ['user/speed', 'cam/image']
>>> types = ['float', 'image']
>>> t=Tub(path=path, inputs=inputs, types=types)
"""
def __init__(self, path, inputs=None, types=None):
self.path = os.path.expanduser(path)
logger.info('path_in_tub: {}'.format(self.path))
self.meta_path = os.path.join(self.path, 'meta.json')
self.df = None
exists = os.path.exists(self.path)
if exists:
# load log and meta
logger.info('Tub exists: {}'.format(self.path))
with open(self.meta_path, 'r') as f:
self.meta = json.load(f)
self.current_ix = self.get_last_ix() + 1
elif not exists and inputs:
logger.info('Tub does NOT exist. Creating new tub...')
# create log and save meta
os.makedirs(self.path)
self.meta = {'inputs': inputs, 'types': types}
with open(self.meta_path, 'w') as f:
json.dump(self.meta, f)
self.current_ix = 0
logger.info('New tub created at: {}'.format(self.path))
else:
msg = "The tub path you provided doesn't exist and you didnt pass any meta info (inputs & types)" + \
"to create a new tub. Please check your tub path or provide meta info to create a new tub."
raise AttributeError(msg)
self.start_time = time.time()
def get_last_ix(self):
index = self.get_index()
if len(index) >= 1:
return max(index)
return -1
def update_df(self):
df = pd.DataFrame([self.get_json_record(i) for i in self.get_index(shuffled=False)])
self.df = df
def get_df(self):
if self.df is None:
self.update_df()
return self.df
def get_index(self, shuffled=True):
files = next(os.walk(self.path))[2]
record_files = [f for f in files if f[:6] == 'record']
def get_file_ix(file_name):
try:
name = file_name.split('.')[0]
num = int(name.split('_')[1])
except:
num = 0
return num
nums = [get_file_ix(f) for f in record_files]
if shuffled:
random.shuffle(nums)
else:
nums = sorted(nums)
return nums
@property
def inputs(self):
return list(self.meta['inputs'])
@property
def types(self):
return list(self.meta['types'])
def get_input_type(self, key):
input_types = dict(zip(self.inputs, self.types))
return input_types.get(key)
def write_json_record(self, json_data):
path = self.get_json_record_path(self.current_ix)
try:
with open(path, 'w') as fp:
json.dump(json_data, fp)
except TypeError:
logger.warn('troubles with record: {}'.format(json_data))
except FileNotFoundError:
raise
except:
logger.error('Unexpected error: {}'.format(sys.exc_info()[0]))
raise
def get_num_records(self):
import glob
files = glob.glob(os.path.join(self.path, 'record_*.json'))
return len(files)
def make_record_paths_absolute(self, record_dict):
d = {}
for k, v in record_dict.items():
if type(v) == str: # filename
if '.' in v:
v = os.path.join(self.path, v)
d[k] = v
return d
def check(self, fix=False):
"""
Iterate over all records and make sure we can load them.
Optionally remove records that cause a problem.
"""
logger.info('Checking tub: {}'.format(self.path))
logger.info('Found: {} records'.format(self.get_num_records()))
problems = False
for ix in self.get_index(shuffled=False):
try:
self.get_record(ix)
except:
problems = True
if fix is False:
logger.warning('problems with record {} : {}'.format(ix, self.path))
else:
logger.warning('problems with record {}, removing: {}'.format(ix, self.path))
self.remove_record(ix)
if not problems:
logger.info('No problems found.')
def remove_record(self, ix):
"""
remove data associate with a record
"""
record = self.get_json_record_path(ix)
os.unlink(record)
def put_record(self, data):
"""
Save values like images that can't be saved in the csv log and
return a record with references to the saved values that can
be saved in a csv.
"""
json_data = {}
for key, val in data.items():
typ = self.get_input_type(key)
if typ in ['str', 'float', 'int', 'boolean']:
json_data[key] = val
elif typ is 'image':
name = self.make_file_name(key, ext='.jpg')
val.save(os.path.join(self.path, name))
json_data[key] = name
elif typ == 'image_array':
img = Image.fromarray(np.uint8(val))
name = self.make_file_name(key, ext='.jpg')
img.save(os.path.join(self.path, name))
json_data[key] = name
else:
msg = 'Tub does not know what to do with this type {}'.format(typ)
raise TypeError(msg)
self.write_json_record(json_data)
self.current_ix += 1
return self.current_ix
def get_json_record_path(self, ix):
# fill zeros
# return os.path.join(self.path, 'record_'+str(ix).zfill(6)+'.json')
# don't fill zeros
return os.path.join(self.path, 'record_' + str(ix) + '.json')
def get_json_record(self, ix):
path = self.get_json_record_path(ix)
try:
with open(path, 'r') as fp:
json_data = json.load(fp)
except UnicodeDecodeError:
raise Exception('bad record: %d. You may want to run `python manage.py check --fix`' % ix)
except FileNotFoundError:
raise
except:
logger.error('Unexpected error: {}'.format(sys.exc_info()[0]))
raise
record_dict = self.make_record_paths_absolute(json_data)
return record_dict
def get_record(self, ix):
json_data = self.get_json_record(ix)
data = self.read_record(json_data)
return data
def read_record(self, record_dict):
data = {}
for key, val in record_dict.items():
typ = self.get_input_type(key)
# load objects that were saved as separate files
if typ == 'image_array':
img = Image.open((val))
val = np.array(img)
data[key] = val
return data
def make_file_name(self, key, ext='.png'):
# name = '_'.join([str(self.current_ix).zfill(6), key, ext])
name = '_'.join([str(self.current_ix), key, ext]) # don't fill zeros
name = name = name.replace('/', '-')
return name
def delete(self):
""" Delete the folder and files for this tub. """
import shutil
shutil.rmtree(self.path)
def shutdown(self):
""" Required by the Part i
没有合适的资源?快使用搜索试试~ 我知道了~
PARTS用于深度学习训练
共79个文件
pyc:40个
py:27个
js:5个
需积分: 0 0 下载量 190 浏览量
2023-05-06
01:41:40
上传
评论
收藏 320KB ZIP 举报
温馨提示
PARTS用于深度学习训练
资源推荐
资源详情
资源评论
收起资源包目录
parts.zip (79个子文件)
parts
imu.py 1KB
__init__.py 0B
controller.py 13KB
camera.py 2KB
web_controller
__init__.py 35B
web.py 4KB
templates
vehicle.html 7KB
base.html 2KB
static
style.css 2KB
jquery-3.1.1.min.js 85KB
nipple.js 37KB
bootstrap
3.3.7
js
bootstrap.min.js 36KB
css
bootstrap.min.css 118KB
fonts
glyphicons-halflings-regular.ttf 44KB
glyphicons-halflings-regular.woff 23KB
glyphicons-halflings-regular.woff2 18KB
main.js 18KB
ui
1.12.1
jquery-ui.min.js 248KB
__pycache__
__init__.cpython-35.pyc 211B
web.cpython-35.pyc 5KB
web.cpython-36.pyc 5KB
__init__.cpython-36.pyc 228B
socket_clien.py 1KB
base_image.py 633B
img_predict.py 2KB
simulation.py 2KB
actuator.py 4KB
yolo_cut.py 7KB
uart.py 808B
clock.py 105B
img_cut.py 14KB
I2C_test.py 239B
autorope.py 4KB
encoder.py 4KB
uart_usb1.py 2KB
transform.py 2KB
__pycache__
controller.cpython-35.pyc 9KB
actuator.cpython-35.pyc 5KB
I2C_test.cpython-35.pyc 420B
keras.cpython-35.pyc 4KB
ch340.cpython-35.pyc 1KB
actuator.cpython-36.pyc 4KB
keras.cpython-36.pyc 3KB
img_predict_people.cpython-35.pyc 4KB
transform.cpython-35.pyc 2KB
uart.cpython-36.pyc 1012B
encoder.cpython-35.pyc 4KB
socket_clien.cpython-35.pyc 1KB
clock.cpython-36.pyc 466B
uart.cpython-35.pyc 1KB
img_cut.cpython-35.pyc 11KB
controller的二手ps3手柄树莓派.cpython-35.pyc 9KB
base_image.cpython-35.pyc 847B
__init__.cpython-35.pyc 146B
recognition_drive.cpython-36.pyc 2KB
transform.cpython-36.pyc 2KB
clock.cpython-35.pyc 467B
camera.cpython-36.pyc 2KB
simulation.cpython-35.pyc 3KB
datastore.cpython-36.pyc 21KB
uart_usb1.cpython-35.pyc 2KB
recognition_drive.cpython-35.pyc 2KB
datastore.cpython-35.pyc 23KB
controller.cpython-36.pyc 8KB
img_predict.cpython-35.pyc 2KB
__init__.cpython-36.pyc 167B
imu.cpython-35.pyc 2KB
autorope.cpython-35.pyc 4KB
yolo_cut.cpython-35.pyc 7KB
img_predict_red.cpython-35.pyc 3KB
camera.cpython-35.pyc 2KB
uart_usb1.cpython-36.pyc 2KB
img_predict_red.py 2KB
img_predict_people.py 4KB
controller的二手ps3手柄树莓派.py 13KB
keras.py 4KB
datastore.py 21KB
recognition_drive.py 4KB
ch340.py 906B
共 79 条
- 1
资源评论
zhuayun
- 粉丝: 205
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功