python - 不明白这个SyntaxError : illegal target for annotation

标签 python python-3.x if-statement syntax-error conditional

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

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

# 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()

如果Elif 是标题词,当它们应该小写时,这样做:

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

相反。

关于python - 不明白这个SyntaxError : illegal target for annotation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53734530/

相关文章:

python - .get 和字典

python - 'utf- 8' codec can' t 解码字节 0x92 在位置 18 : invalid start byte

python - 当我使用python消耗wsdl时,得到了xml.parsers.expat.ExpatError

python-3.x - 在 sagemaker 中从 S3 安装自定义包

java - 无法检查 if 语句中的 LongAdder 值

javascript - if 语句更新变量

python - vtkSplineRepresentation 的 Actor ()

python - 在 Pandas 中阅读维基百科表格时数值呈现不当

python - 如何使用 python 向当前终端历史记录添加命令?

java - if 语句的问题