-
Notifications
You must be signed in to change notification settings - Fork 6
/
train.py
65 lines (51 loc) · 2.03 KB
/
train.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
from src import *
import argparse
def main():
parser = argparse.ArgumentParser(description='options for training, one by one each pet')
parser.add_argument(
'-f', '--folder', required=True, type=str, help='path to the folder image where you have the pet you want to recognize')
parser.add_argument(
'-n', '--name', required=True, type=str, help='pet\'s name')
parser.add_argument(
'-m', '--model', required=False, type=str, help='path to previous model')
args = parser.parse_args()
yolob = Yolobody()
yoloe = Yoloeye()
yolof = Yoloface()
haar = HaarCascadeExt()
face_database = Model(args.model)
try:
with open('match.json','r') as fp: match = json.load(fp)
except:
match = {}
next_idx = len(match)
succ = 0
specimen = []
for idx,im_path in enumerate(os.listdir(args.folder),start=0):
im = cv2.imread(im_path)
ret = yolob.detect(im)
if not len(ret): print('We do not detect neither dog or cat in the {} image'.format(idx)) ; continue
if len(ret) > 1: print('We detected more than one dog or cat in the {} image'.format(idx)) ; continue
x,y,w,h = ret[0][2]
X,Y,W,H = cent2rect(x,y,w,h)
croped = im[Y:Y+H,X:X+W]
ret2 = yolof.detect(croped)
if not len(ret2): print('We do not detect neither dog or cat face in the {} image'.format(idx)) ; continue
if len(ret2) > 1: print('We detected more than one dog or cat face in the {} image'.format(idx)) ; continue
x,y,w,h = ret2[0][2]
X,Y,W,H = cent2rect(x,y,w,h)
closeup = croped[Y:Y+H,X:X+W]
ret3 = haar.detect(closeup,(0,0))[0]
if not (len(ret3)): print('We do not detect neither dog or cat face in the {} image'.format(idx)) ; continue
#if len(ret3) > 1: print('We detected more than one dog or cat face in the {} image'.format(idx)) ; continue
nX,nY,nW,nH = ret3
specimen.append(closeup[nY:nY+nH,nX:nX+nW])
succ += 1
print('Only {} images were taken to train'.format(succ))
if succ:
face_database.training(specimen,next_idx)
match[next_idx] = args.name
with open('match.json', 'w') as fp:
json.dump(match, fp)
if __name__ == '__main__':
main()