簡體   English   中英

奇怪的 python 語法錯誤

[英]Weird python syntax errors

所以我有一些代碼給出了三個語法錯誤,但它運行良好,它幾乎就像語法錯誤是裝飾性的或其他東西。 它在一個使用谷歌雲視覺 API 的非常大的項目中,我可能會提供更多的代碼來診斷問題,但我認為它很容易解析,我認為可能很容易看到整個方法來了解什么是重要的

(錯誤的代碼基本上只是取自 google vision API 示例,但我確實需要對其進行一些調整,所以也許這就是問題所在,但它有效,所以我很困惑)

(同樣值得注意的是,問題可能出在 visual studio 代碼上,而不是代碼本身)

def detect_object_cord(path): """在本地圖像中定位對象。

def detect_object_cord(path): 
    """Localize objects in the local image.
    Args:
    path: The path to the local file.
    """

    client = vision.ImageAnnotatorClient(credentials=GOOGLE_APPLICATION_CREDENTIALS)

    f = 1


    with open(path, 'rb') as image_file: 
        content = image_file.read()
    image = vision.Image(content=content)#content=content

    objects = client.object_localization(image=image).localized_object_annotations

    print('Number of objects found: {}'.format(len(objects)))
    for object_ in objects:
        print('\n{} (confidence: {})'.format(object_.name, object_.score)) 
    
        for vertex in object_.bounding_poly.normalized_vertices:
        
            if FaceCount == 1:
                if f<= 4:
                    cordAryX.append(vertex.x  )
                    cordAryY.append(vertex.y  )

            if FaceCount == 2:
                if f<= 8 and f> 4:
                    cordAryX.append(vertex.x  )
                    cordAryY.append(vertex.y  )
                
            if FaceCount == 3:
                if f<= 12 and f > 8:
                    cordAryX.append(vertex.x  )
                    cordAryY.append(vertex.y  )
                
            if FaceCount == 4:
                if f<= 16 and f > 12:
                    cordAryX.append(vertex.x  )
                    cordAryY.append(vertex.y  )
                
            if FaceCount == 5:
                if f<= 20 and f > 16:
                    cordAryX.append(vertex.x  )
                    cordAryY.append(vertex.y  )
                
            if FaceCount == 6:
                if f<= 24 and f > 20:
                    cordAryX.append(vertex.x  )
                    cordAryY.append(vertex.y  )
                
            if FaceCount == 7:
                if f<= 28 and f > 24:
                    cordAryX.append(vertex.x  )
                    cordAryY.append(vertex.y  )
                
            if FaceCount == 8:
                if f<= 32 and f > 28:
                    cordAryX.append(vertex.x  )
                    cordAryY.append(vertex.y  )
                
            if FaceCount == 9:
                if f<= 36 and f > 32:
                    cordAryX.append(vertex.x  )
                    cordAryY.append(vertex.y  )
                
            if FaceCount == 10:
                if f<= 40 and f > 36:
                    cordAryX.append(vertex.x  )
                    cordAryY.append(vertex.y  )
               
            f = f+1

    print()
    print('user selection was: ' + FaceChoice)
    print('x: ', end='')
    print(cordAryX)
    print('y: ', end='')
    print(cordAryY)

所有三行的錯誤只是說“SyntaxError:無效語法”,錯誤的行是:

“打開(路徑,'rb')作為圖像文件:”

“內容= image_file.read()”

“對於對象中的對象:”

或者至少那些是問題面板的線條

我試着認真地看着它,但我的大腦看不出有什么不對勁。 我看不到任何不合適的縮進或空格。 而且我總體上看不出有什么不妥。 我曾嘗試刪除然后手動重新輸入這些行,但沒有用。 正如我之前所說,代碼按預期運行和工作,但錯誤讓我抓狂,我只希望它看起來很完美。

我想通了,它列在這篇文章中( SyntaxError: Invalid syntax jedi ),如果你在 visual studio code 中遇到錯誤,那么基本上只是禁用然后重新啟用你的包。 對我來說就像一個魅力。

暫無
暫無

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

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