Exemple #1
0
def getFurnitureShape(inputFile, inputDataFile, outputFile):
    '''
	입력받은 inputFile과 그 분석 파일 inputDataFile을 통해 grayscale 및 segmentation 된 데이터를 만든다.
	만든 데이터는 outputFile로 ( Grayscale 사진 ) Output.
	'''
    if utility.is_exist(inputDataFile):
        [divided_class, _, class_total, _] = utility.load_result(inputDataFile)
    else:
        segment(inputFile, None, inputDataFile)
        [divided_class, _, class_total, _] = utility.load_result(inputDataFile)

    gray_image = image_processing.to_gray_scale(inputFile)
    utility.print_image(gray_image)
    utility.save_image(gray_image, outputFile)
Exemple #2
0
def colorTransferToColor(inputFile, inputDataFile, outputFileName, destColor,
                         srcColor):
    '''
	입력받은 inputFile의 정해진 부분( srcColor와 비슷한 부분 )의 색을 destColor로 변경한다.
	'''
    if utility.is_exist(inputDataFile):
        [divided_class, class_number, class_total, class_border] = \
        utility.load_result(inputDataFile)
        class_count = []
        for ct in class_total:
            class_count.append(len(ct))
    else:
        divided_class, class_number, class_total, class_border, class_count, class_length, class_color, _, _, _ = \
        segmentation.get_divided_class(inputFile)

    class_color = image_processing.get_class_color(
        utility.read_image(inputFile), class_total, class_count)

    destArea = styler.get_similar_color_area(
        divided_class, class_number, class_total, class_color, srcColor,
        240)  # Simmilar Color threshold to 200.
    part_change_image = styler.change_area_color(inputFile, outputFileName,
                                                 destColor, divided_class,
                                                 destArea)
    utility.save_image(part_change_image, outputFileName)
Exemple #3
0
def readResultData(outputFile):
    '''
	Object Detection 한 output file을 읽어서 사용 가능한 형태로 return.
	'''
    [coord, str_tag, number_tag, score,
     filenames] = utility.load_result(outputFile)
    return coord, str_tag
Exemple #4
0
def getRecommandFurnitureForImage(selectedPreferenceImage, str_tag):
    [basePreferenceFiles,
     recommandFile] = utility.load_result(config.RECOMMAND_BASE_FILE)
    temp = recommandFile[basePreferenceFiles.index(
        os.path.basename(selectedPreferenceImage))]
    retRecomandFile = []
    recType = "sofa" if str_tag == "sofa" or str_tag == "chair" else "table"
    for t in temp:
        if t not in retRecomandFile and recType in t:
            retRecomandFile.append(t)
    return retRecomandFile
Exemple #5
0
def colorTransferToCoord(inputFile, inputDataFile, outputFileName, destColor,
                         destCoordList):
    '''
	입력받은 inputFile의 정해진 부분( destCoordList )의 색을 destColor로 변경한다.
	'''
    if utility.is_exist(inputDataFile):
        [divided_class, _, class_total, _] = utility.load_result(inputDataFile)
    else:
        divided_class, _, class_total, _, _, _, _, _, _, _ = \
        segmentation.get_divided_class(inputFile)
    styler.change_dest_color(inputFile, outputFileName, destColor,
                             divided_class, class_total, destCoordList)
Exemple #6
0
def re_segmentation(fileDir, resegIndex):
    fileNames = utility.get_filenames(fileDir)
    for fIndex in range(len(fileNames)):
        f = fileNames[fIndex]
        odf = utility.get_od_bin(f)
        [
            coord, str_tag, number_tag, score, rect_files, additional_infor,
            n_color
        ] = utility.load_result(odf)

        for i in resegIndex[fIndex]:
            rect_data_file = utility.get_bin(rect_files[i])
            print(rect_data_file, " will be re-generated. : ", str_tag[i])
            segment(rect_files[i], utility.add_name(rect_files[i], "_divided"),
                    rect_data_file)
Exemple #7
0
def readParameter(fileDir):
    # File is directory
    files = utility.get_filenames(fileDir)

    bin_files = []
    for f in files:
        if ".bin" in f:
            bin_files.append(f)

    total_parameter = []
    total_files = []

    for bf in bin_files:
        [_, str_tag, _, _, _, _, n_color] = utility.load_result(bf)
        parameter = normParameter(str_tag, n_color)
        image_file = bf[:-7] + ".jpg"
        total_parameter.append(parameter)
        total_files.append(image_file)
    return total_parameter, total_files
Exemple #8
0
def colorTransferWithImage(inputFile, inputDataFile, outputFileName,
                           destImage):
    '''
	입력받은 inputFile의 색을 destImage와 비슷하게 변경해서 outputFileName에 저장한다.
	Segmentation이 된다면 자른 부분만 변경.
	'''
    if utility.is_exist(inputDataFile):
        [_, _, class_total, _] = \
        utility.load_result(inputDataFile)
        class_count = []
        for ct in class_total:
            class_count.append(len(ct))
    else:
        _, _, class_total, _, class_count, _, _, _, _, _ = \
        segmentation.get_divided_class(inputFile)

    _, _, mask_map, (width,
                     height) = segmentation.get_segmented_image(inputFile)
    changed_image = styler.set_color_with_image(inputFile, destImage, mask_map)
    utility.save_image(changed_image, outputFileName)
Exemple #9
0
def textureTransferArea(inputFile, inputDataFile, outputFileName, destTexture,
                        srcColor):
    '''
	입력받은 inputFile의 정해진 부분( srcColor와 비슷한 색 )의 질감을 destTexture로 변경한다.
	'''
    if utility.is_exist(inputDataFile):
        [divided_class, class_number, class_total, _] = \
        utility.load_result(inputDataFile)
        class_count = []
        for ct in class_total:
            class_count.append(len(ct))
    else:
        divided_class, class_number, class_total, _, class_count, _, class_color, _, _, _ = \
        segmentation.get_divided_class(inputFile)

    class_color = image_processing.get_class_color(
        utility.read_image(inputFile), class_total, class_count)

    destArea = styler.get_similar_color_area(
        divided_class, class_number, class_total, class_color, srcColor,
        240)  # Simmilar Color threshold to 200.
    styler.change_area_style(inputFile, outputFileName, destTexture, destArea)
Exemple #10
0
def image_color_match(inputImage):
    # 이미지와 어울리는 컬러 List의 List를 return.
    [fileNames,
     colors] = utility.load_result(RESEARCH_BASE_DIR + "/" + COLOR_SYSTEM_FILE)
    input_colors = getDominantColor(inputImage)
    admitableColors = []
    admitableFiles = []
    while len(admitableColors) == 0:
        admitable = 10
        for input_color in input_colors:
            res_color, files = color_match(input_color, colors, fileNames,
                                           admitable)
            index = 0
            for r in res_color:
                if r not in admitableColors:
                    admitableColors.append(r)
                    admitableFiles.append(files[index])
                index += 1
        admitable += 10

    utility.print_image(utility.color_to_image(input_colors))

    return admitableColors, admitableFiles
Exemple #11
0
def getODandSegment(inputFile, od_model):
    try:
        [coord, str_tag, number_tag, score, rect_files, additional_infor, n_color] = \
        utility.load_result(config.RESEARCH_BASE_DIR + "/" + os.path.basename(utility.get_od_bin(inputFile)))
    except:
        [
            coord, str_tag, number_tag, score, rect_files, additional_infor,
            n_color
        ] = imageClassifier.saveParameter(inputFile, od_model)  # Get OD Data
    for i in range(len(str_tag)):
        if str_tag[i] == "sofa" or str_tag[i] == "chair":
            if utility.is_exist(utility.get_userinput_bin(rect_files[i])):
                rect_data_file = utility.get_userinput_bin(rect_files[i])
            elif utility.is_exist(utility.get_bin(rect_files[i])):
                rect_data_file = utility.get_bin(rect_files[i])
            else:
                rect_data_file = utility.get_bin(rect_files[i])
                segment(rect_files[i],
                        utility.add_name(rect_files[i], "_divided"),
                        rect_data_file)
    return [
        coord, str_tag, number_tag, score, rect_files, additional_infor,
        n_color
    ]
Exemple #12
0
    IMAGE_INDEX) + ".jpg"
OUTPUT_FILE = RESEARCH_BASE_DIR + "/" + IMAGE_BASE_NAME + '/' + utility.add_name(
    IMAGE_NAME.split("/")[-1], "_divided")
SEG_FILE_NAME = RESEARCH_BASE_DIR + '/' + utility.add_name(
    IMAGE_NAME.split("/")[-1], "", extension="bin")
SEG_SAVE_NAME = RESEARCH_BASE_DIR + '/' + utility.add_name(
    IMAGE_NAME.split("/")[-1], "_userInput", extension="bin")
# Constant
CHANGE_DIVIED = "Image/example/temp.jpg"

# Init Global Data for classify segmentation
totalClass = [[]]
nowIndex = 0
eraseMode = False
eraseList = []
load_value = utility.load_result(SEG_FILE_NAME)
if len(load_value) == 5:
    [divided_class, class_number, class_total, class_border, _] = load_value
else:
    [divided_class, class_number, class_total, class_border] = load_value


class MyApp(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.setWindowTitle("Segment GUI Helper")
        grid = QGridLayout()
Exemple #13
0
import mlWrapper
import utility
import sys
import random

# To use, 2번째 인자에 리스트들을 담아서 넘겨주면 된다.
if __name__ == "__main__":
    [selectedPreferenceImages, wfColorChangeImage, outputFile, str_tag, coord, rect_files, i, j, ratio] = utility.load_result(sys.argv[1])
    selectedPreferenceImage = selectedPreferenceImages[random.randint(0, len(selectedPreferenceImages) - 1)]
    partChangedOutFile, out_res_file = mlWrapper.getPartChangedImage(wfColorChangeImage[i], outputFile, str_tag, coord, rect_files, selectedPreferenceImage, i, j, ratio=ratio)
    print()
    print(partChangedOutFile)
    print(out_res_file)
Exemple #14
0
def getStyleChangedImage(inputFile,
                         preferenceImages,
                         od_model,
                         baseLight=[255, 255, 255],
                         changeLight=[178, 220, 240]):
    '''
	입력 Color는 BGR ( [178, 220, 240] 은 주황불빛 )
	preferenceImages 가 4장만 되어도 충분함.
	'''
    if len(preferenceImages) <= 2:
        preferenceImages = preferenceImages + preferenceImages
    print(preferenceImages)
    inputBaseFile, preferenceBaseFile = utility.file_basify(
        inputFile, preferenceImages)

    now = time.time()
    detection_model = pspnet_50_ADE_20K()
    outputFile = utility.get_add_dir(inputFile, "temp")

    # Object Detect & Segmentation
    [coord, str_tag, number_tag, score, rect_files, additional_infor,
     n_color] = getODandSegment(inputBaseFile, od_model)

    (imgHeight, imgWidth, _) = utility.read_image(inputFile).shape
    if imgWidth > destSize[0] and imgHeight > destSize[1]:
        ratio = (destSize[0] / imgWidth, destSize[1] / imgHeight)
    else:
        ratio = (1, 1)
    print("Loading Finished")

    temp = time.time()
    print("Loading Time : ", temp - now)

    # Wall Detection with input image.
    wall_divided = segmentation.detect_wall_floor(inputFile, detection_model)
    wall_divided = utility.resize_2darr(wall_divided, ratio=ratio)
    wall_total, wall_number = matrix_processing.divided_class_into_class_total(
        wall_divided)
    print("Wall Divided.")

    # Get preference image`s data.
    preferWallColor = []
    preferFloorColor = []
    selectedPreferenceImages = []
    [files, domColors, wallColors, floorColors] = utility.load_result(
        config.RESEARCH_BASE_FILE
    )  # Each files` dom color, wall color, floor color will be saved.
    baseNameFiles = [os.path.basename(files[f]) for f in range(len(files))]

    print("Wall Color start.")
    indx = list(range(0, len(preferenceBaseFile)))
    random.shuffle(indx)
    # Select 2 color of above to preferWallColor and preferFloorColor
    for i in range(MAX_WALL_IMAGE):
        ind = indx[i]
        preferImage = preferenceBaseFile[ind]
        loadIndex = baseNameFiles.index(os.path.basename(
            preferImage))  # We do only compare with base name.
        preferWallColor.append(wallColors[loadIndex])
        preferFloorColor.append(floorColors[loadIndex])
        selectedPreferenceImages.append(files[loadIndex])
    print("Wall Colored Selected.")

    # Change wall & floor
    wfColorChangeImage = []
    for i in range(MAX_WALL_IMAGE):
        wfOutputFile = changeWallFloor(inputFile,
                                       outputFile,
                                       wall_divided,
                                       wall_total,
                                       wall_number,
                                       i,
                                       preferWallColor,
                                       preferFloorColor,
                                       ratio=ratio)
        wfColorChangeImage.append(wfOutputFile)
    print("Wall Color Changed")

    temp = time.time()
    print("Wall Coloring Time : ", temp - now)

    # Change Object ( Table and Chair )
    partChangedFiles = []
    procs = []
    recommandFurnitureList = []
    changeFurnitureLocation = []
    changeFurnitureColor = []

    for i in range(MAX_WALL_IMAGE):
        for j in range(MAX_PART_CHANGE_IMAGE):
            # 넘겨줄 인자를 저장하고, Thread를 실행시켜서 속도 향상.
            argvFile = utility.add_name(
                config.SUBPROCESS_ARGV,
                "_" + str(MAX_PART_CHANGE_IMAGE * i + j))
            utility.save_result([
                selectedPreferenceImages, wfColorChangeImage, outputFile,
                str_tag, coord, rect_files, i, j, ratio
            ], argvFile)

            # Subprocess need to calculate with given ratio.
            proc = subprocess.Popen(
                ['python', 'getPartChangedImage.py', argvFile],
                shell=True,
                stdin=subprocess.PIPE,
                stdout=subprocess.PIPE,
                encoding="cp949")
            procs.append(proc)

    for i in range(len(procs)):
        out = procs[i].communicate()[0]
        out = str(out).split("\n")
        tout = []
        for i in range(len(out)):
            if len(out[i]) > 0:
                tout.append(out[i])
        [changed_log, recommand_furniture] = utility.load_result(tout[-1])
        partChangedFiles.append(tout[-2])
        recommandFurnitureList.append(recommand_furniture)
        for i in range(len(changed_log)):
            changeFurnitureLocation.append(changed_log[i][0])
            changeFurnitureColor.append(changed_log[i][1])

    print("Part Changed Finished")
    # Add some plant.
    # partChangedFiles = print() # Image number will not be changed.

    temp = time.time()
    print("Part Changing Time : ", temp - now)

    lightList = []
    # Change Light
    for i in range(MAX_OUT_IMAGE):
        print("Now Proceed : ", i)
        files = utility.add_name(partChangedFiles[i], "_lighter")
        if random.randint(1, MAX_OUT_IMAGE) > 4:
            changed_file = styler.get_light_change(partChangedFiles[i],
                                                   baseLight, changeLight)
            lightList.append(changeLight)
        else:
            changed_file = styler.get_light_change(partChangedFiles[i],
                                                   baseLight, baseLight)
            lightList.append(baseLight)
        utility.save_image(changed_file, files)
        partChangedFiles[i] = files
    # partChangedFiles 가 결국 바뀐 파일들
    temp = time.time()
    print("Total Time : ", temp - now)
    changeLog = makeChangeInfor(preferWallColor, preferFloorColor, [preferenceImages[indx[0]], preferenceImages[indx[1]]], partChangedFiles, lightList, changeFurnitureLocation, changeFurnitureColor, \
     recommandFurnitureList, [])

    resultDictionary = utility.save_log_dictionary(inputFile, partChangedFiles,
                                                   changeLog)
    utility.logging(str(resultDictionary))
    with open(FILE_OUTQUEUE, 'a') as f:
        f.write(str(resultDictionary) + "\n")
Exemple #15
0
def styleTransfer(inputFile,
                  inputDataFile,
                  destFile,
                  inpaintingRandomValue,
                  ratio=(1.0, 1.0)):
    '''
	입력받은 inputFile의 색과 질감을 destFile의 색과 질감으로 임의로 변형해준다. 
	'''
    if utility.is_exist(inputDataFile):
        loadData = utility.load_result(inputDataFile)
        if len(loadData) == 5:
            # Newer Version of segmentation.
            [divided_class, class_number, class_total, _,
             largest_mask] = loadData
        else:
            [divided_class, class_number, class_total, _] = loadData
            largest_mask = None
        class_count = []
        for ct in class_total:
            class_count.append(len(ct))
    else:
        divided_class, class_number, class_total, _, class_count, _, class_color, _, _, _ = \
        segmentation.get_divided_class(inputFile)

    # Init Variables. - TODO : Change this part with largest mask.
    # largest_mask, _, _, (width, height) = segmentation.get_segmented_image(inputFile)
    # class_color = image_processing.get_class_color(utility.read_image(inputFile), class_total, class_count)
    img = utility.read_image(inputFile)
    (height, width, _) = img.shape

    file_extension = "." + inputFile.split(".")[1]
    file_base_name = inputFile.split(".")[0]

    resized_class_total = utility.changed_coords2d(class_total, ratio=ratio)
    # 중복 제거
    temp_class_total = resized_class_total
    resized_class_total = []
    for tc in temp_class_total:
        if tc not in resized_class_total:
            resized_class_total.append(tc)

    input_sample = [
        resized_class_total[i][0] for i in range(len(resized_class_total))
    ]
    if len(input_sample) < MAX_CHANGE_COLOR:
        input_sample *= int(MAX_CHANGE_COLOR // len(input_sample)) + 1
    dest_color = image_processing.get_dominant_color(destFile, clusters=8)

    next_file_name = file_base_name + "_" + str(0) + file_extension
    now_input_sample = random.sample(input_sample, MAX_CHANGE_COLOR)
    now_dest_color = random.sample(dest_color, MAX_CHANGE_COLOR)
    part_change_image = utility.read_image(inputFile)
    part_change_image = utility.resize_image(part_change_image, ratio=ratio)
    randomValue = inpaintingRandomValue

    if randomValue < -1:
        # Image Inpainting
        masking_coord = []
        for ct in resized_class_total:
            masking_coord += ct
        tempFile = utility.add_name(next_file_name, "_temp")
        tempFile = config.RESEARCH_BASE_DIR + "/temp/" + tempFile.split(
            "/")[-1]

        utility.logging("Image Inpainting Starting." + str(randomValue))
        utility.save_image(
            utility.make_whitemask_image(part_change_image, masking_coord),
            tempFile)
        change_image = image_processing.inpainting(part_change_image, tempFile)
        part_change_image = image_processing.add_up_image(
            part_change_image, change_image, masking_coord, width, height)
        now_dest_color = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
    else:
        utility.logging("Image Inpainting Do not proceed. : " +
                        str(randomValue))
        # If not earse, recoloring.
        for j in range(MAX_CHANGE_COLOR):
            change_image = styler.change_dest_color(inputFile, next_file_name, now_dest_color[j], divided_class, resized_class_total,\
             [now_input_sample[j]], save_flag=False, ratio=ratio)
            part_change_image = image_processing.add_up_image(
                part_change_image, change_image,
                resized_class_total[input_sample.index(now_input_sample[j])],
                width, height)
    return part_change_image, now_dest_color