Pytorch实现yolov3(基础款)

Pytorch实现yolov3

Author
Lux Liang

环境配置

  • Win10
  • i7-8565U 16G
  • pytorch1.4.0、torchvision
  • numpy、matplotlib、opencv、pandas

相关资料

  • Github仓库(包含除yolov3.weights之外的必要部分)
  • yolov3.weights:
    链接https://pan.baidu.com/s/1ejsWIWlP9Zn8ZNXnp5iAQw 提取码:ymdp
  • YOLOv3论文:https://pjreddie.com/media/files/papers/YOLOv3.pdf

实现

将上述github中的代码和yolov3权重下载好后,将weights放在与cfg、data、imgs同级的目录下,然后打开detect.py运行即可,如果出现
Pytorch实现yolov3(基础款)_第1张图片
则说明成功
另外如果想实现视频检测或实时检测,我们可如下修改代码:
打开Video_demo.py,如下增加部分代码(其中filepath修改为自己的地址):

videofile = args.video
    
    cap = cv2.VideoCapture('filepath\\pytorch-yolo-v3-master\\Videos\\Video1.mp4') #(0) #(videofile)
    
    assert cap.isOpened(), 'Cannot capture source'
    
    frames = 0
    start = time.time()
    fourcc = cv2.VideoWriter_fourcc('M', 'J', 'P', 'G')#四字符代码,表示视频格式
    fps = 60# 影响输出Video的帧数
    savedPath = 'filepath\\pytorch-yolo-v3-master\\Videos\\saveVideo.avi'  # 保存地址
    ret, frame = cap.read()
    videoWriter = cv2.VideoWriter(savedPath, fourcc, fps, (frame.shape[1], frame.shape[0]))  # 最后为视频图片的形状

    while cap.isOpened():
        
        ret, frame = cap.read()
        if ret:
            

            img, orig_im, dim = prep_image(frame, inp_dim)
            
            im_dim = torch.FloatTensor(dim).repeat(1,2)                        
            
            
            if CUDA:
                im_dim = im_dim.cuda()
                img = img.cuda()
            
            with torch.no_grad():   
                output = model(Variable(img), CUDA)
            output = write_results(output, confidence, num_classes, nms = True, nms_conf = nms_thesh)

            if type(output) == int:
                frames += 1
                print("FPS of the video is {:5.2f}".format( frames / (time.time() - start)))
                cv2.imshow("frame", orig_im)
                key = cv2.waitKey(1)
                c = cv2.waitKey(30) & 0xff
                if key & 0xFF == 27: # ord('q'):
                    break
                continue

你可能感兴趣的:(Pytorch实现yolov3(基础款))