-
Notifications
You must be signed in to change notification settings - Fork 0
/
train-images.py
182 lines (137 loc) · 4.27 KB
/
train-images.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import cv2
import numpy as np
import time
import os
from threading import Thread
from ftplib import FTP
import face_recognition
import pickle
import numpy as np
global keepRunning
keepRunning=True
#image1 = cv2.imread("img1.jpg")[:,:,:3]
global all_face_encodings
all_face_encodings = {}
def encoding(id):
all_face_encodings[str(id)] = face_recognition.face_encodings(face_recognition.load_image_file("EnrolledImages/"+str(id)+".jpg"))[0]
class Montage(object):
def __init__(self,initial_image):
self.montage = initial_image
self.x,self.y = self.montage.shape[:2]
def append(self,image):
image = image[:,:,:3]
x,y = image.shape[0:2]
new_image = cv2.resize(image,(int(y*float(self.x)/x),self.x))
self.montage = np.hstack((self.montage,new_image))
def show(self,iName):
#cv2.imshow(iName,self.montage)
cv2.imwrite("EnrolledImages/"+iName+'.jpg',self.montage)
cv2.waitKey()
cv2.destroyAllWindows()
count=0
countEnc=1
url1 = "rtsp://admin:[email protected]/Streaming/Channels/1"
def get_jetson_gstreamer_source(uri, width, height, latency):
gst_str = ("rtspsrc location={} latency={} ! rtph264depay ! h264parse ! omxh264dec ! "
"nvvidconv ! video/x-raw, width=(int){}, height=(int){}, format=(string)BGRx ! "
"videoconvert ! appsink").format(uri, latency, width, height)
return cv2.VideoCapture(gst_str, cv2.CAP_GSTREAMER)
def getFrame(kepr):
while kepr:
ret,frame= cap.read()
cv2.imshow("run",frame)
cv2.waitKey(1)
return frame
cv2.destroyAllWindows()
cap = get_jetson_gstreamer_source(url1, 1920, 1080, 100)
th= Thread(target=getFrame,args=(keepRunning,))
th.start()
id = input('Enter the ID: ')
host = input('Enter the host IP Address: ')
username=input('Enter Username: ')
password = input('Enter Password: ')
print("Start capturing Images...")
while (count<=4):
print(count)
if(count==0):
print("Look strait to camere")
time.sleep(5)
cv2.imwrite(str(count)+'.jpg',getFrame(keepRunning))
count+=1
elif(count==1):
print("Look upside")
time.sleep(5)
cv2.imwrite(str(count)+'.jpg',getFrame(keepRunning))
count+=1
elif(count==2):
print("Look right")
time.sleep(5)
cv2.imwrite(str(count)+'.jpg',getFrame(keepRunning))
count+=1
elif(count==3):
print("Look left")
time.sleep(5)
cv2.imwrite(str(count)+'.jpg',getFrame(keepRunning))
count+=1
elif(count==4):
print("Look down")
time.sleep(5)
cv2.imwrite(str(count)+'.jpg',getFrame(keepRunning))
count+=1
keepRunning=False
image1 = cv2.imread("0.jpg")[:,:,:3]
image2 = cv2.imread("1.jpg")[:,:,:3]
image3 = cv2.imread("2.jpg")[:,:,:3]
image4 = cv2.imread("3.jpg")[:,:,:3]
image5 = cv2.imread("4.jpg")[:,:,:3]
m = Montage(image1)
m.append(image2)
m.append(image3)
m.append(image4)
m.append(image5)
os.remove("0.jpg")
os.remove("1.jpg")
os.remove("2.jpg")
os.remove("3.jpg")
os.remove("4.jpg")
m.show(id)
ftp = FTP(host=host)
login_status = ftp.login(user=username, passwd=password)
print(login_status)
ftp.cwd('EnrolledImages') #change directory
print(ftp.dir())
print("Image Capturing completed")
print("Start Face Training")
while (countEnc<=int(id)):
try:
fp = open(str(countEnc)+'.jpg', 'wb')
ftp.retrbinary('RETR ' + str(countEnc)+'.jpg', fp.write, 1024)
fp.close()
except:
print("Image is not available on server")
pass
try:
encoding(countEnc)
print("Face Training Completed "+str(countEnc/int(id)*100)+"%")
if (countEnc==int(id)):
fp = open('EnrolledImages/'+str(countEnc)+'.jpg', 'rb')
ftp.storbinary('STOR %s' % os.path.basename(str(countEnc)+'.jpg'), fp, 1024)
fp.close()
os.remove('EnrolledImages/'+id+'.jpg')
countEnc+=1
except:
countEnc+=1
with open('EnrolledImages/dataset_faces.dat', 'wb') as f:
pickle.dump(all_face_encodings, f)
with open('EnrolledImages/dataset_faces.dat', 'rb') as f:
all_face_encodings = pickle.load(f)
# Grab the list of names and the list of encodings
face_names = list(all_face_encodings.keys())
face_encodings = np.array(list(all_face_encodings.values()))
print (face_names)
fp = open('EnrolledImages/dataset_faces.dat', 'rb')
ftp.storbinary('STOR %s' % os.path.basename('dataset_faces.dat'), fp, 1024)
fp.close()
#os.remove('EnrolledImages/dataset_faces.dat')
print(ftp.dir())
print("Faces trained")