示例#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!')
def do_color_analysis():
    print('Initializing main function')

    print('HSV analysis')
    pt1 = np.array([[[129, 120, 116]]], dtype=np.uint8)
    pt2 = np.array([[[99, 101, 101]]], dtype=np.uint8)

    diff = ClassUtils.get_color_diff_rgb([129, 120, 116], [99, 101, 101])
    print(diff)

    hsv1 = cv2.cvtColor(pt1, cv2.COLOR_BGR2HSV)
    hsv2 = cv2.cvtColor(pt2, cv2.COLOR_BGR2HSV)

    hsv2[0, 0, 2] = 129
    pt_con = cv2.cvtColor(hsv2, cv2.COLOR_HSV2BGR)
    print(pt1)
    print(pt2)
    print(pt_con)

    diff = ClassUtils.get_color_diff_rgb(to_rgb_arr(pt1), to_rgb_arr(pt_con))
    print(diff)

    print('LAB analysis')
    lab1 = cv2.cvtColor(pt1, cv2.COLOR_BGR2LAB)
    lab2 = cv2.cvtColor(pt2, cv2.COLOR_BGR2LAB)

    lab2[0, 0, 0] = 129
    pt_con = cv2.cvtColor(lab2, cv2.COLOR_LAB2BGR)
    print(pt1)
    print(pt2)
    print(pt_con)

    diff = ClassUtils.get_color_diff_rgb(to_rgb_arr(pt1), to_rgb_arr(pt_con))
    print(diff)
    def get_people_diff(cls, person1: 'ClassPeopleReId',
                        person2: 'ClassPeopleReId'):
        # Comparing people between list
        diff_upper = ClassUtils.get_color_diff_rgb(person1.color_upper,
                                                   person2.color_upper)
        diff_lower = ClassUtils.get_color_diff_rgb(person1.color_lower,
                                                   person2.color_lower)

        diff_colors_1 = ClassUtils.get_color_diff_rgb(person1.color_upper,
                                                      person1.color_lower)
        diff_colors_2 = ClassUtils.get_color_diff_rgb(person2.color_upper,
                                                      person2.color_lower)

        diff_colors = math.fabs(diff_colors_1 - diff_colors_2)

        diff_k_means = ClassDescriptors.get_kmeans_diff(
            person1.hist_pose, person2.hist_pose)

        distance = cls.get_person_distance(person1, person2)

        return_data = {
            'diffUpper': diff_upper,
            'diffLower': diff_lower,
            'diffColors': diff_colors,
            'distance': distance,
            'diffKMeans': diff_k_means
        }

        return return_data
示例#4
0
def print_points(point1, point2):
    if ClassUtils.check_point_integrity(point1, min_score) and \
            ClassUtils.check_point_integrity(point2, min_score):

        print('Point1: {0} - Point2: {1}'.format(point1, point2))
        distance_plumb = ClassUtils.get_euclidean_distance_pt(point1, point2)
        print('Distance plumb: {0}'.format(distance_plumb))
def process_btf():
    print('Processing BTF transformation')

    # Loading instances
    instance_pose = ClassOpenPose()
    image1, image2, pose1, pose2 = ClassDescriptors.load_images_comparision(
        instance_pose, min_score)

    hists1 = ClassDescriptors.get_color_histograms(pose1,
                                                   min_score,
                                                   image1,
                                                   decode_img=False)
    hists2 = ClassDescriptors.get_color_histograms(pose2,
                                                   min_score,
                                                   image2,
                                                   decode_img=False)

    # 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('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, hists2, hists1)
    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!')
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!')
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
    def load_frame(self, date: datetime):
        logger.debug(self._frame_date)

        logger.debug('Get frame to load')

        date_file = ClassUtils.get_date_file(date)

        # Check fist if date is in list
        found = False

        for frame_item in self._frame_info_list:
            date_video = frame_item['date_video']
            if date_video == date_file:
                found = True
                self._frame_date = date_file
                self._frame_info = frame_item['frame_info']
                break

        if not found:
            logger.debug('Loading file name')
            logger.debug(date_file)
            file_path = ClassUtils.load_path_by_date(date_file,
                                                     self._cam_number,
                                                     self._extension)

            if not path.exists(file_path):
                # Avoid exceptions - Video partial
                print('Path does not exists {0}'.format(file_path))
                frame_info = []
                self._frame_info = frame_info
                self._frame_date = date_file

                if self._last_frame is None:
                    raise Exception('Fist video must exist!')

                self._frame_info_list.append({
                    'date_video': date_file,
                    'frame_info': frame_info
                })
            else:
                logger.debug('Loading video from path')
                frame_info = ClassMjpegReader.process_video_mjpegx(file_path)

                self._frame_info = frame_info
                self._frame_date = date_file

                logger.debug('Frame counter: {0}'.format(len(frame_info)))

                # Must be true -> First frame to send -> Avoid exceptions
                if self._last_frame is None:
                    self._last_frame = frame_info[0]

                self._frame_info_list.append({
                    'date_video': date_file,
                    'frame_info': frame_info
                })

        return self._load_frame_by_date(date)
示例#9
0
    def merge_list():
        global done
        done = True
        for i in range(len(list_new_people) - 1):
            person1 = list_new_people[i]

            list_candidate_merge = []
            for j in range(i + 1, len(list_new_people)):
                person2 = list_new_people[j]

                # To do merging
                # People must be in different cameras
                if person1.cam_number != person2.cam_number:
                    return_data = ClassPeopleReId.get_people_diff(
                        person1, person2)

                    if return_data['distance'] <= 120 and return_data[
                            'diffUpper'] < 40 and return_data['diffLower'] < 40:
                        list_candidate_merge.append({
                            'person': person2,
                            'data': return_data
                        })

            if len(list_candidate_merge) > 0:
                # Iterate over elements to get the elem with the min distance
                min_diff = -1
                person_merge = None

                for candidate in list_candidate_merge:
                    return_data = candidate['data']
                    total_diff = return_data['diffUpper'] + return_data[
                        'diffLower']

                    if min_diff == -1 or total_diff < min_diff:
                        min_diff = total_diff
                        person_merge = candidate['person']

                # Checking what candidate hast the most valid points
                print('Merging {0} {1}'.format(person1.global_pos,
                                               person_merge.global_pos))
                valid1 = ClassUtils.check_valid_vector_points(
                    person1.vectors, min_score)
                valid2 = ClassUtils.check_valid_vector_points(
                    person_merge.vectors, min_score)

                if valid1 >= valid2:
                    list_new_people.remove(person_merge)
                else:
                    list_new_people.remove(person1)

                done = False
                break
            else:
                continue

        return done
示例#10
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 change_extension(self):
        # Change default path

        ext1 = '.mjpeg_tmp'
        ext2 = '.mjpeg'

        path_file1 = ClassUtils.load_path_by_date(self.current_date_file, self.cam_number, ext1)
        path_file2 = ClassUtils.load_path_by_date(self.current_date_file, self.cam_number, ext2)

        if not os.path.exists(path_file1):
            print('Path 1 does not exist')
        else:
            os.rename(path_file1, path_file2)
示例#12
0
def main():
    print('Initializing main function')

    date = datetime(2018, 2, 1, 12, 0, 0, 4563)
    print('Date: {0}'.format(date))
    ticks = ClassUtils.datetime_to_ticks(date)

    print('Ticks: {0}'.format(ticks))

    new_date = ClassUtils.ticks_to_datetime(ticks)
    print('New datetime: {0}'.format(new_date))

    print('Done!')
示例#13
0
def main():
    print('Initializing main function')
    list1 = [359, 0, 1]

    mean = ClassUtils.compute_mean_circular_deg(list1)
    print('Mean list 1: {0}'.format(mean))

    list1 = [180, 90, 1]
    mean = ClassUtils.compute_mean_circular_deg(list1)
    print('Mean list 1: {0}'.format(mean))

    list_rad = [math.pi, 3 * math.pi / 2, 0]
    mean = ClassUtils.compute_mean_circular(list_rad)
    print('Mean list rad: {0}'.format(mean))
示例#14
0
def calculate_poses(option: Option, nn_classifier: ClassNN, svm_classifier: ClassSVM):
    print('Calculating poses using nn')

    # Recalculate all poses and get confidence
    for classInfo in list_classes:
        folder = classInfo['folderPath']
        for root, _, files in os.walk(folder):
            for file in files:
                full_path = os.path.join(root, file)
                ext = ClassUtils.get_filename_extension(full_path)

                if '_rawdata' in file and ext == '.json':
                    print('Processing file: {0}'.format(full_path))

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

                    file_json = json.loads(file_txt)
                    list_poses = file_json['listPoses']

                    for pose in list_poses:
                        angles = pose['angles']
                        transformed_points = pose['transformedPoints']

                        # Using transformed points and angles
                        list_desc = list()
                        list_desc += ClassUtils.get_flat_list(transformed_points)

                        # Convert to numpy
                        list_desc_np = np.asanyarray(list_desc, np.float)

                        if option == Option.NN:
                            result = nn_classifier.predict_model_fast(list_desc_np)
                        else:
                            result = svm_classifier.predict_model(list_desc_np)

                        pose['class'] = int(result['classes'])
                        pose['probabilities'] = result['probabilities'].tolist()

                    # Writing again into file
                    file_txt = json.dumps(file_json, indent=4)
                    new_full_path = ClassUtils.change_ext_training(full_path,
                                                                   '{0}_posedata'.format(option.value))

                    with open(new_full_path, 'w') as f:
                        f.write(file_txt)

                    # Done

    print('Done processing elements')
    def _load_frame_by_date(self, date: datetime):
        """
        Assume that frames are organized
        """
        min_delta = -1
        logger.debug('Loading frame by date')
        found = False

        # Improves processing time
        self._last_index += 1
        selected_frame = None

        if self._last_index < len(self._frame_info):
            frame = self._frame_info[self._last_index]
            ticks = frame[1]

            date_frame = ClassUtils.ticks_to_datetime(ticks)
            delta = (date - date_frame).seconds

            if 0 <= delta <= 0.5:
                selected_frame = frame
                found = True

        # Iterate over all elems
        if selected_frame is None:
            for index, frame in enumerate(self._frame_info):
                ticks = frame[1]

                date_frame = ClassUtils.ticks_to_datetime(ticks)
                delta = (date - date_frame).total_seconds()

                if min_delta == -1 or delta < min_delta:
                    if 0 <= delta <= 0.5:
                        min_delta = delta
                        selected_frame = frame
                        self._last_index = index
                        found = True

        # Get last if not found
        if selected_frame is None:
            selected_frame = self._last_frame
            found = False

        # Add found variable to dir
        selected_frame[2]['found'] = found

        self._last_frame = selected_frame
        return selected_frame
def cnn_reprocess_images():
    print('Re processing images')
    list_folders = list()
    list_folders.append(ClassUtils.cnn_class_folder)

    # Loading instances
    instance_nn = ClassNN(ClassNN.model_dir_pose, classes_number, hidden_layers)

    # File walk
    count = 0
    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 file: {0}'.format(full_path))

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

                    json_data = json.loads(json_txt)
                    list_poses = json_data['listPoses']

                    for pose in list_poses:
                        angles = pose['angles']
                        transformed_points = pose['transformedPoints']

                        list_desc = list()
                        list_desc += angles
                        list_desc += ClassUtils.get_flat_list(transformed_points)

                        list_desc_np = np.asanyarray(list_desc, dtype=np.float)

                        res = instance_nn.predict_model_fast(list_desc_np)
                        pose['keyPose'] = int(res['classes'])
                        pose['probability'] = 1

                    # Writing data again
                    data_txt = json.dumps(json_data, indent=2)
                    with open(full_path, 'w') as f:
                        f.write(data_txt)

                    count += 1

    print('Done')
    print('Total files processed: {0}'.format(count))
    def load_people_from_frame_info(cls, frame_info, date_ref):
        list_people = list()

        params = frame_info[2]['params']

        # Deep copy params to avoid problems
        params_cpy = copy.deepcopy(params)

        found = frame_info[2]['found']

        ticks = ClassUtils.datetime_to_ticks(date_ref)
        counter = 0
        for param in params_cpy:
            cam_number = param['camNumber']
            integrity = param['integrity']
            gpx = param['globalPosition'][0]
            gpy = param['globalPosition'][1]

            # Add elements using valid skeletons
            # Ignore skeletons marked with only pos element
            if integrity and found and 600 >= gpx >= 0 and 698 >= gpy >= -450:
                person_guid = '{0}_{1}_{2}'.format(cam_number, counter, ticks)
                list_people.append(
                    cls(param, date_ref, _person_guid=person_guid))
                counter += 1

        return list_people
示例#18
0
def main():
    global dict_config

    print('Initializing main function')
    text = input('Insert camera number: ')

    dict_config = ClassUtils.load_cam_calib_params(text)

    print('Opening file')

    # Problems in windows version
    # Tk().withdraw()

    print('Generating elements in list')

    init_dir = '/home/mauricio/Oviedo/CameraCalibration/' + text
    if platform == 'win32':
        init_dir = 'C:\\SharedFTP\\CameraCalibration\\' + text

    options = {'initialdir': init_dir}
    filename = askopenfilename(**options)

    if not filename:
        print('File not selected')
    else:
        print('Loading file ' + filename)

        image = cv2.imread(filename)

        if image is None:
            print('Cant read image ' + filename)
        else:
            show_image(image)
def calc_error_prop():
    # Calculating error propagation
    # Points obtained from file /home/mauricio/CSV/err_calc.ods

    list_image_points = np.array([[171, 96.93], [440.3, 91.73],
                                  [32.76, 453.43], [590.23, 447.53]])

    list_obj_points = np.array([[-125, 750], [125, 750], [-125, 250],
                                [125, 250]])

    mat, _ = cv2.findHomography(list_image_points, list_obj_points)
    print('Homography matrix: {0}'.format(mat))

    for index in range(list_image_points.shape[0]):
        point_des = 4
        image_point = list_image_points[index]
        image_point[0] += point_des
        image_point[1] += point_des

        print('Image point: {0}'.format(image_point))

        obj_point = list_obj_points[index]
        proj_point = ClassUtils.project_points(mat, image_point)

        delta_x = proj_point[0] - obj_point[0]
        delta_y = proj_point[1] - obj_point[1]

        print('Projected point: {0}'.format(proj_point))
        print('DeltaX: {0} - DeltaY: {1}'.format(delta_x, delta_y))

    print('Done!')
    def update_values_from_person(self, person, date_ref):
        self.person_param = person.person_param
        self.last_date = date_ref

        ticks = ClassUtils.datetime_to_ticks(date_ref)
        self._add_pose_to_list(ticks, person.person_param)
        self.update_counter = 0
def create_cnn_image_pose(param, instance_nn):
    list_poses = param['listPoses']
    max_confidence = 1

    image_height = cnn_image_height
    image_width = len(list_poses)

    image_np = np.zeros((image_height, image_width), dtype=np.uint8)

    for index_pose, pose in enumerate(list_poses):
        angles = pose['angles']
        transformed_points = pose['transformedPoints']

        list_desc = list()
        list_desc += angles
        list_desc += ClassUtils.get_flat_list(transformed_points)

        list_desc_np = np.asanyarray(list_desc, dtype=np.float)

        res = instance_nn.predict_model_fast(list_desc_np)

        probabilities = res['probabilities']
        for index, value in enumerate(probabilities):
            pixel_value = int(value * 255 / max_confidence)
            image_np[index, index_pose] = pixel_value

    # Resizing image
    image_res = cv2.resize(image_np, (cnn_image_height, cnn_image_width))
    return image_res
def test_full_color_compare():
    print('Process lab adjustment')

    # Loading instances
    instance_pose = ClassOpenPose()

    # Loading images
    image1, image2, pose1, pose2 = ClassDescriptors.load_images_comparision(
        instance_pose, min_score)

    # Performing color comparision
    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)

    # Performing custom comparison first
    diff_upper, diff_lower, delta = ClassUtils.compare_colors(
        upper1, upper2, lower1, lower2)

    print('Diff upper: {0}'.format(diff_upper))
    print('Diff lower: {0}'.format(diff_lower))
    print('Delta: {0}'.format(delta))

    # Showing images
    cv2.namedWindow('main_window', cv2.WND_PROP_AUTOSIZE)
    cv2.imshow('main_window', np.hstack((image1, image2)))

    cv2.waitKey(0)

    print('Done!')
def process_videos(reprocess):
    print('Initializing main function')
    print('Warning! This routine will overwrite the selected files')

    init_dir = '/home/mauricio/Videos/Oviedo/'

    if platform == 'win32':
        init_dir = 'C:\\SharedFTP\\Videos\\'

    options = {'initialdir': init_dir}

    folder = filedialog.askdirectory(**options)

    if folder is None:
        print('Folder not selected')
    else:
        print(folder)
        print('Extracting all mjpeg files')

        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 == '.mjpeg':
                    if not reprocess:
                        mjpegx_path = full_path.replace(".mjpeg", ".mjpegx")
                        if os.path.exists(mjpegx_path):
                            print('Ignoring already converted file {0}'.format(mjpegx_path))
                            continue

                    print('Converting ' + full_path)
                    ClassMjpegConverter.convert_video_mjpeg(full_path)

        print('Done!')
def evaluating_fast_nn():
    print('Initializing evaluating fast nn')

    classes = 8
    hidden_layers = 40
    instance_nn = ClassNN(model_dir=ClassNN.model_dir_pose, classes=classes, hidden_number=hidden_layers)
    instance_pose = ClassOpenPose()

    info = ClassDescriptors.load_images_comparision_ext(instance_pose, min_score=0.05, load_one_img=True)
    pose1 = info['pose1']

    items = ClassDescriptors.get_person_descriptors(pose1, 0.05)

    # Valid pose for detection
    data_to_add = list()
    data_to_add += items['angles']
    data_to_add += ClassUtils.get_flat_list(items['transformedPoints'])

    data_np = np.asanyarray(data_to_add, dtype=np.float)
    result = instance_nn.predict_model_fast(data_np)
    print(result)
    key_pose = result['classes']
    probability = result['probabilities'][key_pose]

    print('Key pose: {0}'.format(key_pose))
    print('Probability: {0}'.format(probability))
    print('Done!')
def main():
    print('Initializing main function')

    color1 = [158, 145, 179]
    color2 = [70, 73, 93]

    delta_e = ClassUtils.get_color_diff_rgb(color1, color2)
    print('Color diff: {0}'.format(delta_e))

    # Showing colors
    # In 100 x 100 window
    # Trying to compare colors visually
    cv2.namedWindow('main_window', cv2.WND_PROP_AUTOSIZE)

    img_size = 100
    image1 = np.zeros((img_size, img_size, 3), np.uint8)
    image2 = np.zeros((img_size, img_size, 3), np.uint8)

    # BGR format -> Generating
    image1[:, :] = (color1[2], color1[1], color1[0])
    image2[:, :] = (color2[2], color2[1], color2[0])

    # Showing image stacked
    img = np.hstack((image1, image2))
    cv2.imshow('main_window', img)

    print('Press a key to continue')
    cv2.waitKey(0)
    print('Done!')
 def get_person_distance(person1: 'ClassPeopleReId',
                         person2: 'ClassPeopleReId'):
     # Comparing person distance
     # x factor penalty
     distance = ClassUtils.get_euclidean_distance(
         person1.global_pos[0] * 1.3, person1.global_pos[1],
         person2.global_pos[0] * 1.3, person2.global_pos[1])
     return distance
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!')
def test_conversion_bgr():
    print('Performing test conversion')

    color1_bgr = [128, 130, 150]
    color2_bgr = [50, 50, 200]

    pt_cv1 = np.array([[color1_bgr]], dtype=np.uint8)
    pt_cv2 = np.array([[color2_bgr]], dtype=np.uint8)

    diff_rgb = ClassUtils.get_color_diff_bgr(pt_cv1, pt_cv2)

    lab1 = cv2.cvtColor(pt_cv1, cv2.COLOR_BGR2LAB)
    lab2 = cv2.cvtColor(pt_cv2, cv2.COLOR_BGR2LAB)

    diff_lab = ClassUtils.get_color_diff_lab(lab1, lab2)
    print('Diff rgb: {0}'.format(diff_rgb))
    print('Diff lab: {0}'.format(diff_lab))
    print('Done!')
def main():
    print('Initializing main function')
    print('Loading elements')

    list_elems = [[1, 2, 3], [4, 5, 6]]
    list_flat = ClassUtils.get_flat_list(list_elems)
    print('list_flat: {0}'.format(list_flat))

    print('Done!')
示例#30
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)