Skip to content

Commit

Permalink
#96_missing_fg-script and docs for fg-script
Browse files Browse the repository at this point in the history
  • Loading branch information
bwstuff committed Feb 21, 2019
1 parent 154564b commit 53c0f9a
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 6 deletions.
94 changes: 94 additions & 0 deletions data_loader/fg-script.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import urllib.request
import os
import csv
import cv2 as cv
import random

face_cascade = cv.CascadeClassifier('haarcascade_frontalface_default.xml')


def createFolder(directory):
try:
if not os.path.exists(directory):
os.makedirs(directory)
except OSError:
print("Error: Creating directory.")

def createClassificationFolders(directory):
createFolder(directory)
for x in range(101):
createFolder(directory + "/" + str(x))

def load_images_from_folder(folder):
images = []
for subfolder in os.listdir(folder):
images.append(os.path.join(folder, subfolder))
return images

if not os.path.exists("../data/FGNET.zip"):
print("Start downloading Zipfile")
urllib.request.urlretrieve("http://yanweifu.github.io/FG_NET_data/FGNET.zip", "../data/FGNET.zip")
print("Finished downloading Zipfile")
else:
print("Zip already downloaded.")

if not os.path.exists("../data/FGNET"):
print("Start unzipping")
import zipfile
with zipfile.ZipFile("../data/FGNET.zip", 'r') as zip_ref:
zip_ref.extractall("../data/FGNET")
print("Finished unzipping")
else:
print("Already unzipped")


age = []
full_path = []
full_path2 = load_images_from_folder("../data/FGNET/FGNET/images")
delta = 5

for a in full_path2:
b = a.split('\\')
full_path.append(b[1])
b = b[1].split('.')

c = b[0].split('A')
d = c[1].strip('a')
d = d.strip('b')
age.append(d)

counter_training = 0
counter_valid = 0
counter_test = 0
print("Printing Images to Classification Folders")

for idx, val in enumerate(full_path):
img = cv.imread("../data/FGNET/FGNET/images/" + val)
faces = face_cascade.detectMultiScale(img, 1.8, 5)
for (x,y,w,h) in faces:
cip = img[y-delta:y+h+delta, x-delta:x+w+delta].copy()
try:
cip = cv.resize(cip, (224, 224))
i = random.random()
if i > 0.5:
counter_training = counter_training + 1
cv.imwrite("../data/LAP/Train/" + str(int(age[idx])) + "/" + val, cip)
print("Written: " + "../data/LAP/Train/" + str(int(age[idx])) + "/" + val)
elif i < 0.25:
counter_valid = counter_valid + 1
cv.imwrite("../data/LAP/Validation/" + str(int(age[idx])) + "/" + val, cip)
print("Written: " + "../data/LAP/Validation/" + str(int(age[idx])) + "/" + val)
else:
counter_test = counter_test + 1
cv.imwrite("../data/LAP/Test/" + str(int(age[idx])) + "/" + val, cip)
print("Written: " + "../data/LAP/Test/" + str(int(age[idx])) + "/" + val)
except:
print("Resize Fail")

f = open("../data/img_counts_fg.txt", "w+")
f.write("Train_img_count = " + str(counter_training) + "\n")
f.write("Validation_img_count = " + str(counter_valid) + "\n")
f.write("Test_img_count = " + str(counter_test) + "\n")
f.close()

print("FINISHED")
17 changes: 17 additions & 0 deletions docs/fg-net.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#The "*fg-script.py*" file will -

<pre><code>
1. look if a file exists at *"../data/FGNET.zip"* exists - do nothing; else - download dataset from
"*http://yanweifu.github.io/FG_NET_data/FGNET.zip"*

2. look if an extracted file at *"../data/data/FGNET"* exists - do nothing; else - extract downloaded dataset

3. have a function to create Classification folders in *"../data/LAP"* ranging from 0 to 100 (not used)

4. open images and creates an array of tupels with the corresponding age value which is extracted from
the image name

5. write the images into the fitting Classification folder

6. create a *"img_counts_fg.txt"* file with the amount of Training, Validiation and Test images
</code></pre>
15 changes: 9 additions & 6 deletions docs/lap_script.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
The lap-script.py file will -
#The "*lap-script.py*" file will -

<pre><code>
1. look if a file exists at *"../data/appa-real-release.zip"* exists - do nothing; else - download dataset
1. look if a file exists at *"../data/appa-real-release.zip"* exists - do nothing; else - download dataset from
*"http://158.109.8.102/AppaRealAge/appa-real-release.zip*"

2. look if an extracted file at *"../data/appa-real-release"* exists - do nothing; else - extract downloaded dataset

3. create Classification folders in *"../data/LAP"* ranging
from 0 to 100
3. create Classification folders in *"../data/LAP"* ranging from 0 to 100

4. open images and creates an array of tupels with the corresponding age value from a csv file

5. write the images into the fitting Classification folder
</code></pre>
5. write the images into the fitting Classification folder

6. create a *"img_counts.txt"* file with the amount of Training, Validiation and Test images
</code></pre>

0 comments on commit 53c0f9a

Please sign in to comment.