Resolved issues with multiple face detection

This commit is contained in:
esimov
2019-08-09 11:48:35 +03:00
parent adad6e2c06
commit f1b1e9e380
2 changed files with 23 additions and 24 deletions

View File

@@ -30,18 +30,17 @@ func FindFaces(pixels []uint8) uintptr {
dets := make([][]int, len(results)) dets := make([][]int, len(results))
for i := 0; i < len(results); i++ { for i := 0; i < len(results); i++ {
dets[i] = append(dets[i], results[i].Row, results[i].Col, results[i].Scale, int(results[i].Q)) dets[i] = append(dets[i], results[i].Row, results[i].Col, results[i].Scale, int(results[i].Q), 1)
// left eye // left eye
puploc := &pigo.Puploc{ puploc := &pigo.Puploc{
Row: results[i].Row - int(0.085*float32(results[i].Scale)), Row: results[i].Row - int(0.085*float32(results[i].Scale)),
Col: results[i].Col - int(0.185*float32(results[i].Scale)), Col: results[i].Col - int(0.185*float32(results[i].Scale)),
Scale: float32(results[i].Scale) * 0.4, Scale: float32(results[i].Scale) * 0.4,
Perturbs: 100, Perturbs: 50,
} }
det := puplocClassifier.RunDetector(*puploc, *imageParams) det := puplocClassifier.RunDetector(*puploc, *imageParams)
if det.Row > 0 && det.Col > 0 { if det.Row > 0 && det.Col > 0 {
dets[i] = append(dets[i], det.Row, det.Col, int(det.Scale), int(results[i].Q)) dets[i] = append(dets[i], det.Row, det.Col, int(det.Scale), int(results[i].Q), 0)
} }
// right eye // right eye
@@ -49,12 +48,12 @@ func FindFaces(pixels []uint8) uintptr {
Row: results[i].Row - int(0.085*float32(results[i].Scale)), Row: results[i].Row - int(0.085*float32(results[i].Scale)),
Col: results[i].Col + int(0.185*float32(results[i].Scale)), Col: results[i].Col + int(0.185*float32(results[i].Scale)),
Scale: float32(results[i].Scale) * 0.4, Scale: float32(results[i].Scale) * 0.4,
Perturbs: 100, Perturbs: 50,
} }
det = puplocClassifier.RunDetector(*puploc, *imageParams) det = puplocClassifier.RunDetector(*puploc, *imageParams)
if det.Row > 0 && det.Col > 0 { if det.Row > 0 && det.Col > 0 {
dets[i] = append(dets[i], det.Row, det.Col, int(det.Scale), int(results[i].Q)) dets[i] = append(dets[i], det.Row, det.Col, int(det.Scale), int(results[i].Q), 0)
} }
} }
@@ -68,7 +67,7 @@ func FindFaces(pixels []uint8) uintptr {
// Include as a first slice element the number of detected faces. // Include as a first slice element the number of detected faces.
// We need to transfer this value in order to define the Python array buffer length. // We need to transfer this value in order to define the Python array buffer length.
coords = append([]int{len(dets), 0, 0, 0}, coords...) coords = append([]int{len(dets), 0, 0, 0, 0}, coords...)
// Convert the slice into an array pointer. // Convert the slice into an array pointer.
s := *(*[]uint8)(unsafe.Pointer(&coords)) s := *(*[]uint8)(unsafe.Pointer(&coords))
@@ -93,7 +92,7 @@ func clusterDetection(pixels []uint8, rows, cols int) []pigo.Detection {
Dim: cols, Dim: cols,
} }
cParams := pigo.CascadeParams{ cParams := pigo.CascadeParams{
MinSize: 100, MinSize: 60,
MaxSize: 600, MaxSize: 600,
ShiftFactor: 0.1, ShiftFactor: 0.1,
ScaleFactor: 1.1, ScaleFactor: 1.1,
@@ -133,7 +132,7 @@ func clusterDetection(pixels []uint8, rows, cols int) []pigo.Detection {
dets := faceClassifier.RunCascade(cParams, 0.0) dets := faceClassifier.RunCascade(cParams, 0.0)
// Calculate the intersection over union (IoU) of two clusters. // Calculate the intersection over union (IoU) of two clusters.
dets = faceClassifier.ClusterDetections(dets, 0.05) dets = faceClassifier.ClusterDetections(dets, 0.0)
return dets return dets
} }

View File

@@ -36,7 +36,7 @@ def process_frame(pixs):
if data_pointer : if data_pointer :
buffarr = ((c_longlong * ARRAY_DIM) * MAX_NDETS).from_address(addressof(data_pointer.contents)) buffarr = ((c_longlong * ARRAY_DIM) * MAX_NDETS).from_address(addressof(data_pointer.contents))
res = np.ndarray(buffer=buffarr, dtype=c_longlong, shape=(MAX_NDETS, 4,)) res = np.ndarray(buffer=buffarr, dtype=c_longlong, shape=(MAX_NDETS, 5,))
# The first value of the buffer aray represents the buffer length. # The first value of the buffer aray represents the buffer length.
dets_len = res[0][0] dets_len = res[0][0]
@@ -44,7 +44,7 @@ def process_frame(pixs):
# We have to consider the pupil pair added into the list. # We have to consider the pupil pair added into the list.
# That's why we are multiplying the detection length with 3. # That's why we are multiplying the detection length with 3.
dets = list(res.reshape(-1, 4))[0:dets_len*3] dets = list(res.reshape(-1, 5))[0:dets_len*3]
return dets return dets
# initialize the camera # initialize the camera
@@ -69,21 +69,21 @@ while(True):
dets = process_frame(pixs) # pixs needs to be numpy.uint8 array dets = process_frame(pixs) # pixs needs to be numpy.uint8 array
if dets is not None: if dets is not None:
for det in dets[0:1]: # We know that the detected faces are taking place in the first positions of the multidimensional array.
for det in dets:
if det[3] > 50: if det[3] > 50:
cv2.circle(frame, (int(det[1]), int(det[0])), int(det[2]/2.0), (0, 0, 255), 2) if det[4] == 1: # 1 == face; 0 == pupil
cv2.circle(frame, (int(det[1]), int(det[0])), int(det[2]/2.0), (0, 0, 255), 2)
else:
if showPupil:
cv2.circle(frame, (int(det[1]), int(det[0])), 4, (0, 0, 255), -1, 8, 0)
if showEyes:
cv2.rectangle(frame,
(int(det[1])-int(det[2]), int(det[0])-int(det[2])),
(int(det[1])+int(det[2]), int(det[0])+int(det[2])),
(0, 255, 0), 2
)
for det in dets[1:]:
if det[3] > 50:
if showPupil:
cv2.circle(frame, (int(det[1]), int(det[0])), 4, (0, 0, 255), -1, 8, 0)
if showEyes:
cv2.rectangle(frame,
(int(det[1])-int(det[2]), int(det[0])-int(det[2])),
(int(det[1])+int(det[2]), int(det[0])+int(det[2])),
(0, 255, 0), 2
)
cv2.imshow('', frame) cv2.imshow('', frame)
key = cv2.waitKey(1) key = cv2.waitKey(1)