Ejemplo n.º 1
0
def main():
    print('Initializing main function')
    file_path = '/home/mauricio/Videos/mjpeg/11-00-00.mjpeg'

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

    images = ClassMjpegReader.process_video(file_path)
    print('Size list')
    print(len(images))
    print('Done')

    print('Reading list using opencv')
    cv2.namedWindow('test')

    for elem in images:
        image = elem[0]
        ticks = elem[1]

        image_np = np.frombuffer(image, dtype=np.uint8)
        print(ticks)
        print(len(image))
        image_cv = cv2.imdecode(image_np, cv2.IMREAD_ANYCOLOR)
        image_recognize = open_pose.recognize_image_draw(image_cv)

        cv2.imshow('test', image_recognize)
        cv2.waitKey(10)

    cv2.destroyAllWindows()
    print('Done!')
    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)
Ejemplo n.º 3
0
def load_video_info(init_dir):
    options = {'initialdir': init_dir}
    filename = askopenfilename(**options)

    if not filename:
        raise Exception('File not selected')

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

    if extension == '.mjpeg':
        list_frames = ClassMjpegReader.process_video(filename)
    elif extension == '.mjpegx':
        list_frames = ClassMjpegReader.process_video_mjpegx(filename)
    else:
        raise Exception('Extension not supported: {0}'.format(extension))

    return filename, list_frames
Ejemplo n.º 4
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)
Ejemplo n.º 5
0
    def convert_video_mjpegx(cls,
                             file_path: str,
                             cam_number: str,
                             instance_nn_pose: ClassNN = None):
        extension = os.path.splitext(file_path)[1]

        if ClassUtils.cam_calib_exists(cam_number):
            calib_params = ClassUtils.load_cam_calib_params(cam_number)
        else:
            print('Warning: Calib params not found for camera {0}'.format(
                cam_number))
            calib_params = None

        if extension != '.mjpegx':
            raise Exception('file_path must have extension .mjpegx')

        print('Reading image')
        output_video = file_path + '_1'

        images = ClassMjpegReader.process_video_mjpegx(file_path)
        print('Len images: ' + str(len(images)))

        with open(output_video, 'wb') as newFile:
            counter = 0
            for elem in images:
                # Avoid previous conversion -> only reading
                image = elem[0]
                ticks = elem[1]
                json_dict = elem[2]

                vectors = json_dict['vectors']

                # Avoid over processing - Only process images with poses
                image_cv = None
                if len(vectors) != 0:
                    image_np = np.frombuffer(image, dtype=np.uint8)
                    image_cv = cv2.imdecode(image_np, cv2.IMREAD_ANYCOLOR)

                params = list()

                for vector in vectors:
                    # Check if vector is bodypart 25
                    # Avoid confusions with COCO processing
                    if len(vector) != 25:
                        raise Exception(
                            'Vector is not bodypart 25 - File: {0} Len Vectors: {1}'
                            .format(file_path, len(vector)))

                    params.append(
                        ClassDescriptors.get_person_descriptors(
                            vector,
                            cls.min_percent,
                            cam_number=cam_number,
                            image=image_cv,
                            calib_params=calib_params,
                            decode_img=False,
                            instance_nn_pose=instance_nn_pose))

                # Save object globally
                # CamNumber
                new_json_dict = {
                    'vectors': vectors,
                    'params': params,
                    'camNumber': cam_number
                }

                # Write in new file - New json dict
                ClassUtils.write_in_file(newFile, ticks, image, new_json_dict)

                counter += 1
                if counter % 100 == 0:
                    print('Counter: ' + str(counter))
                    if calib_params is None:
                        print('Warning: Calib params not found for camera {0}'.
                              format(cam_number))

        # Delete old
        os.remove(file_path)

        # Naming new
        os.rename(output_video, file_path)
Ejemplo n.º 6
0
    def convert_video_mjpegx_reid(cls,
                                  file_path: str,
                                  list_people_reid=None,
                                  save_img=False,
                                  option=None):
        extension = os.path.splitext(file_path)[1]

        if extension != '.mjpegx':
            raise Exception('file_path must have extension .mjpegx')

        print('Reading image')
        output_video = file_path + '_1'

        images = ClassMjpegReader.process_video_mjpegx(file_path)
        print('Len images: ' + str(len(images)))

        with open(output_video, 'wb') as newFile:
            counter = 0
            for elem in images:
                # Avoid previous conversion -> only reading
                image = elem[0]
                ticks = elem[1]
                json_dict = elem[2]

                params = json_dict['params']

                # Update elements
                for param in params:
                    found = False
                    for person_reid in list_people_reid:
                        for item in person_reid.list_poses:
                            if item['poseGuid'] == param['poseGuid']:
                                param['personGuid'] = person_reid.person_guid
                                found = True
                                break

                        if found:
                            # Saving image with pose identified
                            if save_img:
                                cnn_folder = ClassUtils.cnn_folder
                                if option is not None:
                                    cnn_folder = os.path.join(
                                        cnn_folder, option)

                                path_img = os.path.join(
                                    cnn_folder, param['personGuid'])
                                if not os.path.exists(path_img):
                                    os.makedirs(path_img)

                                filename = os.path.join(
                                    path_img, '{0}.jpg'.format(ticks))
                                image_arr = np.frombuffer(image,
                                                          dtype=np.uint8)
                                image_cv = cv2.imdecode(
                                    image_arr, cv2.IMREAD_ANYCOLOR)

                                ClassDescriptors.draw_pose(
                                    image_cv, param['vectors'],
                                    cls.min_percent, param['keyPose'])
                                cv2.imwrite(filename, image_cv)

                            break

                    if not found:
                        param['personGuid'] = ''

                # Saving data in json dict
                ClassUtils.write_in_file(newFile, ticks, image, json_dict)

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

        # Delete old
        os.remove(file_path)

        # Naming new
        os.rename(output_video, file_path)
Ejemplo n.º 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!')