예제 #1
0
def main():
    import sys
    app = QApplication(sys.argv)
    window = ImageSelector2()
    window.resize(300, 50)
    window.show()

    from trainscanner import video
    import cv2
    vl = video.VideoLoader("examples/sample2.mov")
    ret = True
    thumbs = []
    while True:
        nframe, frame = vl.next()
        if nframe == 0:
            break
        h, w = frame.shape[0:2]
        thumbh = 100
        thumbw = w * thumbh // h
        thumb = cv2.resize(frame, (thumbw, thumbh),
                           interpolation=cv2.INTER_CUBIC)
        thumbs.append(cv2toQImage(thumb))
        terminate = False
        for i in range(9):
            nframe = vl.skip()
            if nframe == 0:
                terminate = True
                break
        if terminate:
            break
    window.setThumbs(thumbs)

    sys.exit(app.exec_())
예제 #2
0
    def __init__(self, argv):
        logger = getLogger()
        parser = prepare_parser()
        # これが一番スマートなんだが、動かないので、手動で--fileをさがして処理を行う。
        # parser.add_argument('--file', type=open, action=LoadFromFile)
        for i, arg in enumerate(argv):
            if arg == "--file":
                tsconf = argv[i + 1]
                del argv[i]
                del argv[i]
                with open(tsconf) as f:
                    argv += f.read().splitlines()
                break
        self.params, unknown = parser.parse_known_args(argv[1:])
        #Decide the paths
        moviepath = self.params.filename
        moviedir = os.path.dirname(moviepath)
        moviebase = os.path.basename(moviepath)
        self.tsposfile = ""

        if self.tsposfile == "" or not os.path.exists(self.tsposfile):
            tsconfdir = os.path.dirname(self.params.logbase)
            if tsconfdir == "":
                tsconfdir = "."
            tsconfbase = os.path.basename(self.params.logbase)
            self.tsposfile = tsconfdir + "/" + tsconfbase + ".tspos"
        moviefile = tsconfdir + "/" + moviebase
        self.outfilename = tsconfdir + "/" + tsconfbase + ".png"
        self.cachedir = tsconfdir + "/" + tsconfbase + ".pngs"  #if required
        if not os.path.exists(moviefile):
            moviefile = moviepath
        logger.info("TSPos  {0}".format(self.tsposfile))
        logger.info("Movie  {0}".format(moviefile))
        logger.info("Output {0}".format(self.outfilename))

        self.vl = video.VideoLoader(moviefile)
        self.firstFrame = True
        self.currentFrame = 0  #1 is the first frame

        self.R = None
        self.M = None
        self.transform = trainscanner.transformation(self.params.rotate,
                                                     self.params.perspective,
                                                     self.params.crop)
        # initialization of the super class
        #logger.info("scale:{0}".format(self.params.scale))
        #logger.info("length:{0}".format(self.params.length))
        if self.params.scale == 1 and self.params.length > 0:
            #product length is specified.
            #scale is overridden
            self.params.scale = self.params.length / self.params.canvas[0]
            if self.params.scale > 1:
                self.params.scale = 1  #do not allow stretching
            #for GUI
        self.dimen = [int(x * self.params.scale) for x in self.params.canvas]
예제 #3
0
    def __init__(self, parent=None, filename="", size=0):
        super(AsyncImageLoader, self).__init__(parent)
        self.isRunning = True

        #capture the first frame ASAP to avoid "no frame" errors.
        self.size = size
        logger = logging.getLogger()
        logger.debug("Open video: {0}".format(filename))
        self.vl = video.VideoLoader(filename)
        nframe, frame = self.vl.next()
        if self.size:
            frame = trainscanner.fit_to_square(frame, self.size)
        self.snapshots = [frame]
예제 #4
0
    def before(self):
        """
        prepare for the loop
        note that it is a generator.
        """
        logger = getLogger()
        ####Determine the movie file########################
        found = None
        logger.debug("Basename {0}".format(self.params.filename))
        # First we assume that the movie is in the same directory as tsconf if tsconf is specified.
        # Otherwise we look at the absolute path given in the command line or in the tsconf.
        for dirname in self.dirnames:
            filename = dirname+"/"+self.params.filename
            if os.path.exists(filename):
                found = filename
                break
        if not found:
            logger.error("File not found.")
        #update the file path
        self.params.filename = found
        logger.debug("Found filename {0}".format(found))
        ####prepare tsconf file#############################
        self.tsconf   = ""
        args = trainscanner.deparse(self.parser,self.params)
        self.tsconf += "{0}\n".format(args["__UNNAMED__"])
        for option in args:
            value = args[option]
            if value is None or option in ("__UNNAMED__"):
                continue
            if option == '--option2':
                #Expand "key=value" to "--key\tvalue\n"
                for v in value:
                    equal = v.find("=")
                    if equal >= 0:
                        self.tsconf += "--{0}\n{1}\n".format(v[:equal],v[equal+1:])
                    else:
                        self.tsconf += "--{0}\n".format(v)
            else:
                if option in ("--perspective", "--focus", "--crop", ):  #multiple values
                    self.tsconf += "{0}\n".format(option)
                    for v in value:
                        self.tsconf += "{0}\n".format(v)
                elif option in ("--zero", "--stall",):
                    if value is True:
                        self.tsconf += option+"\n"
                else:
                    self.tsconf += "{0}\n{1}\n".format(option,value)
        #print(self.tsconf)
        #end of the header

        #############Open the video file #############################
        self.vl    = video.VideoLoader(found)
        # self.nframes = 0  #1 is the first frame

        for i in range(self.params.skip):  #skip frames
            nframe = self.vl.skip()
            if nframe==0:
                break
            yield nframe, self.params.skip #report progress
        nframe, frame = self.vl.next()
        if nframe==0:
            logger.debug("End of film.")
            sys.exit(0)
        self.rawframe = frame
        self.lastnframe = nframe # just for iter()
예제 #5
0
    def __init__(self, argv):
        logger = logging.getLogger()
        ####Avoid unknown error.
        #myargparse does not show the usage correctly.
        #ap0 = argparse.ArgumentParser(fromfile_prefix_chars='@',
        #                                description='TrainScanner stitcher')
        #prepare_parser(ap0).parse_known_args(argv[1:])
        ####
        ap = myargparse.MyArgumentParser(fromfile_prefix_chars='@',
                                         description='TrainScanner stitcher')
        self.parser = prepare_parser(ap)
        self.params, unknown = self.parser.parse_known_args(argv[1:])
        #Decide the paths
        moviepath = self.params.filename
        moviedir = os.path.dirname(moviepath)
        moviebase = os.path.basename(moviepath)
        self.tsposfile = ""

        if self.parser.fromfile_name is not None:
            #When the "@"file is specified,
            #read tsconf there. (actually it is alread read.)
            #read tspos at the same path
            tsconfdir = os.path.dirname(self.parser.fromfile_name)
            if tsconfdir == "":
                tsconfdir = "."
            tsconfbase = os.path.basename(self.parser.fromfile_name)
            if tsconfbase[-7:] == ".tsconf":
                tsconfbase = tsconfbase[:-7]
            self.tsposfile = tsconfdir + "/" + tsconfbase + ".tspos"
        #or tspos in the logbase
        if self.tsposfile == "" or not os.path.exists(self.tsposfile):
            tsconfdir = os.path.dirname(self.params.logbase)
            if tsconfdir == "":
                tsconfdir = "."
            tsconfbase = os.path.basename(self.params.logbase)
            self.tsposfile = tsconfdir + "/" + tsconfbase + ".tspos"
        moviefile = tsconfdir + "/" + moviebase
        self.outfilename = tsconfdir + "/" + tsconfbase + ".png"
        self.cachedir = tsconfdir + "/" + tsconfbase + ".pngs"  #if required
        if not os.path.exists(moviefile):
            moviefile = moviepath
        logger.info("TSPos  {0}".format(self.tsposfile))
        logger.info("Movie  {0}".format(moviefile))
        logger.info("Output {0}".format(self.outfilename))

        self.vl = video.VideoLoader(moviefile)
        self.firstFrame = True
        self.currentFrame = 0  #1 is the first frame

        self.R = None
        self.M = None
        self.transform = trainscanner.transformation(self.params.rotate,
                                                     self.params.perspective,
                                                     self.params.crop)
        # initialization of the super class
        #logger.info("scale:{0}".format(self.params.scale))
        #logger.info("length:{0}".format(self.params.length))
        if self.params.scale == 1 and self.params.length > 0:
            #product length is specified.
            #scale is overridden
            self.params.scale = self.params.length / self.params.canvas[0]
            if self.params.scale > 1:
                self.params.scale = 1  #do not allow stretching
            #for GUI
        self.dimen = [int(x * self.params.scale) for x in self.params.canvas]