From f497bdd61a515941027c8532fd7d4f13dc1eac0c Mon Sep 17 00:00:00 2001 From: John McCardle Date: Tue, 21 Dec 2021 02:19:12 -0500 Subject: [PATCH] Initial commit of SIFT search for ship sprites The data returned by the gamemodel function is overly verbose and not interpreted well yet. I'd like to return a (pt, wh, label) tuple like the find_asteroids function does. This would make it compatible with the existing display_results function. --- gamemodel.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/gamemodel.py b/gamemodel.py index af91961..aefc27f 100644 --- a/gamemodel.py +++ b/gamemodel.py @@ -15,6 +15,10 @@ class GameModel: ("normal", cv2.imread("images/game_assets/rock-normal.png", 0)), ("small", cv2.imread("images/game_assets/rock-small.png", 0)) ] + self.ships = [ + ("ship_off", cv2.imread("images/game_assets/spaceship-off.png", 0)), + ("ship_on", cv2.imread("images/game_assets/spaceship-on.png", 0)) + ] self.frame = None self.cv_template_thresh = 0.6 # reconfigurable at runtime self.duplicate_dist_thresh = 10 @@ -59,6 +63,21 @@ class GameModel: cv2.imshow("Results", displayable) cv2.waitKey(0) + @with_frame + def find_ships(self): + sift = cv2.SIFT_create() + frame_kp, frame_desc = sift.detectAndCompute(self.frame, None) + kp_desc = [] # list of (keypoints, descriptions) for all ship sprites + for label, s in self.ships: + kp_desc.append((label, sift.detectAndCompute(s, None))) + bf = cv2.BFMatcher(cv2.NORM_L1, crossCheck=True) + matchsets = [] + for label, kpdesc in kp_desc: + _, desc = kpdesc + matchsets.append((label, bf.match(frame_desc, desc))) + return { "matchsets": matchsets, + "kp_desc": kp_desc + } if __name__ == '__main__': import platform