unindent不匹配任何外部缩进级别1

问题描述 投票:0回答:1

我正在使用python3来精简这段代码,但是每当这样做时,它都会给我错误

unindent与任何外部缩进级别都不匹配,我需要的以下代码是我的帮助

import torch
import torchvision
import cv2
import PIL
from torch.autograd import Variable
from data import BaseTransform, VOC_CLASSES as labelmap
from ssd import build_ssd
import imageio

def detect(frame, net, tansform):
      height, width = frame.shape[:2]
      frame_t =transform(frame)[0]
      x = torch.from_numpy(frame_t).permute(2, 0, 1)
      x = Variable(x.unsequeeze(0))
      y = net(x)
      detections = y.data
      scale = torch.Tensor([width, height, width, height])

      for 1 in range(detections.size(1)):
          j = 0
          while detections(0, i, j, 0) >=0.6:
              pt = (detections[0, i, j, 1:]*scale)
              cv2.rectangle(frame, (int(pt[0]), int(pt[1])), (int(pt[2]), int(pt[3])),(255, 0, 0), 2)
              cv2.putText(frame, labelmap[i - 1], (int(pt[0]), int(pt[1])), cv2.FONT_HERSHEY_SIMPLEX, 2, (255,255,255), 2, cv2.LINE_AA)
              j +=1
              return frame

            net = build_ssd('test')
            net.load_state_dict(torch.load('ssd300_mAP_77.43_v2.pth', map_location=lambda storage, loc: storage))

            transform = BaseTransform(net.size, (104/256.0,117/256.0,123/256.0))

                                reader=imageio.get_reader(funny_dog.mp4)
                                fps = reader.get_meta_data()['fps']
                                writer = imageio.get_writer('output.mp4',fps = fps)
            for i, frame in enumerate(reader):
                        frame = detect(frame, net.eval(), transform)
                    writer.append_data(frame)
                    print(i)
                writer.close()
python-3.x
1个回答
0
投票

这些行看起来好像缩进不正确:

net = build_ssd('test')
net.load_state_dict(torch.load('ssd300_mAP_77.43_v2.pth', map_location=lambda storage, loc: storage))

这里缩进的代码正确:

import torch
import torchvision
import cv2
import PIL
from torch.autograd import Variable
from data import BaseTransform, VOC_CLASSES as labelmap
from ssd import build_ssd
import imageio


def detect(frame, net, tansform):
    height, width = frame.shape[:2]
    frame_t = transform(frame)[0]
    x = torch.from_numpy(frame_t).permute(2, 0, 1)
    x = Variable(x.unsequeeze(0))
    y = net(x)
    detections = y.data
    scale = torch.Tensor([width, height, width, height])

    for 1 in range(detections.size(1)):
        j = 0
        while detections(0, i, j, 0) >= 0.6:
            pt = (detections[0, i, j, 1:] * scale)
            cv2.rectangle(frame, (int(pt[0]), int(pt[1])), (int(pt[2]), int(pt[3])), (255, 0, 0), 2)
            cv2.putText(frame, labelmap[i - 1], (int(pt[0]), int(pt[1])), cv2.FONT_HERSHEY_SIMPLEX, 2, (255, 255, 255),
                        2, cv2.LINE_AA)
            j += 1
            return frame

        net = build_ssd('test')
        net.load_state_dict(torch.load('ssd300_mAP_77.43_v2.pth', map_location=lambda storage, loc: storage))

        transform = BaseTransform(net.size, (104 / 256.0, 117 / 256.0, 123 / 256.0))

        reader = imageio.get_reader(funny_dog.mp4)
        fps = reader.get_meta_data()['fps']
        writer = imageio.get_writer('output.mp4', fps=fps)
    for i, frame in enumerate(reader):
        frame = detect(frame, net.eval(), transform)
    writer.append_data(frame)
    print(i)


writer.close()
© www.soinside.com 2019 - 2024. All rights reserved.