Exemplo n.º 1
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.º 2
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.º 3
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