''' Created on Jul 1, 2015 Copyright Brett Paufler @author: Brett Paufler Not Refactored when I wrapped football up on 9-4-15. This could use all sorts of help: 1 - Numbers are probably better off being imported Img 2 - Timer isn't working, so imported images 3 - Lines are a hack, so maybe everything, imported images Other than that, we have output. So, it's served me well. Will refactor another day. ''' #import opencv #from football_roster import football_roster import numpy as np from skimage.io import imsave import scipy.misc #import imresize, imreshape from scipy.ndimage.io import imread d_arrow = np.array([[1,0,0,0], [1,1,0,0], [1,1,0,0], [1,1,1,0], [1,1,1,1], [1,1,1,0], [1,1,0,0], [1,0,0,0]]) d_zero = np.array([[1,1,1,1,1], [1,0,0,0,1], [1,0,0,0,1], [1,0,0,0,1], [1,0,0,0,1], [1,0,0,0,1], [1,0,0,0,1], [1,1,1,1,1]]) d_one = np.fliplr( np.array([[0,0,0,0,1], [0,0,0,1,1], [0,0,1,1,1], [0,0,0,0,1], [0,0,0,0,1], [0,0,0,0,1], [0,0,0,0,1], [0,0,0,0,1]])) d_two = np.fliplr( np.array([[1,1,1,1,1], [0,0,0,0,1], [0,0,0,0,1], [1,1,1,1,1], [1,0,0,0,0], [1,0,0,0,0], [1,0,0,0,0], [1,1,1,1,1]])) d_three = np.fliplr( np.array([[1,1,1,1,1], [0,0,0,0,1], [0,0,0,0,1], [1,1,1,1,1], [0,0,0,0,1], [0,0,0,0,1], [0,0,0,0,1], [1,1,1,1,1]])) d_four = np.fliplr( np.array([[0,0,0,1,1], [0,0,1,1,1], [0,1,1,0,1], [1,1,0,0,1], [1,1,1,1,1], [0,0,0,0,1], [0,0,0,0,1], [0,0,0,0,1]])) d_five = np.fliplr( np.array([[1,1,1,1,1], [1,0,0,0,0], [1,0,0,0,0], [1,1,1,1,1], [0,0,0,0,1], [0,0,0,0,1], [0,0,0,0,1], [1,1,1,1,1]])) d_six = np.fliplr( np.array([[1,0,0,0,0], [1,0,0,0,0], [1,0,0,0,0], [1,0,0,0,0], [1,1,1,1,1], [1,0,0,0,1], [1,0,0,0,1], [1,1,1,1,1]])) d_seven = np.fliplr( np.array([[1,1,1,1,1], [0,0,0,0,1], [0,0,0,0,1], [0,0,0,0,1], [0,0,0,0,1], [0,0,0,0,1], [0,0,0,0,1], [0,0,0,0,1]])) d_eight = np.fliplr( np.array([[1,1,1,1,1], [1,0,0,0,1], [1,0,0,0,1], [1,1,1,1,1], [1,0,0,0,1], [1,0,0,0,1], [1,0,0,0,1], [1,1,1,1,1]])) d_nine = np.fliplr( np.array([[1,1,1,1,1], [1,0,0,0,1], [1,0,0,0,1], [1,1,1,1,1], [0,0,0,0,1], [0,0,0,0,1], [0,0,0,0,1], [0,0,0,0,1]])) d_colon = np.fliplr( np.array([[0,0,0,0,0], [0,0,1,0,0], [0,0,1,0,0], [0,0,0,0,0], [0,0,0,0,0], [0,0,1,0,0], [0,0,1,0,0], [0,0,0,0,0]])) class HalfField(): '''Half Field is hard coded at (1700, 1500)''' def __init__(self): self.img_size = (1700, 1500) self.w = self.img_size[0] - 1 self.h = self.img_size[1] - 1 self.logo = self.load_logo(fN='MutaGeneticsLogo.png') def ix(self, x): '''Field_X, converts x_ball coordinates to x_field coordinates, for converting play logic to Img''' #w = img_size[0] - 1.0 x = self.w/2.0 + x * self.w /170.0 if x < 0.0: x = 0.0 if x > self.w: x = self.w return int(x) def iy(self, y): '''Field_Y, converts y_ball coordinates to y_field coordinates, for converting play logic to Img''' #h = img_size[1] - 1.0 y = 0.60 * self.h - y * self.h /150.0 if y < 0.0: y = 0.0 if y > self.h: y = self.h #print y return int(y) def load_logo(self, fN): logo = imread(fN) #print logo.shape logo = logo[:,:,3] #print logo return logo.transpose((1,0)) def img_field(self, sN='./images/original.png'): #width = 160 + 5 + 5 = 170 #height = 20 + 40 = 60 r = np.zeros(self.img_size, dtype=int) g = np.zeros(self.img_size, dtype=int) b = np.zeros(self.img_size, dtype=int) #WHITE X (TO KILL LONG TERM) #for Img in [r,g,b]: # Img[self.ix(-3):self.ix(3), self.iy(0)] = 255 # # Img[self.ix(0), self.iy(3):self.iy(-3)] = 255 #Five Yard Horizontal Markers for y in range(-60, 91, 15): g[self.ix(-80):self.ix(80), self.iy(y)] = 255 #Vertical Side Lines g[self.ix(-80), self.iy(999):self.iy(-999)] = 255 g[self.ix(80), self.iy(999):self.iy(-999)] = 255 #Dotted Yard Markers for y in range(-60, 91, 3): for x in [-79, -14, 12, 77]: g[self.ix(x):self.ix(x+2), self.iy(y)] = 255 #ZEROS for y in [-30, 0, 30, 60, 90]: #LEFT g[self.ix(-65):self.ix(-50), self.iy(y-1):self.iy(y-7)] = scipy.misc.imresize( d_zero, (150,60)) for y in [-30, 0, 30, 60]: #RIGHT g[self.ix(50):self.ix(65), self.iy(y+7):self.iy(y+1)] = scipy.misc.imresize( d_zero, (150,60)) #ARROWS for y in [-30, 0, 30, 60, 90]: g[self.ix(-60):self.ix(-55), self.iy(y-9):self.iy(y-13)] = scipy.misc.imresize( d_arrow, (50,40)) g[self.ix(55):self.ix(60), self.iy(y-9):self.iy(y-13)] = scipy.misc.imresize( d_arrow, (50,40)) #NUMBERS for y, d in zip([-30, 0, 30, 60], [d_one, d_two, d_three, d_four]): g[self.ix(-65):self.ix(-50), self.iy(y+7):self.iy(y+1)] = scipy.misc.imrotate( scipy.misc.imresize(d, (150,60)), 180) g[self.ix(50):self.ix(65), self.iy(y-1):self.iy(y-7)] = scipy.misc.imresize( d, (150,60)) y, d = 90, d_five g[self.ix(50):self.ix(65), self.iy(y-1):self.iy(y-7)] = scipy.misc.imresize(d, (150,60)) #MutaGenetics logo r[600:1100, 20:130] = self.logo img = np.dstack((r,g,b)) img = img.transpose((1,0,2)) if sN: imsave(sN, img) return img def player_mask(self, rotation, offense, size=(20,20)): '''returns a player graphical mask offense True=red, False=blue rotation (clockwise starting at zero at top) 0 = up (player faces up on board) 90 = right 180 = down 270 = left ''' body_mask = np.array([ [0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0], [0,0,1,1,1,1,1,1,1,0,0], [0,1,1,1,0,0,0,1,1,1,0], [0,1,1,0,0,1,0,0,1,1,0], [0,1,1,1,0,0,0,1,1,1,0], [0,0,1,1,1,1,1,1,1,0,0], [0,0,0,0,0,0,0,0,0,0,0], ]) torso_mask = np.array([ [0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0], [0,0,1,1,0,0,0,1,1,0,0], [0,0,0,0,1,1,1,0,0,0,0], [0,0,0,1,1,0,1,1,0,0,0], [0,0,1,1,0,1,0,1,1,0,0], [0,0,0,1,1,0,1,1,0,0,0], [0,0,0,0,1,1,1,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0], ]) head_mask = np.array([ [0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,1,0,0,0,0,0], [0,0,0,0,1,1,1,0,0,0,0], [0,0,0,0,0,1,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0], ]) if offense: #red mask = np.dstack((body_mask, torso_mask, head_mask)) else: #blue mask = np.dstack(( torso_mask, head_mask, body_mask)) mask = mask.transpose((1,0,2)) mask = scipy.misc.imresize(mask, size) mask = scipy.misc.imrotate(mask, rotation) return mask def img_to_list(self, scrimmage, turn=0, sN='./images/img_play_test.png'): img = self.img_field(sN=False) img = img.transpose((1,0,2)) #Offense for player in scrimmage.players: x, y = self.ix(player.x), self.iy(player.y) mask = self.player_mask(player.z, offense=(player.number>=0)) if 10 < x < self.w - 10 and 10 < y < self.h - 10: img[x-10:x+10, y-10:y+10] += mask ''' #Defense for player in football_scrimmage.defense: x, y = self.ix(player.x), self.iy(player.y) mask = self.player_mask(player.z, offense=False) if 10 < x < self.w - 10 and 10 < y < self.h - 10: Img[x-10:x+10, y-10:y+10] += mask ''' #1700, 1500 time = '{:0>4}'.format(str(turn * 10)) #print time digit_map = {0:d_zero, 1:d_one, 2:d_two, 3:d_three, 4:d_four, 5:d_five, 6:d_six, 7:d_seven, 8:d_eight, 9:d_nine, 'colon':d_colon} #print time[0] #print digit_map[0] digits = [digit_map[int(time[0])], digit_map[int(time[1])], digit_map['colon'], digit_map[int(time[2])], digit_map[int(time[3])], ] x = range(1100, 1700, 100) #[1350, 1400, 1450, 1500, 1550] for d, x in zip(digits, x): img[x:x+40, 1375:1475, 0] = scipy.misc.imrotate( scipy.misc.imresize(d, (40, 100)), 90) img[img>255] = 255 img = img.transpose((1,0,2)) return img if __name__ == '__main__': hf = HalfField() img = hf.img_field() img_list = [] for x in range(6): img_list.append(img) #hf.small_gif(img_list) #hf.img_play() print 'WRONG THIS IS FIELD!!!'