-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathface_identify.py
More file actions
35 lines (31 loc) · 999 Bytes
/
face_identify.py
File metadata and controls
35 lines (31 loc) · 999 Bytes
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
import numpy as np
import os
import cv2
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
cwd = os.getcwd()
clsf_path = cwd + "/haarcascade_frontalface_default.xml"
face_cascade = cv2.CascadeClassifier(clsf_path)
path = cwd + "/images"
flist = os.listdir(path)
valid_exts = [".jpg",".gif",".png",".tga", ".jpeg"]
count1=0
count2=0
for f in flist:
count1+=1
print(f,count1)
if os.path.splitext(f)[1].lower() not in valid_exts:
os.remove(path+"/"+f)
else:
fullpath = os.path.join(path, f)
img_bgr = cv2.imread(fullpath)
img_rgb = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_RGB2GRAY)
faces = face_cascade.detectMultiScale(img_gray)
#print(f+" "+str(len(faces)))
if len(faces)==1:
count2+=1
pass
else:
os.remove(path+"/"+f)
print("Kept: ",count2," Deleted: ",count1-count2)