#!/usr/bin/env python
# test yolo
#based on
"""
https://github.com/awslabs/amazon-sagemaker-examples/blob/
master/advanced_functionality/tensorflow_bring_your_own/container/cifar10/train
"""

import os
import sys
import subprocess
import traceback
import tqdm

def _run(cmd):
    process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=os.environ)
    # stdout, stderr = process.communicate()
    # line = process.stdout.readline()
    # print(line)
    return_code = process.poll()
    if return_code:
        error_msg = 'Return Code: {}, CMD: {}, Err: {}'.format(return_code, cmd, stderr)
        raise Exception(error_msg)

if __name__ == '__main__':
    try:
        train_cmd = ['./darknet/darknet',
                     'detector',
                     'train',
                     'cfg/boat.data',
                     'cfg/boat.cfg',
                     'cfg/darknet19_448.conv.23']

        _run(train_cmd)

        sys.exit(0)
    except Exception as e:
        trc = traceback.format_exc()
        print('Exception during training: ' + str(e) + '\n' + trc, file=sys.stderr)
        sys.exit(255)

 #./darknet/darknet detector test cfg/ships