Skip to content

Commit

Permalink
Add video support
Browse files Browse the repository at this point in the history
  • Loading branch information
meshtag committed Jun 8, 2021
1 parent 8c870dd commit cf1c2cd
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 43 deletions.
35 changes: 24 additions & 11 deletions CSRT/CSRT.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ def calculate_g_cap_and_channel_weights(self):
G = np.fft.ifft2(res)
G = (G - G.min()) / (G.max() - G.min())
self.G_cap[channel_index] = G
print("In calc", self.G_cap)
print("res", res)
print("f_hat", f_hat)
print("self.h_cap", self.h_cap)

max_value = np.max(G)
G_nms = []
Expand All @@ -169,29 +173,38 @@ def calculate_g_cap_and_channel_weights(self):
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 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]
print("self.G_res before", self.G_res)
print("self.G_cap", self.G_cap)
print("channel w", self.channel_weights)
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())
if (self.G_res.max() - self.G_res.min() != 0):
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)
print("max_pos", max_pos)
print("self.G_res", self.G_res)
print("max_value", 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])
self.p = [int(k) for k in self.p]
# 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()
cv2.waitKey(100)
# cv2.destroyAllWindows()
return self.p

def apply_csrt(self):
pass
7 changes: 4 additions & 3 deletions CSRT/HOG_features.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
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)
fd, hog_image = hog(image, orientations = des_orientations, pixels_per_cell = (image.shape[0] / 100, image.shape[1] / 100), 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



70 changes: 41 additions & 29 deletions CSRT/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,47 @@
from CSRT import *


img = cv2.imread('/home/prathamesh/OpenCV-Trackers/assets/dog_test.png')
roi = cv2.selectROI('select roi',img)
# img = cv2.imread('/home/prathamesh/OpenCV-Trackers/assets/dog_test.png')
# roi = cv2.selectROI('select roi',img)
# # print(roi)
# # print(type(roi))
# # print(type(img))
# # print(img.shape)
# print(roi)
# print(type(roi))
# print(type(img))
# print(img.shape)

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()
# tracker = CSRT(img, roi, 40)
# tracker.set_roiImage()
# tracker.get_spatial_reliability_map()
# tracker.generate_features(8, 2)
# tracker.update_H()
# tracker.calculate_g_cap_and_channel_weights()
# next_roi = tuple(tracker.draw_bbox())
# print(next_roi)

video = cv2.VideoCapture("/home/prathamesh/OpenCV-Trackers/assets/chaplin.mp4")

_, frame1 = video.read()
roi = cv2.selectROI('Select Roi', frame1)
# tracker = CSRT(frame1, roi, 10)
while(video.isOpened()):
_, frame = video.read()
print("roi ", roi)
tracker = CSRT(frame, roi, 1)
tracker.set_roiImage()
tracker.get_spatial_reliability_map()
tracker.generate_features(8, 2)
tracker.update_H()
tracker.calculate_g_cap_and_channel_weights()
print("Here")
roi = tuple(tracker.draw_bbox())
print("Here too")
print("roi", roi)


# tracker.set_roiImage()
# tracker.get_spatial_reliability_map()
# tracker.generate_features(8, 2)
# tracker.update_H()
# tracker.calculate_g_cap_and_channel_weights()
# roi = tuple(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()
# cv2.rectangle(img, (x, y),(x + w, y + h), (0,255,0), 2)
# cv2.imshow('csrt',img)
# cv2.waitKey(0)

0 comments on commit cf1c2cd

Please sign in to comment.