-1

I have defined cap in

def fingerCursor(device):
    global cap
    global cap_height
    global cap_width
    cap = cv2.VideoCapture(device)
        #cap.set(cv2.cap_PROP_FRAME_HEIGHT,720)
        #cap.set(cv2.cap_PROP_FRAME_WIDTH,1280)
    cap_height = cap.get(cv2.cap_PROP_FRAME_HEIGHT)
    cap_width = cap.get(cv2.cap_PROP_FRAME_WIDTH)
        #print(cap_height,cap_width)

But still whenever I am running this part of my code, it says cap not defined. It also has the same problem with cap_height and cap_width.

while(cap.isOpened()):
        ## capture frame-by-frame
        ret, frame_raw = cap.read()
        while not ret:
            ret,frame_raw = cap.read()
        frame_raw = cv2.flip(frame_raw, 1)
        frame = frame_raw[:round(cap_height),:round(cap_width)] # ROI of the image
        cv2.imshow("raw_frame",frame)

2 Answers2

2

Your variables have to be defined outside of your function. And you have to keep your lines starting with global to declare you want to modify those variables outside of your function as well.

Thomas Schillaci
  • 2,348
  • 1
  • 7
  • 19
0

You do not create the variables in the global scope. And anyway, it is generally a bad idea to use global this way. I suggest changing your function to:

def fingerCursor(device):
    cap = cv2.VideoCapture(device)
    cap_height = cap.get(cv2.cap_PROP_FRAME_HEIGHT)
    cap_width = cap.get(cv2.cap_PROP_FRAME_WIDTH)
    return (cap, cap_height, cap_width)

and then using it like this:

cap, cap_height, cap_width = fingerCursor(whatever)
Błotosmętek
  • 12,717
  • 19
  • 29