2021-12-20 09:41:48 +00:00
|
|
|
import gameio
|
|
|
|
import cv2
|
|
|
|
import numpy as np
|
2021-12-24 18:02:52 +00:00
|
|
|
from functools import wraps
|
2021-12-20 09:41:48 +00:00
|
|
|
|
2021-12-23 01:29:36 +00:00
|
|
|
from utility import *
|
|
|
|
import pointcluster
|
2021-12-28 01:30:40 +00:00
|
|
|
from imagepipeline import CVImage, ImagePipeline
|
2021-12-21 06:29:15 +00:00
|
|
|
|
2021-12-20 09:41:48 +00:00
|
|
|
class GameModel:
|
|
|
|
"""Platform-independent representation of the game's state."""
|
|
|
|
def __init__(self, io:gameio.AbstractGameIO):
|
|
|
|
self.gameio = io
|
2021-12-21 06:29:15 +00:00
|
|
|
self.asteroids = [
|
2021-12-29 00:46:52 +00:00
|
|
|
CVImage("big", filename = "images/game_assets/rock-big.png"),
|
|
|
|
CVImage("normal", filename = "images/game_assets/rock-normal.png"),
|
|
|
|
CVImage("small", filename = "images/game_assets/rock-small.png")
|
2021-12-21 06:29:15 +00:00
|
|
|
]
|
2021-12-21 07:19:12 +00:00
|
|
|
self.ships = [
|
2021-12-29 00:46:52 +00:00
|
|
|
CVImage("ship_off", filename = "images/game_assets/spaceship-off.png"),
|
|
|
|
CVImage("ship_on", filename = "images/game_assets/spaceship-on.png")
|
2021-12-21 07:19:12 +00:00
|
|
|
]
|
2021-12-23 01:29:36 +00:00
|
|
|
#self.missile = ("missile", cv2.imread("images/game_assets/missile.png", 0))
|
2021-12-20 09:41:48 +00:00
|
|
|
self.frame = None
|
2021-12-24 18:02:52 +00:00
|
|
|
self.prev_frame = None
|
|
|
|
self.color_frame = None
|
|
|
|
self.score_img = None
|
|
|
|
self.lives_img = None
|
|
|
|
self.lives_rect = ((10,10), (190, 65))
|
|
|
|
self.score_rect = ((600, 25), (780, 65))
|
2021-12-21 06:29:15 +00:00
|
|
|
self.cv_template_thresh = 0.6 # reconfigurable at runtime
|
2021-12-23 01:29:36 +00:00
|
|
|
self.duplicate_dist_thresh = 36
|
2021-12-20 09:41:48 +00:00
|
|
|
|
|
|
|
def with_frame(fn):
|
|
|
|
"""Decorator to process screenshot to cv2 format once upon first requirement, then reuse."""
|
2021-12-24 18:02:52 +00:00
|
|
|
@wraps(fn)
|
2021-12-21 06:29:15 +00:00
|
|
|
def inner(self, *args, **kwargs):
|
2021-12-20 09:41:48 +00:00
|
|
|
if self.frame is None:
|
2021-12-29 00:46:52 +00:00
|
|
|
sshot = self.gameio.fetch_sshot()
|
|
|
|
open_cv_image = np.array(sshot)
|
|
|
|
# Convert RGB to BGR
|
|
|
|
array = open_cv_image[:, :, ::-1].copy()
|
|
|
|
self.color_frame = CVImage("gameio frame", np.copy(array))
|
2021-12-28 01:30:40 +00:00
|
|
|
self.frame = CVImage("BW frame", self.color_frame.copy())
|
|
|
|
self.frame.image = self.frame.convert_color(False)
|
2021-12-24 18:02:52 +00:00
|
|
|
self.mask_frame()
|
2021-12-29 00:46:52 +00:00
|
|
|
print(self.frame)
|
2021-12-21 06:29:15 +00:00
|
|
|
return fn(self, *args, **kwargs)
|
2021-12-20 09:41:48 +00:00
|
|
|
return inner
|
|
|
|
|
2021-12-24 18:02:52 +00:00
|
|
|
def mask_frame(self):
|
2021-12-28 01:30:40 +00:00
|
|
|
self.lives_img = CVImage("lives", self.frame.snip(self.lives_rect))
|
|
|
|
self.frame.image = self.frame.mask(self.lives_rect)
|
2021-12-29 00:46:52 +00:00
|
|
|
self.score_img = CVImage("score", self.frame.snip(self.score_rect))
|
|
|
|
self.frame.image = self.frame.mask(self.score_rect)
|
2021-12-24 18:02:52 +00:00
|
|
|
|
2021-12-20 09:41:48 +00:00
|
|
|
def clear_frame(self):
|
2021-12-23 18:57:50 +00:00
|
|
|
self.prev_frame = frame
|
2021-12-20 09:41:48 +00:00
|
|
|
self.frame = None
|
|
|
|
|
|
|
|
@with_frame
|
|
|
|
def find_asteroids(self):
|
2021-12-29 00:46:52 +00:00
|
|
|
results = []
|
|
|
|
for a in self.asteroids:
|
|
|
|
r = self.frame.template_detect(a,
|
|
|
|
self.cv_template_thresh,
|
|
|
|
self.duplicate_dist_thresh)
|
|
|
|
results.extend(r)
|
|
|
|
return results
|
2021-12-21 06:29:15 +00:00
|
|
|
|
|
|
|
@with_frame
|
2021-12-23 01:29:36 +00:00
|
|
|
def display_results(self, rects = [], pointsets = [], circles = []):
|
2021-12-21 06:29:15 +00:00
|
|
|
"""Draws results on the current frame for test purposes."""
|
2021-12-28 01:30:40 +00:00
|
|
|
displayable = CVImage("GameModel results", self.color_frame.copy())
|
|
|
|
label_color = { "big": (255, 0, 0),
|
2021-12-23 01:29:36 +00:00
|
|
|
"normal": (0, 255, 0),
|
|
|
|
"small": (0, 0, 255),
|
2021-12-23 18:57:50 +00:00
|
|
|
"missile": (0, 255, 128),
|
2021-12-23 01:29:36 +00:00
|
|
|
"ship_on": (0, 0, 128),
|
2021-12-28 01:30:40 +00:00
|
|
|
"ship_off": (0, 64, 128)}
|
|
|
|
for r in rects:
|
|
|
|
displayable.draw_rect(r, color=label_color[r.label])
|
2021-12-23 01:29:36 +00:00
|
|
|
for ps in pointsets:
|
2021-12-28 01:30:40 +00:00
|
|
|
displayable.draw_poly(ps, color=(0, 255, 255))
|
2021-12-23 01:29:36 +00:00
|
|
|
|
|
|
|
for center, radius, label in circles:
|
2021-12-28 01:30:40 +00:00
|
|
|
displayable.draw_circle(center, radius)
|
|
|
|
displayable.draw_text(label, center, (255, 255, 0))
|
2021-12-29 00:46:52 +00:00
|
|
|
|
|
|
|
displayable.show()
|
2021-12-21 06:29:15 +00:00
|
|
|
|
2021-12-21 07:19:12 +00:00
|
|
|
@with_frame
|
2021-12-23 01:29:36 +00:00
|
|
|
def frame_sift(self):
|
2021-12-29 00:46:52 +00:00
|
|
|
ship_r = sqrt(rect_radius_squared(*self.ships[0].image.shape[:2]) * 0.85)
|
2021-12-28 01:30:40 +00:00
|
|
|
return self.frame.sift_clusters(cluster_radius = ship_r)
|
2021-12-23 01:29:36 +00:00
|
|
|
|
|
|
|
@with_frame
|
|
|
|
def find_ships(self):
|
2021-12-29 00:46:52 +00:00
|
|
|
results = []
|
|
|
|
for a in self.ships:
|
|
|
|
r = self.frame.template_detect(a,
|
|
|
|
self.cv_template_thresh,
|
|
|
|
self.duplicate_dist_thresh)
|
|
|
|
results.extend(r)
|
|
|
|
return results
|
2021-12-23 01:29:36 +00:00
|
|
|
|
2021-12-23 18:57:50 +00:00
|
|
|
@with_frame
|
|
|
|
def find_missiles(self):
|
2021-12-28 01:30:40 +00:00
|
|
|
p = CVImage.blob_params(minThreshold = 10, maxThreshold = 200,
|
|
|
|
maxArea = 100,
|
|
|
|
minConvexity = 0.95,
|
2021-12-29 00:46:52 +00:00
|
|
|
minInertiaRatio = 0.4)
|
2021-12-28 01:30:40 +00:00
|
|
|
return self.frame.blob_detect(size=9, params=p)
|
2021-12-23 18:57:50 +00:00
|
|
|
|
|
|
|
def analyse_frame(self):
|
|
|
|
rocks = self.find_asteroids()
|
2021-12-24 18:02:52 +00:00
|
|
|
#lives = self.find_ships()
|
2021-12-23 18:57:50 +00:00
|
|
|
shots = self.find_missiles()
|
|
|
|
clusters = self.frame_sift()
|
|
|
|
|
2021-12-24 18:02:52 +00:00
|
|
|
labeled_objects = rocks + shots
|
2021-12-23 18:57:50 +00:00
|
|
|
mystery_clusters = []
|
|
|
|
# TODO: remove these comprehensions and document pretty utility functions.
|
|
|
|
easy_find = lambda cluster: any(
|
2021-12-24 18:02:52 +00:00
|
|
|
[(not cluster.max_distance or cluster.max_distance < max(lo[1][0] - lo[0][0], lo[1][1] - lo[0][1]))
|
2021-12-23 18:57:50 +00:00
|
|
|
and point_in_rect(cluster.center, (lo[0], lo[1]))
|
|
|
|
for lo in labeled_objects])
|
|
|
|
hard_find = lambda cluster: any(
|
2021-12-24 18:02:52 +00:00
|
|
|
[(not cluster.max_distance or cluster.max_distance < max(lo[1][0] - lo[0][0], lo[1][1] - lo[0][1]))
|
2021-12-23 18:57:50 +00:00
|
|
|
and all([point_in_rect(p, (lo[0], lo[1]))
|
|
|
|
for p in cluster.points])
|
|
|
|
for lo in labeled_objects])
|
|
|
|
# Allow me to explain/apologize.
|
|
|
|
## The first term (cluster.max_distance < ...) stops big point clusters from
|
|
|
|
## being regarded as smalll objects. (Player ship being matched "inside" a missile)
|
|
|
|
|
|
|
|
## The second term (point_in_rect(...)) checks for a "cluster" inside a "rect".
|
|
|
|
## easy_find just checks the center.
|
|
|
|
## hard_find checks every point, in case the center is off.
|
|
|
|
|
|
|
|
for i, c in enumerate(clusters):
|
|
|
|
#if easy_find(c): continue
|
|
|
|
if hard_find(c): continue
|
|
|
|
mystery_clusters.append(c)
|
|
|
|
r_circles = [(c.center, c.max_distance or 5, f"mystery_{i}") for i, c in enumerate(mystery_clusters)]
|
|
|
|
gm.display_results(rects=labeled_objects, circles=r_circles)
|
2021-12-20 09:41:48 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
import platform
|
|
|
|
|
|
|
|
if platform.system() == "Windows":
|
|
|
|
io = gameio.WindowsGameIO()
|
|
|
|
# TODO: Detect OSX or show a message of sadness
|
|
|
|
|
|
|
|
else:
|
|
|
|
io = gameio.LinuxGameIO()
|
|
|
|
|
2021-12-21 06:29:15 +00:00
|
|
|
#input("Press <enter> to locate the game at the start screen.")
|
2021-12-20 09:41:48 +00:00
|
|
|
gm = GameModel(io)
|
|
|
|
|
2021-12-21 06:29:15 +00:00
|
|
|
# for testing purposes, populating window location at top-left of my screen
|
|
|
|
# io.loc is None when the title screen isn't found.
|
|
|
|
# manually setting io.loc crops all screenshots as if the title was found.
|
|
|
|
import pyscreeze
|
|
|
|
io.loc = pyscreeze.Box(0, 25, 800, 599)
|
|
|
|
|
2021-12-23 18:57:50 +00:00
|
|
|
from pprint import pprint
|
|
|
|
|
2021-12-21 06:29:15 +00:00
|
|
|
#input("Press <enter> to detect asteroids on screen.")
|
2021-12-23 01:29:36 +00:00
|
|
|
a_results = gm.find_asteroids()
|
|
|
|
print(f"Found {len(a_results)} asteroids")
|
|
|
|
#for a in a_results:
|
|
|
|
# print(a[0]) # position tuple
|
|
|
|
#gm.display_results(results)
|
|
|
|
s_results = gm.frame_sift()
|
|
|
|
ship_results = gm.find_ships()
|
|
|
|
polygons = [c.points for c in s_results]
|
2021-12-24 18:02:52 +00:00
|
|
|
##circles = [(c.center, c.max_distance, f"cluster_{i}") for i, c in enumerate(s_results)]
|
2021-12-29 00:46:52 +00:00
|
|
|
r_circles = [(c.center, sqrt(rect_radius_squared(*gm.ships[0].image.shape[:2])), f"cluster_{i}") for i, c in enumerate(s_results)]
|
2021-12-23 18:57:50 +00:00
|
|
|
missile_results = gm.find_missiles()
|
2021-12-24 18:02:52 +00:00
|
|
|
##m_circles = [(pt, 10, f"missile_{i}") for i, pt in enumerate(missiles)]
|
|
|
|
##pprint(a_results+ship_results+missile_results)
|
2021-12-29 00:46:52 +00:00
|
|
|
rects = a_results
|
|
|
|
if ship_results: rects.extend(ship_results)
|
|
|
|
if missile_results: rects.extend(missile_results)
|
|
|
|
gm.display_results(rects=rects, pointsets=polygons, circles=r_circles)
|
2021-12-23 18:57:50 +00:00
|
|
|
gm.analyse_frame()
|