-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHLEngine_AdvancedImageProcessing.py
More file actions
134 lines (118 loc) · 4.26 KB
/
HLEngine_AdvancedImageProcessing.py
File metadata and controls
134 lines (118 loc) · 4.26 KB
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
#author:Akhil P Jacob
#HLDynamic-Integrations
import os
import cv2
import numpy as np
from PIL import Image
recognizer=cv2.face.LBPHFaceRecognizer_create()
path='dataset'
def collectDataSet(filterName,Cam,TargetID):
faceDetect = cv2.CascadeClassifier(filterName)
cam = cv2.VideoCapture(Cam)
id =TargetID
sampleNum = 0
while (True):
ret, img = cam.read();
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = faceDetect.detectMultiScale(gray, 1.3, 5);
for (x, y, w, h) in faces:
sampleNum += 1
print(sampleNum)
cv2.imwrite("dataset/pythface." + str(id) + "." + str(sampleNum) + ".jpg", gray[y:y + h, x:x + w])
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.waitKey(100)
cv2.imshow('Dataset', img)
cv2.waitKey(1)
if (sampleNum > 100):
break
cam.release()
cv2.destroyAllWindows()
def trainDataSet():
def getImageWithID(path):
imagePaths = [os.path.join(path, f) for f in os.listdir(path)]
faces = []
IDs = []
for imagePath in imagePaths:
faceImg = Image.open(imagePath).convert('L')
faceNp = np.array(faceImg, 'uint8')
ID = int(os.path.split(imagePath)[-1].split('.')[1])
faces.append(faceNp)
IDs.append(ID)
cv2.imshow('training', faceNp)
cv2.waitKey(10)
return np.array(IDs), faces
Ids, faces = getImageWithID(path)
recognizer.train(faces, np.array(Ids))
recognizer.save('recognizer/trainingdata.yml')
cv2.destroyAllWindows()
def lockTarget_IP(filterName,ip,user1,user2,user3,user4,user5):
faceDetect = cv2.CascadeClassifier(filterName)
#camera="http://192.168.1.202:8080/video"
cam = cv2.VideoCapture(ip)
rec = cv2.face.LBPHFaceRecognizer_create();
rec.read('recognizer/trainingdata.yml')
# id=0
# font=cv2.cv.InitFont(cv2.cv.CV_FONT_HERSHEY_COMPLEX_SMALL,5,1,0,4)
font = cv2.FONT_HERSHEY_SIMPLEX
while (True):
ret, img = cam.read();
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = faceDetect.detectMultiScale(gray, 1.3, 5);
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
id, conf = rec.predict(gray[y:y + h, x:x + w])
# if(<50):
if (id == 1):
id = user1
elif (id == 2):
id = user2
elif (id == 3):
id = user3
elif (id == 4):
id = user4
elif (id == 5):
id = user5
else:
id = 'unknown'
# cv2.cv.putText(cv2.cv.fromarray(img),str(id),(x,y+h),font,255)
cv2.putText(img, str(id), (x, y + h), font, 2, (255, 0, 0), 3);
cv2.imshow('face', img)
if (cv2.waitKey(1) == ord('q')):
break;
cam.release()
cv2.destroyAllWindows()
def lockTarget_Camera(filterName,camera,user1,user2,user3,user4,user5):
faceDetect = cv2.CascadeClassifier(filterName)
cam = cv2.VideoCapture(camera)
rec = cv2.face.LBPHFaceRecognizer_create();
rec.read('recognizer/trainingdata.yml')
# id=0
# font=cv2.cv.InitFont(cv2.cv.CV_FONT_HERSHEY_COMPLEX_SMALL,5,1,0,4)
font = cv2.FONT_HERSHEY_SIMPLEX
while (True):
ret, img = cam.read();
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = faceDetect.detectMultiScale(gray, 1.3, 5);
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
id, conf = rec.predict(gray[y:y + h, x:x + w])
# if(<50):
if (id == 1):
id = user1
elif (id == 2):
id = user2
elif (id == 3):
id = user3
elif (id == 4):
id = user4
elif (id == 5):
id = user5
else:
id = 'unknown'
# cv2.cv.putText(cv2.cv.fromarray(img),str(id),(x,y+h),font,255)
cv2.putText(img, str(id), (x, y + h), font, 2, (255, 0, 0), 3);
cv2.imshow('face', img)
if (cv2.waitKey(1) == ord('q')):
break;
cam.release()
cv2.destroyAllWindows()