コード例 #1
0
def main():
    print('Initializing main function')

    # Initialing instances
    instance_pose = ClassOpenPose()

    filename = '/home/mauricio/Pictures/walk.jpg'
    image = cv2.imread(filename)

    arr = instance_pose.recognize_image(image)

    if len(arr) != 1:
        raise Exception(
            'There is more than one person in the image: {0}'.format(len(arr)))

    per_arr = arr[0]
    print(per_arr)

    ClassDescriptors.draw_pose(image, per_arr, min_score)
    cv2.namedWindow('main_window', cv2.WINDOW_AUTOSIZE)

    cv2.imshow('main_window', image)
    cv2.waitKey(0)

    cv2.destroyAllWindows()

    print('Done!')
コード例 #2
0
def get_poses_seq(folder: str,
                  instance_nn: ClassNN,
                  instance_pose: ClassOpenPose,
                  only_json=False):
    # List all folders
    list_files = []
    for file in os.listdir(folder):
        list_files.append(os.path.join(folder, file))

    # Sorting elements
    list_files.sort()

    # Get elements
    list_desc = list()
    for path in list_files:
        ext = ClassUtils.get_filename_extension(path)
        if only_json:
            if ext != '.json':
                print('Ignoring file: {0}'.format(path))
                continue

            with open(path, 'r') as file:
                person_arr_str = file.read()
                person_arr = json.loads(person_arr_str)
        else:
            if ext != '.jpg':
                print('Ignoring file: {0}'.format(path))
                continue

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

            arr = instance_pose.recognize_image(image)

            arr_pass = []
            for person_arr in arr:
                if ClassUtils.check_vector_integrity_part(
                        person_arr, min_score):
                    arr_pass.append(person_arr)

            if len(arr_pass) != 1:
                print('Ignoring file {0} - Len arr_pass: {1}'.format(
                    path, len(arr_pass)))
                continue

            person_arr = arr_pass[0]

        result_desc = ClassDescriptors.get_person_descriptors(
            person_arr, min_score)
        list_desc.append(result_desc['fullDesc'])

    list_desc_np = np.asarray(list_desc, np.float)
    results = instance_nn.predict_model_array(list_desc_np)

    list_classes = []
    for result in results:
        list_classes.append(result['classes'])

    return list_classes
コード例 #3
0
    def convert_video_mjpeg(cls, file_path: str, delete_mjpeg=False):
        extension = os.path.splitext(file_path)[1]

        if extension != '.mjpeg':
            raise Exception('file_path must have extension .mjpeg')
        else:
            output_video = file_path.replace('.mjpeg', '.mjpegx')
            print('output_video: ' + output_video)

            # Converting first with _1 to avoid confusions
            file_path_temp = output_video + '_1'

            print('Initializing instance')
            open_pose = ClassOpenPose()

            images = ClassMjpegReader.process_video(file_path)
            print('Len images: ' + str(len(images)))
            with open(file_path_temp, 'wb') as newFile:
                counter = 0
                for elem in images:
                    image = elem[0]
                    ticks = elem[1]

                    image_np = np.frombuffer(image, dtype=np.uint8)
                    image_cv = cv2.imdecode(image_np, cv2.IMREAD_ANYCOLOR)
                    arr = open_pose.recognize_image(
                        image_cv)  # type: np.ndarray

                    json_dict = {'vectors': arr.tolist()}
                    ClassUtils.write_in_file(newFile, ticks, image, json_dict)

                    counter += 1
                    if counter % 10 == 0:
                        print('Counter: ' + str(counter))

            # Deleting mjpegx file if exists and rename
            if os.path.exists(output_video):
                os.remove(output_video)

            # Naming new
            os.rename(file_path_temp, output_video)

            if delete_mjpeg:
                os.remove(file_path)
コード例 #4
0
def main():
    print('Initializing main function')

    Tk().withdraw()

    # Loading instances
    instance_pose = ClassOpenPose()

    # Loading filename
    init_dir = '/home/mauricio/Pictures/TestPlumb'
    options = {'initialdir': init_dir}
    filename = askopenfilename(**options)

    if not filename:
        filename = '/home/mauricio/Pictures/419.jpg'

    print('Filename: {0}'.format(filename))
    image = cv2.imread(filename)

    arr = instance_pose.recognize_image(image)
    valid_arr = list()

    for person_arr in arr:
        if ClassUtils.check_vector_integrity_pos(person_arr, min_score):
            valid_arr.append(person_arr)

    if len(valid_arr) != 1:
        raise Exception('Invalid len for arr: {0}'.format(len(valid_arr)))

    person_arr = valid_arr[0]
    ClassDescriptors.draw_pose(image, person_arr, min_score)

    cv2.namedWindow('main_window', cv2.WINDOW_AUTOSIZE)
    cv2.imshow('main_window', image)

    print_distances(person_arr)

    cv2.waitKey()
    cv2.destroyAllWindows()

    print('Done!')
コード例 #5
0
def main():
    print('Initializing main function')
    print('Initializing instance')

    instance = ClassOpenPose()
    print('Reading image')

    image = cv2.imread('/home/mauricio/Pictures/2_2.jpg')
    image2 = cv2.imread('/home/mauricio/Pictures/430.jpg')

    print('Recognizing image 1')
    arr, img_open = instance.recognize_image_tuple(image)
    print(arr)

    cv2.namedWindow('main_window', cv2.WINDOW_AUTOSIZE)
    cv2.imshow('main_window', img_open)
    cv2.waitKey()

    print('Recognizing image 2')
    arr = instance.recognize_image(image2)
    print(arr)

    print('Done generating elements!')
コード例 #6
0
def get_sets_folder(base_folder, label, instance_pose: ClassOpenPose):
    list_files = os.listdir(base_folder)

    # Work 70, 30
    total_files = len(list_files)
    training = int(total_files * 70 / 100)

    tr_features = list()
    tr_labels = list()

    eval_features = list()
    eval_labels = list()

    for index, file in enumerate(list_files):
        full_name = os.path.join(base_folder, file)

        image = cv2.imread(full_name)

        if image is None:
            raise Exception('Error reading image: {0}'.format(full_name))

        print('Processing image: {0}'.format(full_name))
        arr = instance_pose.recognize_image(image)

        # Selecting array for list of vectors
        person_array = []
        if len(arr) == 0:
            raise Exception('Invalid len for image {0}: {1}'.format(
                full_name, len(arr)))
        elif len(arr) == 1:
            person_array = arr[0]

            integrity = ClassUtils.check_vector_integrity_part(
                person_array, min_score)

            if not integrity:
                raise Exception(
                    'Invalid integrity for points in image: {0}'.format(
                        full_name))
        else:
            found = False
            for arr_index in arr:
                integrity = ClassUtils.check_vector_integrity_part(
                    arr_index, min_score)

                if integrity:
                    found = True
                    person_array = arr_index
                    break

            if not found:
                raise Exception(
                    'Cant find a valid person array for image {0}'.format(
                        full_name))

        results = ClassDescriptors.get_person_descriptors(
            person_array, min_score)

        if index < training:
            tr_features.append(results['angles'])
            tr_labels.append(label)
        else:
            eval_features.append(results['angles'])
            eval_labels.append(label)

    return tr_features, tr_labels, eval_features, eval_labels
コード例 #7
0
def main():
    print('Initializing main function')

    # Initializing instances
    instance_pose = ClassOpenPose()
    instance_net = ClassNN.load_from_params(model_dir)

    # Withdrawing list
    Tk().withdraw()

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

    if not dir_name:
        print('Directory not selected')
    else:
        # Loading images
        list_files = os.listdir(dir_name)
        list_files.sort()

        desc_list = list()

        for file in list_files:
            full_path = os.path.join(dir_name, file)

            print('Processing image {0}'.format(full_path))
            image = cv2.imread(full_path)
            arr = instance_pose.recognize_image(image)

            arr_pass = list()
            for person_arr in arr:
                if ClassUtils.check_vector_integrity_part(
                        person_arr, min_pose_score):
                    arr_pass.append(person_arr)

            if len(arr_pass) != 1:
                print('Invalid len {0} for image {1}'.format(
                    len(arr_pass), full_path))
                continue
            else:
                result_des = ClassDescriptors.get_person_descriptors(
                    arr_pass[0], min_pose_score)
                descriptor_arr = result_des['fullDesc']

                # Add descriptors to list
                desc_list.append(descriptor_arr)

        # Convert to numpy array
        print('Total poses: {0}'.format(len(desc_list)))

        # Transform list and predict
        desc_list_np = np.asarray(desc_list, dtype=np.float)
        print('ndim pose list: {0}'.format(desc_list_np.ndim))

        list_classes = list()
        predict_results = instance_net.predict_model_array(desc_list_np)
        for result in predict_results:
            list_classes.append(result['classes'])

        print('Predict results: {0}'.format(list_classes))
        print('Classes label: {0}'.format(instance_net.label_names))

        print('Done!')
コード例 #8
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!')
コード例 #9
0
def main():
    Tk().withdraw()

    instance_pose = ClassOpenPose()

    print('Initializing main function')
    list_files1 = os.listdir(base1_folder)
    hists_1 = read_hists(base1_folder, list_files1)

    list_files2 = os.listdir(base2_folder)
    hists_2 = read_hists(base2_folder, list_files2)

    cum_hists_1 = ClassDescriptors.get_cumulative_hists(hists_1)
    cum_hists_2 = ClassDescriptors.get_cumulative_hists(hists_2)

    # Processing img
    filename1 = '/home/mauricio/Pictures/Poses/bend_left/636453039344460032_1.jpg'
    filename2 = '/home/mauricio/Pictures/Poses/left_walk/636550795366450048_420.jpg'

    init_dir = '/home/mauricio/Pictures'
    options = {'initialdir': init_dir}
    filename1 = askopenfilename(**options)

    if not filename1:
        filename1 = '/home/mauricio/Pictures/2_1.jpg'

    ext1 = os.path.splitext(filename1)[1]
    if ext1 != '.jpg' and ext1 != '.jpeg':
        raise Exception('Extension1 is not jpg or jpeg')

    print('Loading filename 2')
    filename2 = askopenfilename(**options)

    if not filename2:
        filename2 = '/home/mauricio/Pictures/2_2.jpg'

    ext2 = os.path.splitext(filename2)[1]
    if ext2 != '.jpg' and ext2 != '.jpeg':
        raise Exception('Extension2 is not jpg or jpeg')

    image1 = cv2.imread(filename1)
    if image1 is None:
        raise Exception('Invalid image in filename {0}'.format(filename1))

    image2 = cv2.imread(filename2)
    if image2 is None:
        raise Exception('Invalid image in filename {0}'.format(filename2))

    is_json = True
    new_file_1 = filename1.replace('.jpeg', '.json')
    new_file_1 = new_file_1.replace('.jpg', '.json')

    new_file_2 = filename2.replace('.jpeg', '.json')
    new_file_2 = new_file_2.replace('.jpg', '.json')

    if not os.path.exists(new_file_1):
        print('File not found: {0}'.format(new_file_1))
        is_json = False

    if not os.path.exists(new_file_2):
        print('File not found: {0}'.format(new_file_2))
        is_json = False

    if not is_json:
        poses1 = instance_pose.recognize_image(image1)
        poses2 = instance_pose.recognize_image(image2)

        if len(poses1) != 1:
            raise Exception('Invalid len for pose1: {0}'.format(len(poses1)))
        if len(poses2) != 1:
            raise Exception('Invalid len for pose2: {0}'.format(len(poses2)))
        if not ClassUtils.check_vector_integrity_pos(poses1[0], min_score):
            raise Exception('Pose 1 not valid')
        if not ClassUtils.check_vector_integrity_pos(poses2[0], min_score):
            raise Exception('Pose 2 not valid')

        pose1 = poses1[0]
        pose2 = poses2[0]
    else:
        with open(new_file_1, 'r') as f:
            obj_json1 = json.loads(f.read())

        with open(new_file_2, 'r') as f:
            obj_json2 = json.loads(f.read())

        pose1 = obj_json1['vector']
        pose2 = obj_json2['vector']

        if not ClassUtils.check_vector_integrity_pos(pose1, min_score):
            raise Exception('Pose 1 not valid')
        if not ClassUtils.check_vector_integrity_pos(pose2, min_score):
            raise Exception('Pose 2 not valid')

    # Plotting
    # plot_histograms(hists1)

    # Showing first images without transformation
    cv2.namedWindow('main_window', cv2.WINDOW_AUTOSIZE)

    upper1, lower1 = ClassDescriptors.process_colors_person(pose1,
                                                            min_score,
                                                            image1,
                                                            decode_img=False)
    upper2, lower2 = ClassDescriptors.process_colors_person(pose2,
                                                            min_score,
                                                            image2,
                                                            decode_img=False)

    print('Upper1 {0} - Lower1 {0}'.format(upper1, lower1))
    print('Upper2 {0} - Lower2 {0}'.format(upper2, lower2))

    print('Diff1: {0}'.format(ClassUtils.get_color_diff_rgb(upper1, upper2)))
    print('Diff2: {0}'.format(ClassUtils.get_color_diff_rgb(lower1, lower2)))

    cv2.imshow('main_window', np.hstack((image1, image2)))
    print('Press any key to continue')
    cv2.waitKey(0)

    # Perform image transformation
    image_tr = ClassDescriptors.transform_image(image2, cum_hists_2,
                                                cum_hists_1)
    upper1, lower1 = ClassDescriptors.process_colors_person(pose1,
                                                            min_score,
                                                            image1,
                                                            decode_img=False)
    upper2, lower2 = ClassDescriptors.process_colors_person(pose2,
                                                            min_score,
                                                            image_tr,
                                                            decode_img=False)

    print('Diff1: {0}'.format(ClassUtils.get_color_diff_rgb(upper1, upper2)))
    print('Diff2: {0}'.format(ClassUtils.get_color_diff_rgb(lower1, lower2)))

    cv2.imshow('main_window', np.hstack((image1, image_tr)))
    print('Press any key to continue')
    cv2.waitKey(0)

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