def calc_background_similarity(self): return min(sum_squared(self.background_gray, frame) for frame in self.recent_frames_gray)
def calc_biggest_diff(self): return min(sum_squared(self.last_frame_gray, frame) for frame in self.recent_frames_gray)
def watch_for_card(camera): has_moved = False been_to_base = False global captures global font captures = [] font = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 1.0, 1.0) img = cv.QueryFrame(camera) size = cv.GetSize(img) n_pixels = size[0] * size[1] grey = cv.CreateImage(size, 8, 1) recent_frames = [cv.CloneImage(grey)] base = cv.CloneImage(grey) cv.CvtColor(img, base, cv.CV_RGB2GRAY) #cv.ShowImage('card', base) tmp = cv.CloneImage(grey) while True: img = cv.QueryFrame(camera) cv.CvtColor(img, grey, cv.CV_RGB2GRAY) biggest_diff = max( sum_squared(grey, frame) / n_pixels for frame in recent_frames) #display the cam view cv.PutText(img, "%s" % biggest_diff, (1, 24), font, (255, 255, 255)) cv.ShowImage('win', img) recent_frames.append(cv.CloneImage(grey)) if len(recent_frames) > 3: del recent_frames[0] #check for keystroke c = cv.WaitKey(10) #if there was a keystroke, reset the last capture if c == 27: return captures elif c == 32: has_moved = True been_to_base = True elif c == 114: base = cv.CloneImage(grey) #if we're stable-ish if biggest_diff < 10: #if we're similar to base, update base #else, check for card #base_diff = max(sum_squared(base, frame) / n_pixels for frame in recent_frames) base_corr = min( ccoeff_normed(base, frame) for frame in recent_frames) #cv.ShowImage('debug', base) """for i, frame in enumerate(recent_frames): tmp = cv.CloneImage(base) cv.Sub(base, frame, tmp) cv.Pow(tmp, tmp, 2.0) cv.PutText(tmp, "%s" % (i+1), (1,24), font, (255, 255, 255)) #my_diff = sum_squared(base, frame) / n_pixels my_diff = ccoeff_normed(base, frame) #score(base, frame, cv.CV_TM_CCOEFF_NORMED) cv.PutText(tmp, "%s" % my_diff, (40, 24), font, (255, 255, 255)) cv.ShowImage('dbg%s' % (i+1), tmp)""" #print "stable. corr = %s. moved = %s. been_to_base = %s" % (base_corr, has_moved, been_to_base) if base_corr > 0.75 and not been_to_base: base = cv.CloneImage(grey) # cv.ShowImage('debug', base) has_moved = False been_to_base = True print "STATE: been to base. waiting for move" elif has_moved and been_to_base: corners = detect_card(grey, base) if corners is not None: card = get_card(grey, corners) cv.Flip(card, card, -1) captures.append(card) update_windows() #cv.ShowImage('card', card) has_moved = False been_to_base = False print "STATE: detected. waiting for go to base" else: if not has_moved: print "STATE: has moved. waiting for stable" has_moved = True
def watch_for_card(camera): has_moved = False been_to_base = False global captures global font captures = [] font = cv.InitFont(cv.CV_FONT_HERSHEY_SIMPLEX, 1.0, 1.0) img = cv.QueryFrame(camera) size = cv.GetSize(img) n_pixels = size[0] * size[1] grey = cv.CreateImage(size, 8, 1) recent_frames = [cv.CloneImage(grey)] base = cv.CloneImage(grey) cv.CvtColor(img, base, cv.CV_RGB2GRAY) # cv.ShowImage('card', base) tmp = cv.CloneImage(grey) while True: img = cv.QueryFrame(camera) cv.CvtColor(img, grey, cv.CV_RGB2GRAY) biggest_diff = max(sum_squared(grey, frame) / n_pixels for frame in recent_frames) # display the cam view cv.PutText(img, "%s" % biggest_diff, (1, 24), font, (255, 255, 255)) cv.ShowImage('win', img) recent_frames.append(cv.CloneImage(grey)) if len(recent_frames) > 3: del recent_frames[0] # check for keystroke c = cv.WaitKey(10) # if there was a keystroke, reset the last capture if c == 27: return captures elif c == 32: has_moved = True been_to_base = True elif c == 114: base = cv.CloneImage(grey) # if we're stable-ish if biggest_diff < 10: # if we're similar to base, update base # else, check for card # base_diff = max(sum_squared(base, frame) / n_pixels for frame in recent_frames) base_corr = 0 try: base_corr = min(ccoeff_normed(base, frame) for frame in recent_frames) except: print ("Unable to calculate the base_corr") pass # cv.ShowImage('debug', base) """for i, frame in enumerate(recent_frames): tmp = cv.CloneImage(base) cv.Sub(base, frame, tmp) cv.Pow(tmp, tmp, 2.0) cv.PutText(tmp, "%s" % (i+1), (1,24), font, (255, 255, 255)) #my_diff = sum_squared(base, frame) / n_pixels my_diff = ccoeff_normed(base, frame) #score(base, frame, cv.CV_TM_CCOEFF_NORMED) cv.PutText(tmp, "%s" % my_diff, (40, 24), font, (255, 255, 255)) cv.ShowImage('dbg%s' % (i+1), tmp)""" # print "stable. corr = %s. moved = %s. been_to_base = %s" % (base_corr, has_moved, been_to_base) if base_corr > 0.75 and not been_to_base: base = cv.CloneImage(grey) # cv.ShowImage('debug', base) has_moved = False been_to_base = True print "STATE: been to base. waiting for move" elif has_moved and been_to_base: corners = detect_card(grey, base) if corners is not None: card = get_card(grey, corners) cv.Flip(card, card, -1) captures.append(card) update_windows() # cv.ShowImage('card', card) has_moved = False been_to_base = False print "STATE: detected. waiting for go to base" else: if not has_moved: print "STATE: has moved. waiting for stable" has_moved = True
def calc_biggest_diff(self): return max(sum_squared(self.last_frame_gray, frame) / self.num_pixels for frame in self.recent_frames_gray)