def test_calibration(self):
     c = Calibrator()
     BAG_NAME = '../../fish_detective/bags/2016-04-06-16-57-50.bag' #1437
     bag = rosbag.Bag(BAG_NAME)
     imgs = self.get_raw_img(bag)
     img = imgs.next()
     c.auto_calibrate(img)
 def test_get_blobs(self):
     c = Calibrator()
     test = np.array([[0,1,0,0,0],
                      [1,1,1,0,0],
                      [0,1,0,0,0],
                      [0,0,0,0,0],
                      [0,0,0,1,1]])
     blobs = c.get_blobs(test)
     self.assertEquals(2, len(blobs))
     test = np.array([[0,1,0,0,1],
                      [1,1,0,0,1],
                      [0,1,0,0,0],
                      [0,0,0,0,0],
                      [0,0,0,1,1]])
     blobs = c.get_blobs(test)
     self.assertEquals(3, len(blobs))
Beispiel #3
0
    def __init__(self, option, devinfo, coupleid, logger=None):
        """
        Mate 의 Constructor. option과 devinfo를 주요 입력으로 함.
        
        :param option: 작동을 위한 설정을 딕셔너리로 전달함
        :param devinfo: 처리하는 장비의 아이디를 딕셔너리 형식으로 전달함. 다음과 같은 형식임.
        id 는 장비의 아이디, dk 는 장비를 확인하기 위한 키값, dt는 장비의 타입, children은 하위 장비가 있는 경우에 하위 장비를 표현하기 위한 용도임.
        devinfo : [
            {"id" : "3", "dk" : "1", "dt": "nd", "children" : [
                {"id" : "4", "dk" : "0", "dt": "sen"},
                {"id" : "5", "dk" : "1", "dt": "sen"},
                {"id" : "6", "dk" : "2", "dt": "act"},
                {"id" : "7", "dk" : "3", "dt": "act/retractable/level0"}
            ]}
        ]
        :param coupleid: 커플아이디.
        :param logger: 로깅을 위한 로거. 없다면 내부적으로 만듬.
        """
        self._option = option
        print("mate initialized. ", option)

        self._coupleid = coupleid
        self._sleep = {
            "time": 3,
            "obs": 19,
            "noti": 19
        } if "sleep" not in option else option["sleep"]
        self._devinfo = DevInfo(devinfo)
        self._writecb = None
        self._executing = False
        self._connected = False
        self._msgq = None

        if "backup" in option and "prefix" in option["backup"]:
            self._backup = True
        else:
            self._backup = False

        if logger is None:
            self._logger = util.getdefaultlogger()
        else:
            self._logger = logger
        self._calibrator = Calibrator(option, self._logger)
    def __init__(self, source_name="camera", calibrate=True, create_gui=True):

        # TODO: way too much responsibility...
        # set and start the GUI, if it exists
        if create_gui:
            app = QtGui.QApplication(sys.argv)
            self.gui = init.init(self)
            sys.exit(app.exec_())

        # load source, pause, record, and play the video
        self.is_recording = False
        self.recorded = []

        # TODO: set defaults with dc .py
        self.displays = []  # update

        self.camera_gain = (
            0.0
        )  # TODO: not right yet!!! #Should they be used to control the camera directly? firewirevideo.c's feature register
        self.camera_bias = 0.0
        self.camera_gamma = 1.0
        self.camera_framerate = FirewireVideo.DC1394_FRAMERATE_15
        self.video_mode = FirewireVideo.DC1394_VIDEO_MODE_640x480_RGB8
        self.parse_args()

        self.open_source(source_name)

        # display pipeline
        # source -> corrector -> scale_offset (with autocontrast on/off?)

        # calibrate camera ??? (TODO: load calibration data if available?)

        self.calibrator = Calibrator(input=self.source, calibrate_now=calibrate)

        # keep the last ProcessObject in the pipeline
        self.source.updatePlayMode()
        self.calibrator.getOutput().update()
        self.scale_offset = Scale_Offset(input=self.calibrator.getOutput(), auto_contrast=False)
        self.output = self.scale_offset

        # TODO: manage displays with GUI, only show the final result at end
        if calibrate:
            self.add_image_display(input=self.calibrator.dark_current_image, title="dc")
            self.add_image_display(input=self.calibrator.flat_field_image * 255, title="ff")
        self.add_display(input=self.source, title="source")
        self.add_display(input=self.calibrator.getOutput(), title="corrected")
        self.add_display(input=self.output, title="microscope")

        self.main_loop()
Beispiel #5
0
        loadUi('../ui/tbs_window.ui', self.tbs_window)
        self.tbs_window.setWindowTitle('Tarım Bilgi Sistemi')

        OceanViewGui.setButtonIcon(self.tbs_window.backtohomeButton,
                                   '../ui/icon/back.png')
        self.tbs_window.backtohomeButton.clicked.connect(
            self.on_backtohomeButton_clicked)

        webView = QtWebEngineWidgets.QWebEngineView(self.tbs_window.webWidget)
        webView.setUrl(
            QtCore.QUrl("https://tbs.tarbil.gov.tr/Authentication.aspx"))
        webView.setObjectName("webView")


if __name__ == '__main__':
    config = Config('../config.ini')
    engine = Engine(
        iomanager=IOManager(),
        analyzer=Analyzer(config=config, database=Database(config)),
        calibrator=Calibrator(input_dir=config.calibration_input_dir,
                              output_dir=config.calibration_output_dir,
                              calibration_eqns=config.calibration_equation),
        config=config)

    app = QApplication(sys.argv)

    widget = OceanViewGui(engine=engine, config=config)
    widget.show()

    sys.exit(app.exec_())
class Main(object):
    """
    Runs the application, manages the GUI, manages the command line, manages the
    pipeline.

    TODO: maybe move?

    Arguments:
    TODO: decide! args: args directly taken from the command line
    source_name: string used in open_source
    """

    def __init__(self, source_name="camera", calibrate=True, create_gui=True):

        # TODO: way too much responsibility...
        # set and start the GUI, if it exists
        if create_gui:
            app = QtGui.QApplication(sys.argv)
            self.gui = init.init(self)
            sys.exit(app.exec_())

        # load source, pause, record, and play the video
        self.is_recording = False
        self.recorded = []

        # TODO: set defaults with dc .py
        self.displays = []  # update

        self.camera_gain = (
            0.0
        )  # TODO: not right yet!!! #Should they be used to control the camera directly? firewirevideo.c's feature register
        self.camera_bias = 0.0
        self.camera_gamma = 1.0
        self.camera_framerate = FirewireVideo.DC1394_FRAMERATE_15
        self.video_mode = FirewireVideo.DC1394_VIDEO_MODE_640x480_RGB8
        self.parse_args()

        self.open_source(source_name)

        # display pipeline
        # source -> corrector -> scale_offset (with autocontrast on/off?)

        # calibrate camera ??? (TODO: load calibration data if available?)

        self.calibrator = Calibrator(input=self.source, calibrate_now=calibrate)

        # keep the last ProcessObject in the pipeline
        self.source.updatePlayMode()
        self.calibrator.getOutput().update()
        self.scale_offset = Scale_Offset(input=self.calibrator.getOutput(), auto_contrast=False)
        self.output = self.scale_offset

        # TODO: manage displays with GUI, only show the final result at end
        if calibrate:
            self.add_image_display(input=self.calibrator.dark_current_image, title="dc")
            self.add_image_display(input=self.calibrator.flat_field_image * 255, title="ff")
        self.add_display(input=self.source, title="source")
        self.add_display(input=self.calibrator.getOutput(), title="corrected")
        self.add_display(input=self.output, title="microscope")

        self.main_loop()

        # TODO: move this out
        # histogram = Histogram(input=self.source)

        # TODO: open GUI, attach displays
        # create a GUI with cv.CreateTrackbar, cv.CreateButton,

    def add_display(self, input=None, title="default"):
        """
        Given a ProcessObject to draw input from, adds a display to manage.
        Arguments:
        input - ProcessObject to display
        title - unique window title
        """
        self.displays.append(Display(input=input, title=title))

    def add_image_display(self, input=None, title="image_default"):
        """
        Given a numpy image, display with a ProcessObject with title.
        Arguments:
        input - valid numpy array for ProcessObject's output
        title - unique window title to display
        """
        image = pipeline.ProcessObject(input=None, inputCount=0)
        image.getOutput(0).setData(input)
        self.add_display(input=image, title=title)

    # def print_modes():
    # def print_framerates():

    # present the user with available video modes dc1394.VIDEO_MODES
    # present the user with available frame rates dc1394.FRAMERATES
    # present the user with a way to change:
    # gain, bias, gamma

    def start_recording(self):
        """
        Clears the recorded buffer and starts recording.
        """
        self.is_recording = True
        self.recorded = []

    # TODO: save file in folder/folder name+number+.png
    def stop_recording(self):
        """
        Stops recording and offers to save, if desired.
        """
        self.is_recording = False
        filename = None
        filenumber = None
        for image in enumerate(self.recorded):
            filenumber = None
            cv2.imwrite(filename + filenumber + ".png", image)

    def main_loop(self):
        """
        Runs the main loop and the gui responsibly; update displays.
        """
        key = None
        frame = 0
        t0 = time.time()
        span = 30

        while key != 27:
            self.source.updatePlayMode()
            self.output.update()

            for display in self.displays:
                display.update()

            # record here...
            if self.is_recording:
                self.recorded.append(self.scale_offset.getInput().getOutput().getData())

            # respond to key presses
            key = cv2.waitKey(33)  # sets the framerate to ~30 frames/second

            if key >= 0:
                char = chr(key & 255)
                print "Key: ", key, char
                if char == "-":
                    scale = self.scale_offset.getScale()
                    self.scale_offset.setScale(scale * 0.95)
                    print "Scale:", str(self.scale_offset.getScale())
                elif char == "=":
                    scale = self.scale_offset.getScale()
                    self.scale_offset.setScale(scale * 1.05)
                    print "Scale:", str(self.scale_offset.getScale())
                elif char == "[":
                    offset = self.scale_offset.getOffset()
                    self.scale_offset.setOffset(offset - 5.0)
                    print "Offset:", str(self.scale_offset.getOffset())
                elif char == "]":
                    offset = self.scale_offset.getOffset()
                    self.scale_offset.setOffset(offset + 5.0)
                    print "Offset:", str(self.scale_offset.getOffset())
                elif char == "r":
                    self.scale_offset.setScale(1.0)
                    self.scale_offset.setOffset(0.0)
                    print ("scale and offset reset")
                elif char == "o":
                    auto_contrast = self.scale_offset.auto_contrast_enabled
                    self.scale_offset.auto_contrast_enabled = not (auto_contrast)
                    print "Auto contrast enabled:", str(not (auto_contrast))
                elif char == "a":
                    self.scale_offset.autocontrast()
                    print "Scale:", str(self.scale_offset.getScale())
                    print "Offset:", str(self.scale_offset.getOffset())

            frame += 1
            if frame % span == 0:
                t1 = time.time()
                print "{0:8.5f} fps".format(span / (t1 - t0))
                t0 = t1

    def open_source(self, source_name="camera"):
        """
        Opens the original video source at self.source
        Arguments:
        source_name: can be 'camera', 'microscope', or a valid directory name 
        """
        if source_name == "microscope":
            self.source = source.CameraFW(
                0, FirewireVideo.DC1394_VIDEO_MODE_640x480_RGB8, FirewireVideo.DC1394_FRAMERATE_15
            )
            self.source.getCamera().setColorAbsolute(whiteBlue=1023, whiteRed=276)
        elif source_name == "camera":
            self.source = source.CameraCV()

        # otherwise, try to open it as a directory
        else:
            # get files from directory and set the source
            path = os.curdir + os.sep + source_name
            files = os.listdir(path)
            files = [path + os.sep + filename for filename in files]
            self.source = source.FileStackReader(files)  # TODO: fail gracefully?
            self.source.setLoop()
            self.source.setPlayMode(source.ImageSource.play)
            # TODO: allow user to control video instead of infinite loop
            # TODO: increment, but at a reasonable time :|

    def parse_args(self):
        """
        Parse command line input and set appropriate options.

        Arguments:
        TODO: decide! argv: a list taken from sys.argv[1:]
        """

        short_options = "hf:c:r:g:G:b:"
        long_options = ["help", "format=", "color=", "framerate=", "gain=", "gamma=", "bias="]

        try:
            options, args = getopt.getopt(sys.argv[1:], short_options, long_options)
            for option, arg in options:
                # if option == '-f' or option == '--format':
                #    self.camera_format = arg
                # elif option == '-c' or option == '--color':
                #    self.camera_color = arg
                # elif option == '-r' or option == '--framerate':
                #    self.camera_framerate = arg
                if option == "-g" or option == "--gain":
                    self.camera_gain = arg
                elif option == "-b" or option == "--bias":
                    self.camera_bias = arg
                elif option == "-G" or option == "--gamma":
                    self.camera_gamma = arg
                elif option == "-h" or option == "--help":
                    print "Command line options:"
                    print " Camera settings:"
                    # print " -c <mode>  or --color <mode>   :"
                    # print " -r <int>   or --framerate <int>:"
                    print " -b <float> or --bias <float>   : set camera bias"
                    print " -g <float> or --gain <float>   : set camera gain"
                    print " -G <float> or --gamma <float>  : set camera gamma"
                    print ""
                    # print " -m       or --modes          : print camera modes"
                    print " -h         or --help           :  prints this help"
                    exit(0)

        except Exception as err:
            print str(err)
Beispiel #7
0
class Mate(object):
    """
    Mate의 기본형을 정의함.
    """
    def __init__(self, option, devinfo, coupleid, logger=None):
        """
        Mate 의 Constructor. option과 devinfo를 주요 입력으로 함.
        
        :param option: 작동을 위한 설정을 딕셔너리로 전달함
        :param devinfo: 처리하는 장비의 아이디를 딕셔너리 형식으로 전달함. 다음과 같은 형식임.
        id 는 장비의 아이디, dk 는 장비를 확인하기 위한 키값, dt는 장비의 타입, children은 하위 장비가 있는 경우에 하위 장비를 표현하기 위한 용도임.
        devinfo : [
            {"id" : "3", "dk" : "1", "dt": "nd", "children" : [
                {"id" : "4", "dk" : "0", "dt": "sen"},
                {"id" : "5", "dk" : "1", "dt": "sen"},
                {"id" : "6", "dk" : "2", "dt": "act"},
                {"id" : "7", "dk" : "3", "dt": "act/retractable/level0"}
            ]}
        ]
        :param coupleid: 커플아이디.
        :param logger: 로깅을 위한 로거. 없다면 내부적으로 만듬.
        """
        self._option = option
        print("mate initialized. ", option)

        self._coupleid = coupleid
        self._sleep = {
            "time": 3,
            "obs": 19,
            "noti": 19
        } if "sleep" not in option else option["sleep"]
        self._devinfo = DevInfo(devinfo)
        self._writecb = None
        self._executing = False
        self._connected = False
        self._msgq = None

        if "backup" in option and "prefix" in option["backup"]:
            self._backup = True
        else:
            self._backup = False

        if logger is None:
            self._logger = util.getdefaultlogger()
        else:
            self._logger = logger
        self._calibrator = Calibrator(option, self._logger)

    def __repr__(self):
        return "{}({},{})".format(self.__class__.__name__, str(self._option),
                                  str(self._devinfo))

    def start(self, _writecb):
        """ Mate가 시작할때 호출됨 """
        self._executing = True
        self._writecb = _writecb
        return True

    def stop(self):
        """ Mate가 중지될때 호출됨 """
        self._executing = False
        return True

    def connect(self):
        self._connected = True
        return True

    def close(self):
        self._connected = False

    def getvalue(self, k, v):
        """ 
        센서값을계산할때 사용함
        Calibrator 를 사용하며, 설정이 없는 경우 raw 값을 돌려줌
        """
        return self._calibrator.calculate(k, v)

    def isexecuting(self):
        """ Mate가 작동중인지를 확인함 """
        return self._executing

    def isconnected(self):
        """ Mate가 연결되어 있는지를 확인함 """
        return self._connected

    def writeblk(self, blk):
        """ 외부에서 데이터 전달을 위해 호출되는 메소드. """
        # external callback
        print("###message : ", blk.get())

    def readmsg(self):
        """ Mate가 메세지를 읽는 함수. 직접구현해야함.  """
        self._msgq = [MBlock(0, BlkType.NONE, None)]

    def backup(self, blk):
        fname = "backup/" + self._option["backup"][
            "prefix"] + "-" + datetime.now().strftime("%Y%d%m") + ".bak"
        with open(fname, "a") as fp:
            fp.write(blk.stringify() + "\n")

    def writecb(self, blk):
        self._writecb(blk)
        # backup
        if self._backup:
            self.backup(blk)

    def sendobs(self):
        """ 관측치를 전송한다. writecb를 사용함. """
        pass

    def sendnoti(self):
        """ 노티를 전송한다. writecb를 사용함. """
        pass

    def run(self):
        print("mate run ... sleep : ", self._sleep["time"])
        scnt = 0
        while self.isexecuting():
            try:
                while self.isexecuting() == True and self.isconnected(
                ) == False:
                    if self.connect() == False:
                        self._logger.info(
                            "sleep 10 seconds and try to connect")
                        time.sleep(10)
                    else:
                        self._logger.info("reconnected!!")

                if self.isexecuting() == False:
                    break

                time.sleep(self._sleep["time"])
                self.readmsg()

                if scnt % self._sleep["obs"] == 0:
                    self.sendobs()
                if scnt % self._sleep["noti"] == 0:
                    self.sendnoti()

                scnt = scnt + 1
            except Exception as ex:
                self._logger.warn("There is an exception : " + str(ex))
                self._logger.warn(str(traceback.format_exc()))
                try:
                    self.close()
                except:
                    pass

        print("mate stop")