-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.py
236 lines (204 loc) · 6.52 KB
/
utils.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import cv2
import numpy as np
import sys
def read_from_csv(csv_f):
with open(csv_f,'r') as f:
movie_data = {}
for line in f.readlines():
line_split = line.strip().split(",")
year = line_split[-1].split("|")
keywords = line_split[-2].split("|")
tagline = line_split[-3].split("|")
director = line_split[-4].split("|")
cast = line_split[-5].split("|")
movie = line_split[0].upper()
movie_data[movie] = [year,keywords,tagline,director,cast]
return movie_data
def get_movie_info(movies_data):
movies_list = list(movies_data.keys())
movie = np.random.choice(movies_list,1)[0].upper()
movie_info = movies_data[movie]
return movie,movie_info
def select_hints(movie_info):
# We will randomly select 3 types of
# hints to display
hints_index = list(np.random.choice(5,3,replace=False))
hints = []
hints_labels = ["Release Year","Keyword","Tagline","Director","Cast"]
labels = []
for hint_index in hints_index:
hint = np.random.choice(movie_info[hint_index],1)[0].upper()
hints.append(hint)
labels.append(hints_labels[hint_index].upper())
return hints,labels
def get_canvas(canvas_file):
img = cv2.imread(canvas_file,1)
return img
def draw_wrong(img,incorrect_attempts):
cv2.putText(img,"WRONG {}/6".format(incorrect_attempts+1),(380,40),\
cv2.FONT_HERSHEY_SIMPLEX,1,\
(0,0,255),2)
return img
def draw_hint(img,hints,labels,incorrect_attempts):
x,y = 20,30
if incorrect_attempts == 0:
return img
elif incorrect_attempts <= 1:
index = 0
elif incorrect_attempts <= 3:
index = 1
elif incorrect_attempts <= 6:
index = 2
cv2.putText(img,"{}".format(labels[index]),(x,y),\
cv2.FONT_HERSHEY_SIMPLEX,0.6,\
(255,0,255),1)
cv2.putText(img,"{}".format(hints[index]),(x,y+30),\
cv2.FONT_HERSHEY_SIMPLEX,0.6,\
(255,0,255),1)
return img
def draw_right(img):
cv2.putText(img,"RIGHT",(380,40),\
cv2.FONT_HERSHEY_SIMPLEX,0.7,\
(0,255,0),2)
return img
def draw_lost(img):
cv2.putText(img,"YOU LOST",(380,40),\
cv2.FONT_HERSHEY_SIMPLEX,0.7,\
(0,0,255),2)
return img
def draw_won(img):
cv2.putText(img,"YOU WON",(380,40),\
cv2.FONT_HERSHEY_SIMPLEX,0.7,\
(0,255,0),2)
return img
def draw_invalid(img):
cv2.putText(img,"INVALID INPUT",(300,40),\
cv2.FONT_HERSHEY_SIMPLEX,0.7,\
(0,0,255),2)
return img
def draw_reuse(img):
cv2.putText(img,"ALREADY USED",(300,40),\
cv2.FONT_HERSHEY_SIMPLEX,0.7,\
(0,0,255),2)
return img
def draw_used_chars(img,chars_entered):
cv2.putText(img,"Letters used:",(300,80),\
cv2.FONT_HERSHEY_SIMPLEX,0.5,\
(0,0,0),1)
y = 120
x = 350
count = 0
for i in chars_entered:
if count == 10:
x += 50
y = 120
cv2.putText(img,i,(x,y),\
cv2.FONT_HERSHEY_SIMPLEX,0.5,\
(0,0,0),1)
y += 20
count += 1
return img
def get_char_coords(movie):
x_coord = 100
y_coord = 400
char_ws = []
char_hs = []
for i in movie:
char_width, char_height = cv2.getTextSize(i,\
cv2.FONT_HERSHEY_SIMPLEX,1,2)[0]
char_ws.append(char_width)
char_hs.append(char_height)
max_char_h = max(char_hs)
max_char_w = max(char_ws)
char_rects = []
for i in range(len(char_ws)):
rect_coord = [(x_coord,y_coord-max_char_h),\
(x_coord+max_char_w,y_coord)]
char_rects.append(rect_coord)
x_coord = x_coord + max_char_w
return char_rects
def draw_blank_rects(movie,char_rects,img):
for i in range(len(char_rects)):
top_left, bottom_right = char_rects[i]
if not movie[i].isalpha() or \
ord(movie[i]) < 65 or \
ord(movie[i]) > 122 or \
(ord(movie[i]) > 90 and \
ord(movie[i]) < 97):
cv2.putText(img,movie[i],(top_left[0],\
bottom_right[1]),\
cv2.FONT_HERSHEY_SIMPLEX,\
1,(0,0,255),2)
continue
cv2.rectangle(img,top_left,\
bottom_right,\
(0,0,255),thickness=1,\
lineType = cv2.LINE_8)
return img
def check_all_chars_found(movie, chars_entered):
chars_to_be_checked = [i for i in movie if i.isalpha()]
for i in chars_to_be_checked:
if i not in chars_entered:
return False
return True
def draw_circle(img):
cv2.circle(img,(190,160),40,(0,0,0),thickness=2,\
lineType=cv2.LINE_AA)
return img
def draw_back(img):
cv2.line(img,(190,200),(190,320),\
(0,0,0),thickness=2,\
lineType=cv2.LINE_AA)
return img
def draw_left_hand(img):
cv2.line(img,(190,240),(130,200),\
(0,0,0),thickness=2,\
lineType=cv2.LINE_AA)
return img
def draw_right_hand(img):
cv2.line(img,(190,240),(250,200),\
(0,0,0),thickness=2,\
lineType=cv2.LINE_AA)
return img
def draw_left_leg(img):
cv2.line(img,(190,320),(130,360),\
(0,0,0),thickness=2,\
lineType=cv2.LINE_AA)
return img
def draw_right_leg(img):
cv2.line(img,(190,320),(250,360),\
(0,0,0),thickness=2,\
lineType=cv2.LINE_AA)
return img
def draw_hangman(img,num_tries):
if num_tries==1:
return draw_circle(img)
elif num_tries==2:
return draw_back(img)
elif num_tries==3:
return draw_left_hand(img)
elif num_tries==4:
return draw_right_hand(img)
elif num_tries==5:
return draw_left_leg(img)
elif num_tries==6:
return draw_right_leg(img)
else:
return img
def revealMovie(movie,img,char_rects):
#img = cv2.imread(canvas_file,1)
for i in range(len(movie)):
top_left, bottom_right = char_rects[i]
cv2.putText(img,movie[i],(top_left[0],bottom_right[1]),\
cv2.FONT_HERSHEY_SIMPLEX,\
1,(0,255,0),2)
return img
def displayLetter(img,letter,movie,char_rects):
for i in range(len(movie)):
if movie[i]==letter:
top_left, bottom_right = char_rects[i]
cv2.putText(img, movie[i],\
(top_left[0],bottom_right[1]),\
cv2.FONT_HERSHEY_SIMPLEX,\
1,(255,0,0),2)
return img