-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_test_data.py
246 lines (183 loc) · 7.8 KB
/
generate_test_data.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
237
238
239
240
241
242
243
244
245
246
import database_commands.database_commands as database_commands
import random
import requests
from io import BytesIO
from PIL import Image, ImageDraw, ImageFont
import os
from bs4 import BeautifulSoup
import shutil
database_commands = database_commands.DataBase()
connection, cursor = database_commands.connect_database("database.db")
all_grocery_items = []
#delte all data from the database but the first two
database_commands.delete_all_data(connection, cursor, "products", "id > 0")
database_commands.delete_all_data(connection, cursor, "images", "id > 0")
# Delete "images2" directory if it exists
if os.path.exists("images2"):
shutil.rmtree("images2")
# Function to generate a random color
def generate_random_color():
r = random.randint(0, 255)
g = random.randint(0, 255)
b = random.randint(0, 255)
if r < 100 and g < 100 or r < 100 and b < 100 or g < 100 and b < 100:
r = r + 100
g = g + 100
b = b + 100
return (r, g, b)
def generate_random_background_color():
i = random.randint(0, 2)
if i == 0:
r = random.randint(0, 255)
g = 0
b = 0
elif i == 1:
r = 0
g = random.randint(0, 255)
b = 0
else:
r = 0
g = 0
b = random.randint(0, 255)
if r < 100 and g < 100 or r < 100 and b < 100 or g < 100 and b < 100:
r = r + 100
g = g + 100
b = b + 100
return (r, g, b)
def generate_random_name():
grocery_items = []
grocery_item_pictures = []
while True:
if not grocery_items: # if grocery_items is empty
url = 'https://www.chefkoch.de/'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
for item in soup.find_all(class_='ds-recipe-card__image-wrap ds-teaser-link__image-wrap'):
grocery_item_pictures.append(item.find('img')['src'])
grocery_items.append(item.find('img')['alt'])
# Shuffle the items to ensure randomness
combined = list(zip(grocery_items, grocery_item_pictures))
random.shuffle(combined)
grocery_items, grocery_item_pictures = zip(*combined)
for grocery_item, grocery_item_picture in zip(grocery_items, grocery_item_pictures):
if grocery_item not in all_grocery_items and "newsletter" not in grocery_item_picture:
all_grocery_items.append(grocery_item)
return grocery_item, grocery_item_picture
# Clear the lists for the next iteration
grocery_items = []
grocery_item_pictures = []
# Specify the number of pictures to generate
num_pictures = 80
# Specify the output directory where the pictures will be saved
output_directory = 'images2/'
# Specify the font file path
print(os.getcwd())
# Ensure the output directory exists
os.makedirs(output_directory, exist_ok=True)
font_size = 30
font = ImageFont.truetype('arial.ttf', font_size)
# Loop to generate the pictures
for i in range(num_pictures):
# Generate a random color
color = generate_random_color()
# Generate a random grocery item name
name, picture_link = generate_random_name()
name = name.replace('\n', '')
name = name.replace('/n', '')
name = name.replace('\r', '')
name = name.replace('/', '')
name = name.replace('\\', '')
name = name.replace('//', '')
name = name.replace('-', ' ')
name = name.replace('„', '')
name = name.replace("“", '')
name = name.strip()
name = name.rstrip()
r, g, b = generate_random_background_color()
# Create a new image with a white background
image = Image.open(BytesIO(requests.get(picture_link).content))
# Create a draw object
draw = ImageDraw.Draw(image)
# Specify the font size and font file
# Calculate the position to write the name on the image
#remove all special characters from the name like ä,ü,ö,ß
name = name.replace('ä', 'ae')
name = name.replace('ü', 'ue')
name = name.replace('ö', 'oe')
name = name.replace('ß', 'ss')
name = name.replace('Ä', 'Ae')
name = name.replace('Ü', 'Ue')
name = name.replace('Ö', 'Oe')
name = name.replace('ß', 'Ss')
name = name.replace('"', '')
name = name.replace("'", '')
name = name.replace(":", '')
name = name.replace(";", '')
name = name.replace("!", '')
name = name.replace("?", '')
name = name.replace("(", '')
name = name.replace(")", '')
try:
bbox = draw.textbbox((0, 0), name)
except UnicodeEncodeError:
print("UnicodeEncodeError", name)
continue
text_width, text_height = draw.textsize(name, font=font)
x = (image.width - text_width) // 2
y = (image.height - text_height) // 2
# Write the name on the image
draw.text((x, y), name, fill=color, font=font)
# Save the image
image.save(output_directory + name + '.png')
#generate 50 funny descriptions for the products
adjectives = ["lustig", "albern", "verrueckt", "hilarisch", "laecherlich", "albern", "verrueckt", "skurril", "komisch", "absurd",
"grotesk", "bizarr", "seltsam", "unheimlich", "merkwuerdig", "sonderbar", "kurios", "eigenartig", "ungewoehnlich",
"absonderlich", "fremdartig", "exzentrisch", "wunderlich", "schrullig", "spassig", "ulkig", "witzig", "scherzhaft",
"humorvoll", "amuesant", "unterhaltsam", "lustig", "heiter", "froehlich", "vergnuegt", "gluecklich", "zufrieden",
"befriedigt", "erfreut", "begeistert", "entzueckt", "hocherfreut", "uebergluecklich"]
nouns = ["Katze", "Hund", "Elefant", "Clown", "Banane", "Alien", "Huhn", "Roboter", "Pirat", "Ninja",
"Auto", "Baum", "Stuhl", "Buch", "Tisch", "Haus", "Schuh", "Schlüssel", "Lampe", "Computer",
"Fenster", "Tür", "Bild", "Uhr", "Telefon", "Brille", "Kugelschreiber", "Tasse", "Teller", "Löffel"]
descriptions1 = []
descriptions2 = []
for _ in range(50):
adjective = random.choice(adjectives)
noun = random.choice(nouns)
descriptions1.append(f"{adjective} {noun}.")
for _ in range(50):
adjective = random.choice(adjectives)
noun = random.choice(nouns)
descriptions2.append(f"{adjective} {noun}.")
which_description1 = random.randint(0, 49)
which_description2 = random.randint(0, 49)
#save image path and name in the database
print("i", i)
count_category = random.randint(1, 4)
if count_category == 1:
count = random.randint(1, 10)
elif count_category == 2:
count = random.randint(11, 100)
elif count_category == 3:
count = random.randint(1, 5)
else:
count = random.randint(20, 200)
price_category = random.randint(1, 6)
if price_category == 1:
price = random.randint(1, 100) / 100
elif price_category == 2:
price = random.randint(1, 1000000000) / 1000
elif price_category == 3:
price = random.randint(1, 100) / 10
elif price_category == 4:
random.randint(1,2 )
if random.randint(1,2) == 1:
price = random.randint(1, 1000000) / 100
else:
price = random.randint(1, 10) / 10
elif price_category == 5:
price = random.randint(1, 500) / 10
else:
price = random.randint(1, 10000)/100
database_commands.insert_data(connection, cursor, "products", "id, name, price, description, count", (i+1, name, price, f"{descriptions1[which_description1]} {descriptions2[which_description2]}", count))
database_commands.insert_data(connection, cursor, "images", "image_path, product_id, image_id", (output_directory + name + '.png', i+1,1))
database_commands.disconnect_database(connection)