def main():
    try:
        # Starting OpenPose
        opWrapper = op.WrapperPython()
        opWrapper.configure(params)
        opWrapper.start()

        # Read frames on directory
        imagePaths = op.get_images_on_directory(args[0].image_dir)
        start = time.time()

        # Process and display images
        for imagePath in imagePaths:
            datum = op.Datum()
            imageToProcess = cv2.imread(imagePath)
            datum.cvInputData = imageToProcess
            opWrapper.emplaceAndPop([datum])

            print("Body keypoints: \n" + str(datum.poseKeypoints))

            #cv2.imshow("OpenPose 1.4.0 - Tutorial Python API", datum.cvOutputData)
            cv2.imwrite("/home/lburget/Pictures/Webcam/test.jpg",
                        datum.cvOutputData)

        end = time.time()
        print("OpenPose demo successfully finished. Total time: " +
              str(end - start) + " seconds")
    except Exception as e:
        print(e)
示例#2
0
            if key not in params: params[key] = "1"
        elif "--" in curr_item and "--" not in next_item:
            key = curr_item.replace('-', '')
            if key not in params: params[key] = next_item

    # Construct it from system arguments
    # op.init_argv(args[1])
    # oppython = op.OpenposePython()

    # Starting OpenPose
    opWrapper = op.WrapperPython()
    opWrapper.configure(params)
    opWrapper.start()

    # Read frames on directory
    imagePaths = op.get_images_on_directory(args[0].image_dir)
    start = time.time()

    # Process and display images
    for imagePath in imagePaths:
        datum = op.Datum()
        imageToProcess = cv2.imread(imagePath)
        datum.cvInputData = imageToProcess
        opWrapper.emplaceAndPop([datum])

        print("Body keypoints: \n" + str(datum.poseKeypoints))

        if not args[0].no_display:
            cv2.imshow("OpenPose 1.5.1 - Tutorial Python API",
                       datum.cvOutputData)
            key = cv2.waitKey(15)
def extract_dataset_features(dataset,
                             isExtractRoiHogBitmap= False,
                             isDisplayImageWithROIs= False):
    #
    # extract keypoints features and HOG features from dataset
    # input:
    #   dataset - the path of dataset for features extracting
    # output:
    #   void - all features data are saved to disk
    #

    try:
        # region initialization for OpenPose to run
        # code from OpenPose library tutorial

        # Import Openpose (Windows/Ubuntu/OSX)
        dir_path = os.path.dirname(os.path.abspath(__file__))
        try:
            # Windows Import
            if platform == "win32":
                # Change these variables to point to the correct folder (Release/x64 etc.)
                sys.path.append(dir_path + '/dependencies/openpose/libs');
                os.environ['PATH'] = os.environ['PATH'] + ';' + dir_path + '/dependencies/openpose/dlls;'
                import pyopenpose as op
            else:
                # Change these variables to point to the correct folder (Release/x64 etc.)
                sys.path.append('../../python');
                # If you run `make install` (default path is `/usr/local/python` for Ubuntu), you can also access the OpenPose/python module from there. This will install OpenPose and the python library at your desired installation path. Ensure that this is in your python path in order to use it.
                # sys.path.append('/usr/local/python')
                from openpose import pyopenpose as op
        except ImportError as e:
            print(
                'Error: OpenPose library could not be found. Did you enable `BUILD_PYTHON` in CMake and have this Python script in the right folder?')
            raise e

        # Custom Params (refer to include/openpose/flags.hpp for more parameters)
        params = dict()
        params["model_folder"] = "./dependencies/openpose/models/"
        params["net_resolution"] = "320x176"

        # Flags
        parser = argparse.ArgumentParser()
        args = parser.parse_known_args()
        # parse other params passed
        for i in range(0, len(args[1])):
            curr_item = args[1][i]
            if i != len(args[1]) - 1:
                next_item = args[1][i + 1]
            else:
                next_item = "1"
            if "--" in curr_item and "--" in next_item:
                key = curr_item.replace('-', '')
                if key not in params:  params[key] = "1"
            elif "--" in curr_item and "--" not in next_item:
                key = curr_item.replace('-', '')
                if key not in params: params[key] = next_item

        # Starting OpenPose
        opWrapper = op.WrapperPython()
        opWrapper.configure(params)
        opWrapper.start()
        # endregion

        # get all training poses as sub-folders of train dataset
        categories = [f.name for f in os.scandir(join(dir_path, dataset)) if f.is_dir()]

        if len(categories) == 0:
            parent, category = os.path.split(dataset)
            dataset = parent
            categories = [category]

        for category in categories:
            # Get key-points features and image name and size from train dataset
            # declare variables hold features and labels
            record_details = []

            # build category path
            cat_path = join(dir_path, dataset, category)

            # create ROI and HOG directory
            roi_and_hog_path = ""
            if isExtractRoiHogBitmap:
                roi_and_hog_path = join(cat_path, "RoI-HOG")
                if not os.path.exists(roi_and_hog_path):
                    os.mkdir(roi_and_hog_path)

            # get all images from the category
            image_paths = op.get_images_on_directory(cat_path)

            # process all images
            for image_path in image_paths:
                print("\nCurrent image: " + image_path + "\n")

                # get image full name (with file extension)
                _, image_full_name = os.path.split(image_path)
                # image name without extension
                image_name = image_full_name[0:image_full_name.rindex('.')]

                # processed by OpenPose
                datum = op.Datum()
                imageToProcess = cv2.imread(image_path)
                height, width, channels = imageToProcess.shape
                datum.cvInputData = imageToProcess
                opWrapper.emplaceAndPop([datum])

                # print("Body keypoints: \n" + str(datum.poseKeypoints))

                # check if exists pose key points data
                if not (datum.poseKeypoints.size < 2):
                    # merging bounding boxes if applicable
                    merged_poseKeypoints = bounding_box_merging(datum.poseKeypoints,
                                            ratio_of_intersec_thresh= 0.36,
                                            ratio_of_distance_thresh= 2)
                    # assign merged_poseKeypoints to datum.poseKeypoints array
                    datum.poseKeypoints = merged_poseKeypoints

                    # an array to save mixed features (keypoints and HOG) of an image
                    keyPoint_HOG_feats_arrs = []
                    if datum.poseKeypoints.size > 1 and datum.poseKeypoints.ndim == 3:
                        # region extract keypoints features
                        keypoints_coordinates = datum.poseKeypoints[:, :, :2]

                        # TODO: 3. after merging with new keypoints sets, these coordinates are translated to their center and scaled by their box size -> added in keypoints features
                        # translate and scale keypoints by its center and box size
                        flatted_features = normalize_keypoint_by_its_bounding_box(keypoints_coordinates)
                        # image_size_in_str = str(width) + 'x' + str(height)
                        for row in flatted_features:
                            keyPoint_HOG_feats_arrs.append(row)
                        # endregion

                        # TODO: 4. RoI and HOG features are extracted -> added in HOG features
                        #region extract HOG features
                        extracted_hog_feats_arrs = \
                            extract_ROI_and_HOG_feature(datum,
                                                        image_name,
                                                        roi_and_hog_path,
                                                        isDisplayImageWithROIs=isDisplayImageWithROIs,
                                                        isExtractRoiHogBitmap=isExtractRoiHogBitmap)

                        for idx in range(0,len(extracted_hog_feats_arrs)):
                            keyPoint_HOG_feats_arrs[idx] = np.append(keyPoint_HOG_feats_arrs[idx],
                                                                extracted_hog_feats_arrs[idx])
                            # keyPoint_HOG_feats_arrs[idx] = np.append(keyPoint_HOG_feats_arrs[idx],
                            #                                          [image_full_name,image_size_in_str])
                        #endregion

                        # add features data to accumulate array
                        for row in keyPoint_HOG_feats_arrs:
                            record_details.append(row)

            # save merged, normalized features to disk
            if len(record_details) > 0:
                data_to_write = pd.DataFrame(record_details)
                data_to_write.to_csv(join(cat_path, category + '.csv'))

    except Exception as e:
        print(e)
        sys.exit(-1)