-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconfig.py
200 lines (154 loc) · 5.52 KB
/
config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import os, sys, requests
#https://pyinstaller.readthedocs.io/en/stable/runtime-information.html
#print(os.environ)
version = "1.3.10"
if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'):
print('running in a PyInstaller bundle', sys._MEIPASS)
application_path = os.path.dirname(sys.executable)
print(application_path)
else:
print('running in a normal Python process')
application_path = None
def fp(filepath):
if getattr(sys, 'frozen', False) and hasattr(sys, '_MEIPASS'):
filepath = os.path.join(sys._MEIPASS, filepath)
return filepath
import pygame
if pygame.get_sdl_version() < (2, 0, 0):
raise Exception("This software requires SDL2. Pygame is probably outdated.")
import datetime
import cv2
import numpy as np
from TetrisUtility import scaleImage
import threading
lock = threading.Lock()
possibleCount = 0
done = False
doneEval = False
numEvalDone = 0
isDepth3 = True
isEvalDepth3 = isDepth3
isAnalysis = False
isLoad = False # true if loaded from analysis text file
NTSC = 0
PAL = 1
gamemode = NTSC
poolSize = 20
hzString = None
session = None
startLevel = -1
isMac = sys.platform.startswith('darwin')
print("Is mac: ", isMac)
pygame.init()
pygame.font.init()
font = pygame.font.Font(fp('Images/Fonts/verdana.ttf'), 34)
fontbold = pygame.font.Font(fp('Images/Fonts/verdanabold.ttf'), 30)
font2 = pygame.font.Font(fp('Images/Fonts/verdana.ttf'), 25)
font2bold = pygame.font.Font(fp('Images/Fonts/verdanabold.ttf'), 25)
fontbig = pygame.font.Font(fp('Images/Fonts/verdana.ttf'), 70)
fontbigbold = pygame.font.Font(fp('Images/Fonts/verdanabold.ttf'), 70)
fontbigbold2 = pygame.font.Font(fp('Images/Fonts/verdanabold.ttf'), 52)
fontbigbold3 = pygame.font.Font(fp('Images/Fonts/verdanabold.ttf'), 48)
fontnum = pygame.font.Font(fp('Images/Fonts/numbers.ttf'), 25)
KEY_DELAY = 500
KEY_INTERVAL = 100
pygame.key.set_repeat(KEY_DELAY, KEY_INTERVAL)
filename = None
isImage = False
fps = 30
totalFrames = 2
VIDEO_X = 0
VIDEO_Y = 0
VIDEO_WIDTH = None
VIDEO_HEIGHT = None
hydrantScale = -1
# Location of the bottom-right corner of the video boundary
X_MAX = 1642
Y_MAX = 1360
# Scale constant for tetris footage
SCALAR = 1.4
COLOR_CALLIBRATION = 15
info = pygame.display.Info()
REAL_WIDTH = info.current_w*0.8
REAL_HEIGHT = REAL_WIDTH * 2856 / 4806
# scaled width and height, arbitrary values that maintain aspect ratio and are high enough to have decent resolution
SCREEN_WIDTH = 1280*2
SCREEN_HEIGHT = 720*2
RATIO = SCREEN_WIDTH / SCREEN_HEIGHT
icon_surf = pygame.image.load(fp("Images/logo.png"))
pygame.display.set_icon(icon_surf)
# Global screen surface variables
# https://stackoverflow.com/questions/34910086/pygame-how-do-i-resize-a-surface-and-keep-all-objects-within-proportionate-to-t
realscreen = pygame.display.set_mode((REAL_WIDTH, REAL_HEIGHT), pygame.HWSURFACE | pygame.DOUBLEBUF | pygame.RESIZABLE)
#screen = realscreen.copy()
#screen = pygame.Surface([1152, 685])
screen = pygame.Surface([SCREEN_WIDTH, SCREEN_HEIGHT])
pygame.display.set_caption('tetrisfish (v{}) by Ansel, powered by StackRabbit'.format(version))
def reset():
global isEvalDepth3, doneEval, done, possibleCount, numEvalDone
isEvalDepth3 = isDepth3
doneEval = isDepth3
done = False
possibleCount = 0
numEvalDone = 0
# Get timestamp at frame
def timestamp(frame):
return str(datetime.timedelta(seconds = round(frame / fps)))
def resizeScreen(pygame, event):
global realscreen
avg = (event.w / SCREEN_WIDTH + event.h / SCREEN_HEIGHT) / 2
realscreen = pygame.display.set_mode([avg * SCREEN_WIDTH, avg * SCREEN_HEIGHT], pygame.HWSURFACE | pygame.DOUBLEBUF | pygame.RESIZABLE)
# Display screen and handle events, keeping ratio when resizing window
# Returns true if exited
def drawWindow(scale = 1):
# Resize window, keep aspect ratio
rs = realscreen.get_rect()
ratio = (screen.get_rect().h / screen.get_rect().w)
realscreen.blit(pygame.transform.smoothscale(screen, [scale*rs.w, scale*rs.w * ratio]), (0, 0))
#realscreen.blit(screen, [0,0])
# Open video from opencv
def getVideo():
vcap = cv2.VideoCapture(filename)
if not vcap.isOpened():
print ("File Cannot be Opened")
assert(False)
return vcap
# Draw video frame
def displayTetrisImage(frame):
"""
uses bounded surf so we don't overblit into the GUI
"""
frame = frame.transpose(1,0,2) # convert from BGR to RGB
surf = pygame.surfarray.make_surface(frame)
surf = scaleImage(surf, SCALAR)
boundedSurf = get_video_render_surface()
boundedSurf.blit(surf,[VIDEO_X, VIDEO_Y])
screen.blit(boundedSurf, (0, 0))
return surf
def get_video_render_surface(transparent=False):
"""
returns a new surface to blit the video onto.
Can also return a transparent version for overlays.
"""
size = [X_MAX,Y_MAX]
if transparent:
return pygame.Surface(size,pygame.SRCALPHA)
return pygame.Surface(size)
# Go to specific frame for video capture
def goToFrame(vcap, framecount, frame = None):
vcap.set(cv2.CAP_PROP_POS_FRAMES, framecount)
ret, newframe = vcap.read()
if type(newframe) == np.ndarray:
frame = np.flip(newframe,2)
else:
print(newframe)
print("framecount: ", framecount)
assert(False)
return frame, framecount
# Scale real (x,y) to scaled (x,y) for screen surface
def getScaledPos(x,y):
x = (x / realscreen.get_rect().width) * SCREEN_WIDTH
y = (y / (realscreen.get_rect().width * SCREEN_HEIGHT / SCREEN_WIDTH)) * SCREEN_HEIGHT
return x,y
NUM_HORIZONTAL_CELLS = 10
NUM_VERTICAL_CELLS = 20