Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2019
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.73 KB | None | 0 0
  1. #! /usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. from __future__ import division, print_function, absolute_import
  5.  
  6. import os
  7. from timeit import time
  8. import warnings
  9. import sys
  10. import cv2
  11. import numpy as np
  12. from PIL import Image
  13. from yolo import YOLO
  14.  
  15. from deep_sort import preprocessing
  16. from deep_sort import nn_matching
  17. from deep_sort.detection import Detection
  18. from deep_sort.tracker import Tracker
  19. from tools import generate_detections as gdet
  20. from deep_sort.detection import Detection as ddet
  21. warnings.filterwarnings('ignore')
  22.  
  23.  
  24. input=sys.argv[1]
  25. output=sys.argv[2]
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34. def main(yolo):
  35.  
  36. # Definition of the parameters
  37. max_cosine_distance = 0.3
  38. nn_budget = None
  39. nms_max_overlap = 1.0
  40.  
  41. # deep_sort
  42. model_filename = 'model_data/mars-small128.pb'
  43. encoder = gdet.create_box_encoder(model_filename,batch_size=1)
  44.  
  45. metric = nn_matching.NearestNeighborDistanceMetric("cosine", max_cosine_distance, nn_budget)
  46. tracker = Tracker(metric)
  47.  
  48. writeVideo_flag = True
  49.  
  50. video_capture = cv2.VideoCapture(input)
  51.  
  52. if writeVideo_flag:
  53. # Define the codec and create VideoWriter object
  54. w = int(video_capture.get(3))
  55. h = int(video_capture.get(4))
  56. fourcc = cv2.VideoWriter_fourcc(*'MJPG')
  57. out = cv2.VideoWriter(output, fourcc, 23, (w, h))
  58. list_file = open('detection.txt', 'w')
  59. frame_index = -1
  60.  
  61. fps = 0.0
  62. while True:
  63. ret, frame = video_capture.read() # frame shape 640*480*3
  64. if ret != True:
  65. break;
  66. t1 = time.time()
  67.  
  68. image = Image.fromarray(frame)
  69. boxs = yolo.detect_image(image)
  70. # print("box_num",len(boxs))
  71. features = encoder(frame,boxs)
  72.  
  73. # score to 1.0 here).
  74. detections = [Detection(bbox, 1.0, feature) for bbox, feature in zip(boxs, features)]
  75.  
  76. # Run non-maxima suppression.
  77. boxes = np.array([d.tlwh for d in detections])
  78. scores = np.array([d.confidence for d in detections])
  79. indices = preprocessing.non_max_suppression(boxes, nms_max_overlap, scores)
  80. detections = [detections[i] for i in indices]
  81.  
  82. # Call the tracker
  83. tracker.predict()
  84. tracker.update(detections)
  85.  
  86. for track in tracker.tracks:
  87. if not track.is_confirmed() or track.time_since_update > 1:
  88. continue
  89. bbox = track.to_tlbr()
  90. cv2.rectangle(frame, (int(bbox[0]), int(bbox[1])), (int(bbox[2]), int(bbox[3])),(255,255,255), 2)
  91. cv2.putText(frame, str(track.track_id),(int(bbox[0]), int(bbox[1])),0, 5e-3 * 200, (0,255,0),2)
  92.  
  93. for det in detections:
  94. bbox = det.to_tlbr()
  95. cv2.rectangle(frame,(int(bbox[0]), int(bbox[1])), (int(bbox[2]), int(bbox[3])),(255,0,0), 2)
  96.  
  97. # cv2.imshow('', frame)
  98.  
  99. if writeVideo_flag:
  100. # save a frame
  101. out.write(frame)
  102. frame_index = frame_index + 1
  103. list_file.write(str(frame_index)+' ')
  104. if len(boxs) != 0:
  105. for i in range(0,len(boxs)):
  106. list_file.write(str(boxs[i][0]) + ' '+str(boxs[i][1]) + ' '+str(boxs[i][2]) + ' '+str(boxs[i][3]) + ' ')
  107. list_file.write('\n')
  108.  
  109. fps = ( fps + (1./(time.time()-t1)) ) / 2
  110. print("fps= %f"%(fps))
  111.  
  112. # Press Q to stop!
  113. if cv2.waitKey(1) & 0xFF == ord('q'):
  114. break
  115.  
  116. video_capture.release()
  117. if writeVideo_flag:
  118. out.release()
  119. list_file.close()
  120. cv2.destroyAllWindows()
  121.  
  122. if __name__ == '__main__':
  123. main(YOLO())
  124. mgr@ui: ~/track/yolo_qdian/deep_sort_yolov3 $
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement