为什么我的柜台报告了意外的价值?

时间:2015-10-20 10:09:28

标签: python scope global counter

我以为我理解了python中的全局变量范围 但我的柜台并没有给出我的期望。 我使用opencv事件捕获鼠标点击和释放,我只想在变量为1时写入输出,所以我需要它按预期进行。

报告的警告:

SyntaxWarning: name 'roicounter' is assigned to before global declaration
SyntaxWarning: name 'imgcounter' is assigned to before global declaration

预期输出

imgcounter: 1
roicounter: 1
imgcounter: 1
roicounter: 2
imgcounter: 2
roicounter: 1
imgcounter: 2
roicounter: 2

收到的输出(roicounter严格按时间增加)

roidcounter before reset: 0
roidcounter after reset: 0
roidcounter before increment on any event: 0
roidcounter after increment: 1
1
roidcounter before increment: 1
roidcounter after increment: 2
2
roidcounter before increment: 2
roidcounter after increment: 3
3

收到输出:绘制一个矩形后:

imgcounter: 1
roidcounter after buttonup (if > 1): 262
roidcounter before increment: 262
roidcounter after increment: 263
263

代码

import cv2
import glob

refPt = []
cropping = False
outputfil = "labels.txt"
global imgcounter, roidcounter
imgcounter = 0
roidcounter = 0

def click_and_crop(event, x, y, flags, param):
    global refPt, cropping, imgcounter, roidcounter
    print("roidcounter before increment: "+str(roidcounter))
    roidcounter+=1
    print("roidcounter after increment: "+str(roidcounter))
    print(roidcounter)

    if event == cv2.EVENT_LBUTTONDOWN:
        refPt = [(x, y)]
        cropping = True
    elif event == cv2.EVENT_LBUTTONUP:
        refPt.append((x, y))
        cropping = False

        # draw a rectangle around the region of interest
        cv2.rectangle(image, refPt[0], refPt[1], (0, 255, 0), 2)
        cv2.imshow("image", image)
        print("imgcounter: "+str(imgcounter))
        # write to output
        if roidcounter > 1:
           print("roidcounter after buttonup (if > 1): "+str(roidcounter))
        else:
           print("othet stuff")
           print("roidcounter after buttonup (else): "+str(roidcounter))
           #write roi data
fils=glob.glob("*.jpg")
for fil in fils:
    print("inloop")
    imgcounter+=1
    print("roidcounter before reset: "+str(roidcounter))
    roidcounter = 0
    print("roidcounter after reset: "+str(roidcounter))
    globalfil = fil
    #write to output
    image = cv2.imread(fil)
    clone = image.copy()
    cv2.namedWindow("image")
    cv2.setMouseCallback("image", click_and_crop)

    while True:
        cv2.imshow("image", image)
        key = cv2.waitKey(1) & 0xFF

        if key == ord("r"):
            image = clone.copy()

        elif key == ord("c"):
            break

    cv2.destroyAllWindows()

2 个答案:

答案 0 :(得分:0)

基于缩进,行files=glob.glob("*.jpg")及其后的所有内容都是主代码块的一部分,而不是函数def的一部分。 global roicounter, imgcounter之后您不需要for file in files:。删除它,看看它是否有帮助。 (为了便于阅读,您可能希望在函数defs之间添加一些空行/间距。)

答案 1 :(得分:0)

roicounter也因鼠标移动而增加。 增量应该在事件条件下。

def click_and_crop(event, x, y, flags, param):
    global refPt, cropping, imgcounter, roidcounter
    print("roidcounter before increment: "+str(roidcounter))
    # roidcounter+=1 // shouldnt be incrmentd here cos of mouse move event.
    print("roidcounter after increment: "+str(roidcounter))
    print(roidcounter)

 if event == cv2.EVENT_LBUTTONDOWN:
        refPt = [(x, y)]
        cropping = True

    elif event == cv2.EVENT_LBUTTONUP:
        # now increment roicounter
        refPt.append((x, y))
        cropping = False