更新手势识别代码
authorzf zf <2919300294@qq.com>
Mon, 26 Jun 2023 09:09:33 +0000 (17:09 +0800)
committerzf zf <2919300294@qq.com>
Mon, 26 Jun 2023 09:09:33 +0000 (17:09 +0800)
app/src/device_gesture.c

index 9ebe07798609f1551cc7d6971921eb6ac43b1245..96b5e64150c424f1bde76bd7f39a2e1e0c14dcf4 100644 (file)
 #include "common/sample_common.h"
 #include "rkmedia_api.h"
 
-
 static int32_t stop = 0;
 static bool quit = false;
-static uint32_t camera_id;
+static uint32_t camera_id = 0;
 static uint32_t my_privacy = 0;
 static const STDetectResult *mg_detectResult = NULL;
 
+typedef enum
+{
+    CAMERA_IR = 0,
+    CAMERA_RGB,
+    CAMERA_DOUBLE
+} camera_num;
+static camera_num gr_camera_num;
+
 #define ST_AI_IMAGE_W 1280 // 1920
 #define ST_AI_IMAGE_H 720  // 1080
-
+#define BODY_ATTRIBUTE
 
 static STResult addModels(STHandle handle, int mode)
 {
@@ -135,12 +142,13 @@ void rk_isp_deinit(char index)
     SAMPLE_COMM_ISP_Stop(index);
 #endif
 }
-void rk_vi_init(VI_PIPE ViPipe, VI_CHN ViChn, uint32_t video_width, uint32_t video_height)
+
+//zf有过修改,将void改成了int
+static int rk_vi_init(VI_PIPE ViPipe, VI_CHN ViChn, uint32_t video_width, uint32_t video_height)
 {
     VI_CHN_ATTR_S vi_chn_attr;
     int ret = 0;
 
-
     memset(&vi_chn_attr, 0, sizeof(vi_chn_attr));
     vi_chn_attr.pcVideoNode = "rkispp_scale0";
     vi_chn_attr.u32BufCnt = 5;
@@ -155,17 +163,17 @@ void rk_vi_init(VI_PIPE ViPipe, VI_CHN ViChn, uint32_t video_width, uint32_t vid
     if (ret)
     {
         printf("Create vi[%d] failed! ret=%d\n", ViPipe, ret);
-        return;
+        return -1;
     }
     ret = RK_MPI_VI_StartStream(ViPipe, ViChn);
     if (ret)
     {
         printf("RK_MPI_VI_StartStream [ViChn] failed! ret=%d\n", ret);
-        return;
+        return -1;
     }
 }
 
-int rk_vi_setfps(VI_CHN ViChn, int fps)
+static int rk_vi_setfps(VI_CHN ViChn, int fps)
 {
     int ret;
     MPP_FPS_ATTR_S fps_attr;
@@ -205,9 +213,8 @@ static int32_t reportdp_and_close_ring_gr(light_ctrl_t led_ctrl)
 }
 
 
-
 // 修改:状态发生变化时再切换摄像头
-static uint32_t last_als_status = -1;
+static int32_t last_als_status = -1;
 static int32_t gr_als_t(gr_ctrl_t *gr_ctrl)
 {
     als_info_t *gr_info;
@@ -217,11 +224,12 @@ static int32_t gr_als_t(gr_ctrl_t *gr_ctrl)
         printf(" gr_ctrl == NULL \n");
         goto error;
     }
-   
-    if (cfg_privacy_mode_read(gr_ctrl->dev) == 0)
+
+    if (gr_camera_num == CAMERA_DOUBLE)
     {
         gr_info = mg_als_read(gr_ctrl->als_ctrl);
-        if(gr_info != NULL){
+        if (gr_info != NULL)
+        {
             if (last_als_status != gr_info->status)
             {
                 last_als_status = gr_info->status;
@@ -245,6 +253,108 @@ error:
     return -1;
 }
 
+static int32_t gr_isp_init(int32_t index)
+{
+    int32_t ret = 0;
+
+    ret = rk_isp_init(index, "/etc/iqfiles/");
+    if (ret < 0)
+    {
+        printf("rk_isp_init %d failed!\n", index);
+        return -1;
+    }
+    return 0;
+}
+
+static int32_t gr_vi_init_and_setfps(int32_t index)
+{
+    int32_t ret = 0;
+
+    ret = rk_vi_init(index, index, ST_AI_IMAGE_W, ST_AI_IMAGE_H);
+    if (ret < 0)
+    {
+        printf("rk_vi_init %d failed!\n", index);
+        return -1;
+    }
+    rk_vi_setfps(index, 15);
+    return 0;
+}
+
+
+//在上面函数的基础上进行修改,新加的单独开启和关闭相机,但重复启动会报错
+// static uint32_t camera_init = 0;
+// static int32_t last_als_status = -1;
+// static int32_t gr_als_t(gr_ctrl_t *gr_ctrl)
+// {
+//     int ret = 0;
+//     als_info_t *gr_info;
+
+//     if (gr_ctrl == NULL)
+//     {
+//         printf(" gr_ctrl == NULL \n");
+//         goto error;
+//     }
+
+//     if (gr_camera_num == CAMERA_DOUBLE)
+//     {
+//         gr_info = mg_als_read(gr_ctrl->als_ctrl);
+//         if (gr_info != NULL)
+//         {
+//             if (last_als_status != gr_info->status)
+//             {
+//                 last_als_status = gr_info->status;
+//                 if (last_als_status == BRIGHT)
+//                 {
+//                     ret = gr_isp_init(0);
+//                     if(!ret){
+//                         //RK_MPI_SYS_Init(); 
+//                         ret = gr_vi_init_and_setfps(1);
+//                         if(!ret){
+//                             ir_led_off(gr_ctrl->light_ctrl); // 禁用红外补光
+//                             camera_id = 1;
+//                             printf("开启rgb,关闭ir\n");
+//                             usleep(100*1000);
+//                             RK_MPI_VI_DisableChn(0, 0);
+//                             rk_isp_deinit(1);
+//                         }else{
+//                             RK_MPI_VI_DisableChn(1, 1);
+//                             rk_isp_deinit(0);
+//                         }
+//                     }else{
+//                         printf("切换rgb失败\n");
+//                     }
+//                 }
+//                 else if (last_als_status == DARK)
+//                 {
+//                     ret = gr_isp_init(1);
+//                     if(!ret){
+//                         //RK_MPI_SYS_Init(); 
+//                         ret = gr_vi_init_and_setfps(0);
+//                         if(!ret){
+//                             ir_led_on(gr_ctrl->light_ctrl, 100); // 开启红外补光
+//                             camera_id = 0;
+//                             printf("开启ir,关闭rgb\n");
+//                             usleep(100*1000);
+//                             RK_MPI_VI_DisableChn(1, 1);
+//                             rk_isp_deinit(0);
+//                         }else{
+//                             RK_MPI_VI_DisableChn(0, 0);
+//                             rk_isp_deinit(1);
+//                         }
+//                     }else{
+//                         printf("切换ir失败\n");
+
+//                     }
+//                 }
+//             }
+//             gr_info = NULL;
+//         }
+//     }
+// error:
+//     return -1;
+// }
+
+
 static int32_t gr_read_gesture(void)
 {
     int32_t ret = -1;
@@ -343,7 +453,7 @@ static int32_t gr_detect_effective(gesture_ev_t ev)
 
 static int32_t gr_ai_detect_palm(void)
 {
-    usleep(800*1000);
+    usleep(800 * 1000);
     if (gr_read_gesture() == GS_PAPER)
     {
         return 0;
@@ -353,10 +463,29 @@ static int32_t gr_ai_detect_palm(void)
 }
 
 
+void gr_close(gr_ctrl_t *gr_ctrl)
+{
+    stop = 1;
+    usleep(100 * 1000);
+
+    if (gr_ctrl)
+    {
+        quit = 1;
+        usleep(100 * 1000);
+        RK_MPI_VI_DisableChn(0, 0);
+        RK_MPI_VI_DisableChn(1, 1);
+        rk_isp_deinit(0);
+        rk_isp_deinit(1);
+        mg_als_close(gr_ctrl->als_ctrl);
+        MG_LOGD("tw8013 gr close!\n");
+    }
+}
+
+
 void *gr_task(void *param)
 {
     int ret;
-    //uint32_t init = 1;
+    // uint32_t init = 1;
     uint32_t dp_timeout, gr_time;
     uint32_t time_flasg;
 
@@ -469,7 +598,6 @@ retry_for_wait_ntp:
     detectImage.format = ST_PIXEL_FORMAT_NV12;
     detectImage.width = ST_AI_IMAGE_W;
     detectImage.height = ST_AI_IMAGE_H;
-    //const STDetectResult *detectResult = NULL; 
 
     while (!quit)
     {
@@ -481,6 +609,10 @@ retry_for_wait_ntp:
                 privacy = 0;
                 printf("===============Exit privacy mode================\n");
             }
+
+            // if(camera_id == -1)
+            //     continue;
+
             mb = RK_MPI_SYS_GetMediaBuffer(RK_ID_VI, camera_id, -1);
             if (!mb)
             {
@@ -490,6 +622,7 @@ retry_for_wait_ntp:
             else
             {
                 memcpy(image_data, RK_MPI_MB_GetPtr(mb), nv12_buffer_size);
+                //printf("zf=========camera_id = %d\n",camera_id);
                 RK_MPI_MB_ReleaseBuffer(mb);
                 STAsteriaDetect(g_SThandle, ST_DETECT_MODE_VIDEO, &detectImage, &mg_detectResult);
             }
@@ -511,13 +644,53 @@ retry_for_wait_ntp:
     return NULL;
 }
 
+static int detect_cameras_num(void)
+{
+    int flag = 0;
+
+    if (!access("/sys/bus/i2c/drivers/gc2053/1-0037", 0))
+    {
+        gr_camera_num = CAMERA_RGB;
+        flag++;
+        printf("Detected rgb camera\n");
+    }
+    else
+    {
+        printf("No rgb camera detected\n");
+    }
+
+    if (!access("/sys/bus/i2c/drivers/gc2053/3-0037", 0))
+    {
+        if (flag == 1)
+        {
+            gr_camera_num = CAMERA_DOUBLE;
+        }
+        else
+        {
+            gr_camera_num = CAMERA_IR;
+        }
+        flag++;
+        printf("Detected IR camera\n");
+    }
+    else
+    {
+        printf("No ir camera detected\n");
+    }
+    return flag;
+}
+
 
 
 int gr_register(gr_ctrl_t *gr_ctrl, uint32_t threshold_high, uint32_t threshold_low)
 {
-    int ret;
+    int32_t ret;
     pthread_t th2;
     pthread_t gr_thread;
+    //zf
+    int32_t flag = 0;
+    als_info_t *gr_info;
+    //zf_end
+    int32_t camera_flag = 0;
 
     gr_ctrl->als_ctrl = mg_als_open();
     if (gr_ctrl->als_ctrl == NULL)
@@ -525,79 +698,119 @@ int gr_register(gr_ctrl_t *gr_ctrl, uint32_t threshold_high, uint32_t threshold_
         printf("als open failed \n");
         goto app_ext1;
     }
-
     mg_als_cfg(gr_ctrl->als_ctrl, threshold_high, threshold_low);
 
-    ret = rk_isp_init(0, "/etc/iqfiles/");
-    if (ret < 0)
-    {
-        printf("rk_isp_init failed!\n");
-        goto app_ext1;
-    }
-
-    ret = rk_isp_init(1, "/etc/iqfiles/");
-    if (ret < 0)
-    {
-        printf("rk_isp_init failed!\n");
-        goto app_ext1;
-    }
+    camera_flag = detect_cameras_num();
+    if(camera_flag){
+        if(camera_flag == 1){
+            if(gr_camera_num == CAMERA_RGB){
+                ret = rk_isp_init(0, "/etc/iqfiles/");
+                if (ret < 0)
+                {
+                    printf("rk_vi_init 1 failed!============================================\n");
+                    goto app_ext1;
+                }else{
+                    RK_MPI_SYS_Init();
+                    rk_vi_init(1, 1, ST_AI_IMAGE_W, ST_AI_IMAGE_H);
+                    rk_vi_setfps(1, 15);
+                    camera_id = 1;  
+                    printf("1-0 rk_vi_init 1 yes ==============================================\n");
+                }
+            }else if(gr_camera_num == CAMERA_IR){
+                ret = rk_isp_init(0, "/etc/iqfiles/");
+                if (ret < 0)
+                {
+                    printf("rk_vi_init 0 failed!============================================\n");
+                    goto app_ext1;
+                }else{
+                    RK_MPI_SYS_Init();
+                    rk_vi_init(0, 0, ST_AI_IMAGE_W, ST_AI_IMAGE_H);
+                    rk_vi_setfps(0, 15);
+                    camera_id = 0;
+                    ir_led_on(gr_ctrl->light_ctrl, 100);
+                    printf("==================\1-1 rk_vi_init 0 yes and Turn on infrared fill light================\n");
+                }
+            }
+        }else{
+            ret = rk_isp_init(0, "/etc/iqfiles/");
+            if (ret < 0)
+            {
+                printf("rk_isp_init 0 failed!=============================================\n");
+            }else{
+                flag++;
+                gr_camera_num = CAMERA_RGB;
+                printf("2\rk_isp_init 0 yes ==============================================\n");
+            }
+           
+            ret = rk_isp_init(1, "/etc/iqfiles/");
+            if (ret < 0)
+            {
+                printf("rk_isp_init 1 failed!\n");
+                if(!flag)
+                    goto app_ext1;
+            }else{
+                if(flag == 1){
+                    gr_camera_num = CAMERA_DOUBLE;
+                }else{
+                    gr_camera_num = CAMERA_IR;
+                }
+                printf("3\rk_isp_init 1 yes ==============================================\n");
+            }
 
-    RK_MPI_SYS_Init();
-    rk_vi_init(0, 0, ST_AI_IMAGE_W, ST_AI_IMAGE_H);
-    rk_vi_setfps(0, 15);
-    rk_vi_init(1,1,ST_AI_IMAGE_W,ST_AI_IMAGE_H);
-    rk_vi_setfps(1,15);
+            RK_MPI_SYS_Init(); 
+            if(gr_camera_num == CAMERA_DOUBLE){
+                rk_vi_init(0, 0, ST_AI_IMAGE_W, ST_AI_IMAGE_H);
+                rk_vi_setfps(0, 15);
+                rk_vi_init(1, 1, ST_AI_IMAGE_W, ST_AI_IMAGE_H);
+                rk_vi_setfps(1, 15);
+            }else if(gr_camera_num == CAMERA_RGB){
+                rk_vi_init(1, 1, ST_AI_IMAGE_W, ST_AI_IMAGE_H);
+                rk_vi_setfps(1, 15);
+                camera_id = 1;
+            }else{
+                rk_vi_init(0, 0, ST_AI_IMAGE_W, ST_AI_IMAGE_H);
+                rk_vi_setfps(0, 15);
+                camera_id = 0;
+                ir_led_on(gr_ctrl->light_ctrl, 100);
+            }
+        }
 
-    usleep(100 * 1000);
+        usleep(100 * 1000);
 
-    if (pthread_create(&th2, NULL, ai_detect,NULL))
-    {
-        printf("create GetBuffer thread failed!\n");
-        quit = 1;
-        goto app_ext1;
-    }
+        if (pthread_create(&th2, NULL, ai_detect,NULL))
+        {
+            printf("create GetBuffer thread failed!\n");
+            quit = 1;
+            goto app_ext1;
+        }
 
-    if (pthread_create(&gr_thread, NULL, gr_task, gr_ctrl) != 0)
-    {
-        printf("create hand_ic failed!");
-        goto app_ext1;
-    }
+        if (pthread_create(&gr_thread, NULL, gr_task, gr_ctrl) != 0)
+        {
+            printf("create hand_ic failed!");
+            stop = 1;
+            goto app_ext1;
+        }
 
-    return 0;
+        return 0;
+   }
 
 app_ext1:
-    RK_MPI_VI_DisableChn(0, 0);
-    RK_MPI_VI_DisableChn(1, 1);
-    rk_isp_deinit(0);
-    rk_isp_deinit(1);
+    gr_close(gr_ctrl);
+    // RK_MPI_VI_DisableChn(0, 0);
+    // RK_MPI_VI_DisableChn(1, 1);
+    // rk_isp_deinit(0);
+    // rk_isp_deinit(1);
+    // mg_als_close(gr_ctrl->als_ctrl);
     return -1;
 }
 
-void gr_close(gr_ctrl_t * gr_ctrl)
-{
-    stop = 1;
-    usleep(100 * 1000);
-
-    if (gr_ctrl)
-    {
-        quit = 1;
-        usleep(100 * 1000);
-        RK_MPI_VI_DisableChn(0, 0);
-        RK_MPI_VI_DisableChn(1, 1);
-        rk_isp_deinit(0);
-        rk_isp_deinit(1);
-
-        mg_als_close(gr_ctrl->als_ctrl);
 
-        MG_LOGD("tw8013 gr close!\n");
-    }
-}
 
 #if 0
 static gr_ctrl_t gr_ctrl;
 
-extern light_ctrl_t  g_led_ctrl;
-extern mg_dev_t g_mg_dev;
+static light_ctrl_t  g_led_ctrl;
+// extern mg_dev_t g_mg_dev;
 
 int32_t main( void )
 {
@@ -606,7 +819,7 @@ int32_t main( void )
        uint32_t als_th_low = 400;
 
        memset( &gr_ctrl, 0, sizeof( gr_ctrl ) );
-
+    //2\跑死的概率更大
     g_led_ctrl = mg_led_open();
     if (!g_led_ctrl)
     {
@@ -615,8 +828,8 @@ int32_t main( void )
     }
 
        gr_ctrl.light_ctrl  =  g_led_ctrl;
-       gr_ctrl.dev = &g_mg_dev;
-       //load_als_threshold( &als_th_high, &als_th_low );
+       // //gr_ctrl.dev = &g_mg_dev;
+       load_als_threshold( &als_th_high, &als_th_low );
 
        signal(SIGINT, sigterm_handler);
        usleep(100 * 1000);
@@ -634,14 +847,17 @@ int32_t main( void )
         usleep(100 * 1000);
     }
 
-       return ret;
+    RK_MPI_VI_DisableChn(0, 0);
+    RK_MPI_VI_DisableChn(1, 1);
+    rk_isp_deinit(0);
+    rk_isp_deinit(1);
 }
 
 #else
 
 static gr_ctrl_t gr_ctrl;
 
-int32_t gesture_recognition_start( mg_dev_t *dev )
+int32_t gesture_recognition_start(mg_dev_t *dev)
 {
     int32_t ret = 0;
     uint32_t als_th_high;
@@ -667,4 +883,3 @@ int32_t gesture_recognition_start( mg_dev_t *dev )
 }
 #endif
 
-