def init(self, img, box): # print(box) box = np.array([ box[1] , box[0] , box[3], box[2]], dtype=np.float32) img_now = read_image(img) self.box = box_deter(box,img_now) center = np.array([box[1] + box[3]/2 , box[0] + box[2]/2] ) gsize = img_now.shape x = np.arange(gsize[1]) y = np.arange(gsize[0]) x_index , y_index = np.meshgrid(x, y) label_now = create_labels(x_index,y_index,self.sigma,center) if img_now.ndim == 3: img_now = cv2.cvtColor(img_now, cv2.COLOR_RGB2GRAY) img_s = img_now[int(box[0]) : int(box[0] + box[2]) , int(box[1]) : int(box[1] + box[3])] img_g = label_now[int(box[0]) : int(box[0] + box[2]) , int(box[1]) : int(box[1] + box[3])] # print(img_g.shape) self.img_G = np.fft.fft2(img_g) img_fi = cv2.resize(img_s ,self.img_G.shape[::-1]) # width height img_fi = pre_pos(img_fi) self.Ai = (self.img_G*(np.fft.fft2(img_fi).conj())) self.Bi = (np.fft.fft2(img_fi)*(np.fft.fft2(img_fi).conj())) for i in range(0,self.N): img_fi= pre_pos(rand_warp(img_s)) self.Ai = self.Ai + (self.img_G*(np.fft.fft2(img_fi).conj())) self.Bi = self.Bi + (np.fft.fft2(img_fi)*(np.fft.fft2(img_fi).conj())) self.Ai = self.eta*self.Ai self.Bi = self.eta*self.Bi
def update(self, img): img_now = ops.read_image(img) if img_now.ndim == 3: img_now = ops.rgb2gray(img_now) x = ops.get_subwindow(img_now, self.pos, self.sz, self.cos_window) # print(x) k = ops.dense_gauss_kernel(self.sigma, x, self.z) kf = pylab.fft2(k) alphaf_kf = pylab.multiply(self.alphaf, kf) response = pylab.real(pylab.ifft2(alphaf_kf)) # Eq. 9 # target location is at the maximum response r = response row, col = pylab.unravel_index(r.argmax(), r.shape) self.pos = self.pos - pylab.floor(self.sz / 2) + [row, col] x = ops.get_subwindow(img_now, self.pos, self.sz, self.cos_window) k = ops.dense_gauss_kernel(self.sigma, x) new_alphaf = pylab.divide(self.yf, (pylab.fft2(k) + self.lambda_value)) # Eq. 7 new_z = x f = self.interpolation_factor self.alphaf = (1 - f) * self.alphaf + f * new_alphaf self.z = (1 - f) * self.z + f * new_z box_new = np.array([ self.pos[1] - (self.sz[1]) / 2 + 1, self.pos[0] - (self.sz[0]) / 2 + 1, self.sz[1], self.sz[0] ], dtype=np.float32) return box_new
def init(self, img, box): img_now = ops.read_image(img) self.target_sz = np.array([box[3], box[2]]) self.pos = np.array([box[1], box[0]]) + self.target_sz / 2 # print(self.pos) # ground_truth = # window size, taking padding into account self.sz = pylab.floor(self.target_sz * (1 + self.padding)) # desired output (gaussian shaped), bandwidth proportional to target size self.output_sigma = pylab.sqrt(pylab.prod( self.target_sz)) * self.output_sigma_factor grid_y = pylab.arange(self.sz[0]) - pylab.floor(self.sz[0] / 2) grid_x = pylab.arange(self.sz[1]) - pylab.floor(self.sz[1] / 2) #[rs, cs] = ndgrid(grid_x, grid_y) rs, cs = pylab.meshgrid(grid_x, grid_y) y = pylab.exp(-0.5 / self.output_sigma**2 * (rs**2 + cs**2)) self.yf = pylab.fft2(y) # print(self.yf) #print("yf.shape ==", yf.shape) #print("y.shape ==", y.shape) # store pre-computed cosine window self.cos_window = pylab.outer(pylab.hanning(self.sz[0]), pylab.hanning(self.sz[1])) if img_now.ndim == 3: img_now = ops.rgb2gray(img_now) x = ops.get_subwindow(img_now, self.pos, self.sz, self.cos_window) k = ops.dense_gauss_kernel(self.sigma, x) self.alphaf = pylab.divide( self.yf, (pylab.fft2(k) + self.lambda_value)) # Eq. 7 self.z = x
def track(self, img_files, box, visualize=False): frame_num = len(img_files) boxes = np.zeros((frame_num, 4)) boxes[0] = box times = np.zeros(frame_num) for f, img_file in enumerate(img_files): img = ops.read_image(img_file) begin = time.time() if f == 0: self.init(img, box) else: boxes[f, :] = self.update(img) times[f] = time.time() - begin if visualize: ops.show_image(img, boxes[f, :]) return boxes, times
def mark(requests): """ :param requests: Image :return: Cropped face """ im = ops.read_image(requests) bounding_boxes, _ = detector.detect_face(im, minsize, pnet, rnet, onet, threshold, factor) if len(bounding_boxes) < 1: return HttpResponse('no face detected.') det = np.squeeze(bounding_boxes[0, 0:4]) bb = np.zeros(4, dtype=np.int32) bb[0] = np.maximum(det[0] - margin / 2, 0) bb[1] = np.maximum(det[1] - margin / 2, 0) bb[2] = np.minimum(det[2] + margin / 2, im.shape[1]) bb[3] = np.minimum(det[3] + margin / 2, im.shape[0]) return HttpResponse(' '.join(map(lambda i: str(i), list(bb))))
def update(self, img ): img_ori = read_image(img) img_now = img_ori if img_now.ndim == 3: img_now = cv2.cvtColor(img_now, cv2.COLOR_RGB2GRAY) else: img_now = img_ori Hi = self.Ai/self.Bi img_fi = img_now[int(self.box[0]) : int(self.box[0] + self.box[2]) , int(self.box[1]) : int(self.box[1] + self.box[3])] img_fi = pre_pos(cv2.resize(img_fi, self.img_G.shape[::-1])) # print(img_fi.shape) img_gi = np.abs(np.fft.ifft2(Hi * np.fft.fft2(img_fi))) img_gik = np.zeros(img_gi.shape) img_gik = cv2.normalize(img_gi , img_gik, 0, 255, cv2.NORM_MINMAX) img_gi = np.asarray(img_gik, dtype=np.uint8) # print(img_gi) gi_max_1 , gi_max_2 = np.where(img_gi == np.amax(img_gi)) loc = np.array((gi_max_1.mean() , gi_max_2.mean())) # print(loc) dheight = loc[0] - self.img_G.shape[0]/2 dwidth = loc[1] - self.img_G.shape[1]/2 # print(box) box = np.array([self.box[0]+dheight , self.box[1]+dwidth , self.box[2], self.box[3]]) box = box_deter(box,img_now) # print(box) # print(img_now.shape) self.box = box img_fi = img_now[int(box[0]) : int(box[0] + box[2]) , int(box[1]) : int(box[1] + box[3])] img_fi = pre_pos(cv2.resize(img_fi, self.img_G.shape[::-1])) self.Ai = self.eta*(self.img_G*(np.fft.fft2(img_fi).conj())) + (1-self.eta)*self.Ai self.Bi = self.eta*(np.fft.fft2(img_fi)*(np.fft.fft2(img_fi).conj())) + (1-self.eta)*self.Bi box = np.array([ box[1] , box[0] , box[3] , box[2]],dtype = np.float32) return box