簡體   English   中英

IndentationError: 期望等待鍵中有一個縮進塊

[英]IndentationError: expected an indented block in waitkey

File "<ipython-input-6-b985bbbd8c62>", line 21

    cv2.rectangle(img,(ix,iy),(x,y),(255,0,0),-1)
      ^
IndentationError: expected an indented block

我的代碼

import cv2

import numpy as np

#variables

#True while mouse button down, False while mouse button up

drawing = False

ix,iy = -1

#Function

def draw_rectangle(event,x,y,param,flags):

    global ix,iy,drawing

    if event == cv2.EVENT_LBUTTONDOWN:

        drawing = True

        ix,iy = x,y

    elif event == cv2.EVENT_MOUSEMOVE:

        if drawing == True:

        cv2.rectangle(img,(ix,iy),(x,y),(255,0,0),-1)

    elif event == cv2.EVENT_LBUTTONUP:

        drawing = False

        cv2.rectangle(img,(ix,iy),(x,y),(255,0,0),-1)

#Showing images with opencv

#black

img = np.zeros((612,612,3))

cv2.namedwindow(winname='draw_painting')

cv2.setMouseCallback('draw_painting',draw_rectangle)

while True:


        cv2.imshow('draw_painting',img)

        cv2.waitkey(20) & 0xFF = 27:

            break

    cv2.destryAllWindows()

這是您當前的代碼:

elif event == cv2.EVENT_MOUSEMOVE:
    if drawing == True:
    cv2.rectangle(img,(ix,iy),(x,y),(255,0,0),-1)

請注意,在 if 語句之后,您需要縮進代碼:

elif event == cv2.EVENT_MOUSEMOVE:
    if drawing == True:
        cv2.rectangle(img,(ix,iy),(x,y),(255,0,0),-1)

其他錯誤:

此外,您似乎打算將最終的 if 語句放在 if 塊內,並記住使用==而不是=來檢查是否相等。

您需要在 if 語句之后給行縮進

if drawing == True:

    cv2.rectangle(img,(ix,iy),(x,y),(255,0,0),-1)

暫無
暫無

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

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