def update_background(self, frame=None): if frame is None: frame = self.last_frame if frame is None: frame = self.grab_frame() self.background = clone_image(frame) self.background_gray = to_gray_image(self.background) self.background_flipped = flip_image(self.background)
def grab_frame(self): ret, frame = self.camera.read() frame_gray = to_gray_image(frame) frame_flipped = flip_image(frame) # Set the size of things if this is the first image we have seen if self.last_frame is None: self.size = frame.shape self.num_pixels = self.size[0]*self.size[1] self.recent_frames_gray.append(frame_gray) self.recent_frames.append(frame) if len(self.recent_frames) > self.recent_frames_max: del self.recent_frames[0] del self.recent_frames_gray[0] self.last_frame = clone_image(frame) self.last_frame_flipped = frame_flipped self.last_frame_gray = frame_gray return frame
def grab_frame(self): frame = cv.QueryFrame(self.camera) frame_gray = to_gray_image(frame) frame_flipped = flip_image(frame) # Set the size of things if this is the first image we have seen if self.last_frame is None: self.size = cv.GetSize(frame) self.num_pixels = self.size[0]*self.size[1] self.recent_frames_gray.append(frame_gray) self.recent_frames.append(frame) if len(self.recent_frames) > self.recent_frames_max: del self.recent_frames[0] del self.recent_frames_gray[0] self.last_frame = clone_image(frame) self.last_frame_flipped = frame_flipped self.last_frame_gray = frame_gray return frame
def check_for_card(self): found = False base_corr = 0 self.grab_frame() # if the user didn't set the background, get the first frame and consider it the background if self.background is None: print "Updating background on the account of it being NOne" self.update_background() history_diff = self.calc_biggest_diff() # Detect movement if history_diff > 10: self.has_moved = True # No movement now, but there was before elif self.has_moved == True: base_corr = self.calc_background_similarity() # Looking at the background, false alarm # set the frame as the new background, this helps with lightning change if base_corr >= 0.75: #self.update_background() self.has_moved = False else: # background and current frame are < 25% similar corners = detect_card(self.last_frame_gray, self.background_gray) if corners is not None: self.snapshot = get_card(self.last_frame_gray, corners) self.snapshot = flip_image(self.snapshot) found = True self.has_moved = False else: self.has_moved = False self.display_background() self.display_live("%.4f [0-10 stable, >10 unstable] | %.4f [>0.75 is background, <0.75 contains foreground]" % (history_diff, base_corr)) if found is True: return clone_image(self.snapshot) return None