Re: [問題] 關於python+opencv的錯誤訊息
※ 引述《shamen (watin)》之銘言:
: 標題: [問題] 關於python+opencv的錯誤訊息
: 時間: Wed Mar 30 15:43:17 2011
:
: 目前在linux 上使用python+opencv 遇到了一個錯誤
: 想請各位幫我看一下,該從哪邊下手解決?
:
: --------------------錯誤訊息------------------------------------
:
: Traceback (most recent call last):
: File "PythonOpencvFacedetect.py", line 87, in <module>
: if input_name.isdigit():
: AttributeError: 'int' object has no attribute 'isdigit'
:
: ----------------------------------------------------------------
:
: 麻煩各位了,謝謝!!
:
: --
: ※ 發信站: 批踢踢實業坊(ptt.cc)
: ◆ From: 163.26.229.128
: 推 AlanSung:input_name 應該是 str (才有 isdigit()), 你的是 int 03/30 16:02
: 推 hilorrk:讓我猜 你應該是用python3.X? openCV是用2.6/2.7 03/30 16:03
: → hilorrk:因為input()改變了 3.X變string 2.7的是eval(raw_input()) 03/30 16:05
: → hilorrk:不對 這樣應該是反過來才對 猜錯了XD 03/30 16:08
我直接po我的python,各位看一下
---------------------------------------------------------
import sys
sys.path.append("/home/pc128/opencv/lib/site-packages")
from opencv.cv import *
from opencv.highgui import *
# Global Variables
cascade = None
storage = cvCreateMemStorage(0)
cascade_name =
"/home/pc128/opencv/opencv/OpenCV-2.0.0/data/haarcascades/haarcascade_frontalface_alt.xml"
input_name = 0
# Parameters for haar detection
# From the API:
# The default parameters (scale_factor=1.1, 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 = cvSize(20,20)
image_scale = 1.3
haar_scale = 1.2
min_neighbors = 2
haar_flags = 0
def detect_and_draw( img ):
# allocate temporary images
gray = cvCreateImage( cvSize(img.width,img.height), 8, 1 )
small_img = cvCreateImage((cvRound(img.width/image_scale),
cvRound (img.height/image_scale)), 8, 1 )
# convert color input image to grayscale
cvCvtColor( img, gray, CV_BGR2GRAY )
# scale input image for faster processing
cvResize( gray, small_img, CV_INTER_LINEAR )
cvEqualizeHist( small_img, small_img )
cvClearMemStorage( storage )
if( cascade ):
t = cvGetTickCount()
faces = cvHaarDetectObjects( small_img, cascade,storage,haar_scale,
min_neighbors, haar_flags, min_size)
t = int(cvGetTickCount())-int(t)
print "detection time = %gms" %
(float(t)/(cvGetTickFrequency()*1000.))
if faces:
for face_rect in faces:
# the input to cvHaarDetectObjects was resized, so scale the
# bounding box of each face and convert it to two CvPoints
pt1 = cvPoint( int(face_rect.x*image_scale),
int(face_rect.y*image_scale))
pt2 = cvPoint( int((face_rect.x+face_rect.width)*image_scale),
int((face_rect.y+face_rect.height)*image_scale) )
cvRectangle( img, pt1, pt2, CV_RGB(255,0,0), 3, 8, 0 )
cvShowImage( "result", img )
if __name__ == '__main__':
if len(sys.argv) > 1:
if sys.argv[1].startswith("--cascade="):
cascade_name = sys.argv[1][ len("--cascade="): ]
if len(sys.argv) > 2:
input_name = sys.argv[2]
elif sys.argv[1] == "--help" or sys.argv[1] == "-h":
print "Usage: facedetect
--cascade=\"<cascade_path>\"filename|camera_index]\n"
sys.exit(-1)
else:
input_name = sys.argv[1]
# the OpenCV API says this function is obsolete, but we can't
# cast the output of cvLoad to a HaarClassifierCascade, so use this
anyways
# the size parameter is ignored
cascade = cvLoadHaarClassifierCascade( cascade_name, cvSize(1,1) )
if not cascade:
print "ERROR: Could not load classifier cascade"
sys.exit(-1)
if input_name.isdigit(): ←←← 錯誤的地方
capture = cvCreateCameraCapture( int(input_name) )
else:
capture = cvCreateFileCapture( input_name )
cvNamedWindow( "result", 1 )
if capture:
frame_copy = None
while True:
frame = cvQueryFrame( capture )
if not frame:
cvWaitKey(0)
break
if not frame_copy:
frame_copy = cvCreateImage( cvSize(frame.width,frame.height),
IPL_DEPTH_8U, frame.nChannels )
if frame.origin == IPL_ORIGIN_TL:
cvCopy( frame, frame_copy )
else:
cvFlip( frame, frame_copy, 0 )
detect_and_draw( frame_copy )
if( cvWaitKey( 10 ) >= 0 ):
break
else:
image = cvLoadImage( input_name, 1 )
if image:
detect_and_draw( image )
cvWaitKey(0)
cvDestroyWindow("result")
-----------------------------------------------------------------------------
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 163.26.229.155
※ 編輯: shamen 來自: 163.26.229.155 (03/30 20:57)
→
03/30 20:58, , 1F
03/30 20:58, 1F
→
03/30 20:59, , 2F
03/30 20:59, 2F
→
03/30 21:07, , 3F
03/30 21:07, 3F
→
03/30 21:35, , 4F
03/30 21:35, 4F
→
03/30 21:38, , 5F
03/30 21:38, 5F
→
03/31 11:58, , 6F
03/31 11:58, 6F
→
03/31 11:58, , 7F
03/31 11:58, 7F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 2 篇):
Python 近期熱門文章
PTT數位生活區 即時熱門文章