Last active
April 7, 2019 16:19
-
-
Save mattn/a4744a420cb79c830a4225d216142de3 to your computer and use it in GitHub Desktop.
Using libfacedetection with video capture
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
The MIT License (MIT) | |
Copyright (c) 2018-2019 Shiqi Yu | |
shiqi.yu@gmail.com | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. | |
*/ | |
#include <stdio.h> | |
#include <opencv2/opencv.hpp> | |
#include "facedetectcnn.h" | |
//define the buffer size. Do not change the size! | |
#define DETECT_BUFFER_SIZE 0x20000 | |
using namespace cv; | |
int main(int argc, char* argv[]) | |
{ | |
if(argc != 2) | |
{ | |
printf("Usage: %s <image_file_name>\n", argv[0]); | |
return -1; | |
} | |
char *err = nullptr; | |
VideoCapture cap; | |
int camera = strtol(argv[1], &err, 10); | |
if (err && *err) | |
cap = VideoCapture(argv[1]); | |
else | |
cap = VideoCapture(camera); | |
if (!cap.isOpened()) { | |
fprintf(stderr, "Can not open VideoCapture.\n"); | |
return -1; | |
} | |
while (true) | |
{ | |
int key = waitKey(1); | |
if (key == 27) | |
break; | |
Mat frame; | |
cap >> frame; | |
if(frame.empty()) | |
{ | |
fprintf(stderr, "Can not load the frame %s.\n", argv[1]); | |
continue; | |
} | |
float scale = 640.0 / (float)frame.size().width; | |
Mat image(frame.size().height * scale, frame.size().width * scale, frame.type()); | |
resize(frame, image, image.size(), INTER_CUBIC); | |
int * pResults = NULL; | |
//pBuffer is used in the detection functions. | |
//If you call functions in multiple threads, please create one buffer for each thread! | |
unsigned char * pBuffer = (unsigned char *)malloc(DETECT_BUFFER_SIZE); | |
if(!pBuffer) | |
{ | |
fprintf(stderr, "Can not alloc buffer.\n"); | |
return -1; | |
} | |
/////////////////////////////////////////// | |
// CNN face detection | |
// Best detection rate | |
////////////////////////////////////////// | |
//!!! The input image must be a RGB one (three-channel) | |
//!!! DO NOT RELEASE pResults !!! | |
pResults = facedetect_cnn(pBuffer, (unsigned char*)(image.ptr(0)), image.cols, image.rows, (int)image.step); | |
printf("%d faces detected.\n", (pResults ? *pResults : 0)); | |
Mat result_cnn = image.clone(); | |
//print the detection results | |
for(int i = 0; i < (pResults ? *pResults : 0); i++) | |
{ | |
short * p = ((short*)(pResults+1))+142*i; | |
int x = p[0]; | |
int y = p[1]; | |
int w = p[2]; | |
int h = p[3]; | |
//int neighbors = p[4]; | |
//int angle = p[5]; | |
//printf("face_rect=[%d, %d, %d, %d], neighbors=%d, angle=%d\n", x,y,w,h,neighbors, angle); | |
rectangle(result_cnn, Rect(x, y, w, h), Scalar(0, 255, 0), 2); | |
} | |
imshow("result_cnn", result_cnn); | |
//release the buffer | |
free(pBuffer); | |
} | |
destroyAllWindows(); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SRCS = \ | |
libfacedetectcnn-example.cpp \ | |
../src/facedetectcnn.cpp \ | |
../src/facedetectcnn-floatdata.cpp \ | |
../src/facedetectcnn-int8data.cpp \ | |
../src/facedetectcnn-model.cpp | |
OBJS = $(subst .cc,.o,$(subst .cxx,.o,$(subst .cpp,.o,$(SRCS)))) | |
CXXFLAGS = -I../src $(shell pkg-config --cflags opencv4) -Wall -O5 -mavx -mfma -ffast-math | |
LIBS = $(shell pkg-config --libs opencv4) | |
TARGET = example | |
ifeq ($(OS),Windows_NT) | |
TARGET := $(TARGET).exe | |
endif | |
.SUFFIXES: .cpp .cxx .o | |
all : $(TARGET) | |
$(TARGET) : $(OBJS) | |
g++ -std=c++14 -o $@ $(OBJS) $(LIBS) | |
.cxx.o : | |
g++ -std=c++14 -c $(CXXFLAGS) -I. $< -o $@ | |
.cpp.o : | |
g++ -std=c++14 -c $(CXXFLAGS) -I. $< -o $@ | |
clean : | |
rm -f *.o $(TARGET) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment