-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
225 lines (171 loc) · 5.22 KB
/
main.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
import pygame
from pygame.math import Vector3 as Vec, Vector2 as Vec2
from time import time
from math import radians, sqrt
from random import randint, choice
import string
import os
def make_if_not_exists(path):
if not os.path.exists(path):
os.mkdir(path)
else:
for file in os.listdir(path):
os.remove(path + file)
img_path = 'Plates/'
make_if_not_exists(img_path)
loc_path = 'Locations/'
make_if_not_exists(loc_path)
pygame.init()
pygame.font.init()
size = (640, 480)
screen = pygame.display.set_mode(size)
screen.fill([255, 255, 255])
pygame.display.set_icon(screen)
clock, fps = pygame.time.Clock(), 0
delta_time = 0
frame_start_time = 0
def get(v):
return (int(v.x), int(v.y), int(v.z))
def distance_sorter(polygon):
p = Vec()
for v in polygon:
p.x += v[0]
p.y += v[1]
p.z += v[2]
p.x /= 4
p.y /= 4
p.z /= 4
return p.distance_to(Vec(300, 300, -50))
def translate(value, leftMin, leftMax, rightMin, rightMax):
# Figure out how 'wide' each range is
leftSpan = leftMax - leftMin
rightSpan = rightMax - rightMin
# Convert the left range into a 0-1 range (float)
valueScaled = float(value - leftMin) / float(leftSpan)
# Convert the 0-1 range into a value in the right range.
return rightMin + (valueScaled * rightSpan)
class Plate:
def __init__(self, px, py, pz, rx, ry, rz, sx, sy, sz):
self.pos = Vec(px, py, pz)
self.rotation = Vec(rx, ry, rz)
self.size = Vec(sx, sy, sz)
self.load_image()
self.image_size = Vec2(self.image.get_size())
self.plate = ''
self.font = pygame.font.Font('assets/UKNumberPlate.ttf', 290)
self.verts = [
Vec(-1, -1, -1),
Vec(-1, -1, 1),
Vec(1, -1, 1),
Vec(1, -1, -1),
Vec(-1, 1, -1),
Vec(-1, 1, 1),
Vec(1, 1, 1),
Vec(1, 1, -1),
]
self.faces = [
(3, 7, 6, 2),
(4, 3, 7, 8),
(1, 4, 8, 5),
(2, 1, 5, 6),
(2, 3, 4, 1),
(6, 7, 8, 5)
]
def load_image(self):
self.image = pygame.image.load('assets/blankNumPlate.jpg')
def generate_number(self):
no_iqz = string.ascii_uppercase.replace(
'I', '').replace('Q', '').replace('Z', '')
plate = choice(no_iqz) + choice(no_iqz)
plate += choice(string.digits) + choice(string.digits)
plate += ' '
plate += choice(no_iqz) + choice(no_iqz) + choice(no_iqz)
self.plate = plate
return plate
def add_number(self):
lbl = self.font.render(self.generate_number(), True, [0, 0, 0])
self.image.blit(lbl, (120, 50))
def display(self, screen):
self.load_image()
self.add_number()
polygon = []
verts = []
for v in self.verts:
cv = Vec(v.x, v.y, v.z)
cv.x *= self.size.x
cv.y *= self.size.y
cv.z *= self.size.z
cv = cv.rotate_x_rad(radians(self.rotation.x))
cv = cv.rotate_y_rad(radians(self.rotation.y))
cv = cv.rotate_z_rad(radians(self.rotation.z))
cv.x += self.pos.x
cv.y += self.pos.y
cv.z += self.pos.z
verts.append(cv)
ps = []
front = None
for i1, i2, i3, i4 in map(lambda f: tuple([i-1 for i in f]), self.faces):
polygon = [
get(verts[i1]),
get(verts[i2]),
get(verts[i3]),
get(verts[i4])
]
if self.faces[2] == (i1+1, i2+1, i3+1, i4+1):
front = polygon
ps.append(polygon)
ps.sort(key=distance_sorter, reverse=True)
for p in ps:
check = p
p = list(map(lambda elem: elem[:2], p))
pygame.draw.polygon(screen, [150, 150, 0], p)
pygame.draw.polygon(screen, [0, 0, 0], p, 1)
if check == front:
top_left = Vec2(p[0])
top_right = Vec2(p[1])
bottom_right = Vec2(p[2])
bottom_left = Vec2(p[3])
start = Vec2(p[0])
end = Vec2(p[1])
current = Vec2(p[0])
x_move = (end - start).normalize()
y_move = (bottom_left - top_left).normalize()
while True:
current += x_move
draw = True
uv = Vec2(translate(current.x, start.x, end.x, 0, self.image_size.x), translate(
current.y, top_left.y, bottom_left.y, 0, self.image_size.y))
try:
c = self.image.get_at((int(uv.x), int(uv.y)))
except IndexError:
draw = False
if draw:
screen.set_at((int(current.x), int(current.y)), c)
if abs(current.x - end.x) + abs(current.y - end.y) < 1:
start += y_move
end += y_move
current = Vec2(start.x, start.y)
if abs(start.x - bottom_left.x) + abs(start.y - bottom_left.y) < 1:
break
return front
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
frame_start_time = time()
screen.fill(0)
scale = randint(3, 6) / 10
plate = Plate(300, 300, 300, randint(-45, 45), randint(-45,
45), 0, 200 * scale, 42 * scale, 5 * scale)
front_face = plate.display(screen)
top_left = front_face[0][:2]
bottom_right = front_face[2][:2]
filename = plate.plate.replace(' ', '_').lower()
pygame.image.save(screen, img_path + filename + '.png')
with open(loc_path + filename + '.txt', 'w') as file:
file.write(str(top_left) + ',' + str(bottom_right))
pygame.display.update()
clock.tick(fps)
delta_time = time() - frame_start_time
pygame.display.set_caption(f'{int(clock.get_fps())} Plates generated per second | {len(os.listdir("Locations"))} Total')