mmdetection2.x windows下 PermissionError:

mmcv造成的shutil.copyfile(),复制文件,在复制的时候目标 文件是不能存在的,但是打印发现复制之前.py就已经存在。先将.py删掉,然后再复制就能出结果了。

可以先打印一下文件夹下存在的文件:

mmdetection2.x windows下 PermissionError:_第1张图片

修改:

                temp_config_file = tempfile.NamedTemporaryFile(
                    dir=temp_config_dir, suffix='.py')
                temp_config_name = osp.basename(temp_config_file.name)
                
                if os.path.exists(osp.join(temp_config_dir, temp_config_name)):
                    os.remove(osp.join(temp_config_dir, temp_config_name))

                shutil.copyfile(filename,
                                osp.join(temp_config_dir, temp_config_name))

测试:

import time
import cv2
import os
import numpy as np
from mmdet.apis import init_detector, inference_detector
from mmdet.datasets.voc import VOCDataset

s=time.time()
class_data = VOCDataset
class_dic = class_data.CLASSES
class_color = [(0, 255, 0), (0, 0, 255), (255, 0, 255), (0, 255, 255), (255, 255, 0), ]

config_file = r'./configs\faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py'
checkpoint_file = r'D:\0_code\2_highmm\tools/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth'
result_path = "./result/"
img_path = r'D:\0_code\2_highmm/'

if not os.path.exists(result_path):
    os.makedirs(result_path)
model = init_detector(config_file, checkpoint_file)

print("init_detector_use time :", time.time()-s)

filelist = os.listdir(img_path)
for item in filelist:
    if item.endswith('.jpg'):
        img = img_path+item
        print("------")
        print(item)
        img = cv2.imread(img)
        star = time.time()
        result = inference_detector(model, img)
        for i in range(len(class_dic)):
            sores = result[i][:, -1]
            if len(sores) > 0:
                for j in range(len(sores)):
                    score_ = round(sores[j], 3)
                    if score_ > 0.2:
                        # print("图像:", item, "第 ", i, "类, 得分: ", sores[j])
                        label_text = class_dic[i] + "::"+str(score_)
                        bboxes = result[i][j, :-1]
                        # print(bboxes)
                        bbox_int = bboxes.astype(np.int32)
                        left_top = (bbox_int[0], bbox_int[1])
                        right_bottom = (bbox_int[2], bbox_int[3])
                        cv2.rectangle(img, left_top, right_bottom, color=class_color[1],thickness=2)
                        cv2.putText(img, label_text, (bbox_int[0], bbox_int[1] - 4),
                                    cv2.FONT_HERSHEY_COMPLEX, 0.8, class_color[1])
            else:
                continue
        cv2.imwrite(result_path+item, img)
        cv2.imshow("result: ", img)
        cv2.waitKey(100000)
        print("Test one pic use time:", time.time()-star)

print("time", time.time()-s)

结果

mmdetection2.x windows下 PermissionError:_第2张图片

你可能感兴趣的:(小故障,pytorch,目标检测)