簡體   English   中英

python初學者錯誤中的變量增量

[英]variable increment in python beginner error

我試圖在每次檢測到對象時增加計數器。

count=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 "time taken for detection = %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) 
            count += 1
            print count

但是我不明白為什么會有這個錯誤。我試圖在仍然但仍然相同的錯誤之后進行增量。 這是第一次使用python,所以我無法獲得作用域規則。 我應該修改什么?

 File "detect.py", line 42, in detect_and_draw
    count +=1
UnboundLocalError: local variable 'count' referenced before assignment

您有一個全球性的計數。 該功能不知道。

def detect_and_draw(img, cascade):
    global count

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM