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
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 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
import numpy as np from detect_card import detect_card def multiple_crop(photopath): # Now let's use the detector as you would in a normal application. First we # will load it from disk. detector = dlib.simple_object_detector("detector.svm") img = io.imread(photopath) dets = detector(img) detected_rectangles= [] for k, d in enumerate(dets): detected_rectangles.append([d.left(),d.top(),d.right(),d.bottom()]) # print(detected_rectangles) cropped_vector=crop_cards(img, detected_rectangles) # print(cropped_vector) return cropped_vector def crop_cards(img, rectanglelist): cropped_img=[] for i in rectanglelist: single_crop=img[i[0]:i[2], i[1]:i[3]] cropped_img.append(single_crop) return cropped_img crops=multiple_crop("Training/7.jpg") gray=cv2.cvtColor(crops[0],0) base= cv2.imread("base.jpg",0) detect_card(gray,base)