tracker.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. from deep_sort.utils.parser import get_config
  2. from deep_sort.deep_sort import DeepSort
  3. import torch
  4. import cv2
  5. import logging
  6. logging.basicConfig(filename='detection_log.txt', level=logging.INFO,
  7. format='%(asctime)s - %(message)s')
  8. palette = (2 ** 11 - 1, 2 ** 15 - 1, 2 ** 20 - 1)
  9. cfg = get_config()
  10. cfg.merge_from_file("deep_sort/configs/deep_sort.yaml")
  11. deepsort = DeepSort(cfg.DEEPSORT.REID_CKPT,
  12. max_dist=cfg.DEEPSORT.MAX_DIST, min_confidence=cfg.DEEPSORT.MIN_CONFIDENCE,
  13. nms_max_overlap=cfg.DEEPSORT.NMS_MAX_OVERLAP, max_iou_distance=cfg.DEEPSORT.MAX_IOU_DISTANCE,
  14. max_age=cfg.DEEPSORT.MAX_AGE, n_init=cfg.DEEPSORT.N_INIT, nn_budget=cfg.DEEPSORT.NN_BUDGET,
  15. use_cuda=True)
  16. def plot_bboxes(image, bboxes, line_thickness=None):
  17. # Plots one bounding box on image img
  18. tl = line_thickness or round(0.002 * (image.shape[0] + image.shape[1]) / 2) + 1 # line/font thickness
  19. for (x1, y1, x2, y2, cls_id, pos_id) in bboxes:
  20. if cls_id == 'person':
  21. color = (0, 0, 255)
  22. # 输出日志信息
  23. logging.info(f'Detected: {cls_id} ID-{pos_id}') # 记录检测到的类别和ID
  24. else:
  25. color = (0, 255, 0)
  26. c1, c2 = (x1, y1), (x2, y2)
  27. cv2.rectangle(image, c1, c2, color, thickness=tl, lineType=cv2.LINE_AA)
  28. tf = max(tl - 1, 1) # font thickness
  29. t_size = cv2.getTextSize(cls_id, 0, fontScale=tl / 3, thickness=tf)[0]
  30. c2 = c1[0] + t_size[0], c1[1] - t_size[1] - 3
  31. cv2.rectangle(image, c1, c2, color, -1, cv2.LINE_AA) # filled
  32. cv2.putText(image, '{} ID-{}'.format(cls_id, pos_id), (c1[0], c1[1] - 2), 0, tl / 3,
  33. [225, 255, 255], thickness=tf, lineType=cv2.LINE_AA)
  34. return image
  35. def update_tracker(target_detector, image):
  36. new_faces = []
  37. _, bboxes = target_detector.detect(image)
  38. bbox_xywh = []
  39. confs = []
  40. clss = []
  41. for x1, y1, x2, y2, cls_id, conf in bboxes:
  42. obj = [
  43. int((x1+x2)/2), int((y1+y2)/2),
  44. x2-x1, y2-y1
  45. ]
  46. bbox_xywh.append(obj)
  47. confs.append(conf)
  48. clss.append(cls_id)
  49. xywhs = torch.Tensor(bbox_xywh)
  50. confss = torch.Tensor(confs)
  51. outputs = deepsort.update(xywhs, confss, clss, image)
  52. bboxes2draw = []
  53. face_bboxes = []
  54. current_ids = []
  55. for value in list(outputs):
  56. x1, y1, x2, y2, cls_, track_id = value
  57. bboxes2draw.append(
  58. (x1, y1, x2, y2, cls_, track_id)
  59. )
  60. current_ids.append(track_id)
  61. if cls_ == 'face':
  62. if not track_id in target_detector.faceTracker:
  63. target_detector.faceTracker[track_id] = 0
  64. face = image[y1:y2, x1:x2]
  65. new_faces.append((face, track_id))
  66. face_bboxes.append(
  67. (x1, y1, x2, y2)
  68. )
  69. ids2delete = []
  70. for history_id in target_detector.faceTracker:
  71. if not history_id in current_ids:
  72. target_detector.faceTracker[history_id] -= 1
  73. if target_detector.faceTracker[history_id] < -5:
  74. ids2delete.append(history_id)
  75. for ids in ids2delete:
  76. target_detector.faceTracker.pop(ids)
  77. print('-[INFO] Delete track id:', ids)
  78. image = plot_bboxes(image, bboxes2draw)
  79. return image, new_faces, face_bboxes