Ejemplo n.º 1
0
    def initWithParams(self,filename="",seek=0,angle=0,pers=None,crop=[0,1000],every=1, identity=2.0, margin=0, focus=[333,666,333,666], zero=False, trailing=10, antishake=5, ):
        self.filename    = filename
        self.cap      = cv2.VideoCapture(filename)
        self.every    = every
        self.identity = identity
        self.margin   = margin
        self.zero     = zero
        self.trailing = trailing
        self.angle    = angle
        self.focus    = focus
        self.antishake= antishake
        self.nframes  = 0  #1 is the first frame
        self.crop     = crop
        ret = True
        for i in range(seek):  #skip frames
            ret = self.cap.grab()
            if not ret:
                break
            #ret, frame = cap.read()
            self.nframes += 1
        if not ret:
            sys.exit(0)
        ret, frame = self.cap.read()
        self.nframes += 1
        if not ret:
            sys.exit(0)
        self.rawframe = frame.copy()
        original_h, original_w, d = frame.shape
        self.rotated_h, self.rotated_w = original_h,original_w
        if angle:
            #Apply rotation
            self.R, self.rotated_w,self.rotated_h = trainscanner.rotate_matrix(-angle*math.pi/180, original_w, original_h)
            frame = cv2.warpAffine(frame, self.R, (self.rotated_w,self.rotated_h))

        if pers is not None:
            self.M = trainscanner.warp_matrix(pers,self.rotated_w,self.rotated_h)
            frame = cv2.warpPerspective(frame,self.M,(self.rotated_w,self.rotated_h))
        else:
            self.M = None
        #cropping
        self.frame = frame[crop[0]*self.rotated_h/1000:crop[1]*self.rotated_h/1000, :, :]
        self.cropped_h,self.cropped_w = self.frame.shape[0:2]

        #Prepare a scalable canvas with the origin.
        self.canvas = [self.cropped_w,self.cropped_h,100,0,0]
        #sys.stderr.write("canvas size{0} {1} {2} {3}\n".format(self.canvas[0],self.canvas[1],*self.crop))
        #sys.exit(1)
        self.LOG = sys.stdout
        self.LOG.write("{0}\n".format(filename))
        self.LOG.write("#-r {0}\n".format(angle))
        if pers is not None:
            self.LOG.write("#-p {0},{1},{2},{3}\n".format(*pers))
        self.LOG.write("#-c {0},{1}\n".format(*crop))
        #end of the header
        self.LOG.write("\n")
Ejemplo n.º 2
0
 def show_snapshots(self, region=None):
     """
     put the snapshots in the preview panes
     """
     if self.frame < 0:
         return
     image = self.snapshots[self.frame]
     h,w = image.shape[0:2]
     #Left image: unwarped
     R, width,height = trainscanner.rotate_matrix(-self.angle_degree*math.pi/180,w,h)
     image = cv2.warpAffine(image, R, (width,height))
     processed = image.copy()
     #trainscanner.draw_guide(image, self.pers, gauge=False)  #second is pers
     #color order is different between cv2 and pyqt
     self.put_cv2_image(image, self.raw_image_pane)
     #Right image: warped
     M = trainscanner.warp_matrix(self.pers,width,height)
     processed = cv2.warpPerspective(processed,M,(width,height))
     processed = processed[self.croptop*height/1000:self.cropbottom*height/1000, :, :]
     if region is not None:
         print(region)
         #assume the QLabel size is square preview_size x preview_size
         top, left, bottom, right = region.top(), region.left(), region.bottom(), region.right()
         if top < 0:
             top = 0
         if left < 0:
             left = 0
         if right > self.preview_size:
             right = self.preview_size
         if bottom > self.preview_size:
             bottom = self.preview_size
         #and also assume that the cropped image is centered and sometimes shrinked.
         top    -= self.preview_size/2
         bottom -= self.preview_size/2
         left   -= self.preview_size/2
         right  -= self.preview_size/2
         #expected image size in the window
         height, width = processed.shape[0:2]
         if height > width:
             if height > self.preview_size:
                 width = width * self.preview_size / height
                 height = self.preview_size
         else:
             if width > self.preview_size:
                 height = height * self.preview_size / width
                 width  = self.preview_size
         #indicate the region size relative to the image size
         print(left,right,top,bottom)
         top    = top    * 1000 / height + 500
         bottom = bottom * 1000 / height + 500
         left   = left   * 1000 / width + 500
         right  = right  * 1000 / width + 500
         print(left,right,top,bottom)
         if top < 0:
             top = 0
         if top > 1000:
             top = 1000
         if bottom < 0:
             bottom = 0
         if bottom > 1000:
             bottom = 1000
         if left < 0:
             left = 0
         if left > 1000:
             left = 1000
         if right < 0:
             right = 0
         if right > 1000:
             right = 1000
         self.focus = left,right,top,bottom
         print(self.focus)
         
     #trainscanner.draw_focus_area(processed, self.focus)
     #draw_slitpos(processed, self.slitpos)
     self.put_cv2_image(processed, self.processed_pane)
     print(self.sizeHint()," hint")