Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hackassin
GitHub Repository: hackassin/learnopencv
Path: blob/master/Hangman/utils.py
3118 views
1
import cv2
2
import numpy as np
3
import sys
4
5
def read_from_csv(csv_f):
6
with open(csv_f,'r') as f:
7
movie_data = {}
8
for line in f.readlines():
9
line_split = line.strip().split(",")
10
year = line_split[-1].split("|")
11
keywords = line_split[-2].split("|")
12
tagline = line_split[-3].split("|")
13
director = line_split[-4].split("|")
14
cast = line_split[-5].split("|")
15
movie = line_split[0].upper()
16
movie_data[movie] = [year,keywords,tagline,director,cast]
17
return movie_data
18
19
def get_movie_info(movies_data):
20
movies_list = list(movies_data.keys())
21
movie = np.random.choice(movies_list,1)[0].upper()
22
movie_info = movies_data[movie]
23
return movie,movie_info
24
25
def select_hints(movie_info):
26
# We will randomly select 3 types of
27
# hints to display
28
hints_index = list(np.random.choice(5,3,replace=False))
29
hints = []
30
hints_labels = ["Release Year","Keyword","Tagline","Director","Cast"]
31
labels = []
32
for hint_index in hints_index:
33
hint = np.random.choice(movie_info[hint_index],1)[0].upper()
34
hints.append(hint)
35
labels.append(hints_labels[hint_index].upper())
36
return hints,labels
37
38
def get_canvas(canvas_file):
39
img = cv2.imread(canvas_file,1)
40
return img
41
42
def draw_wrong(img,incorrect_attempts):
43
cv2.putText(img,"WRONG {}/6".format(incorrect_attempts+1),(380,40),\
44
cv2.FONT_HERSHEY_SIMPLEX,1,\
45
(0,0,255),2)
46
return img
47
48
def draw_hint(img,hints,labels,incorrect_attempts):
49
x,y = 20,30
50
if incorrect_attempts == 0:
51
return img
52
elif incorrect_attempts <= 1:
53
index = 0
54
elif incorrect_attempts <= 3:
55
index = 1
56
elif incorrect_attempts <= 6:
57
index = 2
58
cv2.putText(img,"HINT: {}".format(labels[index]),(x,y),\
59
cv2.FONT_HERSHEY_SIMPLEX,0.6,\
60
(255,0,255),1)
61
cv2.putText(img,"{}".format(hints[index]),(x,y+30),\
62
cv2.FONT_HERSHEY_SIMPLEX,0.6,\
63
(255,0,255),1)
64
return img
65
66
def draw_right(img):
67
cv2.putText(img,"RIGHT",(380,40),\
68
cv2.FONT_HERSHEY_SIMPLEX,0.7,\
69
(0,255,0),2)
70
return img
71
72
def draw_lost(img):
73
cv2.putText(img,"YOU LOST",(380,40),\
74
cv2.FONT_HERSHEY_SIMPLEX,0.7,\
75
(0,0,255),2)
76
return img
77
78
def draw_won(img):
79
cv2.putText(img,"YOU WON",(380,40),\
80
cv2.FONT_HERSHEY_SIMPLEX,0.7,\
81
(0,255,0),2)
82
return img
83
84
def draw_invalid(img):
85
cv2.putText(img,"INVALID INPUT",(300,40),\
86
cv2.FONT_HERSHEY_SIMPLEX,0.7,\
87
(0,0,255),2)
88
return img
89
90
def draw_reuse(img):
91
cv2.putText(img,"ALREADY USED",(300,40),\
92
cv2.FONT_HERSHEY_SIMPLEX,0.7,\
93
(0,0,255),2)
94
return img
95
96
def draw_used_chars(img,chars_entered,letter):
97
cv2.putText(img,"Letters used:",(300,80),\
98
cv2.FONT_HERSHEY_SIMPLEX,0.5,\
99
(0,0,0),1)
100
y = 120
101
x = 350
102
count = 0
103
for i in chars_entered:
104
if count == 10:
105
x += 50
106
y = 120
107
if i==letter:
108
cv2.putText(img,i,(x,y),\
109
cv2.FONT_HERSHEY_SIMPLEX,0.5,\
110
(0,0,255),1)
111
else:
112
cv2.putText(img,i,(x,y),\
113
cv2.FONT_HERSHEY_SIMPLEX,0.5,\
114
(0,0,0),1)
115
y += 20
116
count += 1
117
return img
118
119
def get_char_coords(movie):
120
x_coord = 100
121
y_coord = 400
122
123
char_ws = []
124
char_hs = []
125
126
for i in movie:
127
char_width, char_height = cv2.getTextSize(i,\
128
cv2.FONT_HERSHEY_SIMPLEX,1,2)[0]
129
char_ws.append(char_width)
130
char_hs.append(char_height)
131
132
max_char_h = max(char_hs)
133
max_char_w = max(char_ws)
134
135
char_rects = []
136
137
for i in range(len(char_ws)):
138
rect_coord = [(x_coord,y_coord-max_char_h),\
139
(x_coord+max_char_w,y_coord)]
140
char_rects.append(rect_coord)
141
x_coord = x_coord + max_char_w
142
143
return char_rects
144
145
def draw_blank_rects(movie,char_rects,img):
146
147
for i in range(len(char_rects)):
148
top_left, bottom_right = char_rects[i]
149
if not movie[i].isalpha() or \
150
ord(movie[i]) < 65 or \
151
ord(movie[i]) > 122 or \
152
(ord(movie[i]) > 90 and \
153
ord(movie[i]) < 97):
154
cv2.putText(img,movie[i],(top_left[0],\
155
bottom_right[1]),\
156
cv2.FONT_HERSHEY_SIMPLEX,\
157
1,(0,0,255),2)
158
continue
159
cv2.rectangle(img,top_left,\
160
bottom_right,\
161
(0,0,255),thickness=1,\
162
lineType = cv2.LINE_8)
163
164
return img
165
166
def check_all_chars_found(movie, chars_entered):
167
chars_to_be_checked = [i for i in movie if i.isalpha()]
168
for i in chars_to_be_checked:
169
if i not in chars_entered:
170
return False
171
return True
172
173
def draw_circle(img):
174
cv2.circle(img,(190,160),40,(0,0,0),thickness=2,\
175
lineType=cv2.LINE_AA)
176
return img
177
178
def draw_back(img):
179
cv2.line(img,(190,200),(190,320),\
180
(0,0,0),thickness=2,\
181
lineType=cv2.LINE_AA)
182
return img
183
184
def draw_left_hand(img):
185
cv2.line(img,(190,240),(130,200),\
186
(0,0,0),thickness=2,\
187
lineType=cv2.LINE_AA)
188
return img
189
190
def draw_right_hand(img):
191
cv2.line(img,(190,240),(250,200),\
192
(0,0,0),thickness=2,\
193
lineType=cv2.LINE_AA)
194
return img
195
196
def draw_left_leg(img):
197
cv2.line(img,(190,320),(130,360),\
198
(0,0,0),thickness=2,\
199
lineType=cv2.LINE_AA)
200
return img
201
202
def draw_right_leg(img):
203
cv2.line(img,(190,320),(250,360),\
204
(0,0,0),thickness=2,\
205
lineType=cv2.LINE_AA)
206
return img
207
208
def draw_hangman(img,num_tries):
209
if num_tries==1:
210
return draw_circle(img)
211
elif num_tries==2:
212
return draw_back(img)
213
elif num_tries==3:
214
return draw_left_hand(img)
215
elif num_tries==4:
216
return draw_right_hand(img)
217
elif num_tries==5:
218
return draw_left_leg(img)
219
elif num_tries==6:
220
return draw_right_leg(img)
221
else:
222
return img
223
224
def revealMovie(movie,img,char_rects):
225
#img = cv2.imread(canvas_file,1)
226
for i in range(len(movie)):
227
top_left, bottom_right = char_rects[i]
228
cv2.putText(img,movie[i],(top_left[0],bottom_right[1]),\
229
cv2.FONT_HERSHEY_SIMPLEX,\
230
1,(0,255,0),2)
231
return img
232
233
def displayLetter(img,letter,movie,char_rects):
234
for i in range(len(movie)):
235
if movie[i]==letter:
236
top_left, bottom_right = char_rects[i]
237
cv2.putText(img, movie[i],\
238
(top_left[0],bottom_right[1]),\
239
cv2.FONT_HERSHEY_SIMPLEX,\
240
1,(255,0,0),2)
241
return img
242
243