Exemplo n.º 1
0
    def autoconfig(self):
        """
        Sets the shutterspeed and white balance automatically using the
        framerate provided in the configuration file
        """

        self._setup_cam(auto=True)
        with self.rawCapture as stream:
            for a in range(5):
                self.cam.capture(stream, format="bgr", use_video_port=True)
                image = stream.array
                stream.seek(0)
                stream.truncate()

            self.config.cam.shutterspeed = self.cam.exposure_speed
            self.config.cus.gains = tuple(
                [round(float(i), 2) for i in self.cam.awb_gains])
            self.config.save()
            lineprint("Shutterspeed set to " + str(self.cam.exposure_speed))
            lineprint("White balance gains set to " +
                      str(self.config.cus.gains))

        stream.close()
        self.rawCapture.close()
        self.cam.close()
Exemplo n.º 2
0
    def __init__(self, system="auto", framerate=8, vidsize=0.2, internal=False,
                 rotation = 0):

        """Opens a video stream with user interface to calibrate the camera"""

        if internal:
            lineprint("Running calibrate function.. ")

        self.system = system
        self.framerate = framerate
        self.vidsize = vidsize
        self.rotation = rotation
        self.cross = False
        self.stream = True
        self.exit = False
        self.roi = False
        self.fullscreen = False
        self.tempcol = draw.namedcols("orange")
        self.col = draw.namedcols("red")

        cv2.namedWindow("Image", cv2.WND_PROP_FULLSCREEN)
        self.m = draw.mouse_events()
        cv2.setMouseCallback('Image', self.m.draw)
        time.sleep(1)

        self.drawer()
Exemplo n.º 3
0
    def _setup_cam(self, auto = False, fps = None):

        """Sets up the raspberry pi camera based on the configuration"""

        import picamera
        import picamera.array

        self.cam = picamera.PiCamera()
        self.cam.rotation = self.config.cus.rotation
        self.cam.exposure_compensation = self.config.cam.compensation

        if self.config.rec.rectype in ["img","imgseq"]:
            self.cam.resolution = literal_eval(self.config.img.imgdims)
            self.cam.framerate = self.config.img.imgfps
        if self.config.rec.rectype in ["vid","vidseq"]:
            self.cam.resolution = picamconv(literal_eval(self.config.vid.viddims))
            self.cam.framerate = self.config.vid.vidfps
        if fps != None:
            self.cam.framerate = fps

        if self.config.cus.roi is None:
            self.cam.zoom = (0,0,1,1)
            self.resize = self.cam.resolution
        else:
            self.cam.zoom = literal_eval(self.config.cus.roi)
            w = int(self.cam.resolution[0]*self.cam.zoom[2])
            h = int(self.cam.resolution[1]*self.cam.zoom[3])
            if self.config.rec.rectype in ["vid","vidseq"]:
                self.resize = picamconv((w,h))
            else:
                self.resize = (w,h)

        self.longexpo = False if self.cam.framerate >= 6 else True

        self.cam.exposure_mode = "auto"
        self.cam.awb_mode = "auto"
        lineprint("Camera warming up..")
        if auto or self.config.cam.automode:
            self.cam.shutter_speed = 0
            sleep(2)
        elif self.cam.framerate >= 6:
            sleep(6) if self.cam.framerate > 1.6 else sleep(10)
        else:
            sleep(2)
        if not auto and self.config.cam.automode == False:
            self.cam.shutter_speed = self.config.cam.shutterspeed
            self.cam.exposure_mode = "off"
            self.cam.awb_mode = "off"
            self.cam.awb_gains = eval(self.config.cus.gains)
            sleep(0.1)

        brightness = self.config.cam.brightness + self.config.cus.brighttune
        self.cam.brightness = brightness
        self.cam.contrast = self.config.cam.contrast
        self.cam.saturation = self.config.cam.saturation
        self.cam.iso = self.config.cam.iso
        self.cam.sharpness = self.config.cam.sharpness

        self.rawCapture = picamera.array.PiRGBArray(self.cam,
                          size=self.cam.resolution)
Exemplo n.º 4
0
    def draw_stream(self):

        lineprint("Streaming video..")

        self.vid = VideoIn(system=self.system, framerate=self.framerate,
                           vidsize=self.vidsize, rotation=self.rotation)
        self.vid.start()

        while True:
            self.img = self.vid.read()
            if self.cross:
                draw.draw_cross(self.img, pt2 = self.vid.res)
            cv2.imshow("Image", self.img)

            k = cv2.waitKey(1) & 0xFF
            if k == ord("c"):
                self.cross = not self.cross

            if k == ord("f"):
                self.fullscreen = not self.fullscreen
                if self.fullscreen:
                    cv2.setWindowProperty("Image",cv2.WND_PROP_FULLSCREEN,cv2.WINDOW_FULLSCREEN)
                else:
                    cv2.setWindowProperty("Image",cv2.WND_PROP_AUTOSIZE, cv2.WINDOW_NORMAL)
                    cv2.resizeWindow("Image", self.vid.res[0], self.vid.res[1])

            if k == ord("d"):
                self.stream = False
                break
            if k == 27:
                lineprint("User exited..")
                self.exit = True
                break

        self.vid.stop()
Exemplo n.º 5
0
    def camconfig(self, fps=None, vidsize=0.4):

        lineprint("Opening stream for interactive configuration..")
        fps = max(self.config.vid.vidfps,1) if fps==None else int(fps)
        self._setup_cam(fps=fps)
        configout = Camconfig(self.cam, auto=self.config.cam.automode,
                              vidsize=vidsize)
        if len(configout)>0:
            self.settings(**configout)
Exemplo n.º 6
0
    def checktimeplan(self):
        """Checks timeplan and prints description"""

        valid = crontab.CronSlices.is_valid(self.jobtimeplan)
        if valid:
            timedesc = get_description(self.jobtimeplan)
            lineprint("Your timeplan will run " + timedesc)
        else:
            lineprint("Timeplan is not valid..")

        return valid
Exemplo n.º 7
0
    def enable_job(self):
        """Enables/disables a specific job"""

        if self.jobenable == "True" or self.jobenable == True:
            self.job.enable(True)
            lineprint(self.jobname[4:] + " job enabled..")
        else:
            self.job.enable(False)
            lineprint(self.jobname[4:] + " job disabled..")
        self.cron.write()
        self.jobsshow = True
Exemplo n.º 8
0
    def set_gains(self, auto=True):
        """Find the best gains for the raspberry pi camera"""

        if self.config.cus.roi == None:
            zoom = (0, 0, 1, 1)
        else:
            zoom = literal_eval(self.config.cus.roi)
        (rg, bg) = setgains(startgains=checkfrac(self.config.cus.gains),
                            zoom=zoom,
                            auto=auto)
        self.set_config(gains="(%5.2f, %5.2f)" % (rg, bg), internal="")
        lineprint("Gains: " + "(R:%5.2f, B:%5.2f)" % (rg, bg) + " stored..")
Exemplo n.º 9
0
    def draw_frame(self):

        lineprint("Selecting roi..")
        self.imgbak = self.img.copy()

        while True:
            img = self.imgbak.copy()
            if self.m.twoPoint is not None:
                draw.draw_crosshair(img, self.m.pos)
            if self.m.posDown is not None:
                cv2.rectangle(img, self.m.posDown, self.m.pos, self.tempcol, 2)
            if self.m.posUp is not None:
                cv2.rectangle(img, self.m.pts[-1], self.m.posUp,
                              self.col, 2)
            cv2.imshow("Image", img)

            k = cv2.waitKey(1) & 0xFF
            if k == ord("s"):
                if self.m.twoPoint is not None:
                    self.m.twoPoint = checkroi(self.m.twoPoint, self.vid.res)
                    self.roi = roi_to_zoom(self.m.twoPoint, self.vid.res)
                    lineprint("roi "+str(self.roi)+" stored..")
                    break
                else:
                    lineprint("Nothing to store..")

            if k == ord("e"):
                self.roi = False
                lineprint("new roi erased..")

            if k == ord("z"):
                if self.m.twoPoint is not None:
                    lineprint("Creating zoomed image..")
                    self.vid2 = VideoIn(system=self.system, vidsize=self.vidsize,
                                        crop=self.m.twoPoint, rotation=self.rotation)
                    zimg = self.vid2.img()
                    cv2.namedWindow("Zoomed", cv2.WINDOW_NORMAL)
                    while True:
                        cv2.imshow("Zoomed", zimg)
                        cv2.resizeWindow("Zoomed", self.vid2.roiw, self.vid2.roih)
                        k = cv2.waitKey(1) & 0xFF
                        if k == 27:
                            break

                    k = 255
                    cv2.destroyWindow("Zoomed")

            if k == 27:
                self.stream = True
                self.m.twoPoint = None
                break
Exemplo n.º 10
0
    def set_job(self):
        """Creates/modifies a specific job"""

        if len(self.jobfits) > 0:
            self.job = self.jobfits[0]
            self.job.command = self.task
        else:
            self.job = self.cron.new(command=self.task, comment=self.jobname)
        if self.jobtimeplan is not None:
            self.job.setall(self.jobtimeplan)
            self.cron.write()
            lineprint(self.jobname[4:] + " job succesfully set..")

        if self.jobenable is not None:
            self.enable_job()
Exemplo n.º 11
0
    def _setup_cam(self):
        """Sets up the raspberry pi camera based on configuration"""

        #load picamera module in-function so pirecorder is installable on all OS
        import picamera
        import picamera.array

        self.cam = picamera.PiCamera()
        self.cam.rotation = self.config.cus.rotation
        self.cam.exposure_compensation = self.config.cam.compensation

        if self.config.rec.rectype in ["img", "imgseq"]:
            self.cam.resolution = literal_eval(self.config.img.imgdims)
            self.cam.framerate = self.config.img.imgfps
        if self.config.rec.rectype in ["vid", "vidseq"]:
            self.cam.resolution = literal_eval(self.config.vid.viddims)
            self.cam.framerate = self.config.vid.vidfps
        self.rawCapture = picamera.array.PiRGBArray(self.cam,
                                                    size=self.cam.resolution)

        if self.config.cus.roi is None:
            self.cam.zoom = (0, 0, 1, 1)
            self.resize = self.cam.resolution
        else:
            self.cam.zoom = literal_eval(self.config.cus.roi)
            w = int(self.cam.resolution[0] * self.cam.zoom[2])
            h = int(self.cam.resolution[1] * self.cam.zoom[3])
            self.resize = picamconv((w, h))

        self.longexpo = False if self.cam.framerate >= 6 else True
        if self.longexpo:
            lineprint("Long exposure, warming up camera..")
            sleep(6) if self.cam.framerate > 1.6 else sleep(10)
        else:
            lineprint("Camera warming up..")
            sleep(2)

        self.cam.shutter_speed = self.config.cam.shutterspeed
        self.cam.exposure_mode = "off"
        self.cam.awb_mode = "off"
        self.cam.awb_gains = checkfrac(self.config.cus.gains)
        brightness = self.config.cam.brightness + self.config.cus.brighttune
        self.cam.brightness = brightness

        self.cam.contrast = self.config.cam.contrast
        self.cam.saturation = self.config.cam.saturation
        self.cam.iso = self.config.cam.iso
        self.cam.sharpness = self.config.cam.sharpness
Exemplo n.º 12
0
    def conv_single(self, filein):

        try:
            filebase = os.path.basename(filein)
            fileout = filein if self.outdir == "" else self.outdir + "/" + filebase
            lineprint("Start converting " + filebase, label="pirecorder")

            if self.withframe:
                vid = cv2.VideoCapture(filein)
                fps, width, height, _ = get_vid_params(vid)
                if self.fps is None:
                    self.fps = fps
                vidout = videowriter(fileout, width, height, self.fps,
                                     self.resizeval)

                while True:
                    flag, frame = vid.read()
                    if flag:
                        if self.resizeval != 1:
                            frame = imgresize(frame, self.resizeval)
                        frame_nr = int(vid.get(cv2.CAP_PROP_POS_FRAMES))
                        draw_text(frame,
                                  str(frame_nr), (10, 10),
                                  0.9,
                                  col="white",
                                  shadow=True)
                        vidout.write(frame)
                    if not flag:
                        break

            else:
                if self.resizeval != 1:
                    comm = "' -vf 'scale=iw*" + str(self.resizeval) + ":-2' '"
                else:
                    comm = "' -vcodec copy '"
                bashcomm = "ffmpeg"
                if self.fps is not None:
                    bashcomm = bashcomm + " -r " + str(self.fps)
                bashcomm = bashcomm + " -i '" + filein + comm + fileout[:-len(
                    self.type)] + ".mp4'"
                bashcomm = bashcomm + " -y -nostats -loglevel 0"
                output = subprocess.check_output(['bash', '-c', bashcomm])

            lineprint("Finished converting " + filebase, label="pirecorder")

        except KeyboardInterrupt:
            raise KeyboardInterruptError()
Exemplo n.º 13
0
def rec():
    """To run pirecorder from the command line"""

    parser = argparse.ArgumentParser(
        prog="record", description="Runs PiRecorder record function")
    parser.add_argument("-c",
                        "--configfile",
                        default="pirecorder.conf",
                        action="store",
                        help="pirecorder configuration file")
    args = parser.parse_args()
    if not isrpi():
        lineprint("PiRecorder only works on a raspberry pi. Exiting..")
        return
    rec = PiRecorder(args.configfile)
    rec.settings(internal=True)
    rec.record()
Exemplo n.º 14
0
    def set_roi(self):
        """
        Dynamically draw a region of interest

        Explanation
        ===========
        This function will open a video stream of the raspberry pi camera. Enter
        'd' to start drawing the region of interest on an image taken from the
        video stream. When happy with the region selected, press 's' to store
        the coordinates, or 'esc' key to exit drawing on the image. To exist the
        video stream enter 'esc' key again.
        """

        C = Calibrate(internal=True, rotation=self.config.cus.rotation)
        if C.roi:
            self.set_config(roi=C.roi, internal="")
            lineprint("Roi stored..")
        else:
            lineprint("No roi selected..")
Exemplo n.º 15
0
    def __init__(self, indir = "", outdir = "", type = ".h264",
                 withframe = False, overwrite = False, delete = False,
                 pools = 4, resizeval = 1, fps = None, imgfps = 25,
                 internal = False, sleeptime = None):

        if internal:
            lineprint("Running convert function..", label="pirecorder")

        self.indir = os.getcwd() if indir == "" else indir
        assert os.path.exists(self.indir), "in-directory does not exist.."
        self.outdir = self.indir if outdir == "" else outdir
        if not os.path.exists(self.outdir):
            os.makedirs(self.outdir)

        self.rootdir = os.getcwd()
        os.chdir(self.indir)

        self.type = type
        self.withframe = withframe
        self.delete = delete
        self.pools = int(pools)
        self.resizeval = float(resizeval)
        self.fps = int(fps) if fps is not None else None
        self.imgfps = int(imgfps)
        self.terminated = False

        while True:
            files = listfiles(self.indir, self.type, keepdir = False)
            old = listfiles(self.indir, self.type, keepext = False)
            new = listfiles(self.outdir, ".mp4", keepext = False)
            self.todo = files
            if not overwrite:
                self.todo = [files[i] for i,file in enumerate(old) if file not in new]
            if self.type in [".jpg",".jpeg",".png"] and len(self.todo)>0:
                 if len([f for f in new if commonpref(self.todo) in f])>0 and not overwrite:
                     self.todo = []
            self.convertpool()
            msg = "No files to convert.."
            if sleeptime == None:
                if not self.terminated:
                    lineprint(msg, label="pirecorder")
                return
            else:
                try:
                    lineprint(msg+" rechecking in "+str(sleeptime)+"s..", label="pirecorder")
                    time.sleep(sleeptime)
                except KeyboardInterrupt:
                    lineprint("Terminating checking for files..", label="pirecorder")
                    return
Exemplo n.º 16
0
    def show_jobs(self):
        """Displays a table of all scheduled jobs"""

        if len(self.cron) > 0:
            lineprint("Current job schedule:")
            for job in self.cron:
                lenjob = max(8, len(job.comment[4:]))
                lenplan = max(8, len(str(job)[:str(job).find(" /")]))
            header = "Job" + " " * (lenjob - 1) + "Timeplan" + " " * (
                lenplan - 7) + "Next recording"
            print(header)
            print("-" * len(header))
            self.jobs = self.get_jobs()
            for job in self.jobs:
                sch = job.schedule(date_from=datetime.datetime.now())
                jobname = job.comment[4:] + " " * (lenjob -
                                                   (len(job.comment[4:]) - 2))
                plan = str(job)[:str(job).find(" /") - 2]
                plan = plan[2:] if plan[0] == "#" else plan
                plan = plan + " " * (lenplan - (len(plan) - 1))
                next = str(sch.get_next()) if job.is_enabled() else "disabled"
                print(jobname + plan + next)
        else:
            lineprint("Currently no jobs scheduled..")
Exemplo n.º 17
0
    def __init__(self,
                 jobname=None,
                 timeplan=None,
                 enable=None,
                 showjobs=False,
                 delete=None,
                 test=False,
                 internal=False,
                 configfile="pirecorder.conf",
                 logfolder="/home/pi/pirecorder/"):

        if internal:
            lineprint("Running schedule function.. ")

        self.cron = crontab.CronTab(user=getpass.getuser())

        if jobname is not None:
            self.jobname = "REC_" + jobname
            pexec = sys.executable + " -c "
            pcomm1 = """'import pirecorder; """
            pcomm2 = """R=pirecorder.PiRecorder("%s"); R.record()'""" % configfile
            log1 = " >> " + logfolder + "$(date +%y%m%d)_"
            log2 = str(self.jobname[4:]) + ".log 2>&1"
            self.task = pexec + pcomm1 + pcomm2 + log1 + log2
        else:
            self.jobname = None

        self.jobtimeplan = timeplan
        self.jobenable = enable
        self.jobsshow = showjobs
        self.jobsclear = delete
        if self.jobsclear not in [None, "all"] and self.jobname == None:
            self.jobname = "REC_" + self.jobsclear

        self.jobs = self.get_jobs()
        self.jobfits = self.get_jobs(name=self.jobname)

        if self.jobsclear is not None:
            self.clear_jobs()
        elif not self.jobsshow:
            if self.jobtimeplan is None and self.jobname is None and not test:
                self.jobsshow = True
            elif self.jobtimeplan is None and self.jobname is not None and not test and len(
                    self.jobfits) == 0:
                lineprint("No timeplan provided..")
            elif self.jobtimeplan is not None and self.jobname is None and not test:
                lineprint("No jobname provided..")
            elif test and self.jobtimeplan is not None:
                self.checktimeplan()
            else:
                if enable is None:
                    self.checktimeplan()
                self.set_job()
        if self.jobsshow:
            self.show_jobs()
Exemplo n.º 18
0
    def stream(self, fps=None):
        """Shows an interactive video stream"""

        lineprint("Opening stream for cam positioning and roi extraction..")
        vidstream = Stream(internal=True,
                           rotation=self.config.cus.rotation,
                           maxres=self.config.rec.maxres)
        if vidstream.roi:
            self.settings(roi=vidstream.roi, internal="")
            lineprint("Roi stored..")
        else:
            lineprint("No roi selected..")
Exemplo n.º 19
0
    def clear_jobs(self):
        """Clears a specific or all jobs currently scheduled"""

        if self.jobsclear == None:
            pass
        elif self.jobsclear == "all":
            for job in self.jobs:
                if job.comment[:3] == "REC":
                    self.cron.remove(job)
            lineprint("All scheduled jobs removed..")
        else:
            if len(self.jobfits) > 0:
                self.cron.remove(self.jobfits[0])
                lineprint(self.jobname[4:] + " job removed..")
            else:
                lineprint("No fitting job found to remove..")
        self.cron.write()
        self.jobsshow = True
Exemplo n.º 20
0
    def __init__(self, system = "auto", framerate = 8, vidsize = 0.2,
                 rotation = 0, internal = False, maxres = None,
                 imgoverlay = None):

        """
        Opens a video stream with user interface to help position and
        adjust the camera

        parameters
        -----------
        system : str, default = "auto"
            If the system should be automatically determined. Should detect if
            the computer is a raspberry pi or not. If this somehow fails, set
            to "rpi" manually.
        framerate : int, default = 8
            The framerate of the displayed video stream. Lower framerates take
            longer to start up. When using an image overlay, maximum possible
            framerate is 5, to avoid getting shutter effects
        vidsize : float, default = 0.2
            The relative size of the video window to the maximum resolution of
            the raspberry pi camera type.
        rotation : int, default = 180
            If the camera should be rotated or not. 0 and 180 are valid values.
        maxres : str or tuple, default = "v2"
            The maximum potential resolution of the camera used. Either provide
            a tuple of the max resolution, or use "v1.5", "v2" (default), or
            "hq" to get the maximum resolution associated with the official cameras
            directly.
        imgoverlay : str, default = None
            The path to an image that will be overlaid on the video stream. This
            can be helpful for accurate positioning of the camera in line with
            previous recordings or setups.

        interface
        -----------
        This interactive module stores mouse position and clicks and responds to
        the following keypresses:
        f-key : display the stream fullscreen/display the stream in a window
        c-key : display/hide a diagonal cross across the screen
        s-key : save the coordinates of the rectangular area when drawn
        e-key : erase the rectangular area when drawn
        z-key : show a zoomed-in section of the video inside the rectangular
            area in maximum resolution
        n-key : refresh the zoom-in image
        o-key : if the potential overlay image should be shown or not
        [- and ]-keys : decrease or increase the relative opacity of the
            potential overlay image with 5%
        esc-key : exit the the zoom window; exit the calibrate function
        """

        self.internal = internal
        if self.internal:
            lineprint("Running stream function.. ")

        self.system = system
        self.framerate = framerate
        self.vidsize = vidsize
        self.rotation = rotation
        self.maxres = maxres

        if imgoverlay == None:
            self.overlay = False
            self.waitms = 1
        else:
            if os.path.isfile(imgoverlay):
                self.overlay = True
                self.overlayimg = cv2.imread(imgoverlay)
                self.alpha = 0.5
                self.waitms = 200
                self.framerate = min(self.framerate, 5)
            else:
                print("Image file could not be loaded..")

        self.cross = False
        self.stream = True
        self.exit = False
        self.roi = False
        self.fullscreen = False
        self.tempcol = draw.namedcols("orange")
        self.col = draw.namedcols("red")

        cv2.namedWindow("Image", cv2.WND_PROP_FULLSCREEN)
        self.m = draw.mouse_events()
        cv2.setMouseCallback('Image', self.m.draw)
        time.sleep(1)

        self.drawer()
Exemplo n.º 21
0
    def record(self):
        """Runs the Recorder instance"""

        self._setup_cam()
        self._namefile()

        if self.config.rec.rectype == "img":

            self.filename = self.filename + strftime("%H%M%S") + self.filetype
            self.cam.capture(self.filename,
                             format="jpeg",
                             resize=self.resize,
                             quality=self.config.img.imgquality)
            lineprint("Captured " + self.filename)

        elif self.config.rec.rectype == "imgseq":

            timepoint = datetime.now()
            for i, img in enumerate(
                    self.cam.capture_continuous(
                        self.filename,
                        format="jpeg",
                        resize=self.resize,
                        quality=self.config.img.imgquality)):
                if i < self.config.img.imgnr - 1:
                    timepassed = (datetime.now() - timepoint).total_seconds()
                    delay = max(0, self.config.img.imgwait - timepassed)
                    lineprint("Captured " + img + ", sleeping " +
                              str(round(delay, 2)) + "s..")
                    sleep(delay)
                    timepoint = datetime.now()
                else:
                    lineprint("Captured " + img)
                    break

        elif self.config.rec.rectype in ["vid", "vidseq"]:

            # Temporary fix for flicker at start of (first) video..
            self.cam.start_recording(BytesIO(),
                                     format="h264",
                                     resize=self.resize)
            self.cam.wait_recording(2)
            self.cam.stop_recording()

            for session in ["_S%02d" % i for i in range(1, 999)]:
                session = "" if self.config.rec.rectype == "vid" else session
                filename = self.filename + strftime(
                    "%H%M%S") + session + self.filetype
                self.cam.start_recording(filename,
                                         resize=self.resize,
                                         quality=self.config.vid.vidquality)
                lineprint("Start recording " + filename)
                self.cam.wait_recording(self.config.vid.vidduration +
                                        self.config.vid.viddelay)
                self.cam.stop_recording()
                lineprint("Finished recording " + filename)
                if self.config.rec.rectype == "vid":
                    break
                else:
                    if input("\nAny key for new session, e to exit: ") == "e":
                        break

        self.cam.close()
Exemplo n.º 22
0
def Camconfig(cam=None,
              auto=None,
              iso=200,
              framerate=20,
              res=(1640, 1232),
              vidsize=0.4):
    """
    Opens a video stream to configure a wide array of camera parameters
    Note: A screen resolution of at least 800x600 is strongly recommended
    """

    if not isrpi():
        lineprint("PiRecorder only works on a raspberry pi. Exiting..")
        return

    import picamera
    import picamera.array

    def nothing(x):
        pass

    if cam == None:
        cam = picamera.PiCamera()
        cam.resolution = res
        cam.iso = iso
        cam.framerate = framerate
        time.sleep(2)
    if cam == None or (auto == None or auto == True):
        cam.exposure_mode = "auto"
        set_auto = 1
    else:
        set_auto = 0

    res = (cam.resolution[0] * vidsize, cam.resolution[1] * vidsize)
    res = (int(res[0] * cam.zoom[2]), int(res[1] * cam.zoom[3]))
    if (res[0] * res[1]) >= (1640 * 1232):
        res = maxrect(dims, maxdims=(1640, 1232), decimals=0)
    cam.resolution = picamconv(res)

    cv2.namedWindow("Stream", cv2.WND_PROP_FULLSCREEN)
    cv2.namedWindow("Config", cv2.WINDOW_NORMAL)
    cv2.setWindowProperty("Stream", cv2.WND_PROP_AUTOSIZE, cv2.WINDOW_NORMAL)
    cv2.resizeWindow("Stream", cam.resolution[0], cam.resolution[1])

    config = []
    isos = [100, 200, 320, 400, 500, 640, 800]

    set_rot = cam.rotation * 180
    set_fps = int(cam.framerate)
    set_iso = [i for i, j in enumerate(isos) if j == cam.iso][0]
    set_comp = cam.exposure_compensation + 25
    set_shut = cam.shutter_speed
    set_red = int(float(cam.awb_gains[0]) * 10)
    set_blue = int(float(cam.awb_gains[1]) * 10)
    set_bri = cam.brightness
    set_con = cam.contrast + 100
    set_sat = cam.saturation + 100
    set_shar = cam.sharpness + 100

    cv2.createTrackbar("rotation (0deg/180deg)", "Config", set_rot, 1, nothing)
    cv2.createTrackbar("framerate", "Config", set_fps, 40, nothing)
    cv2.createTrackbar("automatic (off/on)", "Config", set_auto, 1, nothing)
    cv2.createTrackbar("iso", "Config", set_iso, 6, nothing)
    cv2.createTrackbar("compensation", "Config", set_comp, 50, nothing)
    cv2.createTrackbar("shutterspeed (ms)", "Config", set_shut, 99999, nothing)
    cv2.createTrackbar("red gain", "Config", set_red, 80, nothing)
    cv2.createTrackbar("blue gain", "Config", set_blue, 80, nothing)
    cv2.createTrackbar("brightness", "Config", set_bri, 100, nothing)
    cv2.createTrackbar("contrast", "Config", set_con, 200, nothing)
    cv2.createTrackbar("saturation", "Config", set_sat, 200, nothing)
    cv2.createTrackbar("sharpness", "Config", set_shar, 200, nothing)

    rawCapture = picamera.array.PiRGBArray(cam, size=cam.resolution)
    lineprint("Streaming interactive video..")
    with rawCapture as stream:
        while True:
            cam.capture(stream, format="bgr", use_video_port=True)
            image = stream.array

            rot = cv2.getTrackbarPos("rotation (0deg/180deg)", "Config")
            fps = cv2.getTrackbarPos("framerate", "Config")
            auto = cv2.getTrackbarPos("automatic (off/on)", "Config")
            iso = cv2.getTrackbarPos("iso", "Config")
            comp = cv2.getTrackbarPos("compensation", "Config")
            shut = cv2.getTrackbarPos("shutterspeed (ms)", "Config")
            red = cv2.getTrackbarPos("red gain", "Config")
            blue = cv2.getTrackbarPos("blue gain", "Config")
            bri = cv2.getTrackbarPos("brightness", "Config")
            con = cv2.getTrackbarPos("contrast", "Config")
            sat = cv2.getTrackbarPos("saturation", "Config")
            shar = cv2.getTrackbarPos("sharpness", "Config")

            cam.rotation = [0, 180][rot]
            cam.framerate = max(fps, 1)
            cam.exposure_mode = ["off", "auto"][auto]
            cam.iso = isos[iso]
            cam.exposure_compensation = comp - 25
            cam.awb_mode = cam.exposure_mode

            if cam.exposure_mode == "off":
                cam.shutter_speed = shut
                maxshut = int(float(1 / cam.framerate) * 1000000)
                if shut > maxshut:
                    cv2.setTrackbarPos("shutterspeed (ms)", "Config", maxshut)
                cam.awb_gains = (red / 10, blue / 10)

            if cam.exposure_mode == "auto":
                cam.shutter_speed = 0
                shut = cam.exposure_speed
                red, blue = [int(float(i) * 10) for i in cam.awb_gains]
                cv2.setTrackbarPos("shutterspeed (ms)", "Config", shut)
                cv2.setTrackbarPos("red gain", "Config", red)
                cv2.setTrackbarPos("blue gain", "Config", blue)

            cam.brightness = bri
            cam.contrast = con - 100
            cam.saturation = sat - 100
            cam.sharpness = shar - 100

            cv2.imshow("Stream", image)
            stream.truncate(0)

            k = cv2.waitKey(10) & 0xFF
            if k == ord("s"):
                if cam.shutter_speed == 0:
                    shutterspeed = cam.exposure_speed
                else:
                    shutterspeed = cam.shutter_speed
                gains = tuple([round(float(i), 2) for i in cam.awb_gains])
                config = {
                    "automode": [False, True][auto],
                    "vidfps": cam.framerate,
                    "rotation": cam.rotation,
                    "gains": gains,
                    "brightness": cam.brightness,
                    "contrast": cam.contrast,
                    "saturation": cam.saturation,
                    "iso": cam.iso,
                    "sharpness": cam.sharpness,
                    "compensation": cam.exposure_compensation,
                    "shutterspeed": shutterspeed
                }
                break
            if k == 27:
                lineprint("User exited..")
                break

        rawCapture.close()
        cam.close()
        cv2.destroyAllWindows()
        cv2.waitKey(1)

        return config
Exemplo n.º 23
0
    def record(self):

        """
        Starts a recording as configured and returns either one or multiple
        .h264 or .jpg files that are named automatically according to the label,
        the host name, date, time and potentially session number or count nr.

        Example output files:
        rectype = "img" : test_180312_pi13_101300.jpg
        rectype = "vid" : test_180312_pi13_102352.h264
        rectype = "imgseq" : test_180312_pi13_img00231_101750.jpg
        rectype = "vidseq" : test_180312_pi13_101810_S01.h264
        """

        self._setup_cam()
        self._namefile()

        if self.config.rec.rectype == "img":

            self.filename = self.filename + strftime("%H%M%S") + self.filetype
            self.cam.capture(self.filename, format="jpeg", resize = self.resize,
                             quality = self.config.img.imgquality)
            lineprint("Captured "+self.filename)

        elif self.config.rec.rectype == "imgseq":

            timepoint = datetime.now()
            for i, img in enumerate(self.cam.capture_continuous(self.filename,
                                    format="jpeg", resize = self.resize,
                                    quality = self.config.img.imgquality)):
                if i < self.config.img.imgnr-1:
                    timepassed = (datetime.now() - timepoint).total_seconds()
                    delay = max(0, self.config.img.imgwait - timepassed)
                    lineprint("Captured "+img+", sleeping "+str(round(delay,2))+"s..")
                    sleep(delay)
                    timepoint = datetime.now()
                else:
                    lineprint("Captured "+img)
                    break

        elif self.config.rec.rectype in ["vid","vidseq"]:

            # Temporary fix for flicker at start of (first) video
            self.cam.start_recording(BytesIO(), format = "h264",
                                     resize = self.resize, level = "4.2")
            self.cam.wait_recording(2)
            self.cam.stop_recording()

            for session in ["_S%02d" % i for i in range(1,999)]:
                session = "" if self.config.rec.rectype == "vid" else session
                filename = self.filename+strftime("%H%M%S")+session+self.filetype
                self.cam.start_recording(filename, resize = self.resize,
                                         quality = self.config.vid.vidquality,
                                         level = "4.2")
                lineprint("Start recording "+filename)
                self.cam.wait_recording(self.config.vid.vidduration+self.config.vid.viddelay)
                self.cam.stop_recording()
                lineprint("Finished recording "+filename)
                if self.config.rec.rectype == "vid":
                    break
                else:
                    msg = "\nPress Enter for new session, or e and Enter to exit: "
                    if input(msg) == "e":
                        break
        self.cam.close()
Exemplo n.º 24
0
    def __init__(self,
                 indir="",
                 outdir="",
                 type=".h264",
                 withframe=False,
                 overwrite=False,
                 delete=False,
                 pools=4,
                 resizeval=1,
                 fps=None,
                 imgfps=25,
                 internal=False,
                 sleeptime=None,
                 interrupt=True):

        if internal:
            lineprint("Running convert function..")

        global interrupted
        interrupted = False
        if interrupt:

            def keythread():
                global interrupted
                input()
                interrupted = True

            Thread(target=keythread).start()

        self.indir = os.getcwd() if indir == "" else indir
        assert os.path.exists(self.indir), "in-directory does not exist.."
        self.outdir = self.indir if outdir == "" else outdir
        assert os.path.exists(self.outdir), "out-directory does not exist.."

        self.rootdir = os.getcwd()
        os.chdir(self.indir)

        self.type = type
        self.withframe = withframe
        self.delete = delete
        self.pools = int(pools)
        self.resizeval = float(resizeval)
        self.fps = int(fps) if fps is not None else None
        self.imgfps = int(imgfps)

        while not interrupted:
            files = listfiles(self.indir, self.type, keepdir=False)
            old = listfiles(self.indir, self.type, keepext=False)
            new = listfiles(self.outdir, ".mp4", keepext=False)
            self.todo = files
            if not overwrite:
                self.todo = [
                    files[i] for i, file in enumerate(old) if file not in new
                ]
            if self.type in [".jpg", ".jpeg", ".png"] and len(self.todo) > 0:
                if len([f for f in new if commonpref(self.todo) in f]) > 0:
                    self.todo = []
            self.convertpool()
            msg = "No files to convert.."
            if sleeptime == None:
                lineprint(msg)
                break
            else:
                lineprint(msg + " rechecking in " + str(sleeptime) + "s..")
                time.sleep(sleeptime)
Exemplo n.º 25
0
    def __init__(self, configfile="pirecorder.conf", logging=True):

        if not isrpi():
            lineprint("PiRecorder only works on a raspberry pi. Exiting..")
            return

        self.system = "auto"
        self.host = gethostname()
        self.home = homedir()
        self.setupdir = self.home + "pirecorder"
        self.logfolder = self.setupdir + "/logs/"
        if not os.path.exists(self.logfolder):
            os.makedirs(self.setupdir)
            os.makedirs(self.logfolder)
            lineprint("Setup folder created (" + self.setupdir + ")..")
        if not os.path.exists(self.logfolder):
            lineprint("Setup folder exists but was not set up properly..")

        if logging:
            self.log = Logger(self.logfolder + "/pirecorder.log")
            self.log.start()
            print("")

        lineprint("pirecorder " + __version__ + " started!", date=True)
        lineprint("=" * 47, False)

        self.brightfile = self.setupdir + "/cusbright.yml"
        self.configfilerel = configfile
        self.configfile = self.setupdir + "/" + configfile

        self.config = LocalConfig(self.configfile, compact_form=True)
        if not os.path.isfile(self.configfile):
            lineprint("Config file " + configfile +
                      " not found, new file created..")
            for section in ["rec", "cam", "cus", "img", "vid"]:
                if section not in list(self.config):
                    self.config.add_section(section)
            self.settings(recdir="pirecorder/recordings",
                          subdirs=False,
                          label="test",
                          rectype="img",
                          rotation=0,
                          brighttune=0,
                          roi=None,
                          gains=(1.0, 2.5),
                          brightness=45,
                          contrast=10,
                          saturation=0,
                          iso=200,
                          sharpness=0,
                          compensation=0,
                          shutterspeed=8000,
                          imgdims=(2592, 1944),
                          maxres=None,
                          viddims=(1640, 1232),
                          imgfps=1,
                          vidfps=24,
                          imgwait=5.0,
                          imgnr=12,
                          imgtime=60,
                          imgquality=50,
                          vidduration=10,
                          viddelay=10,
                          vidquality=11,
                          automode=True,
                          internal="",
                          maxviddur=3600,
                          maxvidsize=0)
            lineprint("Config settings stored..")

        else:
            lineprint("Config file " + configfile + " loaded..")
            lineprint("Recording " + self.config.rec.rectype + " in " +\
                          self.home + self.config.rec.recdir)

        self._imgparams()
        self._shuttertofps()
        if self.config.rec.rectype == "imgseq":
            if self.config.cam.shutterspeed / 1000000. >= (
                    self.config.img.imgwait / 5):
                lineprint("imgwait is not enough for provided shutterspeed" + \
                          ", will be overwritten..")

        if self.config.rec.recdir == "NAS":
            if not os.path.ismount(self.config.rec.recdir):
                self.recdir = self.home
                lineprint("Recdir not mounted, storing in home directory..")
        self.recdir = self.home + self.config.rec.recdir
        if not os.path.exists(self.recdir):
            os.makedirs(self.recdir)

        os.chdir(self.recdir)
Exemplo n.º 26
0
    def draw_stream(self):

        lineprint("Streaming video..")

        self.vid = VideoIn(system=self.system, framerate=self.framerate,
                           vidsize=self.vidsize, rotation=self.rotation,
                           maxres=self.maxres)
        self.vid.start()

        if self.overlay:
            h, w, _ = self.vid.read().shape
            self.overlayimg = imgresize(self.overlayimg, resize=1, dims=(w,h))

        while True:
            self.img = self.vid.read()

            if self.overlay:
                overlay = self.img.copy()
                overlay[0:h,0:w] = self.overlayimg
                cv2.addWeighted(overlay, self.alpha, self.img, 1-self.alpha, 0, self.img)
            if self.cross:
                draw.draw_cross(self.img, pt2 = self.vid.res)
            if self.m.twoPoint is not None:
                draw.draw_crosshair(self.img, self.m.pos)
            if self.m.posDown is not None:
                cv2.rectangle(self.img, self.m.posDown, self.m.pos,
                              self.tempcol, 2)
            if self.m.posUp is not None:
                cv2.rectangle(self.img, self.m.pts[-1], self.m.posUp,
                              self.col, 2)
            cv2.imshow("Image", self.img)

            k = cv2.waitKey(self.waitms) & 0xFF
            if k == ord("o"):
                self.overlay = not self.overlay
            if k == ord("c"):
                self.cross = not self.cross
            if k == ord("f"):
                self.fullscreen = not self.fullscreen
                if self.fullscreen:
                    cv2.setWindowProperty("Image", cv2.WND_PROP_FULLSCREEN,
                                          cv2.WINDOW_FULLSCREEN)
                else:
                    cv2.setWindowProperty("Image", cv2.WND_PROP_AUTOSIZE,
                                          cv2.WINDOW_NORMAL)
                    cv2.resizeWindow("Image", self.vid.res[0], self.vid.res[1])
            if k == ord("s"):
                if self.m.twoPoint is not None:
                    self.m.twoPoint = checkroi(self.m.twoPoint, self.vid.res)
                    self.roi = roi_to_zoom(self.m.twoPoint, self.vid.res)
                    if self.internal is True:
                        lineprint("roi "+str(self.roi)+" stored..")
                    else:
                        lineprint("roi: "+str(self.roi))
                else:
                    lineprint("Nothing selected..")
            if k == ord("e"):
                self.m.posUp = None
                self.roi = False
                lineprint("roi data erased..")
            if k == ord("z"):
                if self.m.twoPoint is not None:
                    lineprint("Creating zoomed image..")
                    self.stream = False
                    break
            if self.overlay and k == ord("["):
                self.alpha = max(self.alpha-0.05, 0)
            if self.overlay and k == ord("]"):
                self.alpha = min(self.alpha+0.05, 1)
            if k == 27:
                lineprint("User exited..")
                self.exit = True
                break

        self.vid.stop()
        time.sleep(1)
Exemplo n.º 27
0
    def record(self):
        """
        Starts a recording as configured and returns either one or multiple
        .h264 or .jpg files that are named automatically according to the label,
        the host name, date, time and potentially session number or count nr.

        Example output files:
        rectype = "img" : test_180312_pi13_101300.jpg
        rectype = "vid" : test_180312_pi13_102352.h264
        rectype = "imgseq" : test_180312_pi13_img00231_101750.jpg
        rectype = "vidseq" : test_180312_pi13_101810_S01.h264
        """

        self._setup_cam()
        self._namefile()

        if self.config.rec.rectype == "img":

            self.filename = self.filename + strftime("%H%M%S") + self.filetype
            self.cam.capture(self.filename,
                             format="jpeg",
                             resize=self.resize,
                             quality=self.config.img.imgquality)
            lineprint("Captured " + self.filename)

        elif self.config.rec.rectype == "imgseq":

            starttime = datetime.now()
            timepoint = starttime
            for i, img in enumerate(
                    self.cam.capture_continuous(
                        self.filename,
                        format="jpeg",
                        resize=self.resize,
                        quality=self.config.img.imgquality)):
                tottimepassed = (datetime.now() - starttime).total_seconds()
                if i < self.config.img.imgnr - 1 and tottimepassed < self.config.img.imgtime:
                    timepassed = (datetime.now() - timepoint).total_seconds()
                    delay = max(0, self.config.img.imgwait - timepassed)
                    lineprint("Captured " + img + ", sleeping " +
                              str(round(delay, 2)) + "s..")
                    sleep(delay)
                    timepoint = datetime.now()
                else:
                    lineprint("Captured " + img)
                    break

        elif self.config.rec.rectype in ["vid", "vidseq"]:

            # Temporary fix for flicker at start of (first) video
            self.cam.start_recording(BytesIO(),
                                     format="h264",
                                     resize=self.resize,
                                     level="4.2")
            self.cam.wait_recording(2)
            self.cam.stop_recording()

            for session in ["_S%02d" % i for i in range(1, 999)]:
                session = "" if self.config.rec.rectype == "vid" else session
                filename = self.filename + strftime("%H%M%S") + session
                timeremaining = self.config.vid.vidduration + self.config.vid.viddelay
                counter = 0
                while timeremaining > 0:
                    counter += 1
                    waittime = timeremaining
                    if self.config.vid.maxviddur > 0:
                        waittime = min(timeremaining,
                                       self.config.vid.maxviddur)
                    if waittime == timeremaining and self.config.vid.maxvidsize == 0:
                        nr = ""
                    else:
                        nr = "_v" + str(counter).zfill(2)
                    finalname = filename + nr + self.filetype
                    video = VidOutput(finalname)
                    self.cam.start_recording(
                        video,
                        resize=self.resize,
                        quality=self.config.vid.vidquality,
                        level="4.2",
                        format=self.filetype[1:])
                    lineprint("Start recording " + filename)
                    rectime = 0
                    while video.size < self.maxvidsize * 1000000 and rectime < waittime:
                        rectime += 0.1
                        self.cam.wait_recording(0.1)
                    timeremaining -= rectime
                    self.cam.stop_recording()
                    vidinfo = " (" + str(round(rectime)) + "s; " + str(
                        round(video.size / 1000000, 2)) + "MB)"
                    lineprint("Finished recording " + finalname + vidinfo)
                if self.config.rec.rectype == "vid":
                    break
                else:
                    msg = "\nPress Enter for new session, or e and Enter to exit: "
                    if input(msg) == "e":
                        break
        self.cam.close()
Exemplo n.º 28
0
    def settings(self, **kwargs):
        """
        Configure the camera and recording settings

        Parameters
        ---------------
        recdir : str, default = "pirecorder/recordings"
            The directory where media will be stored. Default is "recordings".
            If different, a folder with name corresponding to location will be
            created inside the home directory. If no name is provided (""), the
            files are stored in the home directory. If "NAS" is provided it will
            additionally check if the folder links to a mounted drive.
        subdirs : bool, default = False
            If files of individual recordings should be stored in subdirectories
            or not, to keep all files of a single recording session together.
        label : str, default = "test"
            Label that will be associated with the specific recording and stored
            in the filenames.
        rectype : ["img", "imgseq", "vid", "vidseq"], default = "img"
            Recording type, either a single image or video or a sequence of
            images or videos.
        automode : bool, default = True
            If the shutterspeed and white balance should be set automatically
            and dynamically for each recording.
        maxres : str or tuple, default = "v2"
            The maximum potential resolution of the camera used. Either provide
            a tuple of the max resolution, or use "v1.5", "v2" (default), or
            "hq" to get the maximum resolution associated with the official
            cameras directly.
        rotation : int, default = 0
            Custom rotation specific to the Raspberry Pi, should be either 0 or
            180.
        brighttune : int, default = 0
            A rpi-specific brightness compensation factor to standardize light
            levels across multiple rpi"s, an integer between -10 and 10.
        roi : tuple, default = None
            Region of interest to be used for recording. Consists of coordinates
            of top left and bottom right coordinate of a rectangular area
            encompassing the region of interest. Can be set with the set_roi()
            method.
        gains : tuple, default = (1.0, 2.5)
            Sets the blue and red gains to acquire the desired white balance.
            Expects a tuple of floating values (e.g. "(1.5, 1.85)"). Can be
            automatically set with the autoconfig() function and interactively
            with the camconfig() function using a live video stream.
        brightness : int, default = 45
            Sets the brightness level of the camera. Expects an integer value
            between 0 and 100. Higher values result in brighter images.
        contrast : int, default = 20
            Sets the contrast for the recording. Expects an integer value
            between 0 and 100. Higher values result in images with higher
            contrast.
        saturation : int, default 0
            Sets the saturation level for the recording. Expects an integer
            value between -100 and 100.
        iso : int, default = 200
            Sets the camera ISO value. Should be one of the following values:
            [100, 200, 320, 400, 500, 640, 800]. Higher values result in
            brighter images but with higher gain.
        sharpness : int, default = 50
            Sets the sharpness of the camera. Expects an integer value between
            -100 and 100. Higher values result in sharper images.
        compensation : int, default = 0
            Adjusts the camera’s exposure compensation level before recording.
            Expects a value between -25 and 25, with each increment representing
            1/6th of a stop and thereby a brighter image.
        shutterspeed : int, detault = 10000
            Sets the shutter speed of the camera in microseconds, i.e. a value
            of 10000 would indicate a shutterspeed of 1/100th of a second. A
            longer shutterspeed will result in a brighter image but more motion
            blur. Important to consider is that the framerate of the camera will
            be adjusted based on the shutterspeed. At low shutterspeeds (i.e.
            above ~ 0.2s) the required waiting time between images increases
            considerably due to the raspberry pi hardware. To control for this,
            automatically a standard `imgwait` time should be chosen that is at
            least 6x the shutterspeed. For example, for a shutterspeed of 300000
            imgwait should be > 1.8s.
        imgdims : tuple, default = (2592, 1944)
            The resolution of the images to be taken in pixels. The default is
            the max resolution for the v1.5 model, the v2 model has a max
            resolution of 3280 x 2464 pixels, and the hq camera 4056 x 3040
            pixels.
        viddims : tuple, default = (1640, 1232)
            The resolution of the videos to be taken in pixels. The default is
            the max resolution that does not return an error for this mode.
        imgfps : int, default = 1
            The framerate for recording images. Will be set automatically based
            on the imgwait setting so should not be set by user.
        vidfps : int, default = 24
            The framerate for recording video.
        imgwait : float, default = 5.0
        	The delay between subsequent images in seconds. When a delay is
            provided that is less than ~x5 the shutterspeed, the camera
            processing time will take more time than the provided imgwait
            parameter and so images are taken immideately one after the other.
            To take a sequence of images at the exact right delay interval the
            imgwait parameter should be at least 5x the shutterspeed (e.g.
            shutterspeed of 400ms needs imgwait of 2s).
        imgnr : int, default = 12
            The number of images that should be taken. When this number is
            reached, the recorder will automatically terminate.
        imgtime : integer, default = 60
            The time in seconds during which images should be taken. The minimum
            of a) imgnr and b) nr of images based on imgwait and imgtime will be
            used.
        imgquality : int, default = 50
            Specifies the quality that the jpeg encoder should attempt to
            maintain. Use values between 1 and 100, where higher values are
            higher quality.
        vidduration : int, default = 10
            Duration of video recording in seconds.
        viddelay : int, default = 0
            Extra recording time in seconds that will be added to vidduration.
            Its use is to add a standard amount of time to the video that can be
            easily cropped or skipped, such as for tracking, but still provides
            useful information, such as behaviour during acclimation.
        vidquality : int, default = 11
            Specifies the quality that the h264 encoder should attempt to
            maintain. Use values between 10 and 40, where 10 is extremely high
            quality, and 40 is extremely low.
        maxviddur : int, default = 3600
            The maximum duration in seconds for single videos, beyond which
            videos will be automatically split. A value of 0 indicates there is
            no maximum file duration.
        maxvidsize : int, default = 0
            The maximum file size in Megabytes for single videos, beyond which
            videos will be automatically split. A value of 0 indicates there is
            no maximum file size.
        """

        if "recdir" in kwargs:
            self.config.rec.recdir = kwargs["recdir"]
        if "subdirs" in kwargs:
            self.config.rec.subdirs = kwargs["subdirs"]
        if "label" in kwargs:
            self.config.rec.label = kwargs["label"]
        if "rectype" in kwargs:
            self.config.rec.rectype = kwargs["rectype"]
        if "maxres" in kwargs:
            self.config.rec.maxres = kwargs["maxres"]
            if isinstance(self.config.rec.maxres, tuple):
                self.config.img.imgdims = self.config.rec.maxres
            elif self.config.rec.maxres == "v2":
                self.config.img.imgdims = (3264, 2464)
            elif self.config.rec.maxres == "hq":
                self.config.img.imgdims = (4056, 3040)
        if "rotation" in kwargs:
            self.config.cus.rotation = kwargs["rotation"]
        if "brighttune" in kwargs:
            self.config.cus.brighttune = kwargs["brighttune"]
        if "roi" in kwargs:
            self.config.cus.roi = kwargs["roi"]
        if "gains" in kwargs:
            self.config.cus.gains = kwargs["gains"]

        if "automode" in kwargs:
            self.config.cam.automode = kwargs["automode"]
        if "brightness" in kwargs:
            self.config.cam.brightness = kwargs["brightness"]
        if "contrast" in kwargs:
            self.config.cam.contrast = kwargs["contrast"]
        if "saturation" in kwargs:
            self.config.cam.saturation = kwargs["saturation"]
        if "iso" in kwargs:
            self.config.cam.iso = kwargs["iso"]
        if "sharpness" in kwargs:
            self.config.cam.sharpness = kwargs["sharpness"]
        if "compensation" in kwargs:
            self.config.cam.compensation = kwargs["compensation"]
        if "shutterspeed" in kwargs:
            self.config.cam.shutterspeed = kwargs["shutterspeed"]

        if "imgdims" in kwargs:
            self.config.img.imgdims = kwargs["imgdims"]
        if "viddims" in kwargs:
            self.config.vid.viddims = kwargs["viddims"]
        if "imgfps" in kwargs:
            self.config.img.imgfps = kwargs["imgfps"]
        if "vidfps" in kwargs:
            self.config.vid.vidfps = kwargs["vidfps"]

        if "imgwait" in kwargs:
            self.config.img.imgwait = kwargs["imgwait"]
        if "imgnr" in kwargs:
            self.config.img.imgnr = kwargs["imgnr"]
        if "imgtime" in kwargs:
            self.config.img.imgtime = kwargs["imgtime"]
        if "imgquality" in kwargs:
            self.config.img.imgquality = kwargs["imgquality"]

        if "vidduration" in kwargs:
            self.config.vid.vidduration = kwargs["vidduration"]
        if "viddelay" in kwargs:
            self.config.vid.viddelay = kwargs["viddelay"]
        if "maxviddur" in kwargs:
            self.config.vid.maxviddur = kwargs["maxviddur"]
        if "maxvidsize" in kwargs:
            self.config.vid.maxvidsize = kwargs["maxvidsize"]

        brightchange = False
        if os.path.exists(self.brightfile):
            with open(self.brightfile) as f:
                brighttune = yaml.load(f, Loader=yaml.FullLoader)
                if brighttune != self.config.cus.brighttune:
                    if "internal" not in kwargs:
                        lineprint("cusbright.yml file found and loaded..")
                    self.config.cus.brighttune = brighttune
                    brightchange = True

        if len(kwargs) > 0 or brightchange:

            self._imgparams()
            self._shuttertofps()
            if self.config.rec.rectype == "imgseq":
                if self.config.cam.shutterspeed / 1000000. >= (
                        self.config.img.imgwait / 5):
                    lineprint("imgwait is not enough for provided shutterspeed" + \
                              ", will be overwritten..")
            self.config.save()

            if "internal" not in kwargs:
                lineprint("Config settings stored and loaded..")
Exemplo n.º 29
0
    def set_config(self, **kwargs):
        """ Dynamically sets the configuration file """

        if "recdir" in kwargs:
            self.config.rec.recdir = kwargs["recdir"]
        if "subdirs" in kwargs:
            self.config.rec.subdirs = kwargs["subdirs"]
        if "label" in kwargs:
            self.config.rec.label = kwargs["label"]
        if "rectype" in kwargs:
            self.config.rec.rectype = kwargs["rectype"]

        if "rotation" in kwargs:
            self.config.cus.rotation = kwargs["rotation"]
        if "brighttune" in kwargs:
            self.config.cus.brighttune = kwargs["brighttune"]
        if "roi" in kwargs:
            self.config.cus.roi = kwargs["roi"]
        if "gains" in kwargs:
            self.config.cus.gains = kwargs["gains"]

        if "brightness" in kwargs:
            self.config.cam.brightness = kwargs["brightness"]
        if "contrast" in kwargs:
            self.config.cam.contrast = kwargs["contrast"]
        if "saturation" in kwargs:
            self.config.cam.saturation = kwargs["saturation"]
        if "iso" in kwargs:
            self.config.cam.iso = kwargs["iso"]
        if "sharpness" in kwargs:
            self.config.cam.sharpness = kwargs["sharpness"]
        if "compensation" in kwargs:
            self.config.cam.compensation = kwargs["compensation"]
        if "shutterspeed" in kwargs:
            self.config.cam.shutterspeed = kwargs["shutterspeed"]

        if "imgdims" in kwargs:
            self.config.img.imgdims = kwargs["imgdims"]
        if "viddims" in kwargs:
            self.config.vid.viddims = kwargs["viddims"]
        if "imgfps" in kwargs:
            self.config.img.imgfps = kwargs["imgfps"]
        if "vidfps" in kwargs:
            self.config.vid.vidfps = kwargs["vidfps"]

        if "imgwait" in kwargs:
            self.config.img.imgwait = kwargs["imgwait"]
        if "imgnr" in kwargs:
            self.config.img.imgnr = kwargs["imgnr"]
        if "imgtime" in kwargs:
            self.config.img.imgtime = kwargs["imgtime"]
        if "imgquality" in kwargs:
            self.config.img.imgquality = kwargs["imgquality"]

        if "vidduration" in kwargs:
            self.config.vid.vidduration = kwargs["vidduration"]
        if "viddelay" in kwargs:
            self.config.vid.viddelay = kwargs["viddelay"]
        if "vidquality" in kwargs:
            self.config.vid.vidquality = kwargs["vidquality"]

        brightchange = False
        if os.path.exists(self.brightfile):
            with open(self.brightfile) as f:
                brighttune = yaml.load(f, Loader=yaml.FullLoader)
                if brighttune != self.config.cus.brighttune:
                    self.config.cus.brighttune = brighttune
                    brightchange = True

        if len(kwargs) > 0 or brightchange:

            self._imgparams()
            self._shuttertofps()
            self.config.save()

            if "internal" not in kwargs:
                lineprint("Config settings stored and loaded..")
Exemplo n.º 30
0
    def convertpool(self):

        if len(self.todo) > 0:

            if self.type in [".h264", ".mp4", ".avi"]:

                pool = Pool(min(self.pools, len(self.todo)))
                try:
                    pool.map(self.conv_single, self.todo)
                    pool.close()
                    lineprint("Done converting all videofiles!",
                              label="pirecorder")
                except KeyboardInterrupt:
                    lineprint("User terminated converting pool..",
                              label="pirecorder")
                    self.terminated = True
                    pool.terminate()
                    return
                except Exception as e:
                    excep = "Got exception: %r, terminating pool" % (e, )
                    lineprint(excep, label="pirecorder")
                    pool.terminate()
                finally:
                    pool.join()

                if self.delete:
                    for filein in self.todo:
                        os.remove(filein)
                    lineprint("Deleted all original videofiles..",
                              label="pirecorder")

            elif self.type in [".jpg", ".jpeg", ".png"]:

                vidname = commonpref(self.todo)
                lineprint("Start converting " + str(len(self.todo)) +
                          " images",
                          label="pirecorder")

                frame_array = []
                for filename in self.todo:
                    frame = cv2.imread(filename)
                    frame_array.append(frame)
                    #os.rename(filename, self.outdir+"/"+filename)
                h, w, _ = frame_array[0].shape
                if self.outdir != "":
                    vidname = self.outdir + "/" + os.path.basename(vidname)
                vidout = videowriter(vidname, w, h, self.imgfps,
                                     self.resizeval)
                for i in range(len(frame_array)):
                    vidout.write(frame_array[i])
                vidout.release()
                lineprint("Finished converting " + os.path.basename(vidname),
                          label="pirecorder")

            else:
                lineprint("No video or image files found..",
                          label="pirecorder")