Mar. 14. 2018

Dependencies:   GraphicsFramework GR-PEACH_video LCD_shield_config AsciiFont R_BSP USBHost_custom

Revision:
5:49a61433290a
Parent:
3:0760680f06d8
Child:
6:8c0c70710090
--- a/recognition_proc/recognition_proc.cpp	Tue Aug 08 04:50:44 2017 +0000
+++ b/recognition_proc/recognition_proc.cpp	Tue Sep 05 10:01:51 2017 +0000
@@ -5,6 +5,7 @@
 #include "USBHostSerial.h"
 #include "LCD_shield_config_4_3inch.h"
 #include "recognition_proc.h"
+#include "STBWrap.h"
 
 #define UART_SETTING_TIMEOUT              1000            /* HVC setting command signal timeout period */
 #define UART_REGIST_EXECUTE_TIMEOUT       7000            /* HVC registration command signal timeout period */
@@ -15,6 +16,16 @@
 
 #define ERROR_02                         "Error: Number of detected faces is 2 or more"
 
+#define STB_RETRYCOUNT_DEFAULT              25            /* Retry Count for STB */
+#define STB_POSSTEADINESS_DEFAULT           30            /* Position Steadiness for STB */
+#define STB_SIZESTEADINESS_DEFAULT          30            /* Size Steadiness for STB */
+#define STB_PE_FRAME_DEFAULT                10            /* Complete Frame Count for property estimation in STB */
+#define STB_PE_ANGLEUDMIN_DEFAULT          -15            /* Up/Down face angle minimum value for property estimation in STB */
+#define STB_PE_ANGLEUDMAX_DEFAULT           20            /* Up/Down face angle maximum value for property estimation in STB */
+#define STB_PE_ANGLELRMIN_DEFAULT          -20            /* Left/Right face angle minimum value for property estimation in STB */
+#define STB_PE_ANGLELRMAX_DEFAULT           20            /* Left/Right face angle maximum value for property estimation in STB */
+#define STB_PE_THRESHOLD_DEFAULT           300            /* Threshold for property estimation in STB */
+
 #define DISP_PIXEL_WIDTH                 (320)
 #define DISP_PIXEL_HEIGHT                (240)
 
@@ -272,6 +283,13 @@
     UINT8 status;
     HVC_VERSION version;
     HVC_RESULT *pHVCResult = NULL;
+
+    int nSTBFaceCount;
+    STB_FACE *pSTBFaceResult;
+    int nSTBBodyCount;
+    STB_BODY *pSTBBodyResult;
+    int nIndex;
+
     HVC_IMAGE *pImage = NULL;
     INT32 execFlag;
     INT32 imageNo;
@@ -282,6 +300,7 @@
     uint32_t i;
     char Str_disp[32];
     Timer resp_time;
+    INT32 TrackingID;
 
     /* Register the button */
     button.fall(&button_fall);
@@ -298,6 +317,13 @@
         mbed_die();
     }
 
+    /* STB Initialize */
+    ret = STB_Init(STB_FUNC_BD | STB_FUNC_DT | STB_FUNC_PT | STB_FUNC_AG | STB_FUNC_GN);
+    if (ret != 0) {
+        printf("STB_Init Error : %d\n", ret);
+        mbed_die();
+    }
+
     /* Image Structure allocation */
     pImage = (HVC_IMAGE *)malloc(sizeof(HVC_IMAGE));
     if (pImage == NULL) {
@@ -364,6 +390,17 @@
                     if ((ret != 0) || (status != 0)) {
                         break;
                     }
+
+                    /* Set STB Parameters */
+                    ret = STB_SetTrParam(STB_RETRYCOUNT_DEFAULT, STB_POSSTEADINESS_DEFAULT, STB_SIZESTEADINESS_DEFAULT);
+                    if (ret != 0) {
+                        break;
+                    }
+                    ret = STB_SetPeParam(STB_PE_THRESHOLD_DEFAULT, STB_PE_ANGLEUDMIN_DEFAULT, STB_PE_ANGLEUDMAX_DEFAULT,
+                                         STB_PE_ANGLELRMIN_DEFAULT, STB_PE_ANGLELRMAX_DEFAULT, STB_PE_FRAME_DEFAULT);
+                    if (ret != 0) {
+                        break;
+                    }
                 }
 
                 /* Execute Registration */
@@ -407,11 +444,52 @@
                 if ((execFlag & HVC_ACTIV_FACE_DETECTION) == 0) {
                     execFlag &= ~(HVC_ACTIV_AGE_ESTIMATION | HVC_ACTIV_GENDER_ESTIMATION | HVC_ACTIV_EXPRESSION_ESTIMATION);
                 }
+                if (execFlag != 0) { // for STB
+                    execFlag |= HVC_ACTIV_FACE_DIRECTION;
+                }
                 imageNo = imageNo_setting;
                 resp_time.reset();
                 resp_time.start();
                 ret = HVC_ExecuteEx(UART_EXECUTE_TIMEOUT, execFlag, imageNo, pHVCResult, &status);
                 resp_time.stop();
+
+                /* STB */
+                if (STB_Exec(pHVCResult->executedFunc, pHVCResult, &nSTBFaceCount, &pSTBFaceResult, &nSTBBodyCount, &pSTBBodyResult) == 0) {
+                    for ( i = 0; i < nSTBBodyCount; i++ )
+                    {
+                        if ( pHVCResult->bdResult.num <= i ) break;
+
+                        nIndex = pSTBBodyResult[i].nDetectID;
+                        pHVCResult->bdResult.bdResult[nIndex].posX = (short)pSTBBodyResult[i].center.x;
+                        pHVCResult->bdResult.bdResult[nIndex].posY = (short)pSTBBodyResult[i].center.y;
+                        pHVCResult->bdResult.bdResult[nIndex].size = pSTBBodyResult[i].nSize;
+                    }
+                    for (i = 0; i < nSTBFaceCount; i++)
+                    {
+                        if (pHVCResult->fdResult.num <= i) break;
+
+                        nIndex = pSTBFaceResult[i].nDetectID;
+                        pHVCResult->fdResult.fcResult[nIndex].dtResult.posX = (short)pSTBFaceResult[i].center.x;
+                        pHVCResult->fdResult.fcResult[nIndex].dtResult.posY = (short)pSTBFaceResult[i].center.y;
+                        pHVCResult->fdResult.fcResult[nIndex].dtResult.size = pSTBFaceResult[i].nSize;
+
+                        if (pHVCResult->executedFunc & HVC_ACTIV_AGE_ESTIMATION) {
+                            pHVCResult->fdResult.fcResult[nIndex].ageResult.confidence += 10000; // During
+                            if (pSTBFaceResult[i].age.status >= STB_STATUS_COMPLETE) {
+                                pHVCResult->fdResult.fcResult[nIndex].ageResult.age = pSTBFaceResult[i].age.value;
+                                pHVCResult->fdResult.fcResult[nIndex].ageResult.confidence += 10000; // Complete
+                            }
+                        }
+                        if (pHVCResult->executedFunc & HVC_ACTIV_GENDER_ESTIMATION) {
+                            pHVCResult->fdResult.fcResult[nIndex].genderResult.confidence += 10000; // During
+                            if (pSTBFaceResult[i].gender.status >= STB_STATUS_COMPLETE) {
+                                pHVCResult->fdResult.fcResult[nIndex].genderResult.gender = pSTBFaceResult[i].gender.value;
+                                pHVCResult->fdResult.fcResult[nIndex].genderResult.confidence += 10000; // Complete
+                            }
+                        }
+                    }
+                }
+
                 if ((ret == 0) && (status == 0)) {
                     if (imageNo == HVC_EXECUTE_IMAGE_QVGA_HALF) {
                         DrawImage(0, 0, pHVCResult->image.width, pHVCResult->image.height, pHVCResult->image.image, 2);
@@ -452,34 +530,53 @@
                                            pHVCResult->fdResult.fcResult[i].dtResult.size,
                                            detection_colour);
                             }
+                            TrackingID = 0;
+                            for (int j = 0; j < nSTBFaceCount; j++)
+                            {
+                                if (pSTBFaceResult[j].nDetectID == i) {
+                                    TrackingID = pSTBFaceResult[j].nTrackingID;
+                                    break;
+                                }
+                            }
+                            memset(Str_disp, 0, sizeof(Str_disp));
                             if (pHVCResult->executedFunc & HVC_ACTIV_FACE_RECOGNITION) {
                                 /* Recognition */
-                                if (-128 == pHVCResult->fdResult.fcResult[i].recognitionResult.uid) {
-                                    DrawString("Not possible", 0x0000f0ff);
-                                } else if (pHVCResult->fdResult.fcResult[i].recognitionResult.uid < 0) {
-                                    DrawString("Not registered", 0x0000f0ff);
+                                if (pHVCResult->fdResult.fcResult[i].recognitionResult.uid < 0) {
+                                    sprintf(Str_disp, "ID:%d", TrackingID);
                                 } else {
-                                    memset(Str_disp, 0, sizeof(Str_disp));
                                     sprintf(Str_disp, "USER%03d", pHVCResult->fdResult.fcResult[i].recognitionResult.uid + 1);
-                                    DrawString(Str_disp, 0x0000f0ff);
                                 }
+                            } else {
+                                sprintf(Str_disp, "ID:%d", TrackingID);
                             }
+                            DrawString(Str_disp, 0x0000f0ff);
                             if (pHVCResult->executedFunc & HVC_ACTIV_AGE_ESTIMATION) {
                                 /* Age */
                                 if (-128 != pHVCResult->fdResult.fcResult[i].ageResult.age) {
                                     memset(Str_disp, 0, sizeof(Str_disp));
                                     sprintf(Str_disp, "Age:%d", pHVCResult->fdResult.fcResult[i].ageResult.age);
+                                    if (pHVCResult->fdResult.fcResult[i].ageResult.confidence < 20000) {
+                                        strcat(Str_disp, " (?)");
+                                    }
                                     DrawString(Str_disp, 0x0000f0ff);
                                 }
                             }
                             if (pHVCResult->executedFunc & HVC_ACTIV_GENDER_ESTIMATION) {
                                 /* Gender */
                                 if (-128 != pHVCResult->fdResult.fcResult[i].genderResult.gender) {
+                                    uint32_t wk_color;
+                                    memset(Str_disp, 0, sizeof(Str_disp));
                                     if (1 == pHVCResult->fdResult.fcResult[i].genderResult.gender) {
-                                        DrawString("Male", 0x0000fff4);
+                                        sprintf(Str_disp, "Male");
+                                        wk_color = 0x0000fff4;
                                     } else {
-                                        DrawString("Female", 0x00006dff);
+                                        sprintf(Str_disp, "Female");
+                                        wk_color = 0x00006dff;
                                     }
+                                    if ( pHVCResult->fdResult.fcResult[i].genderResult.confidence < 20000 ) {
+                                        strcat(Str_disp, " (?)");
+                                    }
+                                    DrawString(Str_disp, wk_color);
                                 }
                             }
                             if (pHVCResult->executedFunc & HVC_ACTIV_EXPRESSION_ESTIMATION) {