def sliding_window(cls, pview): #Perspective View coordinates x1, y1, x2, y2 = pview #Initial height,width & aspect ratio of perspective view iheight = y2 - y1 iwidth = x2 - x1 aspect_ratio = iwidth / iheight #Resized height,width nheight = 64 nwidth = int(nheight * aspect_ratio) #Resized Perspective view pmg = cv2.resize(cls.image[y1:y2, x1:x2, :], (nwidth, nheight)) #Hog features of the entire perspective view hog_features = FeatureExtractor.extract_hog_features(pmg) #Sliding window on perspective view with a sliding of 8 pixel increments #Slide size is 8 pixels is to be able to subsample the hog features as it has 8 pixels per block img_block_size = 64 slide_size = 8 hog_block_size = 8 xend = nwidth - img_block_size + 1 for idx, vx in enumerate(range(0, xend, slide_size)): #Select the sub-image specified by the sliding window smg = pmg[:, vx:vx + img_block_size] #Get the features for the current sliding window sfeatures = FeatureExtractor.extract_spatial_features(smg) hfeatures = FeatureExtractor.extract_histogram_features(smg) gfeatures0 = hog_features[0][:, idx:idx + hog_block_size].ravel() gfeatures1 = hog_features[1][:, idx:idx + hog_block_size].ravel() gfeatures2 = hog_features[2][:, idx:idx + hog_block_size].ravel() features = np.concatenate( (sfeatures, hfeatures, gfeatures0, gfeatures1, gfeatures2)) pred = cls.model.predict(features) #bmg=cv2.cvtColor(smg,cv2.COLOR_HSV2BGR) #cv2.imwrite("genimgs/img" + str(random.randint(0,1000)) + ".png" ,bmg) if (pred == 1): #Calculating the window coordinates in main image basing on the resized image coordinates rx1 = int(vx * (iwidth / nwidth)) + x1 ry1 = y1 rx2 = rx1 + iheight ry2 = ry1 + iheight cls.hot_windows.append(((rx1, ry1), (rx2, ry2))) #cv2.rectangle(cls.frame_image,(rx1,ry1),(rx2,ry2), (0,255,0), 1) if (vx > xend - 32): cls.hot_windows.append(((rx1, ry1), (rx2, ry2)))
def get_features_labels(cls): v_features = [] n_features = [] t1=time.time() for img in cls.v_images: sfeatures = FE.extract_spatial_features(img) hfeatures = FE.extract_histogram_features(img) gfeatures = FE.extract_hog_features(img) features=np.concatenate( (sfeatures,hfeatures,gfeatures[0].ravel(),gfeatures[1].ravel(),gfeatures[2].ravel()) ) v_features.append(features) for img in cls.n_images: sfeatures = FE.extract_spatial_features(img) hfeatures = FE.extract_histogram_features(img) gfeatures = FE.extract_hog_features(img) features=np.concatenate( (sfeatures,hfeatures,gfeatures[0].ravel(),gfeatures[1].ravel(),gfeatures[2].ravel()) ) n_features.append(features) cls.features = np.vstack((v_features, n_features)).astype(np.float64) cls.labels = np.hstack((np.ones(len(v_features)), np.zeros(len(n_features)))) t2=time.time() print("Feature & Label time - " ,round(t2-t1, 2) )
def find_cars(image, classifier, ystart, ystop, scale=1.0): """ scan the image within given ystart and ystop for car """ img_tosearch = image[ystart:ystop, :, :] ctrans_tosearch = Helpers.convert_color(img_tosearch, color_space=classifier.color_space) if scale != 1: imshape = ctrans_tosearch.shape ctrans_tosearch = cv2.resize(ctrans_tosearch, (np.int(imshape[1]/scale), np.int(imshape[0]/scale))) ch1 = ctrans_tosearch[:, :, 0] ch2 = ctrans_tosearch[:, :, 1] ch3 = ctrans_tosearch[:, :, 2] # define blocks and steps as above nxblocks = (ch1.shape[1] // classifier.pix_per_cell) - classifier.cell_per_block + 1 nyblocks = (ch1.shape[0] // classifier.pix_per_cell) - classifier.cell_per_block + 1 # 64 was the original sampling rate, with 8 cells and 8 pix per cell window = 64 nblocks_per_window = (window // classifier.pix_per_cell) - classifier.cell_per_block + 1 # compute individual channel HOG features for the entire image hog1 = FeatureExtractor.get_hog_features(ch1, classifier.orient, classifier.pix_per_cell, classifier.cell_per_block, vis=False, feature_vec=False) hog2 = FeatureExtractor.get_hog_features(ch2, classifier.orient, classifier.pix_per_cell, classifier.cell_per_block, vis=False, feature_vec=False) hog3 = FeatureExtractor.get_hog_features(ch3, classifier.orient, classifier.pix_per_cell, classifier.cell_per_block, vis=False, feature_vec=False) bboxes = [] max_x_step = nxblocks - nblocks_per_window max_y_step = nyblocks - nblocks_per_window for y_step in range(max_y_step): y_pos_cells = y_step x_step = 0 while x_step < max_x_step: x_pos_cells = x_step # extract hog for this patch hog_feat1 = hog1[y_pos_cells:y_pos_cells+nblocks_per_window, x_pos_cells:x_pos_cells+nblocks_per_window].ravel() hog_feat2 = hog2[y_pos_cells:y_pos_cells+nblocks_per_window, x_pos_cells:x_pos_cells+nblocks_per_window].ravel() hog_feat3 = hog3[y_pos_cells:y_pos_cells+nblocks_per_window, x_pos_cells:x_pos_cells+nblocks_per_window].ravel() hog_features = np.hstack((hog_feat1, hog_feat2, hog_feat3)) x_left_pixels = x_pos_cells * classifier.pix_per_cell y_top_pixels = y_pos_cells * classifier.pix_per_cell # extract the image patch subimg = cv2.resize(ctrans_tosearch[y_top_pixels:y_top_pixels+window, x_left_pixels:x_left_pixels+window], (64, 64)) # get color features spatial_features = FeatureExtractor.extract_spatial_features(subimg, size=classifier.spatial_size) hist_features = FeatureExtractor.extract_hist_features(subimg, nbins=classifier.hist_bins) # scale features and make a prediction test_features = classifier.x_scaler.transform(np.hstack((spatial_features, hist_features, hog_features)).reshape(1, -1)) test_prediction = classifier.svc.predict(test_features) if test_prediction == 1: xbox_left = np.int(x_left_pixels*scale) ytop_draw = np.int(y_top_pixels*scale) win_draw = np.int(window*scale) bbox = [(xbox_left, ytop_draw + ystart), (xbox_left + win_draw, ytop_draw + ystart + win_draw)] bboxes.append(bbox) x_step += 1 else: x_step += 2 return bboxes