【问题标题】:IndentationError: unindent does not match any outer indentation level error in pythonIndentationError: unindent 不匹配 python 中的任何外部缩进级别错误
【发布时间】:2018-11-22 03:37:45
【问题描述】:
# USAGE
# python real_time_object_detection.py --prototxt MobileNetSSD_deploy.prototxt.txt --model MobileNetSSD_deploy.caffemodel
# import the necessary packages
import win32api
import keyboard as keyboard
import pygame as pygame
import pythoncom
import win32con
from PIL import ImageGrab
from imutils.video import VideoStream
from imutils.video import FPS
import numpy as np
import argparse
import imutils
import time
import cv2
import pyautogui
# construct the argument parse and parse the arguments
from keyboard._mouse_event import RIGHT
ap = argparse.ArgumentParser()
ap.add_argument("-p", "--prototxt", required=False,
                help="path to Caffe 'deploy' prototxt file")
ap.add_argument("-m", "--model", required=False,
                help="path to Caffe pre-trained model")
ap.add_argument("-c", "--confidence", type=float, default=0.6,
                help="minimum probability to filter weak detections")
args = vars(ap.parse_args())
prott1 = r'C:\Users\ianyy\Documents\MobileNetSSD_deploy.prototxt.txt'
prott2 = r'C:\Users\ianyy\Documents\MobileNetSSD_deploy.caffemodel'
# initialize the list of class labels MobileNet SSD was trained to
# detect, then generate a set of bounding box colors for each class
CLASSES = ["background", "aeroplane", "bicycle", "bird", "boat",
           "bottle", "bus", "car", "cat", "chair", "cow", "diningtable",
           "dog", "horse", "motorbike", "person", "pottedplant", "sheep",
           "sofa", "train", "tvmonitor"]
COLORS = np.random.uniform(0, 255, size=(len(CLASSES), 3))
# load our serialized model from disk
print("[INFO] loading model...")
net = cv2.dnn.readNetFromCaffe(prott1, prott2)
# initialize the video stream, allow the cammera sensor to warmup,
# and initialize the FPS counter
print("[INFO] starting video stream...")
#vs = VideoStream(src=0).start()
#time.sleep(2.0)
#fps = FPS().start()
# loop over the frames from the video stream
HSX = 100;
LSX = 1000;
HSY = 100;
LSY = 1000;
HEX = 100;
LEX = 1000;
HEY = 100;
LEY = 1000;
while True:
    # grab the frame from the threaded video stream and resize it
    # to have a maximum width of 400 pixels
   frame = np.array(ImageGrab.grab(bbox=(0, 40, 1820, 1240)))
    # frame = imutils.resize(frame, width=400)
    # grab the frame dimensions and convert it to a blob
    (h, w) = frame.shape[:2]
    blob = cv2.dnn.blobFromImage(cv2.resize(frame, (300, 300)),
                                 0.007843, (300, 300), 127.5)
    # pass the blob through the network and obtain the detections and
    # predictions
    net.setInput(blob)
    detections = net.forward()
    # loop over the detections
    for i in np.arange(0, detections.shape[2]):
        # extract the confidence (i.e., probability) associated with
        # the prediction
        confidence = detections[0, 0, i, 2]
       # filter out weak detections by ensuring the `confidence` is
        # greater than the minimum confidence
        if confidence > args["confidence"]:
            # extract the index of the class label from the
            # `detections`, then compute the (x, y)-coordinates of
            # the bounding box for the object
            idx = int(detections[0, 0, i, 1])
            box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
            (startX, startY, endX, endY) = box.astype("int")
           # draw the prediction on the frame
           label = "{}: {:.2f}%".format(CLASSES[idx],
                                        confidence * 100)    cv2.rectangle(frame, (startX, startY), (endX, endY),
                          COLORS[idx], 2)
            y = startY - 15 if startY - 15 > 15 else startY + 15
            cv2.putText(frame, label, (startX, y),
                        cv2.FONT_HERSHEY_SIMPLEX, 0.5, COLORS[idx], 2)
           if 'person' in label:
                pygame.init()
                pygame.event.get()
                if pygame.mouse.get_pressed():
                    print 'pressing'
                    #tried to detect my character's offset and add the best way to exclude it, failed most tests.
                    if startX > 369 & startX < 1402 & startY > -1 & startY < 725 & endX > 339 & endX < 1805 & endY > 806 & endY < 1017:
                        print 'found myself'
                    else:
                        #print 'found somebody else'
                        nosum = int(round(startX * 1)) + int(round(startX * 0.06))
                        nosum2 = int(round(y * 1)) + int(round(y * 0.7))
                        halfX = (endX - startX) / 2
                        halfY = (endY - startY) / 2
                        finalX = startX + halfX
                        finalY = startY + halfY
                    #    pyautogui.moveTo(finalX, finalY)
                        #win32api.SetCursorPos((finalX, finalY))
                    #    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, finalX, finalY, 0, 0)
                    #    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, finalX, finalY, 0, 0)
                        #print 'Pressed L'
                    if 'HSX' not in locals():
                        HSX = startX
                    if 'LSX' not in locals():
                       LSX = startX
                    if 'HSY' not in locals():
                        HSY = startY
                   if 'LSY' not in locals():
                       LSY = startY
                    if 'HEX' not in locals():
                        HEX = endX
                    if 'LEX' not in locals():
                        LEX = endX
                    if 'HEY' not in locals():
                        HEY = endY
                    if 'LEY' not in locals():
                        LEY = endY
                    if startX > HSX:
                        HSX = startX
                    if startX < LSX:
                        LSX = startX
                    if startY > HSY:
                        HSY = startY
                    if startY < LSY:
                        LSY = startY
                    if endX > HEX:
                        HEX = endX
                    if endX < LEX:
                        LEX = endX
                    if endY > HEY:
                        HEY = endY
                    if endY < LEY:
                        LEY = endY
                    print 'LStartX: ' + str(LSX)
                    print 'HStartY: ' + str(HSY)
                    print 'LStartY: ' + str(LSY)
                    print 'HendX: ' + str(HEX)
                    print 'LendX: ' + str(LEX)
                    print 'HendY: ' + str(HEY)
                    print 'LendY: ' + str(LEY)
                #print args["confidence"]
#             click(10,10)
   # show the output frame
    cv2.imshow("Frame", frame)
    key = cv2.waitKey(1) & 0xFF
    # if the `k` key was pressed, break from the loop
    if key == ord("k"):
       break
    # update the FPS counter
# stop the timer and display FPS information
# do a bit of cleanup
cv2.destroyAllWindows()

我得到了这段代码,但我不确定它有什么问题我得到了 IndentationError: unindent does not match any external indentation level error 我读到这与间距和制表符之类的东西有关 我不是可以肯定,但我看不出有什么问题,请帮忙。我不确定我需要添加哪些其他细节,因为这是我能想到的所有需要​​您提供帮助的信息,如果您需要更多详细信息,请告诉我,我会尽力告诉您。

【问题讨论】:

    标签: python


    【解决方案1】:

    在这部分代码中,label = ... 行仅缩进三个空格,而其所有相邻行均缩进四个空格。

    if confidence > args["confidence"]:
        # extract the index of the class label from the
        # `detections`, then compute the (x, y)-coordinates of
        # the bounding box for the object
        idx = int(detections[0, 0, i, 1])
        box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
        (startX, startY, endX, endY) = box.astype("int")
       # draw the prediction on the frame
       label = "{}: {:.2f}%".format(CLASSES[idx],
                                    confidence * 100)    cv2.rectangle(frame, (startX, startY), (endX, endY),
                      COLORS[idx], 2)
        y = startY - 15 if startY - 15 > 15 else startY + 15
        cv2.putText(frame, label, (startX, y),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.5, COLORS[idx], 2)
    

    【讨论】:

      【解决方案2】:

      您上面的代码有多个不一致的缩进(请参阅代码中的我的 cmets),应该有一个一致的空格/制表符用于缩进您的代码。 你也可以看看这个:How to fix Python indentation

      您还可以使用可以帮助您轻松指出缩进错误的编辑器。我个人在用 python 编码时使用 PyCharm,因为它对 PEP 标准有很好的支持。

      # USAGE
      # python real_time_object_detection.py --prototxt MobileNetSSD_deploy.prototxt.txt --model MobileNetSSD_deploy.caffemodel
      # import the necessary packages
      import win32api
      import keyboard as keyboard
      import pygame as pygame
      import pythoncom
      import win32con
      from PIL import ImageGrab
      from imutils.video import VideoStream
      from imutils.video import FPS
      import numpy as np
      import argparse
      import imutils
      import time
      import cv2
      import pyautogui
      # construct the argument parse and parse the arguments
      from keyboard._mouse_event import RIGHT
      ap = argparse.ArgumentParser()
      ap.add_argument("-p", "--prototxt", required=False,
                      help="path to Caffe 'deploy' prototxt file")
      ap.add_argument("-m", "--model", required=False,
                      help="path to Caffe pre-trained model")
      ap.add_argument("-c", "--confidence", type=float, default=0.6,
                      help="minimum probability to filter weak detections")
      args = vars(ap.parse_args())
      prott1 = r'C:\Users\ianyy\Documents\MobileNetSSD_deploy.prototxt.txt'
      prott2 = r'C:\Users\ianyy\Documents\MobileNetSSD_deploy.caffemodel'
      # initialize the list of class labels MobileNet SSD was trained to
      # detect, then generate a set of bounding box colors for each class
      CLASSES = ["background", "aeroplane", "bicycle", "bird", "boat",
                 "bottle", "bus", "car", "cat", "chair", "cow", "diningtable",
                 "dog", "horse", "motorbike", "person", "pottedplant", "sheep",
                 "sofa", "train", "tvmonitor"]
      COLORS = np.random.uniform(0, 255, size=(len(CLASSES), 3))
      # load our serialized model from disk
      print("[INFO] loading model...")
      net = cv2.dnn.readNetFromCaffe(prott1, prott2)
      # initialize the video stream, allow the cammera sensor to warmup,
      # and initialize the FPS counter
      print("[INFO] starting video stream...")
      #vs = VideoStream(src=0).start()
      #time.sleep(2.0)
      #fps = FPS().start()
      # loop over the frames from the video stream
      HSX = 100;
      LSX = 1000;
      HSY = 100;
      LSY = 1000;
      HEX = 100;
      LEX = 1000;
      HEY = 100;
      LEY = 1000;
      while True:
          # grab the frame from the threaded video stream and resize it
          # to have a maximum width of 400 pixels
          frame = np.array(ImageGrab.grab(bbox=(0, 40, 1820, 1240)))  # corrected indent
          # frame = imutils.resize(frame, width=400)
          # grab the frame dimensions and convert it to a blob
          (h, w) = frame.shape[:2]
          blob = cv2.dnn.blobFromImage(cv2.resize(frame, (300, 300)),
                                       0.007843, (300, 300), 127.5)
          # pass the blob through the network and obtain the detections and
          # predictions
          net.setInput(blob)
          detections = net.forward()
          # loop over the detections
          for i in np.arange(0, detections.shape[2]):
              # extract the confidence (i.e., probability) associated with
              # the prediction
              confidence = detections[0, 0, i, 2]
              # filter out weak detections by ensuring the `confidence` is
              # greater than the minimum confidence
              if confidence > args["confidence"]:
                  # extract the index of the class label from the
                  # `detections`, then compute the (x, y)-coordinates of
                  # the bounding box for the object
                  idx = int(detections[0, 0, i, 1])
                  box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
                  (startX, startY, endX, endY) = box.astype("int")
                  # draw the prediction on the frame
                  label = "{}: {:.2f}%".format(CLASSES[idx], confidence * 100)  # inconsistent indentation here
                  cv2.rectangle(frame, (startX, startY), (endX, endY), COLORS[idx], 2)  # and also here
                  y = startY - 15 if startY - 15 > 15 else startY + 15
                  cv2.putText(frame, label, (startX, y),
                              cv2.FONT_HERSHEY_SIMPLEX, 0.5, COLORS[idx], 2)
                  if 'person' in label:  # another here
                      pygame.init()
                      pygame.event.get()
                      if pygame.mouse.get_pressed():
                          print 'pressing'
                          #tried to detect my character's offset and add the best way to exclude it, failed most tests.
                          if startX > 369 & startX < 1402 & startY > -1 & startY < 725 & endX > 339 & endX < 1805 & endY > 806 & endY < 1017:
                              print 'found myself'
                          else:
                              #print 'found somebody else'
                              nosum = int(round(startX * 1)) + int(round(startX * 0.06))
                              nosum2 = int(round(y * 1)) + int(round(y * 0.7))
                              halfX = (endX - startX) / 2
                              halfY = (endY - startY) / 2
                              finalX = startX + halfX
                              finalY = startY + halfY
                          #    pyautogui.moveTo(finalX, finalY)
                              #win32api.SetCursorPos((finalX, finalY))
                          #    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, finalX, finalY, 0, 0)
                          #    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, finalX, finalY, 0, 0)
                              #print 'Pressed L'
                          if 'HSX' not in locals():
                              HSX = startX
                          if 'LSX' not in locals():
                             LSX = startX
                          if 'HSY' not in locals():
                              HSY = startY
                          if 'LSY' not in locals():  # another here
                              LSY = startY
                          if 'HEX' not in locals():
                              HEX = endX
                          if 'LEX' not in locals():
                              LEX = endX
                          if 'HEY' not in locals():
                              HEY = endY
                          if 'LEY' not in locals():
                              LEY = endY
                          if startX > HSX:
                              HSX = startX
                          if startX < LSX:
                              LSX = startX
                          if startY > HSY:
                              HSY = startY
                          if startY < LSY:
                              LSY = startY
                          if endX > HEX:
                              HEX = endX
                          if endX < LEX:
                              LEX = endX
                          if endY > HEY:
                              HEY = endY
                          if endY < LEY:
                              LEY = endY
                          print 'LStartX: ' + str(LSX)
                          print 'HStartY: ' + str(HSY)
                          print 'LStartY: ' + str(LSY)
                          print 'HendX: ' + str(HEX)
                          print 'LendX: ' + str(LEX)
                          print 'HendY: ' + str(HEY)
                          print 'LendY: ' + str(LEY)
                      #print args["confidence"]
      #             click(10,10)
         # show the output frame
          cv2.imshow("Frame", frame)
          key = cv2.waitKey(1) & 0xFF
          # if the `k` key was pressed, break from the loop
          if key == ord("k"):
              break  # and another here
          # update the FPS counter
      # stop the timer and display FPS information
      # do a bit of cleanup
      cv2.destroyAllWindows()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-10-04
        • 1970-01-01
        相关资源
        最近更新 更多