def test_stimulator(StimulatorClass, InterfaceClass, remove_db_file=True, *args, **kwargs): tmp = tempfile.mkstemp(suffix="_ethoscope_test.db")[1] print("Making a tmp db: " + tmp) cam = MovieVirtualCamera(VIDEO, drop_each=15) rb = SleepMonitorWithTargetROIBuilder() rois = rb.build(cam) cam.restart() connection = HardwareConnection(InterfaceClass) try: # stimulators = [MockSDStimulator(connection,min_inactive_time= 10) for _ in rois ] stimulators = [ StimulatorClass(connection, *args, **kwargs) for _ in rois ] mon = Monitor(cam, AdaptiveBGModel, rois, stimulators=stimulators) drawer = DefaultDrawer(draw_frames=DRAW_FRAMES) with SQLiteResultWriter(tmp, rois) as rw: mon.run(result_writer=rw, drawer=drawer) # cred = {"name": "ethoscope_db", # "user": "******", # "password": "******"} # with ResultWriter( cred , rois) as rw: # mon.run(result_writer=rw, drawer=drawer) finally: if remove_db_file: print("Removing temp db (" + tmp + ")") os.remove(tmp) else: print("db file lives in (" + tmp + ")") connection.stop()
def test_API(self): random.seed(1) cam = MovieVirtualCamera(VIDEO) rb = SleepMonitorWithTargetROIBuilder() rois = rb.build(cam) hc = HardwareConnection(MockInterface) stimulators = [MockStimulator(hc) for _ in rois] cam.restart() mon = Monitor(cam, AdaptiveBGModel, rois, stimulators) drawer = DefaultDrawer(draw_frames=DRAW_FRAMES) tmp = tempfile.mkstemp(suffix="_ethoscope_test.db")[1] try: print("Making a tmp db: " + tmp) with SQLiteResultWriter(tmp, rois) as rw: mon.run(result_writer=rw, drawer=drawer) except: self.fail("testAPI raised ExceptionType unexpectedly!") finally: hc.stop() cam._close() print("Removing temp db (" + tmp + ")") os.remove(tmp)
def __init__(self, port=None): self._serial = MockSerial() class MockSDInterface(MockLynxMotionInterface, SleepDepriverInterface): pass class MockSDStimulator(SleepDepStimulator): _HardwareInterfaceClass = MockSDInterface tmp = tempfile.mkstemp(suffix="_ethoscope_test.db")[1] print("Making a tmp db: " + tmp) cam = MovieVirtualCamera(VIDEO, drop_each=15) rb = SleepMonitorWithTargetROIBuilder() rois = rb.build(cam) cam.restart() connection = HardwareConnection(MockSDInterface) stimulators = [ MockSDStimulator(connection, min_inactive_time=10) for _ in rois ] mon = Monitor(cam, AdaptiveBGModel, rois, stimulators=stimulators) drawer = DefaultDrawer(draw_frames=DRAW_FRAMES) try: with SQLiteResultWriter(tmp, rois) as rw: mon.run(result_writer=rw, drawer=drawer) finally:
#sub_grey = grey[st_y:st_y+50, st_x:st_x+50] contours,hierarchy = cv2.findContours(thr_im, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) if len(contours) == 0: continue x,y,w,h = cv2.boundingRect(contours[0]) sub_grey= sub_grey[y : y + h, x : x + w] # cv2.imshow("t",sub_grey) # cv2.waitKey(30) sub_grey= cv2.resize(sub_grey, (24,24)) x_arrstr = np.char.mod('%i', sub_grey) out = ", ".join(list(x_arrstr.flatten())) + "\n" self._path.write(out) # change these three variables according to how you name your input/output files INPUT_VIDEO = "/home/quentin/comput/ethoscope-git/src/ethoscope/tests/integration_server_tests/test_video.mp4" cam = MovieVirtualCamera(INPUT_VIDEO) nm = NegativeMaker() nm.run(cam)
parser.add_option("-p", "--prefix", dest="prefix", help="The prefix for result dir") (options, args) = parser.parse_args() option_dict = vars(options) INPUT = option_dict["input"] OUTPUT = os.path.splitext(INPUT)[0] + ".db" OUTPUT = option_dict["prefix"] + "/" + OUTPUT try: os.makedirs(os.path.dirname(OUTPUT)) except OSError: pass print INPUT + " ===> " + OUTPUT cam = MovieVirtualCamera(INPUT) rois = SleepMonitorWithTargetROIBuilder().build(cam) drawer = DefaultDrawer(draw_frames=True) mon = Monitor(cam, AdaptiveBGModel, rois) #fixme date = datetime.datetime.strptime("2016-05-03_08-25-02", "%Y-%m-%d_%H-%M-%S") ts = int(calendar.timegm(date.timetuple())) #todo parse metadata from filename # metadata = {} with SQLiteResultWriter(OUTPUT, rois) as rw: mon.run(rw, drawer)
#cv2.imwrite(os.path.join(self._path, "positive_%09d.png" % (self._idx)), sub_grey) self._idx += 1 class ABGMImageSaver(AdaptiveBGModel): fg_model = ObjectModelImageSaver() # change these three variables according to how you name your input/output files INPUT_VIDEO = "/home/quentin/comput/ethoscope-git/src/ethoscope/tests/integration_server_tests/test_video.mp4" OUTPUT_VIDEO = "/tmp/my_output.avi" OUTPUT_DB = "/tmp/results.db" # We use a video input file as if it was a "camera" cam = MovieVirtualCamera(INPUT_VIDEO) # here, we generate ROIs automatically from the targets in the images roi_builder = SleepMonitorWithTargetROIBuilder() rois = roi_builder.build(cam) # Then, we go back to the first frame of the video cam.restart() # we use a drawer to show inferred position for each animal, display frames and save them as a video drawer = DefaultDrawer(OUTPUT_VIDEO, draw_frames=True) # We build our monitor monitor = Monitor(cam, ABGMImageSaver, rois) # Now everything ius ready, we run the monitor with a result writer and a drawer with SQLiteResultWriter(OUTPUT_DB, rois) as rw:
def main(argv): parser = ArgumentParser( description='Runs an Ethoscope machinery on the given video file,' + ' which is meant to be a recording of single daphnia moving' + ' in a bunch of wells.' + ' The area of each well is determined by non-black regions' + ' in the supplied regions of interest (ROI) image file.' + ' Optionally an output video may be produced, documenting the ' + ' detection of animals.') parser.add_argument("-i", "--input-video", dest="inp_video_filename", required=True, help="The video file to be processed.", metavar='<input video filename>') parser.add_argument("-o", "--output-db", dest="db_filename", required=True, help="Create Sqlite DB file for storing results.", metavar='<output DB filename>') parser.add_argument( "-r", "--roi_image", dest="roi_filename", required=True, help="Create Sqlite DB file DB_FILENAME for storing results.", metavar='<roi image>') parser.add_argument("-a", "--output-video", dest="outp_video_filename", help="The annotated output video file.", metavar='<output video filename>') parser.add_argument( "-b", "--single-roi-video", dest="single_roi_video_filename", help= "For debugging purpose a video file of a single roi may be produced.", metavar='<single roi video filename>') parser.add_argument( "-n", "--single-roi-video-roi-nbr", dest="single_roi_video_roi_nbr", type=int, default=0, help="Select the number of the roi to produce a debugging video from." + " If not specified, roi 0 is the default.", metavar='<single roi video roi number>') args = parser.parse_args() # change these variables according to how you name your input/output files INPUT_DATA_DIR = "/home/lukas/tmp/AAA-Video/" OUTPUT_DATA_DIR = "/home/lukas/tmp/ethoscope/" #ROI_IMAGE = INPUT_DATA_DIR + "irbacklit_20200109_1525_4x4x6_squaremask_valued.png" ROI_IMAGE = INPUT_DATA_DIR + "4xWellplates4x6_registred_squaremask_tight.png" #INPUT_VIDEO = INPUT_DATA_DIR + "Basler_acA5472-17um__23065142__20200109_152536071.mp4" INPUT_VIDEO = INPUT_DATA_DIR + "Basler_acA5472-17um__23065142__20200205_172106124.mp4" logfile = OUTPUT_DATA_DIR + '20200205.log' OUTPUT_VIDEO = OUTPUT_DATA_DIR + "20200205.avi" OUTPUT_DB = OUTPUT_DATA_DIR + "results20200205.db" #logfile = OUTPUT_DATA_DIR + 'results.log' #OUTPUT_VIDEO = OUTPUT_DATA_DIR + "output.avi" #OUTPUT_DB = OUTPUT_DATA_DIR + "output.db" dbgImgWinSizeX = 2200 dbgImgWinSizeY = 1500 # setup logging logging.basicConfig(filename=logfile, level=logging.INFO) #logging.basicConfig(filename=logfile, level=logging.DEBUG) # define a Handler which writes INFO messages or higher to the sys.stderr console = logging.StreamHandler() console.setLevel(logging.INFO) # add the handler to the root logger logging.getLogger('').addHandler(console) # Make the ethoscope packages accessible package_path = os.path.join(os.path.dirname(sys.path[0]), '') logging.info("path of ethoscope package: %s" % package_path) sys.path.insert(0, package_path) import cv2 # import the bricks from ethoscope package # Use a mask image to define rois. Mask image must have black background, every non-black # region defines a roi. from ethoscope.roi_builders.img_roi_builder import ImgMaskROIBuilder from ethoscope.core.monitor import Monitor from ethoscope.trackers.adaptive_bg_tracker import AdaptiveBGModel from ethoscope.utils.io import SQLiteResultWriter from ethoscope.hardware.input.cameras import MovieVirtualCamera from ethoscope.drawers.drawers import DefaultDrawer # Generate ROIs from the mask image logging.info("reading roi mask") roi_builder = ImgMaskROIBuilder(args.roi_filename) logging.info("building rois from \"%s\"" % args.roi_filename) roi_builder.build( None) # use image already loaded by ImgMaskROIBuilder instance rois = roi_builder.gridSort(50, 50) #for r in rois: # print("Roi %d: value: %d, (%d,%d)" % (r.idx, r._value, r._rectangle[0], r._rectangle[1])) # We use a video input file as if it were a camera cam = MovieVirtualCamera(args.inp_video_filename) logging.info("Loading \"%s\"-encoded movie with %d FPS of duration %d s." % (cam.fourcc, cam.frames_per_sec, cam._total_n_frames / cam.frames_per_sec)) # we use a drawer to show inferred position for each animal, display frames and save them as a video do_draw_frames = False if args.outp_video_filename is not None: do_draw_frames = True drawer = DefaultDrawer(args.outp_video_filename, draw_frames=do_draw_frames, framesWinSizeX=dbgImgWinSizeX, framesWinSizeY=dbgImgWinSizeY) # We build our monitor #monitor = Monitor(cam, AdaptiveBGModel, rois) if args.single_roi_video_filename is not None: monitor = Monitor( cam, AdaptiveBGModel, rois, dbg_roi_value=args.single_roi_video_roi_nbr, dbg_roi_video_filename=args.single_roi_video_filename) else: monitor = Monitor(cam, AdaptiveBGModel, rois) # Now everything is ready, we run the monitor with a result writer and a drawer logging.info("run monitor with drawer") with SQLiteResultWriter(args.db_filename, rois) as rw: monitor.run(rw, drawer)
import optparse import os import cv2 OUT_DIR = "/data/ethoscope_female_videos/rois/" IN_AVI_FILE = "/data/ethoscope_female_videos/whole_2016-04-18_14-06-08_035aeeee10184bb39b0754e75cef7900__1920x1080@25_00000.mp4" REF_ROI_IMG = "/data/ethoscope_female_videos/whole_2016-04-18_14-06-08_035aeeee10184bb39b0754e75cef7900__1920x1080@25_00000.png" MAX_TIME = 50 * 3600 # 50h ROI_TO_EXCLUDE = {} if __name__ == "__main__": parser = optparse.OptionParser() cam = MovieVirtualCamera(IN_AVI_FILE, use_wall_clock=False) #ref = cv2.imread(REF_ROI_IMG) roi_builder = ImgMaskROIBuilder(REF_ROI_IMG) # rois = roi_builder.build(REF_ROI_IMG) rois = roi_builder.build(cam) rois.sort(key=lambda r: r.get_feature_dict()["x"]) for i, r in enumerate(rois): r.set_value(i + 1) for r in rois: if r.value in ROI_TO_EXCLUDE: continue d = r.get_feature_dict()