Exemplo n.º 1
0
 def before(self):
     """
     prepare for the loop
     """
     self.onWork = 0
     self.absx,self.absy = 0, 0
     self.lastdx, self.lastdy = 0, 0
     self.tr = 0
     self.preview_size = 500
     self.preview = trainscanner.fit_to_square(self.frame, self.preview_size)
     self.preview_ratio = float(self.preview.shape[0]) / self.frame.shape[0]
Exemplo n.º 2
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
        self.cap = cv2.VideoCapture(filename)
        ret, frame = self.cap.read()
        if self.size:
            frame = trainscanner.fit_to_square(frame, self.size)
        self.snapshots = [frame]
Exemplo n.º 3
0
 def task(self):
     if not self.isRunning:
         self.isRunning = True
         
     while self.isRunning:
         ret, frame = self.cap.read()
         if not ret:
             break
         if self.count % 10 == 0:  #load every 10 frames; it might be too many.
             if self.size:
                 frame = trainscanner.fit_to_square(frame, self.size)
             self.snapshots.append(frame)
             #print(len(self.snapshots))
             #these are for the preview, so we can shrink it.
             self.frameIncreased.emit(len(self.snapshots))
         self.count += 1
Exemplo n.º 4
0
 def task(self):
     if not self.isRunning:
         self.isRunning = True
         
     while self.isRunning:
         ret, frame = self.cap.read()
         if not ret:
             break
         if self.size:
             frame = trainscanner.fit_to_square(frame, self.size)
         self.snapshots.append(frame)
         self.frameIncreased.emit(self.snapshots)
         for i in range(9):
             ret = self.cap.grab()
             if not ret:
                 break
Exemplo n.º 5
0
    def onestep(self):
        ret = True
        ##### Pick up every x frame
        for i in range(self.every-1):
            ret = self.cap.grab()
            self.nframes += 1
            if not ret:
                return None
        ret, nextframe = self.cap.read()
        self.nframes += 1
        ##### return None if the frame is empty
        if not ret:
            return None
        ##### compare with the previous raw frame
        diff = cv2.absdiff(nextframe,self.rawframe)
        diff = np.sum(diff) / np.product(diff.shape)
        if diff < self.identity:
            sys.stderr.write("skip identical frame #{0}\n".format(diff))
            #They are identical frames
            #This happens when the frame rate difference is compensated.
            return True
        ##### preserve the raw frame for next comparison.
        self.rawframe = nextframe.copy()
        ##### Warping the frame
        #######(1) rotation
        if self.angle:
            nextframe = cv2.warpAffine(nextframe, self.R, (self.rotated_w,self.rotated_h))
            #w and h are sizes after rotation
        #######(2) skew deformation
        if self.M is not None:
            #this does not change the aspect ratio
            nextframe = cv2.warpPerspective(nextframe,self.M,(self.rotated_w,self.rotated_h))
        #######(3) top-bottom crop
        nextframe = nextframe[self.crop[0]*self.rotated_h/1000:self.crop[1]*self.rotated_h/1000, :, :]
        ##### Make the preview image
        nextpreview = trainscanner.fit_to_square(nextframe,self.preview_size)
        ##### motion detection.
        #if margin is set, motion detection area becomes very narrow
        #assuming that the train is running at constant speed.
        #This mode is activated after the 10th frames.
        
        if self.margin > 0 and self.onWork > 10:
            #do not apply margin for the first 10 frames
            #because the velocity uncertainty.
            dx,dy = trainscanner.motion(self.frame, nextframe, focus=self.focus, margin=self.margin, delta=(self.lastdx,self.lastdy) )
        else:
            dx,dy = trainscanner.motion(self.frame, nextframe, focus=self.focus)
            
        sys.stderr.write("{0} {1} {2} #{3}\n".format(self.nframes,dx,dy,np.amax(diff)))

        if self.zero:
            if abs(dx) < abs(dy):
                dx = 0
            else:
                dy = 0
        diff_img = diffImage(nextpreview,self.preview,int(dx*self.preview_ratio),int(dy*self.preview_ratio),focus=self.focus)
        if (abs(dx) > self.antishake or abs(dy) > self.antishake):
            self.onWork += 1
            self.tr = 0
        else:
            if self.onWork:
                if self.tr <= self.trailing:
                    self.tr += 1
                    dx = self.lastdx
                    dy = self.lastdy
                    sys.stderr.write(">>({2}) {0} {1} #{3}\n".format(dx,dy,self.tr,np.amax(diff)))
                else:
                    #end of work
                    return None
        self.absx += dx
        self.absy += dy
        if self.onWork:
            self.lastdx, self.lastdy = dx,dy
            self.canvas = canvas_size(self.canvas, nextframe, self.absx, self.absy)
            #sys.stderr.write("canvas size{0} {1}\n".format(self.canvas[0],self.canvas[1]))
            self.LOG.write("{0} {1} {2} {3} {4}\n".format(self.nframes,self.absx,self.absy,dx,dy))
            self.LOG.flush()
            #This flushes the buffer, that causes immediate processing in the next command connected by a pipe "|"
        self.frame   = nextframe
        self.preview = nextpreview
        return diff_img