-2

When i run this scipt an error appears: IndentationError: unindent does not match any outer indentation level. I dont know what it exactly means but i´ve tried in different code editors like sublime or visual studio code and there is the same problem. I´ve never had this problem before. The code:

import cv2
import numpy as np

video = cv2.VideoCapture(0)

while True:

    check, frame = video.read()

    if not check:
        print("Camera doesn´t work")
        break

    pressed = cv2.waitKey(1)
    if pressed == ord("q"):
        break


    blur = cv2.GaussianBlur(frame, (21, 21), 0)
    hsv = cv2.cvtColor(blur, cv2.COLOR_BGR2HSV)
    lower = [18, 50, 50]
    upper = [35, 255, 255]
    lower = np.array(lower, dtype="uint8")
    upper = np.array(upper, dtype="uint8")
    mask = cv2.inRange(hsv, lower, upper)

    output = cv2.bitwise_and(frame,hsv, mask=mask)
    cv2.imshow("Frame", output)

video.release()

cv2.DestroyAllWindows()

Edit: There is a missing closed at the mask line and there is a line missing (import numpy as np), i made this error when I was copyng the code but any way the code gives me the same error.

Noel
  • 11
  • 3
  • 2
    You've mixed tabs and spaces. Don't do that. Find the "convert tabs to spaces" button in your editor and hit it. – user2357112 May 19 '20 at 21:12

4 Answers4

0

There is no closing bracket on the line 24

mask = cv2.inRange(hsv, lower, upper
UbadahJ
  • 46
  • 6
  • That's the next syntax error they would have run into, but the current syntax error is a mixed-tabs-and-spaces issue. – user2357112 May 19 '20 at 21:12
0

Python works different than most other languages. New blocks of code are dependend on indentation. That means, that code blocks that belong together have to be indented exactly the same - this also means the same amount of tabs OR spaces (in other languages this is usually achieved by brackets or statements).

You should decide for one method. Either use tabs or use spaces. I would prefer spaces here.

Rennnyyy
  • 86
  • 5
-1

There is no problem with indentation in the code you included. There is a missing left parenthesis in the line:

    mask = cv2.inRange(hsv, lower, upper

You also do not import the module np. You need to add to the beginning of you code:

 import numpy as np

After fixing these two issues, the code runs.

Amitai Irron
  • 1,973
  • 1
  • 12
  • 14
  • "There is no problem with indentation in the code you included." - there is. You don't see it because it's a mixed tabs and spaces issue. – user2357112 May 19 '20 at 21:14
  • I´ve got the same error – Noel May 19 '20 at 21:24
  • @user2357112supportsMonica - While the code in the OP's environment may have contained tabs, the code that was included in the question (when I saw it) could be copied as is into an editor, without any tabs. I later tried accessing the code through "edit" (which I could not, originally), and there it had tabs, but in the quoted code of the message, no tabs existed. – Amitai Irron May 19 '20 at 21:27
-1

I got this code working in python 3

import cv2
import numpy as np

video = cv2.VideoCapture(0)

while True:

    check, frame = video.read()

    if not check:
        print("Camera doesn´t work")
        break

    pressed = cv2.waitKey(1)
    if pressed == ord("q"):
        break

    blur = cv2.GaussianBlur(frame, (21, 21), 0)
    hsv = cv2.cvtColor(blur, cv2.COLOR_BGR2HSV)
    lower = [18, 50, 50]
    upper = [35, 255, 255]
    lower = np.array(lower, dtype="uint8")
    upper = np.array(upper, dtype="uint8")
    mask = cv2.inRange(hsv, lower, upper) # close the command 

    output = cv2.bitwise_and(frame, hsv, mask=mask)
    cv2.imshow("Frame", output)

video.release()

cv2.destroyAllWindows() # destroy with lower case 'd'. 

No indentation error at all. I highly suggest using a proper debugger like PyCharm when weird issues like this crop up

medic17
  • 415
  • 5
  • 16