Skip to content

Commit

Permalink
Merge pull request #85 from Paeti/enh/prepare_dataset_28
Browse files Browse the repository at this point in the history
Enh/prepare dataset 28
  • Loading branch information
DZvO authored Dec 17, 2018
2 parents d0fb910 + 2369750 commit 63625e0
Show file tree
Hide file tree
Showing 27 changed files with 534,891 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,5 @@ venv.bak/

# mypy
.mypy_cache/
mains/.idea
preprocessing/.idea
57 changes: 57 additions & 0 deletions mains/check_new_image.py
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
65 changes: 65 additions & 0 deletions mains/extract_faces.py
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()
Loading

0 comments on commit 63625e0

Please sign in to comment.