Download cpp/facedetectcnn-model.cpp from metrika-labs/FaceDetector-tiny: direct link, hf CLI and curl.
- Browser
- Download file 5.37 kB
-
https://huggingface.co/metrika-labs/FaceDetector-tiny/resolve/main/cpp/facedetectcnn-model.cpp
- Command line
-
hf download hf://metrika-labs/FaceDetector-tiny/cpp/facedetectcnn-model.cpp
-
curl -L -o facedetectcnn-model.cpp https://huggingface.co/metrika-labs/FaceDetector-tiny/resolve/main/cpp/facedetectcnn-model.cpp
5.37 kB
| // Adapted from Shiqi Yu's libfacedetection (3-clause BSD) for FaceDetector (yunet_s, box-only). | |
| extern ConvInfoStruct param_pConvInfo[NUM_CONV_LAYER]; | |
| Filters<float> g_pFilters[NUM_CONV_LAYER]; | |
| bool param_initialized = false; | |
| void init_parameters() | |
| { | |
| for (int i = 0; i < NUM_CONV_LAYER; i++) | |
| g_pFilters[i] = param_pConvInfo[i]; | |
| } | |
| std::vector<FaceRect> objectdetect_cnn(const unsigned char* rgbImageData, int width, int height, int step) | |
| { | |
| if (!param_initialized) | |
| { | |
| init_parameters(); | |
| param_initialized = true; | |
| } | |
| auto fx = setDataFrom3x3S2P1to1x1S1P0FromImage(rgbImageData, width, height, 3, step); | |
| /***************CONV0*********************/ | |
| fx = convolution(fx, g_pFilters[0]); | |
| fx = convolutionDP(fx, g_pFilters[1], g_pFilters[2]); | |
| fx = maxpooling2x2S2(fx); | |
| /***************CONV1*********************/ | |
| fx = convolution4layerUnit(fx, g_pFilters[3], g_pFilters[4], g_pFilters[5], g_pFilters[6]); | |
| /***************CONV2*********************/ | |
| fx = convolution4layerUnit(fx, g_pFilters[7], g_pFilters[8], g_pFilters[9], g_pFilters[10]); | |
| /***************CONV3*********************/ | |
| fx = maxpooling2x2S2(fx); | |
| auto fb1 = convolution4layerUnit(fx, g_pFilters[11], g_pFilters[12], g_pFilters[13], g_pFilters[14]); | |
| /***************CONV4*********************/ | |
| fx = maxpooling2x2S2(fb1); | |
| auto fb2 = convolution4layerUnit(fx, g_pFilters[15], g_pFilters[16], g_pFilters[17], g_pFilters[18]); | |
| /***************CONV5*********************/ | |
| fx = maxpooling2x2S2(fb2); | |
| auto fb3 = convolution4layerUnit(fx, g_pFilters[19], g_pFilters[20], g_pFilters[21], g_pFilters[22]); | |
| // yunet_s: shared_stacked_convs=0, use_kps=False | |
| CDataBlob<float> pred_reg[3], pred_cls[3], pred_obj[3]; | |
| /***************branch5*********************/ | |
| fb3 = convolutionDP(fb3, g_pFilters[27], g_pFilters[28]); | |
| pred_cls[2] = convolutionDP(fb3, g_pFilters[33], g_pFilters[34], false); | |
| pred_reg[2] = convolutionDP(fb3, g_pFilters[39], g_pFilters[40], false); | |
| pred_obj[2] = convolutionDP(fb3, g_pFilters[45], g_pFilters[46], false); | |
| /*****************add5*********************/ | |
| fb2 = elementAdd(upsampleX2(fb3), fb2); | |
| /*****************branch4*********************/ | |
| fb2 = convolutionDP(fb2, g_pFilters[25], g_pFilters[26]); | |
| pred_cls[1] = convolutionDP(fb2, g_pFilters[31], g_pFilters[32], false); | |
| pred_reg[1] = convolutionDP(fb2, g_pFilters[37], g_pFilters[38], false); | |
| pred_obj[1] = convolutionDP(fb2, g_pFilters[43], g_pFilters[44], false); | |
| /*****************add4*********************/ | |
| fb1 = elementAdd(upsampleX2(fb2), fb1); | |
| /***************branch3*********************/ | |
| fb1 = convolutionDP(fb1, g_pFilters[23], g_pFilters[24]); | |
| pred_cls[0] = convolutionDP(fb1, g_pFilters[29], g_pFilters[30], false); | |
| pred_reg[0] = convolutionDP(fb1, g_pFilters[35], g_pFilters[36], false); | |
| pred_obj[0] = convolutionDP(fb1, g_pFilters[41], g_pFilters[42], false); | |
| /***************PRIORBOX*********************/ | |
| auto prior3 = meshgrid(fb1.cols, fb1.rows, 8); | |
| auto prior4 = meshgrid(fb2.cols, fb2.rows, 16); | |
| auto prior5 = meshgrid(fb3.cols, fb3.rows, 32); | |
| bbox_decode(pred_reg[0], prior3, 8); | |
| bbox_decode(pred_reg[1], prior4, 16); | |
| bbox_decode(pred_reg[2], prior5, 32); | |
| auto cls = concat3(blob2vector(pred_cls[0]), blob2vector(pred_cls[1]), blob2vector(pred_cls[2])); | |
| auto reg = concat3(blob2vector(pred_reg[0]), blob2vector(pred_reg[1]), blob2vector(pred_reg[2])); | |
| auto obj = concat3(blob2vector(pred_obj[0]), blob2vector(pred_obj[1]), blob2vector(pred_obj[2])); | |
| // Box-only model (use_kps=False): supply zeroed kps vector to keep upstream detection_output ABI intact. | |
| CDataBlob<float> kps(1, 1, obj.channels * 10); | |
| if (kps.data) | |
| std::memset(kps.data, 0, size_t(kps.cols) * size_t(kps.rows) * size_t(kps.channelStep)); | |
| sigmoid(cls); | |
| sigmoid(obj); | |
| // Threshold: 0.45f on sqrt(cls*obj) (~0.2025 on cls*obj), NMS IoU 0.45f | |
| return detection_output(cls, reg, kps, obj, 0.45f, 0.45f, 1000, 512); | |
| } | |
| int* facedetect_cnn(unsigned char* result_buffer, | |
| unsigned char* rgb_image_data, int width, int height, int step) | |
| { | |
| if (!result_buffer) | |
| { | |
| fprintf(stderr, "%s: null buffer memory.\n", __FUNCTION__); | |
| return NULL; | |
| } | |
| result_buffer[0] = 0; | |
| result_buffer[1] = 0; | |
| result_buffer[2] = 0; | |
| result_buffer[3] = 0; | |
| std::vector<FaceRect> faces = objectdetect_cnn(rgb_image_data, width, height, step); | |
| int num_faces = (int)faces.size(); | |
| num_faces = MIN(num_faces, FACEDETECTION_RESULT_MAX_FACES); | |
| int* pCount = (int*)result_buffer; | |
| pCount[0] = num_faces; | |
| for (int i = 0; i < num_faces; i++) | |
| { | |
| short* p = ((short*)(result_buffer + 4)) + FACEDETECTION_RESULT_STRIDE_SHORTS * size_t(i); | |
| p[0] = (short)(faces[i].score * 100); | |
| p[1] = (short)faces[i].x; | |
| p[2] = (short)faces[i].y; | |
| p[3] = (short)faces[i].w; | |
| p[4] = (short)faces[i].h; | |
| for (int lmidx = 0; lmidx < 10; lmidx++) | |
| p[5 + lmidx] = 0; | |
| } | |
| return pCount; | |
| } | |