def detect(self, image, windim, winstep=2, pyramidscale=1.5, minprob=0.7): #initialize the list of bounding boxes and associated probabilities boxes = [] probs = [] #loop over the image pyramid for layer in helper.pyramid(image, scale=pyramidscale, minSize=windim): # determine the current scale of the pyramid scale = image.shape[0] / float(layer.shape[0]) #loop over the sliding windows for the current pyramid layer for (x, y, window) in helper.sliding_window(layer, winstep, windim): # grab the dimensions of the window (winh, winw) = window.shape[:2] #ensure the window dimensions match the supplied sliding window dimensions if winh == windim[1] and winw == windim[0]: #extract HOG features from the current window and classify whether or # not this window contains an object we are interested in features = self.desc.describe(window).reshape(1, -1) prob = self.model.predict_proba(features)[0][1] #check to see if the classifier has found an object with sufficient probability if prob > minprob: #compute the (x,y)-coordinates of the bounding box using the current scale of the image pyramid (startx, starty) = (int(scale * x), int(scale * y)) endx = int(startx + (scale * winw)) endy = int(starty + (scale * winh)) #update the list of bounding boxes and probablities boxes.append((startx, starty, endx, endy)) probs.append(prob) #return a tuple of the bounding boxes and probabilities return (boxes, probs)
def detect_faster(img, feature_type, downscale=1.5, visualize=False, apply_nms=True, jobs=2): """use sliding window and pyramid to detect object with batch sliding window and batch classification""" detections = [] # detected candidates min_window_size = (int(config.img_width * 1.5), int(config.img_height * 1.5)) min_ws = (config.img_width, config.img_height) classifier = joblib.load(config.model_path) scale_level = 0 for scaled_img in helper.pyramid(img, downscale, min_window_size): # detections at current scale used for visualization curr_scale_dets = [] x_vec, y_vec, windows = helper.sliding_window_faster(scaled_img, min_ws, config.step_size) pool = Pool(processes=config.jobs) partial_compute_feature = partial(compute_feature, feature_type=feature_type) features = pool.map(partial_compute_feature, windows) features = np.array(features) pool.close() pool.join() preds = classifier.predict(features) confidence = classifier.decision_function(features) idxs = np.where(preds == 1)[0] print ('Detected {} candidates with scale level {}'.format(len(idxs), scale_level)) expand_rate = downscale ** scale_level for i in idxs: attr_vec = np.array([x_vec[i], y_vec[i], min_ws[0] + x_vec[i], min_ws[1] + y_vec[i]]) curr_scale_dets.append((attr_vec, confidence[i])) attr_vec = np.around(attr_vec * expand_rate).astype('int') detections.append((attr_vec, confidence[i])) if visualize: im_copy = scaled_img.copy() for det, _ in curr_scale_dets: cv2.rectangle(im_copy, (det[0], det[1]), (det[2], det[3]), color=(0, 0, 0), thickness=2) cv2.imshow('sliding window', im_copy) cv2.waitKey(20) scale_level += 1 if not apply_nms: # withour non-maximum suppression, return with confidence # can be used for hard-negative mining and graphcut segmentation return detections # apply non-maximum suppression dets = np.array([i[0] for i in detections]) detections = non_max_suppression(dets, only_one=True) if visualize: im_copy = img.copy() helper.draw_detections(im_copy, detections) return detections
import numpy as np import cv2 faces_image = np.load('olivetti_faces.npy') #faces_target = np.load('label_target_face.npy') #note that when you are testing age prediction,you should comment the previous line and de-comment the next line faces_target = np.load('olivetti_faces_target.npy') import time #faces_image = np.load('training_image.npy') #faces_image #print(faces_image.shape) #faces_target = np.load('label_target_face.npy') # load the image and define the window width and height image = cv2.imread('physics.png') (winW, winH) = (64, 64) result = {} for resized in pyramid(image, scale=1.5): # loop over the sliding window for each layer of the pyramid for (x, y, window) in sliding_window(resized, stepSize=19, windowSize=(winW, winH)): # if the window does not meet our desired window size, ignore it if window.shape[0] != winH or window.shape[1] != winW: continue #knn classifiers nn = NearestNeighbor() faces_image = faces_image.reshape(400, 64 * 64) print(faces_target.shape) nn.train(faces_image, faces_target) window = cv2.cvtColor(window, cv2.COLOR_BGR2GRAY) window = window.reshape(1, 64 * 64) # print(1)
def detect(img, feature_type, downscale=1.5, visualize=False, apply_nms=True): """use sliding window and pyramid to detect object""" detections = [] # detected candidates min_window_size = (int(config.img_width * 1.5), int(config.img_height * 1.5)) min_ws = (config.img_width, config.img_height) classifier = joblib.load(config.model_path) scale_level = 0 for scaled_img in helper.pyramid(img, downscale, min_window_size): # detections at current scale used for visualization curr_scale_dets = [] for (x, y, im_window) in helper.sliding_window(scaled_img, min_ws, config.step_size): # filter out not standard spliting window if im_window.shape[0] != config.img_height or \ im_window.shape[1] != config.img_width: continue # compute feature feature = compute_feature(im_window, feature_type) # prediction pred = classifier.predict([feature])[0] confidence = classifier.decision_function([feature])[0] if pred == 1: print ('Detection at location ({}, {})'.format(x, y)) print ('scale level: {}, confidence: {}'.format(scale_level, confidence)) # TODO: potential bug for coordinate restore # attr_vec: [x1, y1, x2, y2] attr_vec = np.array([x, y, min_ws[0] + x, min_ws[1] + y]) curr_scale_dets.append((attr_vec, confidence)) expand_rate = downscale ** scale_level attr_vec = np.around(attr_vec * expand_rate).astype('int') # detection: ([x1, y1, x2, y2], confidence) detections.append((attr_vec, confidence)) # visualize: draw current sliding withdow # and detections at this scale # TODO: show confidence on the bounding box if visualize: im_copy = scaled_img.copy() for det, _ in curr_scale_dets: cv2.rectangle(im_copy, (det[0], det[1]), (det[2], det[3]), color=(0, 0, 0), thickness=2) cv2.rectangle(im_copy, (x, y), (x + im_window.shape[1], y + im_window.shape[0]), color=(255, 255, 255), thickness=2) cv2.imshow('sliding window', im_copy) cv2.waitKey(20) scale_level += 1 if not apply_nms: # withour non-maximum suppression, return with confidence # can be used for hard-negative mining and graphcut segmentation return detections # apply non-maximum suppression dets = np.array([i[0] for i in detections]) detections = non_max_suppression(dets, only_one=True) if visualize: im_copy = img.copy() helper.draw_detections(im_copy, detections) return detections
# Construct the argument and parse the arguments ap = argparse.ArgumentParser() ap.add_argument("-i", "--image", required=True, help="Path to the image") ap.add_argument("-s", "--scale", type=float, default=1.05, help="Scale factor size") args = vars(ap.parse_args()) # Load the image image = cv2.imread(args["image"]) # Method 1: No smooth, just scaling # Loop over the image pyramid for (i, resized) in enumerate(pyramid(image, scale=args["scale"])): # show the resized image cv2.imshow("Layer {}".format(i + 1), resized) cv2.waitKey(0) # Close all the windows cv2.destroyAllWindows() # Method 2: Resizing + Gaussian Smoothing for (i, resized) in enumerate(pyramid_gaussian(image, downscale=2)): # if the image is too small, break from the loop if resized.shape[0] < 30 or resized.shape[1] < 30: break # Show the resized image cv2.imshow("Layer {}".format(i + 1), resized)