Skip to content

Commit f1b1e9e

Browse files
committed
Resolved issues with multiple face detection
1 parent adad6e2 commit f1b1e9e

File tree

2 files changed

+23
-24
lines changed

2 files changed

+23
-24
lines changed

examples/puploc/puploc.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,31 +30,30 @@ func FindFaces(pixels []uint8) uintptr {
3030
dets := make([][]int, len(results))
3131

3232
for i := 0; i < len(results); i++ {
33-
dets[i] = append(dets[i], results[i].Row, results[i].Col, results[i].Scale, int(results[i].Q))
34-
33+
dets[i] = append(dets[i], results[i].Row, results[i].Col, results[i].Scale, int(results[i].Q), 1)
3534
// left eye
3635
puploc := &pigo.Puploc{
3736
Row: results[i].Row - int(0.085*float32(results[i].Scale)),
3837
Col: results[i].Col - int(0.185*float32(results[i].Scale)),
3938
Scale: float32(results[i].Scale) * 0.4,
40-
Perturbs: 100,
39+
Perturbs: 50,
4140
}
4241
det := puplocClassifier.RunDetector(*puploc, *imageParams)
4342
if det.Row > 0 && det.Col > 0 {
44-
dets[i] = append(dets[i], det.Row, det.Col, int(det.Scale), int(results[i].Q))
43+
dets[i] = append(dets[i], det.Row, det.Col, int(det.Scale), int(results[i].Q), 0)
4544
}
4645

4746
// right eye
4847
puploc = &pigo.Puploc{
4948
Row: results[i].Row - int(0.085*float32(results[i].Scale)),
5049
Col: results[i].Col + int(0.185*float32(results[i].Scale)),
5150
Scale: float32(results[i].Scale) * 0.4,
52-
Perturbs: 100,
51+
Perturbs: 50,
5352
}
5453

5554
det = puplocClassifier.RunDetector(*puploc, *imageParams)
5655
if det.Row > 0 && det.Col > 0 {
57-
dets[i] = append(dets[i], det.Row, det.Col, int(det.Scale), int(results[i].Q))
56+
dets[i] = append(dets[i], det.Row, det.Col, int(det.Scale), int(results[i].Q), 0)
5857
}
5958
}
6059

@@ -68,7 +67,7 @@ func FindFaces(pixels []uint8) uintptr {
6867

6968
// Include as a first slice element the number of detected faces.
7069
// We need to transfer this value in order to define the Python array buffer length.
71-
coords = append([]int{len(dets), 0, 0, 0}, coords...)
70+
coords = append([]int{len(dets), 0, 0, 0, 0}, coords...)
7271

7372
// Convert the slice into an array pointer.
7473
s := *(*[]uint8)(unsafe.Pointer(&coords))
@@ -93,7 +92,7 @@ func clusterDetection(pixels []uint8, rows, cols int) []pigo.Detection {
9392
Dim: cols,
9493
}
9594
cParams := pigo.CascadeParams{
96-
MinSize: 100,
95+
MinSize: 60,
9796
MaxSize: 600,
9897
ShiftFactor: 0.1,
9998
ScaleFactor: 1.1,
@@ -133,7 +132,7 @@ func clusterDetection(pixels []uint8, rows, cols int) []pigo.Detection {
133132
dets := faceClassifier.RunCascade(cParams, 0.0)
134133

135134
// Calculate the intersection over union (IoU) of two clusters.
136-
dets = faceClassifier.ClusterDetections(dets, 0.05)
135+
dets = faceClassifier.ClusterDetections(dets, 0.0)
137136

138137
return dets
139138
}

examples/puploc/puploc.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ def process_frame(pixs):
3636

3737
if data_pointer :
3838
buffarr = ((c_longlong * ARRAY_DIM) * MAX_NDETS).from_address(addressof(data_pointer.contents))
39-
res = np.ndarray(buffer=buffarr, dtype=c_longlong, shape=(MAX_NDETS, 4,))
39+
res = np.ndarray(buffer=buffarr, dtype=c_longlong, shape=(MAX_NDETS, 5,))
4040

4141
# The first value of the buffer aray represents the buffer length.
4242
dets_len = res[0][0]
4343
res = np.delete(res, 0, 0) # delete the first element from the array
4444

4545
# We have to consider the pupil pair added into the list.
4646
# That's why we are multiplying the detection length with 3.
47-
dets = list(res.reshape(-1, 4))[0:dets_len*3]
47+
dets = list(res.reshape(-1, 5))[0:dets_len*3]
4848
return dets
4949

5050
# initialize the camera
@@ -69,21 +69,21 @@ def process_frame(pixs):
6969
dets = process_frame(pixs) # pixs needs to be numpy.uint8 array
7070

7171
if dets is not None:
72-
for det in dets[0:1]:
72+
# We know that the detected faces are taking place in the first positions of the multidimensional array.
73+
for det in dets:
7374
if det[3] > 50:
74-
cv2.circle(frame, (int(det[1]), int(det[0])), int(det[2]/2.0), (0, 0, 255), 2)
75+
if det[4] == 1: # 1 == face; 0 == pupil
76+
cv2.circle(frame, (int(det[1]), int(det[0])), int(det[2]/2.0), (0, 0, 255), 2)
77+
else:
78+
if showPupil:
79+
cv2.circle(frame, (int(det[1]), int(det[0])), 4, (0, 0, 255), -1, 8, 0)
80+
if showEyes:
81+
cv2.rectangle(frame,
82+
(int(det[1])-int(det[2]), int(det[0])-int(det[2])),
83+
(int(det[1])+int(det[2]), int(det[0])+int(det[2])),
84+
(0, 255, 0), 2
85+
)
7586

76-
for det in dets[1:]:
77-
if det[3] > 50:
78-
if showPupil:
79-
cv2.circle(frame, (int(det[1]), int(det[0])), 4, (0, 0, 255), -1, 8, 0)
80-
if showEyes:
81-
cv2.rectangle(frame,
82-
(int(det[1])-int(det[2]), int(det[0])-int(det[2])),
83-
(int(det[1])+int(det[2]), int(det[0])+int(det[2])),
84-
(0, 255, 0), 2
85-
)
86-
8787
cv2.imshow('', frame)
8888

8989
key = cv2.waitKey(1)

0 commit comments

Comments
 (0)