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) )