在C+中使用pthread加速人脸检测+;11/Linux 我是C++初学者, 并尝试在raspberry pi 3和摄像头上使用OpenCV和SIMD库(支持ARM NEON)进行实时人脸检测。使用SIMD库后,FPS约为7。FPS正常,但摄像头有一点延迟
原始程序流程:在C+中使用pthread加速人脸检测+;11/Linux 我是C++初学者, 并尝试在raspberry pi 3和摄像头上使用OpenCV和SIMD库(支持ARM NEON)进行实时人脸检测。使用SIMD库后,FPS约为7。FPS正常,但摄像头有一点延迟,opencv,c++11,raspberry-pi,pthreads,simd-library,Opencv,C++11,Raspberry Pi,Pthreads,Simd Library,原始程序流程: 全局变量:cv::Mat ImageToOfEdetoction, cv::Point upperLeftPoint, cv::点lowerRightPoint 从照相机捕获图像 检测给定图像中的人脸并将输出置于cv::点 在原始图像上绘制矩形 输出到窗口 通过while循环返回(2) 代码如下: cv::Mat imageToDoFaceDetection; cv::Point upperLeftPoint; cv::Point lowerRightPoint; int fra
cv::Mat imageToDoFaceDetection;
cv::Point upperLeftPoint;
cv::Point lowerRightPoint;
int frameCount = 0;
int main(){
while(1){
imageToDoFaceDetection = captureImageFromCamera();
if(frameCount%4==0){ //doing face detection on every 4 frames
faceDetection();
}
drawRectangleOnImage(imageToDoFaceDetection,upperLeftPoint,lowerRightPoint);
showImage(imageToDoFaceDetection);
frameCount++;
}
return 0;
}
void faceDetection(){
...
//Function will read the global var imageToDoFaceDetection,
//And put the outputs to the upperLeftPoint, lowerRightPoint
//Assume that the function is work
return;
}
FPS正常,但摄像头延迟。由于faceDetection()所消耗的时间,将延迟图像输出到窗口
我试图通过使用pthread来解决延迟问题。我假设图像中的人脸移动缓慢,所以我想创建pthread,并使用之前在帧上检测到的位置在pthread和drawRectangleOnImage()上执行faceDetection()
这意味着图像输出到窗口(showImage())将不会等待faceDetection(),两个线程将同时执行此任务。在一个pthread上,drawRectangleOnImage()和showImage()将使用检测到的点,这些点可能在之前的少量帧中检测到。在另一个pthread上,faceDetection()将持续更新点。因为人脸移动缓慢,所以显示faceDetection()矩形的延迟位置是可以的
我试过fork(),但相机只能用于一个进程。
更新代码:
cv::Mat imageToDoFaceDetection;
cv::Point upperLeftPoint;
cv::Point lowerRightPoint;
int frameCount = 0;
pthread_t tid;
int main(){
While(1){
imageToDoFaceDetection = captureImageFromCamera();
//only 2 pthreads will be created.
if(frameCount==0){
pthread_create(&pid,NULL,pthread1_detection,NULL);
}
if(frameCount==2){
pthread_create(&pid,NULL,pthread2_detection,NULL);
}
drawRectangleOnImage(imageToDoFaceDetection,upperLeftPoint,
lowerRightPoint);
showImage(imageToDoFaceDetection);
frameCount++;
}
return 0;
}
void* pthread1_detection(void* data){
while(1){
if(frameCount%4==0){
faceDetection();
}
}
pthread_exit(NULL);
}
void* pthread2_detection(void* data){
while(1){
if(frameCount%4==2){
faceDetection();
}
}
pthread_exit(NULL);
}
void faceDetection(){
...
}
执行代码后,将显示分段错误。我在这个问题上挣扎了好几个小时。。。。。。。。!!!(使用SIMD库解决人脸检测问题的时间更长……)
我想知道原因和解决办法。谢谢大家,祝你们今天愉快。据我所知,OpenCV和Simd库已经使用多线程(对于Pi 3,内部线程数等于4)进行对象检测。因此,尝试使用两个外部线程检测人脸并不能提高性能。据我所知,OpenCV和Simd库已经使用多线程(对于Pi 3,内部线程数等于4)进行对象检测。因此,尝试使用2个外部线程检测人脸并不能提高性能。