diff --git a/CSRT/CSRT.py b/CSRT/CSRT.py index 74fd2be..179145c 100644 --- a/CSRT/CSRT.py +++ b/CSRT/CSRT.py @@ -1,7 +1,4 @@ import cv2 -from skimage import feature -from skimage.feature import hog -from skimage.transform import resize import HOG_features as hogfeat import numpy as np @@ -34,13 +31,11 @@ def cap_func(element): class CSRT(): def __init__(self, frame, roi, num_features, debug = False): - self.debug = None + self.debug = debug self.frame = frame self.roi = roi - self.sigma = 100 - self.mu = 5 - self.beta, self.λ, self.n = 3, 0.01, 0.02 + self.beta, self.λ, self.n, self.sigma, self.mu = 3, 0.01, 0.02, 100, 5 self.g = get_gaussian_map(self.roi, self.sigma) if self.debug: @@ -51,12 +46,12 @@ def __init__(self, frame, roi, num_features, debug = False): self.p = [] # This variable will store position of object in each frame. self.channel_weights = [0] * num_features # Will store channel weights for all channels. self.G_cap = [0] * num_features # Will store individual G_cap/g_tilda values for channels. - self.G_res = 0 # Will store resultant G_cap after using channel_reliability. + # self.G_res = 0 # Will store resultant G_cap after using channel_reliability. def set_roiImage(self): - x,y,w,h = self.roi - self.roi_img = self.frame[y:y+h, x:x+w] + self.x, self.y, self.width, self.height = self.roi + self.roi_img = self.frame[self.y : self.y + self.height, self.x : self.x + self.width] self.h = np.zeros((self.roi_img.shape[0], self.roi_img.shape[1])) self.I = np.zeros_like(self.h) @@ -136,9 +131,9 @@ def update_H(self): self.mu_i *= self.beta self.h_cap[channel_index] = h_cap - print("h_cap for feature channels ", self.h_cap) - print("length h_cap", len(self.h_cap)) - print("Lengths of h_cap", len(h_cap[0]), len(h_cap[1])) + # print("h_cap for feature channels ", self.h_cap) + # print("length h_cap", len(self.h_cap)) + # print("Lengths of h_cap", len(h_cap[0]), len(h_cap[1])) def calculate_g_cap_and_channel_weights(self): @@ -167,19 +162,36 @@ def calculate_g_cap_and_channel_weights(self): sum += self.channel_weights[channel_index] for channel_index in range(len(self.features)): self.channel_weights[channel_index] /= sum - print("channel_weights", self.channel_weights) - print("G_cap", self.G_cap) + # print("channel_weights", self.channel_weights) + # print("G_cap", self.G_cap) if self.debug: print("roi", self.roi) print('G shape', self.features[0].shape) -def calculate_final_g_cap(self): - for channel_index in range(len(self.features)): - self.G_res += self.G_cap[channel_index] * self.channel_weights[channel_index] - return self.G_res - -def apply_csrt(self): - self.p = self.get_new_roi() - # print(self.p) - self.channel_reliability() + def calculate_final_g_cap(self): + for channel_index in range(len(self.features)): + self.G_res += self.G_cap[channel_index] * self.channel_weights[channel_index] + return self.G_res + + def draw_bbox(self): + self.G_res = self.G_cap[0] * self.channel_weights[0] + for channel_index in range(1, len(self.features)): + self.G_res += self.G_cap[channel_index] * self.channel_weights[channel_index] + self.G_res = (self.G_res - self.G_res.min()) / (self.G_res.max() - self.G_res.min()) + max_value = np.max(self.G_res) + max_pos = np.where(self.G_res == max_value) + dy = int(np.mean(max_pos[0]) - self.G_res.shape[0] / 2) + dx = int(np.mean(max_pos[1]) - self.G_res.shape[1] / 2) + self.p = [self.x + dx, self.y + dy, self.x + dx + self.width, self.y + dy + self.height] + print(self.p[0]) + print(self.p[1]) + print(self.p[2]) + print(self.p[3]) + cv2.rectangle(self.frame, (self.p[0], self.p[1]), (self.p[2], self.p[3]), (0, 255, 0), 2) + cv2.imshow("frame", self.frame) + cv2.waitKey(0) + cv2.destroyAllWindows() + + def apply_csrt(self): + pass diff --git a/CSRT/HOG_features.py b/CSRT/HOG_features.py index 5d75721..220f9a6 100644 --- a/CSRT/HOG_features.py +++ b/CSRT/HOG_features.py @@ -1,9 +1,15 @@ import cv2 from skimage.feature import hog -# from skimage.transform import resize +import numpy as np def get_hog_features(image, des_orientations, des_pixels_per_cell): fd, hog_image = hog(image, orientations = des_orientations, pixels_per_cell = (des_pixels_per_cell, des_pixels_per_cell), cells_per_block=(2, 2), visualize=True,multichannel=True) + # np.resize(fd, hog_image.shape) + # print("fd shape", fd.shape) + # print("hog shape", hog_image.shape) + # cv2.imshow("Hog image", hog_image) + # cv2.waitKey(0) + # cv2.destroyAllWindows() return hog_image diff --git a/CSRT/test.py b/CSRT/test.py index d1d0a54..d498e3e 100644 --- a/CSRT/test.py +++ b/CSRT/test.py @@ -1,5 +1,4 @@ import cv2 -from skimage.transform import resize from CSRT import * @@ -10,12 +9,25 @@ # print(type(img)) # print(img.shape) -tracker = CSRT(img, roi, 2) +tracker = CSRT(img, roi, 20) tracker.set_roiImage() tracker.get_spatial_reliability_map() tracker.generate_features(8, 2) tracker.update_H() tracker.calculate_g_cap_and_channel_weights() +tracker.draw_bbox() + +# video = cv2.VideoCapture("/home/prathamesh/OpenCV-Trackers/assets/chaplin.mp4") + +# tracker = CSRT(5) +# _, frame1 = video.read() +# roi = cv2.selectROI('Select Roi', frame1) +# tracker.next_frame(frame1, roi) + +# while(video.isOpened()): +# pass + + # tracker.apply_csrt() # x, y, w, h = tracker.get_new_roi()