繁体   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