
from __future__ import division
import numpy as np
import sys,os

import basic.basic as basic
from PIL import Image
import cv2


class SampleClass(object):
    image = ''      # path of image
    groudT = ''     # path of groud image
    grounE = ''     # path of buidling edge
    id = ''         # file ID
# list of SampleClass
train_data = []

def read_train_data(train_file,file_id=None):
    """
    read the file list
    :param test_file: file tontains the test file list
    :param file_id: id is need in caffe for output result, not need in pytorch_deeplab_resnet
    :return: True if succeful, False otherwise
    """
    if os.path.isfile(train_file) is False:
        basic.outputlogMessage('error: file not exist %s'%train_file)
        return False
    f_obj = open(train_file)
    f_lines = f_obj.readlines()
    f_obj.close()

    if file_id is not None:
        fid_obj = open(file_id)
        fid_lines = fid_obj.readlines()
        fid_obj.close()

        if len(f_lines) != len(fid_lines):
            basic.outputlogMessage('the number of lines in test_file and test_file_id is not the same')
            return False

        for i in range(0,len(f_lines)):
            temp = f_lines[i].split()
            if len(temp) < 1:
                continue
            sample = SampleClass()
            sample.image = temp[0]
            if len(temp) > 1:
                sample.groudT = temp[1]
            sample.id = fid_lines[i].strip()
            train_data.append(sample)
    else:
        for i in range(0, len(f_lines)):
            temp = f_lines[i].split()
            if len(temp) < 1:
                continue
            sample = SampleClass()
            sample.image = temp[0]
            if len(temp) > 1:
                sample.groudT = temp[1]
                train_data.append(sample)

    # prepare file for pytorch_deeplab_resnet
    if len(train_data)< 1:
        basic.outputlogMessage('error, not input train data ')
        return False

    # check all image file and ground true file
    for sample in train_data:
        # check image path
        image_basename = os.path.basename(sample.image)
        if os.path.isfile(sample.image) is False:
            basic.outputlogMessage('error, file not exist: %s'%sample.image)
            return False

        # check ground path
        # if len(sample.groudT)>0 and os.path.isfile(sample.groudT) is False:
        #     sample.groudT = os.path.basename(sample.groudT)
        if  os.path.isfile(sample.groudT) is False:
            basic.outputlogMessage('error, file not exist: %s' % sample.groudT)
            return False

        if len(sample.id)< 1:
            sample.id = os.path.splitext(image_basename)[0]

    basic.outputlogMessage('read train data completed, sample count %d'%len(train_data))
    return True


def convert_groudT_to_groudEdge():
    if len(train_data) < 1:
        basic.outputlogMessage('error, no input images')
        return False
    for i in range(0,len(train_data)):
        groundT = train_data[i].groudT
        img_edge = os.path.join(os.path.split(groundT)[0],train_data[i].id + '_edge.png')
        train_data[i].groundE = img_edge

        im = Image.open(groundT)
        in_ = np.array(im, dtype=np.uint8)
        in_[np.where(in_ == 1)] = 0  # ignore building, only keep boundary
        in_[np.where(in_ == 255)] = 125

        w, h = in_.shape
        save_edge = np.empty((w, h,3),dtype=np.uint8)
        save_edge[:, :, 1] = save_edge[:, :, 2] = save_edge[:,:,0] = in_
        cv2.imwrite(img_edge, save_edge)

    pass

building_list = 'buildings.txt'
if len(sys.argv) ==2:
    building_list = sys.argv[1]
read_train_data(building_list)
convert_groudT_to_groudEdge()

fw_obj = open('train_edge.txt','w')
for item in train_data:
    fw_obj.writelines('%s %s\n'%(item.image,item.groundE))
fw_obj.close()

3bbe73dd167e61b2a11f12698616cab0521dad68
<mask token>


class SampleClass(object):
    image = ''
    groudT = ''
    grounE = ''
    id = ''


<mask token>

<mask token>


class SampleClass(object):
    image = ''
    groudT = ''
    grounE = ''
    id = ''


<mask token>


def read_train_data(train_file, file_id=None):
    """
    read the file list
    :param test_file: file tontains the test file list
    :param file_id: id is need in caffe for output result, not need in pytorch_deeplab_resnet
    :return: True if succeful, False otherwise
    """
    if os.path.isfile(train_file) is False:
        basic.outputlogMessage('error: file not exist %s' % train_file)
        return False
    f_obj = open(train_file)
    f_lines = f_obj.readlines()
    f_obj.close()
    if file_id is not None:
        fid_obj = open(file_id)
        fid_lines = fid_obj.readlines()
        fid_obj.close()
        if len(f_lines) != len(fid_lines):
            basic.outputlogMessage(
                'the number of lines in test_file and test_file_id is not the same'
                )
            return False
        for i in range(0, len(f_lines)):
            temp = f_lines[i].split()
            if len(temp) < 1:
                continue
            sample = SampleClass()
            sample.image = temp[0]
            if len(temp) > 1:
                sample.groudT = temp[1]
            sample.id = fid_lines[i].strip()
            train_data.append(sample)
    else:
        for i in range(0, len(f_lines)):
            temp = f_lines[i].split()
            if len(temp) < 1:
                continue
            sample = SampleClass()
            sample.image = temp[0]
            if len(temp) > 1:
                sample.groudT = temp[1]
                train_data.append(sample)
    if len(train_data) < 1:
        basic.outputlogMessage('error, not input train data ')
        return False
    for sample in train_data:
        image_basename = os.path.basename(sample.image)
        if os.path.isfile(sample.image) is False:
            basic.outputlogMessage('error, file not exist: %s' % sample.image)
            return False
        if os.path.isfile(sample.groudT) is False:
            basic.outputlogMessage('error, file not exist: %s' % sample.groudT)
            return False
        if len(sample.id) < 1:
            sample.id = os.path.splitext(image_basename)[0]
    basic.outputlogMessage('read train data completed, sample count %d' %
        len(train_data))
    return True


<mask token>

<mask token>


class SampleClass(object):
    image = ''
    groudT = ''
    grounE = ''
    id = ''


<mask token>


def read_train_data(train_file, file_id=None):
    """
    read the file list
    :param test_file: file tontains the test file list
    :param file_id: id is need in caffe for output result, not need in pytorch_deeplab_resnet
    :return: True if succeful, False otherwise
    """
    if os.path.isfile(train_file) is False:
        basic.outputlogMessage('error: file not exist %s' % train_file)
        return False
    f_obj = open(train_file)
    f_lines = f_obj.readlines()
    f_obj.close()
    if file_id is not None:
        fid_obj = open(file_id)
        fid_lines = fid_obj.readlines()
        fid_obj.close()
        if len(f_lines) != len(fid_lines):
            basic.outputlogMessage(
                'the number of lines in test_file and test_file_id is not the same'
                )
            return False
        for i in range(0, len(f_lines)):
            temp = f_lines[i].split()
            if len(temp) < 1:
                continue
            sample = SampleClass()
            sample.image = temp[0]
            if len(temp) > 1:
                sample.groudT = temp[1]
            sample.id = fid_lines[i].strip()
            train_data.append(sample)
    else:
        for i in range(0, len(f_lines)):
            temp = f_lines[i].split()
            if len(temp) < 1:
                continue
            sample = SampleClass()
            sample.image = temp[0]
            if len(temp) > 1:
                sample.groudT = temp[1]
                train_data.append(sample)
    if len(train_data) < 1:
        basic.outputlogMessage('error, not input train data ')
        return False
    for sample in train_data:
        image_basename = os.path.basename(sample.image)
        if os.path.isfile(sample.image) is False:
            basic.outputlogMessage('error, file not exist: %s' % sample.image)
            return False
        if os.path.isfile(sample.groudT) is False:
            basic.outputlogMessage('error, file not exist: %s' % sample.groudT)
            return False
        if len(sample.id) < 1:
            sample.id = os.path.splitext(image_basename)[0]
    basic.outputlogMessage('read train data completed, sample count %d' %
        len(train_data))
    return True


def convert_groudT_to_groudEdge():
    if len(train_data) < 1:
        basic.outputlogMessage('error, no input images')
        return False
    for i in range(0, len(train_data)):
        groundT = train_data[i].groudT
        img_edge = os.path.join(os.path.split(groundT)[0], train_data[i].id +
            '_edge.png')
        train_data[i].groundE = img_edge
        im = Image.open(groundT)
        in_ = np.array(im, dtype=np.uint8)
        in_[np.where(in_ == 1)] = 0
        in_[np.where(in_ == 255)] = 125
        w, h = in_.shape
        save_edge = np.empty((w, h, 3), dtype=np.uint8)
        save_edge[:, :, 1] = save_edge[:, :, 2] = save_edge[:, :, 0] = in_
        cv2.imwrite(img_edge, save_edge)
    pass


<mask token>
if len(sys.argv) == 2:
    building_list = sys.argv[1]
read_train_data(building_list)
convert_groudT_to_groudEdge()
<mask token>
for item in train_data:
    fw_obj.writelines('%s %s\n' % (item.image, item.groundE))
fw_obj.close()

<mask token>


class SampleClass(object):
    image = ''
    groudT = ''
    grounE = ''
    id = ''


train_data = []


def read_train_data(train_file, file_id=None):
    """
    read the file list
    :param test_file: file tontains the test file list
    :param file_id: id is need in caffe for output result, not need in pytorch_deeplab_resnet
    :return: True if succeful, False otherwise
    """
    if os.path.isfile(train_file) is False:
        basic.outputlogMessage('error: file not exist %s' % train_file)
        return False
    f_obj = open(train_file)
    f_lines = f_obj.readlines()
    f_obj.close()
    if file_id is not None:
        fid_obj = open(file_id)
        fid_lines = fid_obj.readlines()
        fid_obj.close()
        if len(f_lines) != len(fid_lines):
            basic.outputlogMessage(
                'the number of lines in test_file and test_file_id is not the same'
                )
            return False
        for i in range(0, len(f_lines)):
            temp = f_lines[i].split()
            if len(temp) < 1:
                continue
            sample = SampleClass()
            sample.image = temp[0]
            if len(temp) > 1:
                sample.groudT = temp[1]
            sample.id = fid_lines[i].strip()
            train_data.append(sample)
    else:
        for i in range(0, len(f_lines)):
            temp = f_lines[i].split()
            if len(temp) < 1:
                continue
            sample = SampleClass()
            sample.image = temp[0]
            if len(temp) > 1:
                sample.groudT = temp[1]
                train_data.append(sample)
    if len(train_data) < 1:
        basic.outputlogMessage('error, not input train data ')
        return False
    for sample in train_data:
        image_basename = os.path.basename(sample.image)
        if os.path.isfile(sample.image) is False:
            basic.outputlogMessage('error, file not exist: %s' % sample.image)
            return False
        if os.path.isfile(sample.groudT) is False:
            basic.outputlogMessage('error, file not exist: %s' % sample.groudT)
            return False
        if len(sample.id) < 1:
            sample.id = os.path.splitext(image_basename)[0]
    basic.outputlogMessage('read train data completed, sample count %d' %
        len(train_data))
    return True


def convert_groudT_to_groudEdge():
    if len(train_data) < 1:
        basic.outputlogMessage('error, no input images')
        return False
    for i in range(0, len(train_data)):
        groundT = train_data[i].groudT
        img_edge = os.path.join(os.path.split(groundT)[0], train_data[i].id +
            '_edge.png')
        train_data[i].groundE = img_edge
        im = Image.open(groundT)
        in_ = np.array(im, dtype=np.uint8)
        in_[np.where(in_ == 1)] = 0
        in_[np.where(in_ == 255)] = 125
        w, h = in_.shape
        save_edge = np.empty((w, h, 3), dtype=np.uint8)
        save_edge[:, :, 1] = save_edge[:, :, 2] = save_edge[:, :, 0] = in_
        cv2.imwrite(img_edge, save_edge)
    pass


building_list = 'buildings.txt'
if len(sys.argv) == 2:
    building_list = sys.argv[1]
read_train_data(building_list)
convert_groudT_to_groudEdge()
fw_obj = open('train_edge.txt', 'w')
for item in train_data:
    fw_obj.writelines('%s %s\n' % (item.image, item.groundE))
fw_obj.close()


from __future__ import division
import numpy as np
import sys,os

import basic.basic as basic
from PIL import Image
import cv2


class SampleClass(object):
    image = ''      # path of image
    groudT = ''     # path of groud image
    grounE = ''     # path of buidling edge
    id = ''         # file ID
# list of SampleClass
train_data = []

def read_train_data(train_file,file_id=None):
    """
    read the file list
    :param test_file: file tontains the test file list
    :param file_id: id is need in caffe for output result, not need in pytorch_deeplab_resnet
    :return: True if succeful, False otherwise
    """
    if os.path.isfile(train_file) is False:
        basic.outputlogMessage('error: file not exist %s'%train_file)
        return False
    f_obj = open(train_file)
    f_lines = f_obj.readlines()
    f_obj.close()

    if file_id is not None:
        fid_obj = open(file_id)
        fid_lines = fid_obj.readlines()
        fid_obj.close()

        if len(f_lines) != len(fid_lines):
            basic.outputlogMessage('the number of lines in test_file and test_file_id is not the same')
            return False

        for i in range(0,len(f_lines)):
            temp = f_lines[i].split()
            if len(temp) < 1:
                continue
            sample = SampleClass()
            sample.image = temp[0]
            if len(temp) > 1:
                sample.groudT = temp[1]
            sample.id = fid_lines[i].strip()
            train_data.append(sample)
    else:
        for i in range(0, len(f_lines)):
            temp = f_lines[i].split()
            if len(temp) < 1:
                continue
            sample = SampleClass()
            sample.image = temp[0]
            if len(temp) > 1:
                sample.groudT = temp[1]
                train_data.append(sample)

    # prepare file for pytorch_deeplab_resnet
    if len(train_data)< 1:
        basic.outputlogMessage('error, not input train data ')
        return False

    # check all image file and ground true file
    for sample in train_data:
        # check image path
        image_basename = os.path.basename(sample.image)
        if os.path.isfile(sample.image) is False:
            basic.outputlogMessage('error, file not exist: %s'%sample.image)
            return False

        # check ground path
        # if len(sample.groudT)>0 and os.path.isfile(sample.groudT) is False:
        #     sample.groudT = os.path.basename(sample.groudT)
        if  os.path.isfile(sample.groudT) is False:
            basic.outputlogMessage('error, file not exist: %s' % sample.groudT)
            return False

        if len(sample.id)< 1:
            sample.id = os.path.splitext(image_basename)[0]

    basic.outputlogMessage('read train data completed, sample count %d'%len(train_data))
    return True


def convert_groudT_to_groudEdge():
    if len(train_data) < 1:
        basic.outputlogMessage('error, no input images')
        return False
    for i in range(0,len(train_data)):
        groundT = train_data[i].groudT
        img_edge = os.path.join(os.path.split(groundT)[0],train_data[i].id + '_edge.png')
        train_data[i].groundE = img_edge

        im = Image.open(groundT)
        in_ = np.array(im, dtype=np.uint8)
        in_[np.where(in_ == 1)] = 0  # ignore building, only keep boundary
        in_[np.where(in_ == 255)] = 125

        w, h = in_.shape
        save_edge = np.empty((w, h,3),dtype=np.uint8)
        save_edge[:, :, 1] = save_edge[:, :, 2] = save_edge[:,:,0] = in_
        cv2.imwrite(img_edge, save_edge)

    pass

building_list = 'buildings.txt'
if len(sys.argv) ==2:
    building_list = sys.argv[1]
read_train_data(building_list)
convert_groudT_to_groudEdge()

fw_obj = open('train_edge.txt','w')
for item in train_data:
    fw_obj.writelines('%s %s\n'%(item.image,item.groundE))
fw_obj.close()
