コード例 #1
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!')
コード例 #2
0
def process_single():
    print('Initializing process_single')

    # Loading instances
    instance_pose = ClassOpenPose()

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

    if not filename:
        filename = '/home/mauricio/Pictures/Poses/left_walk/636550788328999936_420.jpg'

    # Reading video extension
    extension = os.path.splitext(filename)[1]

    if extension != '.jpg' and extension != '.jpeg':
        raise Exception('Extension is not jpg or jpeg')

    # Opening filename
    image = cv2.imread(filename)

    # Loading image and array
    arr, processed_img = instance_pose.recognize_image_tuple(image)

    # Showing image to generate elements
    cv2.namedWindow('main_window', cv2.WND_PROP_AUTOSIZE)

    arr_pass = list()
    min_score = 0.05

    # 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_pos(elem, min_score):
            arr_pass.append(elem)

    if len(arr_pass) != 1:
        print('There is more than one person in the image')
        cv2.imshow('main_window', processed_img)
        cv2.waitKey(0)
    else:
        person_array = arr_pass[0]
        print('Person array: {0}'.format(person_array))
        generate_descriptors(person_array, image, processed_img, min_score)
コード例 #3
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!')
コード例 #4
0
    def load_pose_descriptors(cls, type_desc: EnumDesc):
        training_data = list()
        training_labels = list()
        training_files = list()
        eval_data = list()
        eval_labels = list()
        eval_files = list()

        classes_number = 0

        if type_desc == EnumDesc.ANGLES or type_desc == EnumDesc.ANGLES_TRANSFORMED:
            data_folder = cls.list_folder_data_angles
        else:
            data_folder = cls.list_folder_data

        cont = True
        while cont:
            cont = False

            for folder_data in data_folder:
                if folder_data[2] == classes_number:
                    classes_number += 1
                    cont = True
                    break

        # Iterate folder
        for index, item in enumerate(data_folder):
            folder = item[0]
            min_score = item[1]
            label = item[2]

            list_files = os.listdir(folder)
            random.Random(cls.seed).shuffle(list_files)

            total_train = int(len(list_files)) * 70 / 100

            for num_file, file in enumerate(list_files):
                full_path = os.path.join(folder, file)

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

                with open(full_path, 'r') as text_file:
                    arr_json = text_file.read()

                params = json.loads(arr_json)
                vectors = params['vectors']
                angles = params['angles']
                transformed_points = params['transformedPoints']

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

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

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

                # Fill training and eval list
                # Use angles and position information
                data_to_add = cls._get_descriptor_list(angles,
                                                       transformed_points,
                                                       type_desc)
                if num_file < total_train:
                    training_data.append(data_to_add)
                    training_labels.append(label)
                    training_files.append(full_path)
                else:
                    eval_data.append(data_to_add)
                    eval_labels.append(label)
                    eval_files.append(full_path)

        # Convert data to numpy array
        training_data_np = np.asanyarray(training_data, dtype=np.float)
        training_labels_np = np.asanyarray(training_labels, dtype=int)
        print(training_labels_np)

        eval_data_np = np.asanyarray(eval_data, dtype=np.float)
        eval_labels_np = np.asanyarray(eval_labels, dtype=int)
        print(eval_labels_np)

        training_files_np = np.asanyarray(training_files, dtype=np.str)
        eval_files_np = np.asanyarray(eval_files, dtype=np.str)

        # Getting label_names
        label_names = []
        for folder, _, label in data_folder:
            names = folder.split('/')
            label_name = names[-1]

            # Check if last character is /
            if len(label_name) == 0:
                label_names = names[-2]

            label_names.append((label_name, label))

        print('Total training: {0}'.format(len(training_labels)))
        print('Total eval: {0}'.format(len(eval_labels)))
        print('Shape training: {0}'.format(training_data_np.shape))
        print('Shape eval: {0}'.format(eval_data_np.shape))
        print('Classes number: {0}'.format(classes_number))

        results = {
            'trainingData': training_data_np,
            'trainingLabels': training_labels_np,
            'evalData': eval_data_np,
            'evalLabels': eval_labels_np,
            'trainingFiles': training_files_np,
            'evalFiles': eval_files_np,
            'labelNames': label_names,
            'classesNumber': classes_number
        }

        return results
コード例 #5
0
def main():
    print('Initializing main function')
    Tk().withdraw()

    # filename1 = '/home/mauricio/Pictures/Poses/temp/636550787632290048_419.jpg'
    # filename2 = '/home/mauricio/Pictures/Poses/walk_front/636550801813440000_424.jpg'
    filename1 = '/home/mauricio/Pictures/Poses/bend_left/636453039344460032_1.jpg'
    filename2 = '/home/mauricio/Pictures/Poses/left_walk/636550795366450048_420.jpg'

    print('Select first file')
    
    init_dir = '/home/mauricio/Pictures/Poses'
    options = {'initialdir': init_dir}
    filename1_tmp = askopenfilename(**options)

    if filename1_tmp:
        filename1 = filename1_tmp
    
    print('File selected: {0}'.format(filename1))
    print('Select second file')
    filename2_tmp = askopenfilename(**options)

    if filename2_tmp:
        filename2 = filename2_tmp
    
    print('File selected: {0}'.format(filename2))
    img1 = cv2.imread(filename1)
    img2 = cv2.imread(filename2)

    instance_pose = ClassOpenPose()
    vectors1, img_draw1 = instance_pose.recognize_image_tuple(img1)
    vectors2, img_draw2 = instance_pose.recognize_image_tuple(img2)

    if len(vectors1) != 1:
        raise Exception('Invalid len for vector 1')

    if len(vectors2) != 1:
        raise Exception('Invalid len for vector 2')

    person_vector1 = vectors1[0]
    person_vector2 = vectors2[0]

    min_percent = 0.05

    if not ClassUtils.check_vector_integrity_pos(person_vector1, min_percent):
        raise Exception('Invalid integrity for vector 1')

    if not ClassUtils.check_vector_integrity_pos(person_vector2, min_percent):
        raise Exception('Invalid integrity for vector 2')

    colors1 = ClassDescriptors.process_colors(vectors1, min_percent, img1, decode_img=False)
    colors2 = ClassDescriptors.process_colors(vectors2, min_percent, img2, decode_img=False)

    upper1 = colors1[0][0]
    lower1 = colors1[1][0]
    color_diff1 = ClassUtils.get_color_diff_rgb(upper1, lower1)

    upper2 = colors2[0][0]
    lower2 = colors2[1][0]
    color_diff2 = ClassUtils.get_color_diff_rgb(upper2, lower2)

    diff_upper = ClassUtils.get_color_diff_rgb(upper1, upper2)
    diff_lower = ClassUtils.get_color_diff_rgb(lower1, lower2)
    diff_diff = math.fabs(color_diff1 - color_diff2)

    print('Diff upper: {0}'.format(diff_upper))
    print('Diff lower: {0}'.format(diff_lower))
    print('Diff diffs: {0}'.format(diff_diff))

    cv2.namedWindow('main_window', cv2.WND_PROP_AUTOSIZE)
    cv2.imshow('main_window', np.hstack((img_draw1, img_draw2)))

    cv2.waitKey(0)
    cv2.destroyAllWindows()

    print('Done!')
コード例 #6
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!')
コード例 #7
0
def main():
    print('Initializing main function')
    print('Folder selection')

    folder_images = '/home/mauricio/PosesProcessed/folder_images'
    folder_images_draw = '/home/mauricio/PosesProcessed/folder_images_draw'

    Tk().withdraw()

    # Getting options
    init_dir = '/home/mauricio/Videos/Oviedo'
    options = {'initialdir': init_dir}
    dir_name = askdirectory(**options)

    if not dir_name:
        raise Exception('Directory not selected')

    # Create directory if does not exists
    if not os.path.isdir(folder_images):
        os.makedirs(folder_images)

    # Create directory if does not exists
    if not os.path.isdir(folder_images_draw):
        os.makedirs(folder_images_draw)

    # Initializing openpose instance
    instance_pose = ClassOpenPose()

    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(full_path)

            if extension == '.mjpeg':
                file_info = ClassMjpegReader.process_video(full_path)
            else:
                print('Extension ignored: {0}'.format(extension))
                continue

            # Getting idcam from file
            id_cam = full_path.split('/')[-2]
            print('IdCam: {0}'.format(id_cam))

            for index, info in enumerate(file_info):
                print('Processing {0} of {1} from {2}'.format(index, len(file_info), full_path))
                frame = info[0]
                ticks = info[1]

                image_np = np.frombuffer(frame, dtype=np.uint8)
                image = cv2.imdecode(image_np, cv2.IMREAD_ANYCOLOR)

                arr, image_draw = instance_pose.recognize_image_tuple(image)
                min_score = 0.05

                arr_pass = list()
                for elem in arr:
                    if ClassUtils.check_vector_integrity_pos(elem, min_score):
                        arr_pass.append(elem)

                if len(arr_pass) > 0:
                    # Draw rectangles for all candidates
                    for person_arr in arr_pass:
                        pt1, pt2 = ClassUtils.get_rectangle_bounds(person_arr, min_score)
                        cv2.rectangle(image_draw, pt1, pt2, (0, 0, 255), 5)

                    # Overwriting 1
                    full_path_images = os.path.join(folder_images, '{0}_{1}.jpg'.format(ticks, id_cam))
                    print('Writing image {0}'.format(full_path_images))
                    cv2.imwrite(full_path_images, image)

                    # Overwriting 2
                    full_path_draw = os.path.join(folder_images_draw, '{0}_{1}.jpg'.format(ticks, id_cam))
                    print('Writing image {0}'.format(full_path_images))
                    cv2.imwrite(full_path_draw, image_draw)

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

    # Prompt for user input
    cam_number_str = input('Insert camera number to process: ')
    cam_number = int(cam_number_str)

    # Open video from opencv
    cap = cv2.VideoCapture(cam_number)

    # Initializing open pose distance
    instance_pose = ClassOpenPose()

    # Initializing variables
    model_dir = '/home/mauricio/models/nn_classifier'
    instance_nn = ClassNN.load_from_params(model_dir)

    while True:
        # Capture frame-by-frame
        ret, frame = cap.read()

        # Processing frame with openpose
        arr, frame = instance_pose.recognize_image_tuple(frame)

        # Check if there is one frame with vector integrity
        arr_pass = list()

        min_score = 0.05
        # 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_pos(elem, min_score):
                arr_pass.append(elem)

        if len(arr_pass) != 1:
            print('Invalid len for arr_pass: {0}'.format(arr_pass))
        else:
            person_array = arr_pass[0]

            # Getting person descriptors
            results = ClassDescriptors.get_person_descriptors(
                person_array, min_score)

            # Descriptors
            data_to_add = results['angles']
            data_to_add += ClassUtils.get_flat_list(
                results['transformedPoints'])

            data_np = np.asanyarray(data_to_add, dtype=np.float)

            # Getting result predict
            result_predict = instance_nn.predict_model(data_np)
            detected_class = result_predict['classes']

            label_class = get_label_name(instance_nn.label_names,
                                         detected_class)
            print('Detected: {0} - Label: {1}'.format(detected_class,
                                                      label_class))

            # Draw text class into image - Evaluation purposes
            font = cv2.FONT_HERSHEY_SIMPLEX
            font_scale = 0.6
            font_color = (255, 255, 255)
            line_type = 2

            cv2.putText(frame, '{0}'.format(label_class), (0, 0), font,
                        font_scale, font_color, line_type)

        # Display the resulting frame
        cv2.imshow('frame', frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    # When everything done, release the capture
    cap.release()
    cv2.destroyAllWindows()

    print('Done!')