OpenCV 2.2中人脸检测python代码的分段错误
我最近在我的Ubuntu11机器上安装了OpenCV v2.2,使用的说明见。我能够运行示例C程序和一些示例python程序。但是运行OpenCV 2.2中人脸检测python代码的分段错误,python,opencv,Python,Opencv,我最近在我的Ubuntu11机器上安装了OpenCV v2.2,使用的说明见。我能够运行示例C程序和一些示例python程序。但是运行python facedetect.py~/img.jpg会给我“分段错误” 导致此问题的原因是什么?我如何修复它?。下面是facedetect.py的代码 #!/usr/bin/python """ This program is demonstration for face and object detection using haar-like featur
python facedetect.py~/img.jpg
会给我“分段错误”
导致此问题的原因是什么?我如何修复它?。下面是facedetect.py的代码
#!/usr/bin/python
"""
This program is demonstration for face and object detection using haar-like features.
The program finds faces in a camera image or video stream and displays a red box around them.
Original C implementation by: ?
Python implementation by: Roman Stanchak, James Bowman
"""
import sys
import cv
from optparse import OptionParser
# Parameters for haar detection
# From the API:
# The default parameters (scale_factor=2, min_neighbors=3, flags=0) are tuned
# for accurate yet slow object detection. For a faster operation on real video
# images the settings are:
# scale_factor=1.2, min_neighbors=2, flags=CV_HAAR_DO_CANNY_PRUNING,
# min_size=<minimum possible face size
min_size = (20, 20)
image_scale = 2
haar_scale = 1.2
min_neighbors = 2
haar_flags = 0
def detect_and_draw(img, cascade):
# allocate temporary images
gray = cv.CreateImage((img.width,img.height), 8, 1)
small_img = cv.CreateImage((cv.Round(img.width / image_scale),
cv.Round (img.height / image_scale)), 8, 1)
# convert color input image to grayscale
cv.CvtColor(img, gray, cv.CV_BGR2GRAY)
# scale input image for faster processing
cv.Resize(gray, small_img, cv.CV_INTER_LINEAR)
cv.EqualizeHist(small_img, small_img)
if(cascade):
t = cv.GetTickCount()
faces = cv.HaarDetectObjects(small_img, cascade, cv.CreateMemStorage(0),
haar_scale, min_neighbors, haar_flags, min_size)
t = cv.GetTickCount() - t
print "detection time = %gms" % (t/(cv.GetTickFrequency()*1000.))
if faces:
for ((x, y, w, h), n) in faces:
# the input to cv.HaarDetectObjects was resized, so scale the
# bounding box of each face and convert it to two CvPoints
pt1 = (int(x * image_scale), int(y * image_scale))
pt2 = (int((x + w) * image_scale), int((y + h) * image_scale))
cv.Rectangle(img, pt1, pt2, cv.RGB(255, 0, 0), 3, 8, 0)
cv.ShowImage("result", img)
if __name__ == '__main__':
parser = OptionParser(usage = "usage: %prog [options] [filename|camera_index]")
parser.add_option("-c", "--cascade", action="store", dest="cascade", type="str", help="Haar cascade file, default %default", default = "../data/haarcascades/haarcascade_frontalface_alt.xml")
(options, args) = parser.parse_args()
cascade = cv.Load(options.cascade)
if len(args) != 1:
parser.print_help()
sys.exit(1)
input_name = args[0]
if input_name.isdigit():
capture = cv.CreateCameraCapture(int(input_name))
else:
capture = None
cv.NamedWindow("result", 1)
if capture:
frame_copy = None
while True:
frame = cv.QueryFrame(capture)
if not frame:
cv.WaitKey(0)
break
if not frame_copy:
frame_copy = cv.CreateImage((frame.width,frame.height),
cv.IPL_DEPTH_8U, frame.nChannels)
if frame.origin == cv.IPL_ORIGIN_TL:
cv.Copy(frame, frame_copy)
else:
cv.Flip(frame, frame_copy, 0)
detect_and_draw(frame_copy, cascade)
if cv.WaitKey(10) >= 0:
break
else:
image = cv.LoadImage(input_name, 1)
detect_and_draw(image, cascade)
cv.WaitKey(0)
cv.DestroyWindow("result")
#/usr/bin/python
"""
该程序演示了如何使用类haar特征进行人脸和目标检测。
该程序在相机图像或视频流中查找人脸,并在其周围显示一个红色框。
原始C实现由:?
Python实现:Roman Stanchak,James Bowman
"""
导入系统
导入cv
从optpasse导入OptionParser
#haar检测参数
#从API:
#默认参数(比例系数=2,最小邻域=3,标志=0)已调整
#用于精确但缓慢的目标检测。在真实视频上进行更快的操作
#图像设置包括:
#比例因子=1.2,最小邻居=2,标志=CV\U HAAR\U DO\u CANNY\u修剪,
#最小大小==0:
打破
其他:
image=cv.LoadImage(输入名称,1)
检测和绘制(图像、级联)
cv.WaitKey(0)
cv.窗口(“结果”)
请帮忙
谢谢你同样的问题。我已经追踪到了级联上的cv.Load。我使用的是OS X 10.6、OpenCV 2.3.1和Python 2.6。这里是垃圾场:
Exception Type: EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: KERN_INVALID_ADDRESS at 0x0000000000000000
Crashed Thread: 0 Dispatch queue: com.apple.main-thread
Thread 0 Crashed: Dispatch queue: com.apple.main-thread
0 libopencv_objdetect.2.2.dylib 0x00000001007b2899 cv::RTTIImpl<cv::HOGDescriptor>::isInstance(void const*) + 41
1 libopencv_core.2.2.dylib 0x000000010113b65f cvTypeOf + 47
2 cv.so 0x00000001004ba12e pycvLoad(_object*, _object*, _object*) + 206
3 org.python.python 0x00000001000b8d48 PyEval_EvalFrameEx + 28696
4 org.python.python 0x00000001000b9af5 PyEval_EvalCodeEx + 2197
5 org.python.python 0x00000001000b9c16 PyEval_EvalCode + 54
6 org.python.python 0x00000001000dea6e PyRun_FileExFlags + 174
7 org.python.python 0x00000001000ded29 PyRun_SimpleFileExFlags + 489
8 org.python.python 0x00000001000ee40c Py_Main + 2764
9 org.python.python 0x0000000100000f14 0x100000000 + 3860
异常类型:EXC\u坏访问(SIGSEGV)
异常代码:0x0000000000000000处的内核地址无效
崩溃线程:0调度队列:com.apple.main-Thread
线程0崩溃:调度队列:com.apple.main-Thread
0 libopencv_objdetect.2.2.dylib 0x00000001007b2899 cv::RTTIImpl::isInstance(void const*)+41
1 libopencv_core.2.2.dylib 0x000000010113b65f cvTypeOf+47
2 cv.so 0x00000001004ba12e pycvLoad(_对象*,_对象*,_对象*)+206
3 org.python.python 0x00000001000b8d48 PyEval_EvalFrameEx+28696
4 org.python.python 0x00000001000b9af5 PyEval_evalcodex+2197
5 org.python.python 0x00000001000b9c16 PyEval_EvalCode+54
6 org.python.python 0x00000001000dea6e PyRun_FileExFlags+174
7 org.python.python 0x00000001000ded29 PyRun_SimpleFileExFlags+489
8 org.python.python 0x00000001000ee40c Py_Main+2764
9 org.python.python 0x0000000100000f14 0x100000000+3860
我四处寻找,发现了更多,这似乎表明这是由于OpenCV无法找到该文件。因此,请仔细检查“./data/haarcascades/haarcascade_frontalface_alt.xml”是否相对于执行它的位置存在 嗯,是的,你说得对。这很奇怪,因为我安装了2.3.1,但可能是以前的安装。不管怎样,只要我把文件弄对了,我的工作就成功了!