From f1b1e9e3809fc5d499c9c1aa1d4d6dbae01010a9 Mon Sep 17 00:00:00 2001 From: esimov Date: Fri, 9 Aug 2019 11:48:35 +0300 Subject: [PATCH] Resolved issues with multiple face detection --- examples/puploc/puploc.go | 17 ++++++++--------- examples/puploc/puploc.py | 30 +++++++++++++++--------------- 2 files changed, 23 insertions(+), 24 deletions(-) diff --git a/examples/puploc/puploc.go b/examples/puploc/puploc.go index fb424c2..6891d81 100644 --- a/examples/puploc/puploc.go +++ b/examples/puploc/puploc.go @@ -30,18 +30,17 @@ func FindFaces(pixels []uint8) uintptr { dets := make([][]int, len(results)) 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 puploc := &pigo.Puploc{ Row: results[i].Row - int(0.085*float32(results[i].Scale)), Col: results[i].Col - int(0.185*float32(results[i].Scale)), Scale: float32(results[i].Scale) * 0.4, - Perturbs: 100, + Perturbs: 50, } det := puplocClassifier.RunDetector(*puploc, *imageParams) 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 @@ -49,12 +48,12 @@ func FindFaces(pixels []uint8) uintptr { Row: results[i].Row - int(0.085*float32(results[i].Scale)), Col: results[i].Col + int(0.185*float32(results[i].Scale)), Scale: float32(results[i].Scale) * 0.4, - Perturbs: 100, + Perturbs: 50, } det = puplocClassifier.RunDetector(*puploc, *imageParams) 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. // 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. s := *(*[]uint8)(unsafe.Pointer(&coords)) @@ -93,7 +92,7 @@ func clusterDetection(pixels []uint8, rows, cols int) []pigo.Detection { Dim: cols, } cParams := pigo.CascadeParams{ - MinSize: 100, + MinSize: 60, MaxSize: 600, ShiftFactor: 0.1, ScaleFactor: 1.1, @@ -133,7 +132,7 @@ func clusterDetection(pixels []uint8, rows, cols int) []pigo.Detection { dets := faceClassifier.RunCascade(cParams, 0.0) // Calculate the intersection over union (IoU) of two clusters. - dets = faceClassifier.ClusterDetections(dets, 0.05) + dets = faceClassifier.ClusterDetections(dets, 0.0) return dets } diff --git a/examples/puploc/puploc.py b/examples/puploc/puploc.py index 099a3bd..e6cbf00 100644 --- a/examples/puploc/puploc.py +++ b/examples/puploc/puploc.py @@ -36,7 +36,7 @@ def process_frame(pixs): if data_pointer : 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. dets_len = res[0][0] @@ -44,7 +44,7 @@ def process_frame(pixs): # We have to consider the pupil pair added into the list. # 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 # initialize the camera @@ -69,21 +69,21 @@ while(True): dets = process_frame(pixs) # pixs needs to be numpy.uint8 array 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: - 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) key = cv2.waitKey(1)