demo函数

def demo(opt):
  os.environ['CUDA_VISIBLE_DEVICES'] = opt.gpus_str     # 指定gpu的id号
  opt.debug = max(opt.debug, 1)													#  选择可视化的等级1,2,3,4
  detector = Detector(opt)																#  检测器对象

  if opt.demo == 'webcam' or \														#  判断输入的是video还是webcam
    opt.demo[opt.demo.rfind('.') + 1:].lower() in video_ext:
    is_video = True																				 # vedio
    # demo on video stream
    cam = cv2.VideoCapture(0 if opt.demo == 'webcam' else opt.demo)
  else:
    is_video = False																			# webcam
    # Demo on images sequences
    if os.path.isdir(opt.demo):														# 判断输入的是文件夹还是图片
      image_names = []
      ls = os.listdir(opt.demo)
      for file_name in sorted(ls):
          ext = file_name[file_name.rfind('.') + 1:].lower()
          if ext in image_ext:
              image_names.append(os.path.join(opt.demo, file_name))		# 输入为图片序列
    else:
      image_names = [opt.demo]

  # Initialize output video
  out = None
  out_name = opt.demo[opt.demo.rfind('/') + 1:]
  if opt.save_video:																							#  输出结果是否保存为视频
    fourcc = cv2.VideoWriter_fourcc(*'XVID')
    out = cv2.VideoWriter('../results/{}.mp4'.format(							#  初始化输出的视频结果
      opt.exp_id + '_' + out_name),fourcc, opt.save_framerate, (
        opt.video_w, opt.video_h))
  
  if opt.debug < 5:
    detector.pause = False
  cnt = 0															# cnt记录读取第几帧,每次循环加一
  results = {}														#  结果保存到results

  while True:
      if is_video:											# 输入为视频序列,每次读取一帧到img
        _, img = cam.read()
        if img is None:
          save_and_exit(opt, out, results, out_name)
      else:												 		# 读取输入的图片序列,每次读取一帧到img
        if cnt < len(image_names):
          img = cv2.imread(image_names[cnt])
        else:
          save_and_exit(opt, out, results, out_name)
      cnt += 1												

      # resize the original video for saving video results
      if opt.resize_video:										# resize输入的图片
        img = cv2.resize(img, (opt.video_w, opt.video_h))

      # skip the first X frames of the video
      if cnt < opt.skip_first:
        continue
      
      cv2.imshow('input', img)								# 显示本帧图片

      # track or detect the image.
      ret = detector.run(img)									#  使用检测器对本帧图片检测,返回到ret中

      # log run time
      time_str = 'frame {} |'.format(cnt)
      for stat in time_stats:
        time_str = time_str + '{} {:.3f}s |'.format(stat, ret[stat])
      print(time_str)

      # results[cnt] is a list of dicts:
      #  [{'bbox': [x1, y1, x2, y2], 'tracking_id': id, 'category_id': c, ...}]
      results[cnt] = ret['results']

      # save debug image to video
      if opt.save_video:
        out.write(ret['generic'])
        if not is_video:
          cv2.imwrite('../results/demo{}.jpg'.format(cnt), ret['generic'])
      
      # esc to quit and finish saving video
      if cv2.waitKey(1) == 27:
        save_and_exit(opt, out, results, out_name)
        return 
  save_and_exit(opt, out, results)
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值