Detector/DetectorWraper.py
def tracking(self, frameList, thres, relabel=False):
		'''
		Perform detection tracking.
		Input: the list the video frames frameList
		Output: Example:  1. Dict{'track 1':[obj_1, obj_3], 'track 2': [obj_2] ...}
		'''
		if relabel:
			print('detecting objects ...')
			for frame in frameList:
				self._get_positions(frame)
		track_obj = {}
		track_frame = {}
		num_track = 0
		left_over = []
		detected_frames = list(self.detection_frames.keys())

		print('start tracking ...')
		for i, frame in enumerate(detected_frames[:-1]):
			# extact frame image
			img1 = frameList[detected_frames[i]]
			img2 = frameList[detected_frames[i + 1]]
			# frame detections
			frame_1 = self.detection_frames[detected_frames[i]]
			frame_2 = self.detection_frames[detected_frames[i + 1]]
			# detection id
			detects_1 = list(frame_1.keys())
			detects_2 = list(frame_2.keys())

			# =============  frame wise compare stage (consectutive) =============
			# init a heap for later ranking
			HEAP = []
			# get only human left over
			only_human = []
			detects_1 += left_over
			for obj_1 in detects_1:
				max_score = -9999
				next_score = -9999
				pair = None
				ratio = None
				x1, y1, w1, h1, f, text1, _ = self.detection_index[obj_1]
				# pnly track human
				if text1 != 'face' and text1 != 'super woman':
					continue
				only_human.append(obj_1)
				for obj_2 in detects_2:
					x2, y2, w2, h2, _, text2, _ = self.detection_index[obj_2]
					# only track human
					if text2 != 'face' and text2 != 'super woman':
						continue

					patch_1 = img1[y1 : y1 + h1, x1 : x1 + w1, :]
					patch_2 = img2[y2 : y2 + h2, x2 : x2 + w2, :]
					score = self.similarity_score(patch_1, patch_2)

					if score > max_score:
						next_score = max_score
						max_score = score
						pair = obj_1, f, obj_2
						ratio = next_score / max_score
				# calculate min over max ratio	
				if ratio is None or ratio > thres:
					continue
				else: heapq.heappush(HEAP, (-max_score, (pair[0], pair[1], pair[2])))

			# =================  track adding stage  =================
			repeated_obj1 = []
			repeated_obj2 = []
			for _ in range(len(HEAP)):
				track = heapq.heappop(HEAP)
				obj_1, frame_idx, obj_2 = track[1]
				# if already paired, skip
				if obj_2 in repeated_obj2:
					continue

				repeated_obj1.append(obj_1)
				repeated_obj2.append(obj_2)

				isPreviousTracked = False
				for track_id in track_obj.keys():
					if obj_1 in track_obj[track_id]:
						track_obj[track_id].append(obj_2)
						x, y, w, h, f, t, _ = self.detection_index[obj_2]
						self.detection_index[obj_2] = (x, y, w, h, f, t, track_id)
						isPreviousTracked = True
						break
				if isPreviousTracked:
					continue
				# if no previous track, add new one
				track_obj[num_track] = [obj_1, obj_2]
				x1, y1, w1, h1, f1, t1, _ = self.detection_index[obj_1]
				x2, y2, w2, h2, f2, t2, _ = self.detection_index[obj_2]
				self.detection_index[obj_1] = (x1, y1, w1, h1, f1, t1, num_track)
				self.detection_index[obj_2] = (x2, y2, w2, h2, f2, t2, num_track)
				num_track += 1

			left_over = [obj for obj in only_human if obj not in repeated_obj1]
			print(len(left_over))

		self.track_obj = track_obj
		print('saving annotations ...')
		self._annotate_images(frameList)
		print('Done !')

		Perform detection tracking.
		Input: the list the video frames frameList
		Output: Example:  1. Dict{'track 1':[obj_1, obj_3], 'track 2': [obj_2] ...}
		
Perform detection tracking.
Input: the list the video frames frameList
Output: Example:  1.
Perform detection tracking . Input : the list the video frames frameList Output : Example : 1 .
def tracking(self, frameList, thres, relabel=False):
		if relabel:
			print('detecting objects ...')
			for frame in frameList:
				self._get_positions(frame)
		track_obj = {}
		track_frame = {}
		num_track = 0
		left_over = []
		detected_frames = list(self.detection_frames.keys())
		print('start tracking ...')
		for i, frame in enumerate(detected_frames[:-1]):
			img1 = frameList[detected_frames[i]]
			img2 = frameList[detected_frames[i + 1]]
			frame_1 = self.detection_frames[detected_frames[i]]
			frame_2 = self.detection_frames[detected_frames[i + 1]]
			detects_1 = list(frame_1.keys())
			detects_2 = list(frame_2.keys())
			HEAP = []
			only_human = []
			detects_1 += left_over
			for obj_1 in detects_1:
				max_score = -9999
				next_score = -9999
				pair = None
				ratio = None
				x1, y1, w1, h1, f, text1, _ = self.detection_index[obj_1]
				if text1 != 'face' and text1 != 'super woman':
					continue
				only_human.append(obj_1)
				for obj_2 in detects_2:
					x2, y2, w2, h2, _, text2, _ = self.detection_index[obj_2]
					if text2 != 'face' and text2 != 'super woman':
						continue
					patch_1 = img1[y1 : y1 + h1, x1 : x1 + w1, :]
					patch_2 = img2[y2 : y2 + h2, x2 : x2 + w2, :]
					score = self.similarity_score(patch_1, patch_2)
					if score > max_score:
						next_score = max_score
						max_score = score
						pair = obj_1, f, obj_2
						ratio = next_score / max_score
				if ratio is None or ratio > thres:
					continue
				else: heapq.heappush(HEAP, (-max_score, (pair[0], pair[1], pair[2])))
			repeated_obj1 = []
			repeated_obj2 = []
			for _ in range(len(HEAP)):
				track = heapq.heappop(HEAP)
				obj_1, frame_idx, obj_2 = track[1]
				if obj_2 in repeated_obj2:
					continue
				repeated_obj1.append(obj_1)
				repeated_obj2.append(obj_2)
				isPreviousTracked = False
				for track_id in track_obj.keys():
					if obj_1 in track_obj[track_id]:
						track_obj[track_id].append(obj_2)
						x, y, w, h, f, t, _ = self.detection_index[obj_2]
						self.detection_index[obj_2] = (x, y, w, h, f, t, track_id)
						isPreviousTracked = True
						break
				if isPreviousTracked:
					continue
				track_obj[num_track] = [obj_1, obj_2]
				x1, y1, w1, h1, f1, t1, _ = self.detection_index[obj_1]
				x2, y2, w2, h2, f2, t2, _ = self.detection_index[obj_2]
				self.detection_index[obj_1] = (x1, y1, w1, h1, f1, t1, num_track)
				self.detection_index[obj_2] = (x2, y2, w2, h2, f2, t2, num_track)
				num_track += 1
			left_over = [obj for obj in only_human if obj not in repeated_obj1]
			print(len(left_over))
		self.track_obj = track_obj
		print('saving annotations ...')
		self._annotate_images(frameList)
		print('Done !')
def tracking ( self , frameList , thres , relabel = False ) : if relabel : print ( 'detecting objects ...' ) for frame in frameList : self . _get_positions ( frame ) track_obj = { } track_frame = { } num_track = 0 left_over = [ ] detected_frames = list ( self . detection_frames . keys ( ) ) print ( 'start tracking ...' ) for i , frame in enumerate ( detected_frames [ : - 1 ] ) : img1 = frameList [ detected_frames [ i ] ] img2 = frameList [ detected_frames [ i + 1 ] ] frame_1 = self . detection_frames [ detected_frames [ i ] ] frame_2 = self . detection_frames [ detected_frames [ i + 1 ] ] detects_1 = list ( frame_1 . keys ( ) ) detects_2 = list ( frame_2 . keys ( ) ) HEAP = [ ] only_human = [ ] detects_1 += left_over for obj_1 in detects_1 : max_score = - 9999 next_score = - 9999 pair = None ratio = None x1 , y1 , w1 , h1 , f , text1 , _ = self . detection_index [ obj_1 ] if text1 != 'face' and text1 != 'super woman' : continue only_human . append ( obj_1 ) for obj_2 in detects_2 : x2 , y2 , w2 , h2 , _ , text2 , _ = self . detection_index [ obj_2 ] if text2 != 'face' and text2 != 'super woman' : continue patch_1 = img1 [ y1 : y1 + h1 , x1 : x1 + w1 , : ] patch_2 = img2 [ y2 : y2 + h2 , x2 : x2 + w2 , : ] score = self . similarity_score ( patch_1 , patch_2 ) if score > max_score : next_score = max_score max_score = score pair = obj_1 , f , obj_2 ratio = next_score / max_score if ratio is None or ratio > thres : continue else : heapq . heappush ( HEAP , ( - max_score , ( pair [ 0 ] , pair [ 1 ] , pair [ 2 ] ) ) ) repeated_obj1 = [ ] repeated_obj2 = [ ] for _ in range ( len ( HEAP ) ) : track = heapq . heappop ( HEAP ) obj_1 , frame_idx , obj_2 = track [ 1 ] if obj_2 in repeated_obj2 : continue repeated_obj1 . append ( obj_1 ) repeated_obj2 . append ( obj_2 ) isPreviousTracked = False for track_id in track_obj . keys ( ) : if obj_1 in track_obj [ track_id ] : track_obj [ track_id ] . append ( obj_2 ) x , y , w , h , f , t , _ = self . detection_index [ obj_2 ] self . detection_index [ obj_2 ] = ( x , y , w , h , f , t , track_id ) isPreviousTracked = True break if isPreviousTracked : continue track_obj [ num_track ] = [ obj_1 , obj_2 ] x1 , y1 , w1 , h1 , f1 , t1 , _ = self . detection_index [ obj_1 ] x2 , y2 , w2 , h2 , f2 , t2 , _ = self . detection_index [ obj_2 ] self . detection_index [ obj_1 ] = ( x1 , y1 , w1 , h1 , f1 , t1 , num_track ) self . detection_index [ obj_2 ] = ( x2 , y2 , w2 , h2 , f2 , t2 , num_track ) num_track += 1 left_over = [ obj for obj in only_human if obj not in repeated_obj1 ] print ( len ( left_over ) ) self . track_obj = track_obj print ( 'saving annotations ...' ) self . _annotate_images ( frameList ) print ( 'Done !' )
Perform detection tracking.
Perform detection tracking .
'''
		Perform detection tracking.
		Input: the list the video frames frameList
		Output: Example:  1. Dict{'track 1':[obj_1, obj_3], 'track 2': [obj_2] ...}
		''' # extact frame image # frame detections # detection id # =============  frame wise compare stage (consectutive) ============= # init a heap for later ranking # get only human left over # pnly track human # only track human # calculate min over max ratio	 # =================  track adding stage  ================= # if already paired, skip # if no previous track, add new one
self
frameList
thres
relabel