首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >不理解此SyntaxError:注释的目标非法

不理解此SyntaxError:注释的目标非法
EN

Stack Overflow用户
提问于 2018-12-12 08:49:23
回答 1查看 21K关注 0票数 2

我有一些简单的假设..。埃利夫..。在我的python3.6 (& OpenCV 4.0)中,不管我做什么,我总是收到奇怪的错误消息。

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

代码语言:javascript
复制
# 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!")

错误消息如下所示:

代码语言:javascript
复制
File "image_import2.py", line 121
   If (y1 < 0): y_movedown()
   ^
SyntaxError: illegal target for annotation

有没有人看到,我做错了什么?找不到任何提到这样一个错误的地方...

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-12-12 08:51:48

看看下面这几行:

代码语言:javascript
复制
If (y1 < 0): y_movedown()
Elif (y2 > imageheight): y_moveup()
Elif (x1 < 0) : x_moveright()
Elif (x2 > imagewidth): x_moveleft()

IfElif是有标题的单词,当它们应该是小写的时候,所以这样做:

代码语言:javascript
复制
if (y1 < 0): y_movedown()
elif (y2 > imageheight): y_moveup()
elif (x1 < 0) : x_moveright()
elif (x2 > imagewidth): x_moveleft()

而不是。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53734530

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档