-
Notifications
You must be signed in to change notification settings - Fork 0
/
DatasetGenerator.py
159 lines (145 loc) · 5.65 KB
/
DatasetGenerator.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
import os
import sys
from io import BytesIO
import json
import requests
from time import sleep
from PIL import Image
import random
def progressBar(count, total, status=""):
barLength = 60
filledLength = int(round(barLength * count / float(total)))
percent = round(100.0 * count / float(total), 1)
bar = "=" * filledLength + "-" * (barLength - filledLength)
sys.stdout.write(f"\rloading progress : [{bar}] {percent}% {status} ")
sys.stdout.flush()
def generateNegativ():
imgs = [
Image.open("ImagesWithoutCards/" + imgPath)
for imgPath in os.listdir("ImagesWithoutCards")
]
counter = len(os.listdir("CardDetectionData/other/"))
for img in imgs:
width, height = img.size
for i in range(0, width - 146, int((width - 146) * 0.05)):
for j in range(0, height - 204, int((height - 204) * 0.075)):
temp = img.crop((i, j, i + 146, j + 204)).convert("L")
temp.save("CardDetectionData/other/" + str(counter) + ".jpg")
counter += 1
del temp
def pickRandom():
files = []
for folder in os.listdir("SetDetectionData"):
files += os.listdir("SetDetectionData/" + folder)
for i in range(0, 10001):
temp = files.pop(random.randint(0, len(files) - 1))
_filepath = "SetDetectionData/" + temp.split("-")[0] + "/"
temp = Image.open(_filepath + temp)
temp.save("CardDetectionData/card/" + str(i) + ".jpg")
del temp
def getData(save=True, filepath=""):
labelClasses = {
0: "grn",
1: "rna",
2: "war",
3: "m20",
4: "eld",
5: "thb",
6: "iko",
7: "m21",
}
_filepath = filepath
for i in range(8):
filepath = _filepath
if save:
filepath = _filepath + "/" + labelClasses[i]
if not os.path.exists(filepath):
os.mkdir(filepath)
print(i)
sleep(1.0)
try:
response = requests.get("https://api.scryfall.com/sets/" + labelClasses[i])
data = json.load(BytesIO(response.content))
cardCount = data["card_count"]
for j in range(1, cardCount + 1):
progressBar(j, cardCount, status=f"{labelClasses[i]}")
try:
response = requests.get(
f"https://api.scryfall.com/cards/{labelClasses[i]}/{j}"
)
cardData = json.load(BytesIO(response.content))
if "image_uris" in cardData.keys():
try:
response = requests.get(cardData["image_uris"]["small"])
_img = Image.open(BytesIO(response.content))
if save:
_img.save(filepath + f"/{labelClasses[i]}-{j}.jpg")
for k in range(0, 10):
_tempImg = _img.rotate(
random.uniform(-5, 5), expand=True
).resize((146, 204))
if save:
_tempImg.save(
filepath + f"/{labelClasses[i]}-{j}-{k}.jpg"
)
except Exception as e:
print(e)
print("fehler 3")
except Exception as e:
print("fehler 2")
print(e)
except Exception as e:
print("fehler 1")
print(e)
def setupFolder():
setLabels = {
0: "grn",
1: "rna",
2: "war",
3: "m20",
4: "eld",
5: "thb",
6: "iko",
7: "m21",
}
if not os.path.exists("SetDetectionData"):
os.mkdir("SetDetectionData")
if not os.path.exists("CardDetectionData"):
os.mkdir("CardDetectionData")
if not os.path.exists("NumberDetectionData"):
os.mkdir("NumberDetectionData")
NumberDetectionDataList = [i for i in range(1, 300)]
for subfolder in NumberDetectionDataList:
if not os.path.exists("NumberDetectionData" + "/" + str(subfolder)):
os.mkdir("NumberDetectionData" + "/" + str(subfolder))
if not os.path.exists("CardDetectionData" + "/" + "card"):
os.mkdir("CardDetectionData" + "/" + "card")
if not os.path.exists("CardDetectionData" + "/" + "other"):
os.mkdir("CardDetectionData" + "/" + "other")
for subfolder in setLabels.values():
if not os.path.exists("SetDetectionData" + "/" + subfolder):
os.mkdir("SetDetectionData" + "/" + subfolder)
# lade Bilddateien herunter
print("downloading images")
getData(filepath="SetDetectionData")
labelClasses = {}
filepath = "NumberDetectionData"
for folder in os.listdir("SetDetectionData"):
for cardImgName in os.listdir("SetDetectionData" + "/" + folder):
label = cardImgName.split(".")[0].split("-")[1]
if int(label) <= 299:
if label in labelClasses:
_img = Image.open(
"SetDetectionData" + "/" + folder + "/" + cardImgName
)
_img.save(filepath + "/" + str(label) + "/" + cardImgName)
else:
labelClasses[label] = label
_img = Image.open(
"SetDetectionData" + "/" + folder + "/" + cardImgName
)
_img.save(filepath + "/" + str(label) + "/" + cardImgName)
generateNegativ()
pickRandom()
if __name__ == "__main__":
setupFolder()