Segmentation fault: 11
在使用python3 运行mtcnn的时候一直出现Segmentation fault: 11的问题。或者说运行过程中,程序停止运行。如下图所示:
或者
分析原因是因为在mtcnn做第一阶段预测的时候使用了多个特征图进行并行运算,这些进程可能会同时使用opencv,而opencv虽然支持多任务同时执行,但都是共享同一块数据。所以会出现死锁的问题。
类似的问题都可以归结为opencv多任务并行计算出现死锁的问题。解决办法就是找到死锁的位置,然后使用以下方法之一就可以解决:
使用
cv2.setNumThreads(0)
设置opencv不使用多进程运行,但这句命令只在本作用域有效。将所有的多进程都关闭,使用顺序执行。
而对于mtcnn的源码来讲,可以将 mtcnn_detector.py内的detect_face函数中的
for batch in sliced_index:
local_boxes = self.Pool.map( detect_first_stage_warpper, \
zip(repeat(img), self.PNets[:len(batch)], [scales[i] for i in batch], repeat(self.threshold[0])) )
total_boxes.extend(local_boxes)
改为:
for batch in sliced_index:
for i in batch:
local_boxes=detect_first_stage_warpper((img,self.PNets[i%4],scales[i],self.threshold[0]))
if local_boxes is None:
continue
total_boxes.extend(local_boxes)
即可正常运行。