Exemple #1
0
    def thread_init(self):
        logging.info("Starting camera")

        if self._video_file is None:
            cam = V4L2Camera(0, target_fps=10, target_resolution=(560, 420))
        else:
            cam = MovieVirtualCamera(self._video_file)

        logging.info("Building ROIs")
        roi_builder = SleepMonitorWithTargetROIBuilder()
        rois = roi_builder(cam)

        logging.info("Initialising monitor")

        metadata = {
            "machine_id": self._info["machine_id"],
            "date_time": self._info["time"],
            "frame_width": cam.width,
            "frame_height": cam.height,
            "psv_version": pkg_resources.get_distribution("ethoscope").version
        }

        self._monit = Monitor(cam,
                              AdaptiveBGModel,
                              rois,
                              result_dir=self._result_dir,
                              metadata=metadata,
                              *self._monit_args,
                              **self._monit_kwargs)
Exemple #2
0
    def test_all(self):
        cam = MovieVirtualCamera(INPUT_VIDEO, use_wall_clock=False)

        roi_builder = TestROIBuilder
        rois = roi_builder(cam)

        # logging.info("Initialising monitor")

        cam.restart()

        metadata = {
            "machine_id": "None",
            "machine_name": "None",
            "date_time":
            cam.start_time,  #the camera start time is the reference 0
            "frame_width": cam.width,
            "frame_height": cam.height,
            "version": "whatever"
        }

        draw_frames = True

        monit = Monitor(cam,
                        AdaptiveBGModel,
                        rois,
                        draw_every_n=1,
                        draw_results=draw_frames,
                        video_out=OUTPUT_VIDEO)

        try:
            with SQLiteResultWriter(OUTPUT_DB, rois, metadata) as rw:
                logging.info("Running monitor")
                monit.run(rw)
        except KeyboardInterrupt:
            monit.stop()
Exemple #3
0
cam = MovieVirtualCamera("/stk/pysolo_video_samples/sleepdep_150min_night.avi")


rb = ImgMaskROIBuilder("/stk/pysolo_video_samples/maskOf_sleepdep_150min_night.png")

rois = rb(cam)



# inters = [SystemPlaySoundOnStop(500 + i * 30) for i in range(13)]
inters = [SystemPlaySoundOnStop(500 + i * 30) for i in range(13)]




monit = Monitor(cam, AdaptiveBGModel, interactors= inters, roi_builder=rb)
monit.run()

#
# for t,img in cam:
#     for i,r in enumerate(rois):
#         sub_img, mask = r(img)
#
#         cv2.imshow(str(i), sub_img)
#     cv2.imshow(str(i), img)
#     cv2.waitKey(1)
#



__author__ = 'quentin'

from ethoscope.tracking.monitor import Monitor
from ethoscope.tracking.cameras import MovieVirtualCamera
from ethoscope.tracking.trackers import AdaptiveMOGTracker
from ethoscope.tracking.interactors import SystemPlaySoundOnStop

# cam = MovieVirtualCamera("/home/quentin/Desktop/drosoAdult_short.avi")
cam = MovieVirtualCamera(
    "/stk/pysolo_video_samples/singleDamTube2_150min_night.avi")

inter = SystemPlaySoundOnStop(1000)
monit = Monitor(cam, AdaptiveMOGTracker, interactors=[inter])
monit.run()
Exemple #5
0
class ControlThread(Thread):

    _result_dir_basename = "results/"
    _last_img_file = "last_img.jpg"
    _dbg_img_file = "dbg_img.png"
    _log_file = "psv.log"

    _default_monitor_info = {
        "last_positions": None,
        "last_time_stamp": 0,
        "result_files": []
    }

    def __init__(self,
                 machine_id,
                 name,
                 psv_dir,
                 video_file=None,
                 *args,
                 **kwargs):
        self._monit_args = args
        self._monit_kwargs = kwargs

        # We wipe off previous data
        shutil.rmtree(psv_dir, ignore_errors=True)

        self._result_dir = os.path.join(psv_dir, self._result_dir_basename)
        os.makedirs(self._result_dir)

        #self._result_file = os.path.join(result_dir, self._result_db_name)
        self._video_file = video_file

        if name.find('SM') == 0:
            type_of_device = 'sm'
        elif name.find('SD') == 0:
            type_of_device = 'sd'
        else:
            type_of_device = 'Unknown'

        self._info = {
            "status": "stopped",
            "time": time.time(),
            "error": None,
            "log_file": os.path.join(psv_dir, self._log_file),
            "dbg_img": os.path.join(psv_dir, self._dbg_img_file),
            "last_drawn_img": os.path.join(psv_dir, self._last_img_file),
            "machine_id": machine_id,
            "name": name,
            "type": type_of_device,
            "monitor_info": self._default_monitor_info,
        }

        logging.basicConfig(filename=self._info["log_file"],
                            level=logging.INFO)

        logger = logging.getLogger()
        logger.handlers[0].stream.close()
        logger.removeHandler(logger.handlers[0])

        file_handler = logging.FileHandler(self._info["log_file"])
        file_handler.setLevel(logging.INFO)
        formatter = logging.Formatter(
            "%(asctime)s %(filename)s, %(lineno)d, %(funcName)s: %(message)s")
        file_handler.setFormatter(formatter)
        logger.addHandler(file_handler)

        self._monit = None
        super(ControlThread, self).__init__()

    @property
    def info(self):
        self._update_info()
        return self._info

    def _update_info(self):
        if self._monit is None:
            return
        t = self._monit.last_time_stamp
        p = self._monit.last_positions

        r = glob.glob(os.path.join(self._monit.result_dir, "*"))
        r = [s for s in r if not s.endswith("-journal")]

        pos = {}
        for k, v in list(p.items()):
            pos[k] = dict(v)
            pos[k]["roi_idx"] = k

        if not t is None and not r is None and not p is None:
            self._info["monitor_info"] = {
                "last_positions": pos,
                "last_time_stamp": t,
                "result_files": r
            }

        f = self._monit.last_drawn_frame
        if not f is None:
            cv2.imwrite(self._info["last_drawn_img"], f)

    def run(self):
        try:
            logging.info("Starting Monitor thread")
            self._info["status"] = "running"
            self._info["error"] = None
            self._info["time"] = time.time()
            self.thread_init()
            logging.info("Starting monitor")
            self._monit.run()
            logging.info("Stopping Monitor thread")
            self.stop()

        except EthoscopeException as e:
            if e.img is not None:
                cv2.imwrite(self._info["dbg_img"], e.img)
            self.stop(traceback.format_exc())
        except Exception as e:
            self.stop(traceback.format_exc())

    def thread_init(self):
        logging.info("Starting camera")

        if self._video_file is None:
            cam = V4L2Camera(0, target_fps=10, target_resolution=(560, 420))
        else:
            cam = MovieVirtualCamera(self._video_file)

        logging.info("Building ROIs")
        roi_builder = SleepMonitorWithTargetROIBuilder()
        rois = roi_builder(cam)

        logging.info("Initialising monitor")

        metadata = {
            "machine_id": self._info["machine_id"],
            "date_time": self._info["time"],
            "frame_width": cam.width,
            "frame_height": cam.height,
            "psv_version": pkg_resources.get_distribution("ethoscope").version
        }

        self._monit = Monitor(cam,
                              AdaptiveBGModel,
                              rois,
                              result_dir=self._result_dir,
                              metadata=metadata,
                              *self._monit_args,
                              **self._monit_kwargs)

    def stop(self, error=None):

        logging.info("Stopping monitor")
        if not self._monit is None:
            self._monit.stop()
            self._monit = None

        self._info["status"] = "stopped"
        self._info["time"] = time.time()
        self._info["error"] = error
        self._info["monitor_infos"] = self._default_monitor_info
        if error is not None:
            logging.error("Monitor closed with an error:")
            logging.error(error)
        else:
            logging.info("Monitor closed all right")

    def __del__(self):
        self.stop()
    parser.add_option("-d",
                      "--duration",
                      dest="duration",
                      help="The maximal duration of the monitoring (seconds). "
                      "Keyboard interrupt can be use to stop before",
                      default=None,
                      type="int")

    (options, args) = parser.parse_args()

    option_dict = vars(options)

    cam = MovieVirtualCamera(
        "/data/pysolo_video_samples/motion_in_dark_one_tube_at_a_time.avi")

    roi_builder = SleepDepROIBuilder()
    # roi_builder = SleepMonitorWithTargetROIBuilder()

    rois = roi_builder(cam)

    monit = Monitor(
        cam,
        AdaptiveBGModel,
        rois,
        out_file=option_dict["out"],  # save a csv out
        max_duration=option_dict["duration"],  # when to stop (in seconds)
        video_out=option_dict["result_video"],  # when to stop (in seconds)
        draw_results=True,  # draw position on image
        draw_every_n=1)  # only draw 1 every 10 frames to save time
    monit.run()
Exemple #7
0
__author__ = 'quentin'

from ethoscope.tracking.roi_builders import SleepDepROIBuilder
from ethoscope.tracking.cameras import MovieVirtualCamera
from ethoscope.tracking.monitor import Monitor

from ethoscope.tracking.trackers import AdaptiveBGModel
from ethoscope.tracking.interactors import SystemPlaySoundOnStop
from ethoscope.tracking.interactors import SleepDepInteractor
from ethoscope.hardware_control.arduino_api import SleepDepriverInterface

cam = MovieVirtualCamera(
    "/stk/pysolo_video_samples/motion_in_dark_one_tube_at_a_time.avi")
#cam = MovieVirtualCamera("/stk/pysolo_video_samples/long_realistic_recording_with_motion.avi")
#cam = MovieVirtualCamera("/stk/pysolo_video_samples/long_realistic_recording.avi")
# sdi = SleepDepriverInterface()
roi_builder = SleepDepROIBuilder()
rois = roi_builder(cam)

inters = [SystemPlaySoundOnStop(500 + i * 30) for i in range(len(rois))]
# inters = [SleepDepInteractor(i, sdi) for i in range(len(rois))]

monit = Monitor(cam,
                AdaptiveBGModel,
                rois,
                interactors=inters,
                draw_results=True)
monit.run()

#