Exemple #1
0
    def frame_size(self, new_size):
        # closest match for size
        sizes = [
            abs(r[0] - new_size[0]) + abs(r[1] - new_size[1])
            for r in self.uvc_capture.frame_sizes
        ]
        best_size_idx = sizes.index(min(sizes))
        size = self.uvc_capture.frame_sizes[best_size_idx]
        if tuple(size) != tuple(new_size):
            logger.warning(
                "{} resolution capture mode not available. Selected {}.".
                format(new_size, size))
        self.uvc_capture.frame_size = size
        self.frame_size_backup = size

        self._intrinsics = Camera_Model.from_file(self.g_pool.user_dir,
                                                  self.name, self.frame_size)

        if self.should_check_stripes:
            self.stripe_detector = Check_Frame_Stripes()
Exemple #2
0
    def __init__(
        self,
        g_pool,
        source_path=None,
        loop=False,
        buffered_decoding=False,
        fill_gaps=False,
        show_plugin_menu=False,
        *args,
        **kwargs,
    ):
        super().__init__(g_pool, *args, **kwargs)
        if self.timing == "external":
            self.recent_events = self.recent_events_external_timing
        else:
            self.recent_events = self.recent_events_own_timing
        # minimal attribute set
        self.source_path = str(source_path)
        self.loop = loop
        self.fill_gaps = fill_gaps
        rec, set_name = self.get_rec_set_name(self.source_path)

        self._init_videoset()

        self.timestamps = self.videoset.lookup.timestamp
        if len(self.timestamps) > 1:
            self._frame_rate = (self.timestamps[-1] - self.timestamps[0]) / len(
                self.timestamps
            )
        else:
            # TODO: where does the fallback framerate of 1/20 come from?
            self._frame_rate = 20
        self.buffering = buffered_decoding
        # Load video split for first frame
        self.reset_video()
        self._intrinsics = Camera_Model.from_file(rec, set_name, self.frame_size)

        self.show_plugin_menu = show_plugin_menu
Exemple #3
0
    def make_update():
        surface_definitions_path = os.path.join(rec_dir, "surface_definitions")
        if not os.path.exists(surface_definitions_path):
            return

        surface_definitions_dict = fm.Persistent_Dict(surface_definitions_path)
        surface_definitions_backup_path = os.path.join(
            rec_dir, "surface_definitions_deprecated")
        os.rename(surface_definitions_path, surface_definitions_backup_path)

        intrinsics_path = os.path.join(rec_dir, "world.intrinsics")
        if not os.path.exists(intrinsics_path):
            logger.warning(
                "Loading surface definitions failed: The data format of the "
                "surface definitions in this recording "
                "is too old and is no longer supported!")
            return

        valid_ext = (".mp4", ".mkv", ".avi", ".h264", ".mjpeg")
        existing_videos = [
            f for f in glob.glob(os.path.join(rec_dir, "world.*"))
            if os.path.splitext(f)[1] in valid_ext
        ]
        if not existing_videos:
            return

        world_video_path = existing_videos[0]
        world_video = av.open(world_video_path)
        f = world_video.streams.video[0].format
        resolution = f.width, f.height

        intrinsics = Camera_Model.from_file(rec_dir, "world", resolution)

        DEPRECATED_SQUARE_MARKER_KEY = "realtime_square_marker_surfaces"
        if DEPRECATED_SQUARE_MARKER_KEY not in surface_definitions_dict:
            return
        surfaces_definitions_old = surface_definitions_dict[
            DEPRECATED_SQUARE_MARKER_KEY]

        surfaces_definitions_new = []
        for surface_def_old in surfaces_definitions_old:
            surface_def_new = {}
            surface_def_new["deprecated"] = True
            surface_def_new["name"] = surface_def_old["name"]
            surface_def_new["real_world_size"] = surface_def_old[
                "real_world_size"]
            surface_def_new["build_up_status"] = 1.0

            reg_markers = []
            registered_markers_dist = []
            for id, verts in surface_def_old["markers"].items():
                reg_marker_dist = {"id": id, "verts_uv": verts}
                registered_markers_dist.append(reg_marker_dist)

                verts_undist = undistort_vertices(verts, intrinsics)
                reg_marker = {"id": id, "verts_uv": verts_undist}
                reg_markers.append(reg_marker)

            surface_def_new[
                "registered_markers_dist"] = registered_markers_dist
            surface_def_new["reg_markers"] = reg_markers

            surfaces_definitions_new.append(surface_def_new)

        surface_definitions_dict_new = fm.Persistent_Dict(
            surface_definitions_path)
        surface_definitions_dict_new["surfaces"] = surfaces_definitions_new
        surface_definitions_dict_new.save()
Exemple #4
0
 def intrinsics(self):
     if self._intrinsics is None or self._intrinsics.resolution != self.frame_size:
         self._intrinsics = Camera_Model.from_file(self.g_pool.user_dir,
                                                   self.name,
                                                   self.frame_size)
     return self._intrinsics
    def __init__(
        self,
        g_pool,
        frame_size,
        frame_rate,
        name=None,
        preferred_names=(),
        uid=None,
        uvc_controls={},
        check_stripes=True,
        exposure_mode="manual",
        *args,
        **kwargs,
    ):

        super().__init__(g_pool, *args, **kwargs)
        self.uvc_capture = None
        self._last_ts = None
        self._restart_in = 3
        assert name or preferred_names or uid

        if platform.system() == "Windows":
            self.verify_drivers()

        self.devices = uvc.Device_List()

        devices_by_name = {dev["name"]: dev for dev in self.devices}

        # if uid is supplied we init with that
        if uid:
            try:
                self.uvc_capture = uvc.Capture(uid)
            except uvc.OpenError:
                logger.warning(
                    "No avalilable camera found that matched {}".format(preferred_names)
                )
            except uvc.InitError:
                logger.error("Camera failed to initialize.")
            except uvc.DeviceNotFoundError:
                logger.warning(
                    "No camera found that matched {}".format(preferred_names)
                )

        # otherwise we use name or preffered_names
        else:
            if name:
                preferred_names = (name,)
            else:
                pass
            assert preferred_names

            # try to init by name
            for name in preferred_names:
                for d_name in devices_by_name.keys():
                    if name in d_name:
                        uid_for_name = devices_by_name[d_name]["uid"]
                        try:
                            self.uvc_capture = uvc.Capture(uid_for_name)
                            break
                        except uvc.OpenError:
                            logger.info(
                                f"{uid_for_name} matches {name} but is already in use "
                                "or blocked."
                            )
                        except uvc.InitError:
                            logger.error("Camera failed to initialize.")
                if self.uvc_capture:
                    break

        # checkframestripes will be initialized accordingly in configure_capture()
        self.enable_stripe_checks = check_stripes
        self.exposure_mode = exposure_mode
        self.stripe_detector = None
        self.preferred_exposure_time = None

        # check if we were sucessfull
        if not self.uvc_capture:
            logger.error("Could not connect to device! No images will be supplied.")
            self.name_backup = preferred_names
            self.frame_size_backup = frame_size
            self.frame_rate_backup = frame_rate
            self.exposure_time_backup = None
            self._intrinsics = Camera_Model.from_file(
                self.g_pool.user_dir, self.name, self.frame_size
            )
        else:
            self.configure_capture(frame_size, frame_rate, uvc_controls)
            self.name_backup = (self.name,)
            self.frame_size_backup = frame_size
            self.frame_rate_backup = frame_rate
            controls_dict = dict(
                [(c.display_name, c) for c in self.uvc_capture.controls]
            )
            try:
                self.exposure_time_backup = controls_dict[
                    "Absolute Exposure Time"
                ].value
            except KeyError:
                self.exposure_time_backup = None

        self.backup_uvc_controls = {}