Exemplo n.º 1
0
def pre_process_images(list_folders_scores, recalculate):
    print('Start pre_processing images')

    # Loading instances
    instance_pose = ClassOpenPose()

    for folder, min_score in list_folders_scores:
        for file in os.listdir(folder):
            full_path = os.path.join(folder, file)
            extension = ClassUtils.get_filename_extension(full_path)

            if extension != '.jpg':
                print('Ignoring file {0}'.format(full_path))
                continue

            file_no_ext = ClassUtils.get_filename_no_extension(full_path)
            arr_file_name = os.path.join(folder, '{0}.json'.format(file_no_ext))

            # If image recalculation
            if not recalculate:
                if os.path.isfile(arr_file_name):
                    print('File already processed {0}'.format(full_path))
                    continue

            # Processing file
            print('Processing file {0}'.format(full_path))
            image = cv2.imread(full_path)

            arr, img_draw = instance_pose.recognize_image_tuple(image)

            arr_pass = list()

            # Checking vector integrity for all elements
            # Verify there is at least one arm and one leg
            for elem in arr:
                if ClassUtils.check_vector_integrity_part(elem, min_score):
                    arr_pass.append(elem)

            # If there is more than one person with vector integrity
            if len(arr_pass) != 1:
                for elem in arr_pass:
                    pt1, pt2 = ClassUtils.get_rectangle_bounds(elem, ClassUtils.MIN_POSE_SCORE)
                    cv2.rectangle(img_draw, pt1, pt2, (0, 0, 255), 3)

                cv2.namedWindow('main_window')
                cv2.imshow('main_window', img_draw)
                print(arr)
                print(arr_pass)
                cv2.waitKey(0)
                cv2.destroyAllWindows()
                raise Exception('Invalid len: {0} file {1}'.format(len(arr_pass), full_path))

            person_arr = arr_pass[0]

            arr_str = json.dumps(person_arr.tolist())

            with open(arr_file_name, 'w') as text_file:
                text_file.write(arr_str)

    print('Done!')
Exemplo n.º 2
0
def test_color_compare():
    print('Test color comparision')

    print('Loading image comparision')
    # Loading instances
    instance_pose = ClassOpenPose()

    # Avoid to open two prompts
    obj_img = ClassDescriptors.load_images_comparision_ext(instance_pose,
                                                           min_score,
                                                           load_one_img=True)
    hist_pose1 = obj_img['listPoints1']

    list_process = list()

    # Iterating in examples folder
    for root, _, files in os.walk(EXAMPLES_FOLDER):
        for file in files:
            full_path = os.path.join(root, file)
            extension = ClassUtils.get_filename_extension(full_path)

            if extension == '.jpg':
                list_process.append(full_path)

    # Sorting list
    list_process.sort()

    list_result = list()
    score_max_pt = -1

    for full_path in list_process:
        print('Processing file: {0}'.format(full_path))
        json_path = full_path.replace('.jpg', '.json')

        with open(json_path, 'r') as f:
            obj_json = json.loads(f.read())

        his_pose2 = obj_json['histPose']
        diff = ClassDescriptors.get_kmeans_diff(hist_pose1, his_pose2)
        print('Diff {0}'.format(diff))

        if diff <= 15:
            res = True
        else:
            res = False

        list_result.append({
            'filename':
            ClassUtils.get_filename_no_extension(full_path),
            'score':
            res
        })

    # list_result.sort(key=lambda x: x['score'])
    print('Printing list result')
    print(json.dumps(list_result, indent=2))
    print('min_score: {0}'.format(score_max_pt))

    print('Done!')
Exemplo n.º 3
0
def reprocess_images(list_folder_data):
    print('Init reprocess_images')

    for index, item in enumerate(list_folder_data):
        folder = item[0]
        min_score = item[1]

        print('Processing folder: {0}'.format(folder))
        list_files = os.listdir(folder)
        random.Random(seed).shuffle(list_files)

        for num_file, filename in enumerate(list_files):
            file = os.path.join(folder, filename)
            extension = ClassUtils.get_filename_extension(file)

            if extension == '.json':
                with open(file, 'r') as f:
                    data_str = f.read()

                data_json = json.loads(data_str)
                if 'vectors' in data_json:
                    print('Processing json file with new format: {0}'.format(file))
                    person_arr = data_json['vectors']
                else:
                    print('Processing json file: {0}'.format(file))
                    person_arr = data_json

                valid = ClassUtils.check_vector_integrity_pos(person_arr, min_score)
                only_pos = ClassUtils.check_vector_only_pos(person_arr, min_score)

                if not valid:
                    raise Exception('Vector integrity not valid for file: {0}'.format(file))

                if only_pos:
                    raise Exception('Invalid vector to perform detection')

                descriptors = ClassDescriptors.get_person_descriptors(person_arr, min_score, cam_number=0,
                                                                      image=None, calib_params=None,
                                                                      decode_img=False,
                                                                      instance_nn_pose=None)

                with open(file, 'w') as f:
                    f.write(json.dumps(descriptors))

                transformed_points = descriptors['transformedPoints']

                # Save pose for debugging purposes
                re_scale_factor = 100
                new_points = ClassDescriptors.re_scale_pose_factor(transformed_points,
                                                                   re_scale_factor, min_score)
                img_pose = ClassDescriptors.draw_pose_image(new_points, min_score, is_transformed=True)
                new_file_name = ClassUtils.get_filename_no_extension(file) + '_1.jpg'
                cv2.imwrite(new_file_name, img_pose)

    print('Done!')
def cnn_image_generation_folder():
    # Initializing instance nn

    list_folders = list()
    list_folders.append(ClassUtils.cnn_class_folder)
    instance_nn = ClassNN(ClassNN.model_dir_pose, classes_number, hidden_layers)

    # File walk
    for folder in list_folders:
        for root, _, files in os.walk(folder):
            for file in files:
                full_path = os.path.join(root, file)
                extension = ClassUtils.get_filename_extension(full_path)

                if extension == '.json':
                    print('Processing: {0}'.format(full_path))

                    if 'ori' in full_path:
                        raise Exception('Full path contains ori folder!')

                    with open(full_path, 'r') as f:
                        json_txt = f.read()

                    json_data = json.loads(json_txt)

                    # All image generation
                    image_name_pos = ClassUtils.get_filename_no_extension(full_path) + '_p.bmp'
                    image_name_angle = ClassUtils.get_filename_no_extension(full_path) + '_a.bmp'
                    image_name_pose = ClassUtils.get_filename_no_extension(full_path) + '_s.bmp'
                    image_name_angles_black = ClassUtils.get_filename_no_extension(full_path) + '_b.bmp'
                    image_name_pos_black = ClassUtils.get_filename_no_extension(full_path) + '_o.bmp'
                    image_name_pos_black_rem = ClassUtils.get_filename_no_extension(full_path) + '_r.bmp'

                    image_res_pos = create_cnn_image_pos(json_data)
                    image_res_angle = create_cnn_image_angles(json_data)
                    image_res_pose = create_cnn_image_pose(json_data, instance_nn)
                    image_res_angles_black = create_image_cnn_angles_black(json_data)
                    image_res_pos_black = create_cnn_image_pos_black(json_data)
                    image_res_pos_black_rem = create_cnn_image_pos_black_rem(json_data)

                    # print('Writing image pos: {0}'.format(image_name_pos))
                    cv2.imwrite(image_name_pos, image_res_pos)

                    # print('Writing image angle: {0}'.format(image_name_angle))
                    cv2.imwrite(image_name_angle, image_res_angle)

                    # print('Writing image pose: {0}'.format(image_name_pose))
                    cv2.imwrite(image_name_pose, image_res_pose)

                    # print('Writing image angles black: {0}'.format(image_name_angles_black))
                    cv2.imwrite(image_name_angles_black, image_res_angles_black)

                    # print('Writing image pos black: {0}'.format(image_name_pos_black))
                    cv2.imwrite(image_name_pos_black, image_res_pos_black)

                    print('Writing image pos black rem: {0}'.format(image_name_pos_black_rem))
                    cv2.imwrite(image_name_pos_black_rem, image_res_pos_black_rem)

    print('Done!')
Exemplo n.º 5
0
def re_process_folder():
    print('Init folder processing')

    init_dir = '/home/mauricio/Pictures/BTF'
    options = {'initialdir': init_dir}

    folder = filedialog.askdirectory(**options)

    if folder is None:
        raise Exception('Folder not selected!')

    files = os.listdir(folder)

    for file in files:
        full_path = os.path.join(folder, file)
        ext = ClassUtils.get_filename_extension(full_path)

        if ext == '.jpg':
            print('Processing file: {0}'.format(full_path))

            file_json = ClassUtils.get_filename_no_extension(
                full_path) + '.json'

            with open(file_json, 'r') as f:
                json_txt = f.read()

            json_data = json.loads(json_txt)

            if 'vectors' in json_data:
                vectors = json_data['vectors']
            elif 'vector' in json_data:
                vectors = json_data['vector']
            else:
                raise Exception('Vector not found!')

            img_cv = cv2.imread(full_path)

            param = ClassDescriptors.get_person_descriptors(vectors,
                                                            min_score,
                                                            image=img_cv,
                                                            decode_img=False)
            with open(file_json, 'w') as f:
                f.write(json.dumps(param, indent=2))

    print('Done!')
Exemplo n.º 6
0
def main():
    print('Initializing main function')

    # Withdrawing TKInter
    Tk().withdraw()

    # Ask for directory
    init_dir = '/home/mauricio/CNN/Images'
    options = {'initialdir': init_dir}
    dir_name = filedialog.askdirectory(**options)

    if not dir_name:
        print('Directory not selected')
    else:
        for root, subdirs, files in os.walk(dir_name):
            for file in files:
                full_path = os.path.join(root, file)
                print('Processing {0}'.format(full_path))

                extension = ClassUtils.get_filename_extension(file)

                if extension != '.jpg':
                    print('Ignoring file no jpg {0}'.format(full_path))
                    continue

                name = ClassUtils.get_filename_no_extension(file)
                if len(name) < total_digits:
                    new_name = ''
                    for _ in range(total_digits - len(name)):
                        new_name += '0'
                    new_name += name
                    new_name += extension

                    new_full_path = os.path.join(root, new_name)

                    print('Rename file {0} to {1}'.format(full_path, new_full_path))
                    os.rename(full_path, new_full_path)

        print('Done!')
Exemplo n.º 7
0
def main():
    print('Initializing main function')

    # Initializing instances
    instance_pose = ClassOpenPose()

    # Withdrawing tkinter
    Tk().withdraw()

    # Loading folder images
    folder_images = '/home/mauricio/PosesProcessed/folder_images'
    folder_images_draw = '/home/mauricio/PosesProcessed/folder_images_draw'
    pose_base_folder = '/home/mauricio/Pictures/PosesNew'

    # Reading all elements in list
    list_files = sorted(os.listdir(folder_images_draw))

    cv2.namedWindow('main_window', cv2.WND_PROP_AUTOSIZE)
    index = 0

    while True:
        file = list_files[index]
        full_path_draw = os.path.join(folder_images_draw, file)

        image_draw = cv2.imread(full_path_draw)
        cv2.imshow('main_window', image_draw)

        key = cv2.waitKey(0)
        print('Key pressed: {0}'.format(key))
        if key == 52:
            # Left arrow
            index -= 1
            if index < 0:
                index = 0
        elif key == 54:
            # Right arrow
            index += 1
            if index == len(list_files):
                index = len(list_files) - 1
        elif key == 27:
            # Esc
            # Ask are you sure question
            result = messagebox.askyesno("Exit", "Are you sure to exit?")
            print(result)

            if result:
                break
        elif key == 115:
            # Check JSON file
            image_full_path = os.path.join(folder_images, file)
            json_path = image_full_path.replace(".jpg", ".json")

            if not os.path.exists(json_path):
                # Generating JSON array
                image = cv2.imread(image_full_path)
                arr = instance_pose.recognize_image(image).tolist()
            else:
                with open(json_path, 'r') as file:
                    arr_str = file.read()
                    arr = json.load(arr_str)

            arr_pass = []
            min_score = 0.05

            # Drawing candidates
            for elem in arr:
                if ClassUtils.check_vector_integrity_part(elem, min_score):
                    arr_pass.append(elem)

            if len(arr_pass) == 0:
                print('Arr pass size equals to zero')
                continue
            elif len(arr_pass) == 1:
                selection_int = 0
                person_arr = arr_pass[0]
                arr_str = json.dumps(person_arr)
            else:
                # Draw rectangles for all candidates
                for index_elem, person_arr in enumerate(arr_pass):
                    pt1, pt2 = ClassUtils.get_rectangle_bounds(person_arr, min_score)
                    cv2.rectangle(image_draw, pt1, pt2, (0, 0, 255), 5)

                    font = cv2.FONT_HERSHEY_SIMPLEX
                    bottom_left_corner = pt2
                    font_scale = 0.6
                    font_color = (255, 255, 255)
                    line_type = 2

                    cv2.putText(image_draw, '{0}'.format(index_elem),
                                bottom_left_corner,
                                font,
                                font_scale,
                                font_color,
                                line_type)

                while True:
                    print('Select image to put')
                    cv2.imshow('main_window', image_draw)
                    wait_key = cv2.waitKey(0)

                    print('Wait Key: {0}'.format(wait_key))
                    selection_int = ClassUtils.key_to_number(wait_key)

                    print('Selection: {0}'.format(selection_int))
                    if 0 <= selection_int < len(arr_pass):
                        break

                person_arr = arr_pass[selection_int]
                arr_str = json.dumps(person_arr)

            # Getting new image using numpy slicing
            pt1, pt2 = ClassUtils.get_rectangle_bounds(person_arr, min_score)
            image_draw = image_draw[pt1[1]:pt2[1], pt1[0]:pt2[0]]

            # Selecting directory after processing image!
            init_dir = '/home/mauricio/Pictures/PosesNew'
            options = {'initialdir': init_dir}
            dir_name = filedialog.askdirectory(**options)

            if not dir_name:
                print('Directory not selected')
            else:
                new_name = ClassUtils.get_filename_no_extension(file)
                new_name = "{0}_{1}.{2}".format(new_name, selection_int, 'jpg')

                new_image_path = os.path.join(dir_name, new_name)
                new_json_path = os.path.join(dir_name, new_name.replace('.jpg', '.json'))

                cv2.imwrite(new_image_path, image_draw)
                with open(new_json_path, 'w') as file:
                    file.write(arr_str)

                print('File copied from {0} to {1}'.format(full_path_draw, new_image_path))
                index += 1
                if index == len(list_files):
                    index = len(list_files) - 1

    cv2.destroyAllWindows()
    print('Done!')