Python:SyntaxError:注释的非法目标

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (262)

我有一些简单的if ... elif ...在我的python3.6(和OpenCV 4.0)中,但无论我做什么,我都会收到奇怪的错误消息。

我需要根据一些边界框来裁剪一些照片。

# the image to be cropped loads here: 
tobecropped= cv2.imread(img)
images.append(tobecropped)
(imageheight, imagewidth) = tobecropped.shape[:2] 

# sample bounding box: 
y = 833 # center vertically
x = 183 # center horizontally
width = 172
height = 103

# calculation of cropping values adding 10% extra: 
y1 = y-(height/2*1.1)
y2 = y+(height/2*1.1)
x1 = x-(width/2*1.1)
x2 = x+(width/2*1.1)

# return values to int:
y1 = int(y1)
y2 = int(y2)
x1 = int(x1)
x2 = int(x2)

# move the cropping inside the original image: 
If (y1 < 0): y_movedown()
Elif (y2 > imageheight): y_moveup()
Elif (x1 < 0) : x_moveright()
Elif (x2 > imagewidth): x_moveleft()

# actually cropping the image: 
cropped = tobecropped[y1:y2, x1:x2]
cv2.imshow("cropped", cropped)

# functions to move the complete bounding box inside the image boundaries: 
def y_movedown():
    y2=y2-y1
    y1=0
    print("moved down!")

def y_moveup():
    y1=y1-(y2-imageheight)
    y2=imageheight
    print("moved up!")

def x_moveright():
    x2=x2-x1
    x1=0
    print("moved right!")

def x_moveleft():
    x1=x1-(x2-imagewidth)
    x2=imagewidth
    print("moved left!")

错误消息如下所示:

 File "image_import2.py", line 121
    If (y1 < 0): y_movedown()
    ^
SyntaxError: illegal target for annotation

有谁看到,我在这里做错了什么?我自己找不到这样的错误......非常感谢

提问于
用户回答回答于

看看这些行:

If (y1 < 0): y_movedown()
Elif (y2 > imageheight): y_moveup()
Elif (x1 < 0) : x_moveright()
Elif (x2 > imagewidth): x_moveleft()

IfElif当它们应该是小写时,标题为单词,所以用:

if (y1 < 0): y_movedown()
elif (y2 > imageheight): y_moveup()
elif (x1 < 0) : x_moveright()
elif (x2 > imagewidth): x_moveleft()

代替。

扫码关注云+社区

领取腾讯云代金券