Python3.6.4缩进错误及人脸识别代码运行异常求助
Fixing the "unindent does not match any outer indentation level" Error in Your Face Recognition Code
Hey there, that unindent does not match any outer indentation level error is super common in Python—it’s all about messed-up indentation (Python takes this very seriously!). Let’s break down what’s wrong with your code and fix it step by step:
Key Issues in Your Original Code
- Malformed import statements: You tried to cram multiple
importcommands onto a single line, which isn’t valid Python syntax. - Incorrect indentation: The code inside your
getImagesWithIDfunction wasn’t properly indented—Python requires consistent indentation (stick with 4 spaces, never mix spaces and tabs!). - Python2 vs Python3
printsyntax:print imagePathsis Python2 syntax; in Python3,printis a function, so you need parentheses:print(imagePaths). - Function name typo: You defined
getImagesWithIDbut tried to callgetImageWithID(missing ansat the end).
Corrected Code
import os import cv2 import numpy as np from PIL import Image recognizer = cv2.face.LBPHFaceRecognizer_create() path = 'dataSet' def getImagesWithID(path): # Properly indent the function body with 4 spaces imagePaths = [os.path.join(path, f) for f in os.listdir(path)] print(imagePaths) # Python3-compliant print function syntax # Call the correctly named function getImagesWithID(path)
Quick Tips to Avoid This Error Later
- Always use 4 spaces for indentation (most code editors let you set Tab to insert 4 spaces automatically).
- Turn on "show whitespace" in your editor to spot mixed spaces/tabs easily—this is a top culprit for indent errors.
- Double-check function names when calling them—typos are easy to miss but break code fast!
- Keep Python3 syntax rules in mind (like
print()as a function) since you’re running Python 3.6.4.
内容的提问来源于stack exchange,提问作者Muhammad Adnan
相关产品推荐
相关产品推荐

