예제 #1
0
def doit(DIR, HANDLE):
    for x in glob.glob(slashdir(DIR) + '*' + HANDLE + '*/metadata.yaml'):
        inStore = imgstore.new_for_filename(x)
        UND = Undistort(inStore)
        img, (frame_number, frame_timestamp) = inStore.get_next_image()

        newdir = x.rsplit('/', 1)[0] + '_undistorted'
        if os.path.exists(newdir):
            shutil.rmtree(newdir)
        os.mkdir(newdir)
        outStore = imgstore.new_for_format('avc1/mp4',
                                           mode='w',
                                           basedir=newdir,
                                           imgshape=img.shape,
                                           imgdtype=img.dtype,
                                           chunksize=500)

        for i in range(inStore.frame_count - 1):
            try:
                outStore.add_image(UND.undistort(img), frame_number,
                                   frame_timestamp)
                img, (frame_number, frame_timestamp) = inStore.get_next_image()
            except:
                print "failed at frame: ", i, "of", inStore.frame_count, inStore.frame_max

        outStore.close()
        open(x.rsplit('/', 1)[0] + '/undistortion_complete', 'a').close()

    return
예제 #2
0
파일: video.py 프로젝트: stallam-unb/sleap
    def imgstore_from_filenames(
        cls, filenames: list, output_filename: str, *args, **kwargs
    ) -> "Video":
        """Create an imgstore from a list of image files.

        Args:
            filenames: List of filenames for the image files.
            output_filename: Filename for the imgstore to create.

        Returns:
            A `Video` object for the new imgstore.
        """
        # get the image size from the first file
        first_img = cv2.imread(filenames[0], flags=cv2.IMREAD_COLOR)
        img_shape = first_img.shape

        # create the imgstore
        store = imgstore.new_for_format(
            "png", mode="w", basedir=output_filename, imgshape=img_shape
        )

        # read each frame and write it to the imgstore
        # unfortunately imgstore doesn't let us just add the file
        for i, img_filename in enumerate(filenames):
            img = cv2.imread(img_filename, flags=cv2.IMREAD_COLOR)
            store.add_image(img, i, i)

        store.close()

        # Return an ImgStoreVideo object referencing this new imgstore.
        return cls(backend=ImgStoreVideo(filename=output_filename))
예제 #3
0
    def save(self, path):

        import imgstore

        self.action = "save"
        datetime_str = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
        basedir = os.path.join(path, datetime_str)

        os.makedirs(basedir)
        video_format = "mjpeg/avi"
        self._store = None
        i=0
        chunksize=self._camera._target_framerate * 300

        for t_ms, frame in self._camera:
            if self._store is None:
                self._store = imgstore.new_for_format(video_format, mode='w', basedir=basedir, imgshape=frame.shape, imgdtype=frame.dtype, chunksize=chunksize)

            #b=time.time()
            self._store.add_image(frame, i, t_ms)
            #a=time.time()
            #print(a-b)
            i+=1

        return 0
예제 #4
0
 def __init__(self, fmt, queue, stop_queue, *args, **kwargs):
     # Initialize video writer
     self._queue = queue
     self._stop_queue = stop_queue
     #keys = list(inspect.signature(imgstore.new_for_format).parameters.keys())
     #imgstore_kwargs = {k: kwargs.pop(k) for k in keys if k in kwargs}
     #print(imgstore_kwargs)
     self._video_writer = imgstore.new_for_format(fmt=fmt, **kwargs)
     self._framecount = 0
     super().__init__(*args)
예제 #5
0
def create_stitched_video_from_scratch(fnString, pos):

    stores = (fnString + '.21990443', fnString + '.21990445',
              fnString + '.21990447', fnString + '.21990449')

    cams_stores = {}
    for fn in stores:
        store = new_for_filename(op.join(BASE_DATA, fn))
        print store.full_path
        nFrames = store.frame_count
        cams_stores[get_store_camera_serial(store)] = store

    sorted_stores = [cams_stores[i] for i in best_order]

    undistortions = [Undistort(i) for i in sorted_stores]

    aligned = StoreAligner(*sorted_stores)
    aligned.extract_common_frames(StoreAligner.MISSING_POLICY_DROP)

    if args.truncate == True:
        suffix = '.partial_stitch'
        aligned._fns = aligned._fns[
            2000:5000]  # THIS IS HOW WE TRUNCATE. #FIXME hardcoded cut
    elif args.truncate == False:
        suffix = '.stitched'

    if not os.path.exists(fnString + suffix):
        os.mkdir(fnString + suffix)

    out = imgstore.new_for_format('avc1/mp4',
                                  mode='w',
                                  basedir=fnString + suffix,
                                  imgshape=s.panorama_shape,
                                  imgdtype='uint8',
                                  chunksize=500)
    for n, (imgs, (fn, ts)) in enumerate(aligned.iter_imgs(return_times=True)):

        _imgs = []
        for i in range(len(undistortions)):
            _imgs.append(undistortions[i].undistort(imgs[i]))

        #with silence_stdout():
        ok, img = s.stitch_images(*[ensure_color(i) for i in _imgs])
        assert ok

        out.add_image(img, fn, ts)

        printProgressBar(n,
                         nFrames,
                         prefix='Stitching progress:',
                         stdoutpos=pos)
    out.close()

    return
예제 #6
0
def create_stitched_video_from_scratch(path):

    DIRECTORY, fnString = path.rsplit('/', 1)
    DIRECTORY = DIRECTORY + '/'
    stores = (fnString + '.21990443', fnString + '.21990445',
              fnString + '.21990447', fnString + '.21990449')

    cams_stores = {}
    for fn in stores:
        store = new_for_filename(op.join(BASE_DATA, fn))
        print store.full_path
        cams_stores[get_store_camera_serial(store)] = store

    sorted_stores = [cams_stores[i] for i in best_order]

    undistortions = [Undistort(i) for i in sorted_stores]

    aligned = StoreAligner(*sorted_stores)
    aligned.extract_common_frames(StoreAligner.MISSING_POLICY_DROP)
    if not os.path.exists(DIRECTORY + fnString + '.stitched'):
        os.mkdir(DIRECTORY + fnString + '.stitched')
    """
    out = new_for_format('avc1/mp4', DIRECTORY + fnString + '.stitched/metadata.yaml',
                         imgshape=s.panorama_shape,
                         imgdtype=np.uint8)
    """
    out = imgstore.new_for_format('avc1/mp4',
                                  mode='w',
                                  basedir=DIRECTORY + fnString + '.stitched',
                                  imgshape=s.panorama_shape,
                                  imgdtype='uint8',
                                  chunksize=500)
    for n, (imgs, (fn, ts)) in enumerate(aligned.iter_imgs(return_times=True)):

        _imgs = []
        for i in range(len(undistortions)):
            _imgs.append(undistortions[i].undistort(imgs[i]))

        ok, img = s.stitch_images(*[ensure_color(i) for i in _imgs])
        assert ok

        out.add_image(img, fn, ts)

    out.close()

    return
예제 #7
0
def create_stitched_video_from_undistorted(fnString):

    stores = (fnString + '.21990443_undistorted',
              fnString + '.21990445_undistorted',
              fnString + '.21990447_undistorted',
              fnString + '.21990449_undistorted')

    cams_stores = {}
    for fn in stores:
        store = new_for_filename(op.join(BASE_DATA, fn))
        print store.full_path
        cams_stores[get_store_camera_serial(store)] = store

    sorted_stores = [cams_stores[i] for i in best_order]

    aligned = StoreAligner(*sorted_stores)
    aligned.extract_common_frames(StoreAligner.MISSING_POLICY_DROP)
    if not os.path.exists(BASE_DATA + fnString + '.stitched'):
        os.mkdir(BASE_DATA + fnString + '.stitched')

    out = imgstore.new_for_format('avc1/mp4',
                                  mode='w',
                                  basedir=BASE_DATA + fnString,
                                  imgshape=s.panorama_shape,
                                  imgdtype='uint8',
                                  chunksize=500)
    for n, (fn, imgs) in enumerate(aligned.iter_imgs()):

        ok, img = s.stitch_images(*[ensure_color(i) for i in imgs])
        assert ok

        out.add_image(img, fn, 0)

        print n
    out.close()

    return
예제 #8
0
    VIDEO_TIME = '_'.join(
        TOP_LEFT.filename.split('/')[-1].split('.')[0].split('_')[-2:])
    HOMOGRAPHY_FILE = selectHomographyFile(VIDEO_TIME)
    #h = yaml.load(open('stitch/homography_20180827_150058.yml')) #for dev and debugging
    h = yaml.load(open(HOMOGRAPHY_FILE))

    IMG_SHAPE = (TOP_LEFT.image_shape[0] + TOP_RIGHT.image_shape[0],
                 TOP_LEFT.image_shape[1] + BOTTOM_LEFT.image_shape[1])

    if os.path.exists(SAVEAS):
        shutil.rmtree(SAVEAS)
    os.mkdir(SAVEAS)

    outStore = imgstore.new_for_format('avc1/mp4',
                                       mode='w',
                                       basedir=SAVEAS,
                                       imgshape=IMG_SHAPE,
                                       imgdtype='uint8',
                                       chunksize=500)

    store_fns = [
        store.get_frame_metadata()['frame_number'] for store in VIDEOS
    ]

    common = functools.reduce(np.intersect1d, store_fns)

    for i in common:
        imgs = []
        for vid in VIDEOS:
            try:
                img, (f, t) = vid.get_image(i)
            except:
예제 #9
0
파일: video.py 프로젝트: stallam-unb/sleap
    def to_imgstore(
        self,
        path: str,
        frame_numbers: List[int] = None,
        format: str = "png",
        index_by_original: bool = True,
    ) -> "Video":
        """Convert frames from arbitrary video backend to ImgStoreVideo.

        This should facilitate conversion of any video to a loopbio imgstore.

        Args:
            path: Filename or directory name to store imgstore.
            frame_numbers: A list of frame numbers from the video to save.
                If None save the entire video.
            format: By default it will create a DirectoryImgStore with lossless
                PNG format unless the frame_indices = None, in which case,
                it will default to 'mjpeg/avi' format for video.
            index_by_original: ImgStores are great for storing a collection of
                selected frames from an larger video. If the index_by_original
                is set to True then the get_frame function will accept the
                original frame numbers of from original video. If False,
                then it will accept the frame index from the store directly.
                Default to True so that we can use an ImgStoreVideo in a
                dataset to replace another video without having to update
                all the frame indices on :class:`LabeledFrame` objects in the dataset.

        Returns:
            A new Video object that references the imgstore.
        """
        # If the user has not provided a list of frames to store, store them all.
        if frame_numbers is None:
            frame_numbers = range(self.num_frames)

            # We probably don't want to store all the frames as the PNG default,
            # lets use MJPEG by default.
            format = "mjpeg/avi"

        # Delete the imgstore if it already exists.
        if os.path.exists(path):
            if os.path.isfile(path):
                os.remove(path)
            if os.path.isdir(path):
                shutil.rmtree(path, ignore_errors=True)

        # If the video is already an imgstore, we just need to copy it
        # if type(self) is ImgStoreVideo:
        #     new_backend = self.backend.copy_to(path)
        #     return self.__class__(backend=new_backend)

        store = imgstore.new_for_format(
            format,
            mode="w",
            basedir=path,
            imgshape=(self.height, self.width, self.channels),
            chunksize=1000,
        )

        # Write the JSON for the original video object to the metadata
        # of the imgstore for posterity
        store.add_extra_data(source_sleap_video_obj=Video.cattr().unstructure(self))

        import time

        for frame_num in frame_numbers:
            store.add_image(self.get_frame(frame_num), frame_num, time.time())

        # If there are no frames to save for this video, add a dummy frame
        # since we can't save an empty imgstore.
        if len(frame_numbers) == 0:
            store.add_image(
                np.zeros((self.height, self.width, self.channels)), 0, time.time()
            )

        store.close()

        # Return an ImgStoreVideo object referencing this new imgstore.
        return self.__class__(
            backend=ImgStoreVideo(filename=path, index_by_original=index_by_original)
        )
예제 #10
0
        firstFrame = frameDF.ix[frameDF[
            frameDF.frame_time >= startTS].idxmin().frame_time].frame_number
        lastFrame = frameDF.ix[frameDF[
            frameDF.frame_time <= endTS].idxmax().frame_time].frame_number
        nFrames = int(lastFrame - firstFrame)

        print "firstFrame: ", firstFrame, startTS
        print "lastFrame: ", lastFrame, endTS
    #out = cv2.VideoWriter(DIRECTORY + 'annotated.mp4', cv2.cv.FOURCC('a','v','c','1'), FPS, videoSize)

    #out = imgstore.new_for_format('mjpeg', mode='w', basedir=args.output,imgshape=store.image_shape, imgdtype=store.get_image(store.frame_min)[0].dtype, chunksize=1000)

    out = imgstore.new_for_format('mjpeg',
                                  mode='w',
                                  basedir='/home/dan/Desktop/annotated_vid',
                                  imgshape=store.image_shape,
                                  imgdtype=store.get_image(
                                      store.frame_min)[0].dtype,
                                  chunksize=1000)

    nFrames = store.frame_max - store.frame_min
    ccw = cv2.imread('/home/dan/Desktop/arrow.png')[:, :, 1]
    cw = ccw.T  # = cv2.imread('/home/dbath/fishMAD/clockwise.png' )[:,:,1]

    #img, (frame_number, frame_timestamp) = store.get_image(firstFrame)

    for n in range(0, nFrames, 9):
        try:
            img, (frame_number,
                  frame_timestamp) = store.get_image(store.frame_min + n)
        except:
예제 #11
0
                        default='notAssigned',
                        help='output filename')
    args = parser.parse_args()

    for x in glob.glob(args.dir + '*/metadata.yaml'):
        inStore = imgstore.new_for_filename(x)
        UND = Undistort(inStore)
        img, (frame_number, frame_timestamp) = inStore.get_next_image()

        newdir = x.rsplit('/', 1)[0] + '_undistorted'
        if os.path.exists(newdir):
            shutil.rmtree(newdir)
        os.mkdir(newdir)
        outStore = imgstore.new_for_format('avc1/mp4',
                                           mode='w',
                                           basedir=newdir,
                                           imgshape=inStore.image_shape,
                                           imgdtype='uint8',
                                           chunksize=500)

        for i in range(inStore.frame_count - 1):
            try:
                img, (frame_number, frame_timestamp) = inStore.get_next_image()
                outStore.add_image(UND.undistort(img), frame_number,
                                   frame_timestamp)
            except:
                print "failed at frame: ", i, "of", inStore.frame_count, inStore.frame_max

        outStore.close()

    print "done."
예제 #12
0
        bar = bar.sort_values('Timestamp')
        bar['coh'] = bar['coh'].fillna(method='ffill')
        bar['coherence'] = bar['coh']  #silly lazy hack
        bar['speed'] = bar['speed'].fillna(method='ffill').fillna(0)
        bar['dir'] = bar['dir'].fillna(method='ffill').fillna(0)
        bar.loc[:,
                'Time'] = (bar.loc[:, 'Timestamp'] - bar.loc[0, 'Timestamp'])
        if bar.iloc[5].Timestamp - bar.iloc[
                0].Timestamp > 10:  #check if log overlaps with track data
            return 0, bar
        else:
            return 1, bar


if __name__ == "__main__":

    vidfile = '/media/recnodes/recnode_2mfish/reversals3m_1024_dotbot_20181025_123202.stitched'
    data = PIV_Data(vidfile)
    import os
    if not os.path.exists(vidfile + '/sidebyside'):
        os.makedirs(vidfile + '/sidebyside')
    imgshape = data.plot_sidebyside(0, "asdf", setup=True)
    out = imgstore.new_for_format('avc1/mp4',
                                  mode='w',
                                  basedir=vidfile + '/sidebyside',
                                  imgshape=imgshape,
                                  imgdtype='uint8',
                                  chunksize=500)
    for n in range(data.get_frame_count()):
        data.plot_sidebyside(n, out)