-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #85 from Paeti/enh/prepare_dataset_28
Enh/prepare dataset 28
- Loading branch information
Showing
27 changed files
with
534,891 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -102,3 +102,5 @@ venv.bak/ | |
|
||
# mypy | ||
.mypy_cache/ | ||
mains/.idea | ||
preprocessing/.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import os, time | ||
import numpy as np | ||
import cv2 as cv | ||
|
||
face_cascade = cv.CascadeClassifier('haarcascade_frontalface_default.xml') | ||
delta = 5 | ||
|
||
#change directories here | ||
path_to_watch = "tmp" | ||
output_dir = "Output/" | ||
|
||
before = dict ([(f, None) for f in os.listdir (path_to_watch)]) | ||
while 1: | ||
time.sleep (10) | ||
print("Alive") | ||
after = dict ([(f, None) for f in os.listdir (path_to_watch)]) | ||
added = [f for f in after if not f in before] | ||
removed = [f for f in before if not f in after] | ||
if added: | ||
counter = 0 | ||
for element in added: | ||
fp = path_to_watch + "/" + element | ||
cur_pad = os.path.normpath(fp) | ||
img = cv.imread(fp) | ||
faces = face_cascade.detectMultiScale(img, 1.8, 5) | ||
for (x, y, w, h) in faces: | ||
counter += 1 | ||
roi_color = img[y:y + h, x:x + w] | ||
cip = img[y - delta:y + h + delta, x - delta:x + w + delta].copy() | ||
|
||
if counter > 0: | ||
facepath = output_dir + added[0] + str(counter) + ".jpg" | ||
else: | ||
facepath = output_dir + added[0] | ||
cur_path = os.path.normpath(facepath) | ||
print("Created: " + cur_path) | ||
try: | ||
|
||
cip = cv.resize(cip, (224, 224)) | ||
b, g, r = cv.split(cip) | ||
b = b / 3 | ||
g = g / 3 | ||
r = r / 3 | ||
meanRGB = cv.merge((b, g, r)) | ||
cip = cip - meanRGB | ||
cv.imwrite(cur_path, cip) | ||
print("Written: " + cur_path) | ||
except: | ||
print("Resizing failed! !ssize.empty()") | ||
if os.path.exists(cur_pad): | ||
os.remove(cur_pad) | ||
|
||
else: | ||
print("File not found") | ||
if removed: | ||
print("Removed: ", ", ".join (removed)) | ||
before = after |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import numpy as np | ||
import cv2 as cv | ||
import os | ||
from skimage.io import imread_collection | ||
|
||
face_cascade = cv.CascadeClassifier('haarcascade_frontalface_default.xml') | ||
eye_cascade = cv.CascadeClassifier('haarcascade_eye.xml') | ||
|
||
delta = 5 | ||
counter = 0 | ||
|
||
#returns an array of all images in a given folder and its subfolders | ||
def load_images_from_folder(folder): | ||
images = [] | ||
t = () | ||
for subfolder in os.listdir(folder): | ||
test_img = cv.imread(os.path.join(folder, subfolder)) | ||
print(os.path.join(folder, subfolder)) | ||
if test_img is not None: | ||
images.append(subfolder, test_img) | ||
else: | ||
for filename in os.listdir(os.path.join(folder, subfolder)): | ||
print(os.path.join(folder, subfolder, filename)) | ||
imgs = cv.imread(os.path.join(folder, subfolder, filename)) | ||
if imgs is not None: | ||
images.append(filename, imgs) | ||
return images | ||
|
||
# folder where to search for pictures - no non folder/picture files or error | ||
fp = "Inputfiles/imdb_crop" | ||
cur_pad = os.path.normpath(fp) | ||
image_tuples = load_images_from_folder(cur_pad) | ||
|
||
for img_tuple in image_tuples: | ||
counter = 0 | ||
img_name = img_tuple[0] | ||
img = img_tuple[1] | ||
|
||
faces = face_cascade.detectMultiScale(img, 1.8, 5) | ||
for (x,y,w,h) in faces: | ||
counter += 1 | ||
roi_color = img[y:y+h, x:x+w] | ||
cip = img[y-delta:y+h+delta, x-delta:x+w+delta].copy() | ||
|
||
if counter > 0: | ||
facepath = "Outputfiles/" + img_name + str(counter) + ".jpg" | ||
else: | ||
facepath = "Outputfiles/" + img_name | ||
cur_path = os.path.normpath(facepath) | ||
print(cur_path) | ||
try: | ||
|
||
cip = cv.resize(cip, (224,224)) | ||
b, g, r = cv.split(cip) | ||
b = b / 3 | ||
g = g / 3 | ||
r = r / 3 | ||
meanRGB = cv.merge((b, g, r)) | ||
cip = cip - meanRGB | ||
cv.imwrite(cur_path, cip) | ||
except: | ||
print("Resizing failed! !ssize.empty()") | ||
|
||
cv.waitKey(0) | ||
cv.destroyAllWindows() |
Oops, something went wrong.