def plot_images(base_image: str = None, image_df: pd.DataFrame = None) -> str: """ To Plot The Image and its Neighbours params : base_image : String - Contains the Path of Image image_df : DataFrame - Contains the details of the Image Neighbours """ if base_image is not None: img = read_image(base_image, SIZE) plt.imshow(img) n_items = len(image_df) - 1 columns = 10 rows = int(np.ceil(n_items + 1 / columns)) fig = plt.figure(figsize=(rows, 10 * rows)) for i in range(rows): for j in range(columns): try: img = read_image( os.path.join('dataset', image_df.loc[i + j + 1, 'ImageName']), SIZE) fig.add_subplot(rows, columns, i + j + 1) plt.imshow(img) except: pass plt.savefig("neighbor_plot.png") return "neighbor_plot.png"
def main(): im = data.read_image('Grey') fig, ax = plt.subplots(1, 1) ax.imshow(im, extent=[0, 100, 0, 100]) plot_key_points(ax) plt.show()
def scatter_on_image(df, scale=0.1): im = data.read_image() (x, y) = zip(*df.index.values) plt.imshow(im, extent=[0, 100, 0, 100]) plt.scatter(x, y, s=scale * df.values, color='red') plt.scatter(x, y, color='blue', marker='x') plt.axis([0, 100, 0, 100])
def main(): image_paths = glob.glob('images/*png') model_dir = 'Result' class_num = 40 size = (224, 224) gpu_id = 0 model_file = os.path.join(model_dir, 'model_100epoch') out_save_dir = os.path.join(model_dir, 'out') model = chainer.links.Classifier(FCN(class_num=class_num)) serializers.load_npz(model_file, model) if gpu_id >= 0: cuda.get_device_from_id(gpu_id).use() model.to_gpu() for i, f in enumerate(image_paths): # Predict Input Data x = data.read_image(f, size) x = data.image_norm(x) x = cuda.to_gpu(x[np.newaxis, :, :, :]) y = model.predictor(x).data.argmax(axis=1)[0] # Save Predict Image input_file_name = f.split('/')[-1] # get file name save_path = os.path.join(out_save_dir, 'pred{:>05}_'.format(i) + input_file_name) y = cuda.to_cpu(y.astype(np.uint8)) Image.fromarray(y).save(save_path) print(f, save_path)
def main(): im = data.read_image() df = data.read_data('Fri').sort_values(by=['id', 'Timestamp']) for i in range(25, 40): plt.figure(figsize=[12, 9]) plt.imshow(im, extent=[0, 100, 0, 100]) plot_stay_points(df, i) plt.savefig('stop time {} s.png'.format(i), bbox_inches='tight')
def __getitem__(self, i): _, im = read_image(self, i, to_pil=True, invert_color=self.invert_color, n_channel=self.n_channel) if self.transformer: im = self.transformer(im) labels = self.get_multilabels(i) return (im, *labels)
def predict(filename): image = data.read_image(filename, IMAGE_H, IMAGE_W, LABEL_LENGTH) x_data = data.split_image(image, IMAGE_H, IMAGE_W, LABEL_LENGTH) y_preds = model.predict(x_data) label = '' for y_pred in y_preds: label = label + data.onehot2number(y_pred, LABELS) return label
def demo_prediction(): model = load_model("model_-14-0.04.h5") cars, notcars = load_smallset() # Just for fun choose random car / not-car indices and plot example images car_ind = np.random.randint(0, len(cars)) notcar_ind = np.random.randint(0, len(notcars)) # Read in car / not-car images car_image = read_image(cars[car_ind]) notcar_image = read_image(notcars[notcar_ind]) car_prediction = model.predict(np.reshape(car_image, (1, 64, 64, 3))) notcar_prediction = model.predict(np.reshape(notcar_image, (1, 64, 64, 3))) side_by_side_plot(im1=car_image, im2=notcar_image, im1_title="prediction: {}".format(car_prediction), im2_title="prediction: {}".format(notcar_prediction), fontsize=16) # Temporary fix - AttributeError: 'NoneType' object has no attribute 'TF_NewStatus K.clear_session()
def extract_features(imgs, feature_parameter): # Create a list to append feature vectors to features = [] # Iterate through the list of images for file in imgs: # Read in each one by one image = read_image(file) image_features = single_image_features( image=image, feature_parameter=feature_parameter) features.append(image_features) # Return list of feature vectors return features
def demo_spatial_binning(): # Read a color image img = read_image("test_images/000275.png") img_small = cv2.resize(img, (32, 32)) side_by_side_plot(im1=img, im2=img_small, im1_title="Example Image", im2_title="Spatial Binning", fontsize=16) #demo_spatial_binning()
def demo(): vehicle_detection = VehicleDetection() image = read_image('test_images/test1.jpg') #image = read_image('test_images/screen2.png') draw_image = vehicle_detection.detect(image) side_by_side_plot(im1=image, im2=draw_image, im2_cmap='hot', im1_title="Example Image", im2_title="Bounding Boxes", fontsize=16)
def demo_prediction2(): image = read_image("test_images/test1.jpg") model = create_model((260, 1280, 3)) model.load_weights("model_-11-0.01.h5") hot_windows = search_cars(model, image) heatmap = heatmap_filter(image, hot_windows, 3) # Find final boxes from heatmap using label function labels = label(heatmap) draw_img = draw_labeled_bboxes(np.copy(image), labels) side_by_side_plot(im1=image, im2=draw_img) # Temporary fix - AttributeError: 'NoneType' object has no attribute 'TF_NewStatus K.clear_session()
def train_resnet(img_type): #data train_imgs, train_labels = read_image(path, img_type) Y_hat, model_params = ResNet50(input_shape=[256, 256, 1], classes=2) #Y_hat = tf.sigmoid(Z) X = model_params['input'] Y_true = tf.placeholder(dtype=tf.int32, shape=[None, 1]) Z = model_params['out']['Z'] loss = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(logits=Z, labels=Y_true)) train_step = tf.train.AdamOptimizer(1e-3).minimize(loss) correct_prediction = tf.equal(tf.cast(tf.argmax(Y_hat, 1), tf.int32), Y_true) acc = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) saver = tf.train.Saver() with tf.Session() as sess: sess.run(tf.global_variables_initializer()) sess.run(tf.local_variables_initializer()) for epoch in range(1, max_epochs + 1): start_time = time.time() for batch_images, batch_labels in minibatches(train_imgs, train_labels, batch_size, shuffle=True): if epoch % 10 == 0: l, ac, _ = sess.run([loss, acc, train_step], feed_dict={ X: batch_images, Y_true: batch_labels }) print('epoch: ' + str(epoch) + ' loss: ' + str(l) + ' accu: ' + str(ac)) else: sess.run([train_step], feed_dict={ X: batch_images, Y_true: batch_labels }) if epoch % 500 == 0: saver.save(sess, path + './model' + str(epoch) + '.ckpt') end_time = time.time() print(end_time - start_time)
def save_image_grid(base_image: str, image_df: pd.DataFrame, path: str) -> torch.Tensor: """ To Save the Images as a Grid using PyTorch params : image_df : DataFrame - Contains the Images Details path : String - Path to Save the Grid Image """ images = [] to_tensor = transforms.ToTensor() if base_image is not None: img = read_image(base_image, SIZE) img = to_tensor(img) images.append(img) for i, row in image_df.iterrows(): img = read_image(os.path.join('dataset', row['ImageName']), SIZE) img = to_tensor(img) images.append(img) images = torch.stack(images) torchvision.utils.save_image(images, nrow=10, fp=path) logging.info(f"Saved Neighbours Grid at {path}") return images
def main(): df = data.read_data('Fri', 'subset-100').set_index('Timestamp') time = pd.Timestamp(datetime(2014, 6, 6, 12)) df2 = get_positions_at(df, time) im = data.read_image('Grey') fig, ax = plt.subplots() plt.imshow(im, extent=[0, 100, 0, 100]) plt.plot() plt.show() ani = animation.FuncAnimation(fig, animate, blit=False, interval=10, repeat=False)
def test_net(): data_set=TrainDataset() classes=data_set.classes net=SSD(len(classes)+1) _,_,last_time_model=get_check_point() # assign directly # last_time_model='./weights/weights_21_110242' if os.path.exists(last_time_model): model=torch.load(last_time_model) net.load_state_dict(model) print("Using the model from the last check point:`%s`"%(last_time_model)) else: raise ValueError("no model existed...") net.eval() is_cuda=cfg.use_cuda did=cfg.device_id img_src=read_image('./data/img/dog.jpg') w,h=img_src.size img=TestTransform(img_src,torch.tensor([[0,0,1,1]]).float()) # [c,h,w] img=img[None] if is_cuda: net.cuda(did) img=img.cuda(did) boxes,labels,probs=net.predict(img,torch.tensor([[w,h]]).type_as(img))[0] prob_mask=probs>cfg.out_thruth_thresh boxes=boxes[prob_mask ] labels=labels[prob_mask ].long() probs=probs[prob_mask] img_src=np.array(img_src) # [h,w,3] 'RGB' # change to 'BGR' img_src=img_src[:,:,::-1].copy() if len(boxes) !=0: draw_box(img_src,boxes,color='pred', text_list=[ classes[_]+'[%.3f]'%(__) for _,__ in zip(labels,probs) ] ) show_img(img_src,-1)
def demo(): feature_parameter = selected_feature_parameter() #scale = 1.5 scale = 1 clf, X_scaler = read_classifier("classifier.p") #img = read_image('test_images/bbox-example-image.jpg') img = read_image('test_images/test1.jpg') #y_start_stop = (400, 400+32) y_start_stop = (400, img.shape[0]) draw_image = find_cars(img, y_start_stop, scale, clf, X_scaler, feature_parameter) plt.imshow(draw_image) plt.show() #demo()
def plot_next_place(prev, next, ids, ax=None, max_size=None): if ax is None: fig, ax = plt.subplots() kp = data.read_key_points().set_index('place_id') group_info = data.read_group_info('Fri').set_index('group_id') # places = pd.DataFrame(data={'prev': prev, 'next': next}).dropna().astype('int64') places = pd.DataFrame(data={'prev': prev, 'next': next}, index=ids) # drop any rows with 0 for the place id, as we can't plot that. places = places.loc[(places != 0).all(axis=1)] places['size'] = group_info['size'] p2 = places.groupby(['next', 'prev']).sum().reset_index().sort_values('size') # remove the small slices # p2 = p2[p2['size'] >= 8] if max_size is None: max_size = p2['size'].max() # print(max_size) im = data.read_image('Grey') ax.imshow(im, extent=[0, 100, 0, 100]) cmap = plt.get_cmap('plasma') for i, row in enumerate(p2.itertuples()): # index_amt = i / (len(p2) - 1) size_amt = row.size / max_size prev_xy = kp.loc[row.prev, ['X', 'Y']].values next_xy = kp.loc[row.next, ['X', 'Y']].values arrowprops = { 'arrowstyle': 'simple', 'mutation_scale': 50 * size_amt, 'alpha': 0.2 + 0.8 * size_amt, 'lw': 0, 'color': cmap(0.5 * size_amt), 'connectionstyle': "arc3,rad=-0.1" } ax.annotate('', xy=next_xy, xytext=prev_xy, arrowprops=arrowprops) ax.xaxis.set_ticks([]) ax.yaxis.set_ticks([])
def train_vgg(img_type): #data train_imgs, train_labels = read_image(path, img_type) #model vgg = Vgg16() images = tf.placeholder(tf.float32, [None, 256, 256, 1]) labels = tf.placeholder(tf.int32, [None, 1]) probs, logits = vgg.build(images) loss = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=labels)) train_step = tf.train.AdamOptimizer(learning_rate=1e-3).minimize(loss) correct_prediction = tf.equal(tf.cast(tf.argmax(logits, 1), tf.int32), labels) acc = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) saver = tf.train.Saver() #training part with tf.Session() as sess: sess.run(tf.global_variables_initializer()) for epoch in range(1, max_epochs + 1): start_time = time.time() #training for batch_images, batch_labels in minibatches(train_imgs, train_labels, batch_size, shuffle=True): feed_dict = {labels: batch_labels, images: batch_images} _, err, ac = sess.run([train_step, loss, acc], feed_dict=feed_dict) end_time = time.time() print(end_time - start_time) if epoch % 500 == 0: saver.save(sess, './model' + str(epoch) + '.ckpt')
def plot_next_place(prev, next, ids, ax=None, max_size=None): if ax is None: fig, ax = plt.subplots() kp = data.read_key_points().set_index('place_id') group_info = data.read_group_info('Fri').set_index('group_id') # places = pd.DataFrame(data={'prev': prev, 'next': next}).dropna().astype('int64') places = pd.DataFrame(data={'prev': prev, 'next': next}, index=ids) # drop any rows with 0 for the place id, as we can't plot that. places = places.loc[(places != 0).all(axis=1)] places['size'] = group_info['size'] p2 = places.groupby(['next', 'prev']).sum().reset_index().sort_values('size') # remove the small slices # p2 = p2[p2['size'] >= 8] if max_size is None: max_size = p2['size'].max() # print(max_size) im = data.read_image('Grey') ax.imshow(im, extent=[0, 100, 0, 100]) cmap = plt.get_cmap('plasma') for i, row in enumerate(p2.itertuples()): # index_amt = i / (len(p2) - 1) size_amt = row.size / max_size prev_xy = kp.loc[row.prev, ['X', 'Y']].values next_xy = kp.loc[row.next, ['X', 'Y']].values arrowprops = {'arrowstyle': 'simple', 'mutation_scale': 50 * size_amt, 'alpha': 0.2 + 0.8 * size_amt, 'lw': 0, 'color': cmap(0.5 * size_amt), 'connectionstyle': "arc3,rad=-0.1"} ax.annotate('', xy=next_xy, xytext=prev_xy, arrowprops=arrowprops) ax.xaxis.set_ticks([]) ax.yaxis.set_ticks([])
def get_similar_images(img_path: str, features: pd.DataFrame, sf: Callable, tree: Callable, model: nn.Module, neighbours: int, plot: int = 0) -> pd.DataFrame: """ To Get Neighboursfor given Image based on its Path params : img_path : String - Contains Path f Image to get Neighbours features : DataFrame - Contains the features and Image paths tree : Annoy Tree - to get the neighours sf : Callable - The Class which contains the registered PyTorch Hook and Features model : Neural Network Model - to get the Features neighbours : Int - Number of neoghbours to retrieve plot : Int - Whether to plot the Neighbours or Not """ img = read_image(img_path, SIZE) img = transforms.ToTensor()(img) img.unsqueeze_(dim=0) with torch.no_grad(): model.eval() model(img.to(DEVICE)) out = sf.features neighbours_df = get_similar_images_annoy_by_feat(features, out, tree, neighbours=neighbours, print_time=True) if plot == 1: print(f"Plot Saved at {plot_images(img_path, neighbours_df)}") return neighbours_df
import matplotlib matplotlib.use('Qt4Agg') import data import pandas as pd import matplotlib.pyplot as plt import script.predict.preprocess as pp import script.predict.predictors as pdt from sklearn.cross_validation import train_test_split training_size = 0.25 x, y = pp.get_bag_data(['Sat'], 12, pp.common_categories) x_train, x_test, y_train, y_test = train_test_split(x, y, train_size=training_size, random_state=2294967295) im = data.read_image(size=1000) for predictor_name in pdt.all_predictors: predictor = pdt.all_predictors[predictor_name] predictor.fit(x_train, y_train) y_pred = predictor.predict(x_test) y_pred_probs = predictor.predict_proba(x_test) kp = data.read_key_points().set_index('place_id') # Adjust the size of this so that it's proportional to the testing data kp['Training Counts'] = ((1 - training_size) / training_size) * pd.Series(y_train).value_counts() kp['Test Counts'] = pd.Series(y_test).value_counts() kp['Prediction Counts'] = pd.Series(y_pred).value_counts() kp['Prediction Probability Sum'] = pd.DataFrame(y_pred_probs, columns=predictor.classes_).sum() kp.fillna(0, inplace=True) # fig, axs = plt.subplots(2, 2) fig, axs = plt.subplots(1, 3) fig.suptitle(predictor_name)
def preprocess_func(x, y): x = read_image(x) x = decode_png(x) x = resize(x, img_height, img_width) return x, y
def AugmentDS(self,Augentries): datagen = ImageDataGenerator( # featurewise_center=False, # featurewise_std_normalization=False, # samplewise_center=False, # samplewise_std_normalization=False, # zca_whitening=True, # rescale=None, rotation_range=3, width_shift_range=0.08, height_shift_range=0.08, shear_range=0.07, zoom_range=0.07, horizontal_flip=True, vertical_flip=True, fill_mode='constant', cval=0. ) args=self.arggen(Augentries) # trimgs,labimgs,data_path,num config['data_path'],config['fext'],num,cr=args[0],args[1],int(args[2]),float(args[3]) fext=config['fext'] data_path=config['data_path'] self.Savefiles(data_path,fext,'trlab') imhdf5=open_hdf5_file(config['image_hdf5_path']) trimgs=np.squeeze(imhdf5.root.data,axis=3) imhdf5.close() labhdf5=open_hdf5_file(config['label_hdf5_path']) labimgs=np.squeeze(labhdf5.root.truth,axis=3) labhdf5.close() nLabels=np.max(labimgs) print('estimated number of lables:',nLabels) if nLabels>1: labimgstmp=[] for i in range(1,nLabels+1): labimgstmp.append(np.ma.masked_not_equal(labimgs,i).filled(0)/i) labimgstmp=np.array(labimgstmp) imgshape=trimgs[0].shape print(imgshape) print('-'*30) print('Augmenting train and labels dataset: ',num,'replica per image...') print('-'*30) # seed = np.random.randint(10000) seed=np.random.randint(10000,size=2*len(trimgs)*num) if tmpf in sorted(os.listdir(config['image_path'])): shutil.rmtree(os.path.join(config['image_path'],tmpf), ignore_errors=True) shutil.rmtree(os.path.join(config['label_path'],tmpf), ignore_errors=True) os.makedirs(os.path.join(config['image_path'],tmpf)) os.makedirs(os.path.join(config['label_path'],tmpf)) global batchdata batchdata=[] j=0 for x in trimgs: x[x==0]=1 x = x.reshape((1,) + x.shape+(1,)) # the .flow() command below generates batches of randomly transformed images # and saves the results to the `preview/` directory i = 0 for batch in datagen.flow(x, batch_size=1,seed=seed[j]): self.save_tif(data_path,os.path.join(image_p,tmpf),'img',batch[0,:,:,0].astype('uint8'),seed[i+j*2*num],fext) i += 1 if i >= 2*num: break # otherwise the generator would loop indefinitely j +=1 if nLabels>1: for k in range(1,nLabels+1): os.makedirs(os.path.join(config['label_path'],tmpf,str(k))) j=0 for y in labimgstmp[k-1]: y = y.reshape((1,) + y.shape+(1,)) i = 0 for batch in datagen.flow(y, batch_size=1,seed=seed[j]): self.save_tif(data_path,os.path.join(label_p,tmpf,str(k)),'img',batch[0,:,:,0].astype('uint8'),seed[i+j*2*num],fext) batchdata.append(batch[0,:,:,0]) i += 1 if i >= 2*num: break # otherwise the generator would loop indefinitely j +=1 imglist=[f for f in sorted(os.listdir(os.path.join(config['image_path'],tmpf))) if fext in f] for n in range(len(imglist)): tmp=sum(read_image(os.path.join(config['label_path'],tmpf,str(k),imglist[n]))*k for k in range(1,nLabels+1)) self.save_tif(data_path,os.path.join(label_p,tmpf),'img',tmp.astype('uint8'),imglist[n].split('.')[0][-4:],fext) for k in range(1,nLabels+1): shutil.rmtree(os.path.join(config['label_path'],tmpf,str(k)), ignore_errors=True) else: j=0 for y in labimgs: y = y.reshape((1,) + y.shape+(1,)) i = 0 for batch in datagen.flow(y, batch_size=1,seed=seed[j]): self.save_tif(data_path,os.path.join(label_p,tmpf),'img',batch[0,:,:,0].astype('uint8'),seed[i+j*2*num],fext) batchdata.append(batch[0,:,:,0]) i += 1 if i >= 2*num: break # otherwise the generator would loop indefinitely j +=1 self.Savefiles(data_path,fext,'trlab',subtask='augtmp') # create_train_data(data_path,os.path.join(image_p,tmpf),os.path.join(label_p,tmpf),fext) imhdf5=open_hdf5_file(config['image_hdf5_path']) tmptr=np.squeeze(imhdf5.root.data,axis=3) imhdf5.close() labhdf5=open_hdf5_file(config['label_hdf5_path']) tmplab=np.squeeze(labhdf5.root.truth,axis=3) labhdf5.close() print(imgshape,cr) lencrop=int(((imgshape[0]*cr)//16)*16),int(((imgshape[1]*cr)//16)*16) print(lencrop) # delta=imgshape[0]-lencrop[0],imgshape[1]-lencrop[1] # print(delta) seltr=[] sellab=[] j=0 for i,img in enumerate(tmptr): tmpres=crop_no_black(tmptr[i],tmplab[i],lencrop) if tmpres is not None: seltr.append(tmpres[0]) sellab.append(tmpres[1]) j += 1 if j > len(trimgs)*(num+1): break seltr=np.array(seltr) sellab=np.array(sellab) print(seltr.shape,sellab.shape) if selfold in sorted(os.listdir(os.path.join(data_path,image_p))): shutil.rmtree(os.path.join(data_path,image_p,selfold), ignore_errors=True) shutil.rmtree(os.path.join(data_path,label_p,selfold), ignore_errors=True) os.makedirs(os.path.join(data_path,image_p,selfold)) os.makedirs(os.path.join(data_path,label_p,selfold)) for i in range(len(seltr)): self.save_tif(data_path,os.path.join(image_p,selfold),'img',seltr[i],i,fext) self.save_tif(data_path,os.path.join(label_p,selfold),'img',sellab[i],i,fext) # create_train_data(data_path,image_p,label_p,fext) if tmpf in sorted(os.listdir(os.path.join(data_path,image_p))): shutil.rmtree(os.path.join(data_path,image_p,tmpf), ignore_errors=True) shutil.rmtree(os.path.join(data_path,label_p,tmpf), ignore_errors=True) self.Savefiles(data_path,fext,'trlab',subtask='augm') print('Done') return
import numpy as np import matplotlib.pyplot as plt kp = data.read_visited_key_points('Fri', extra=['X', 'Y'], grouped=True) groups = data.read_group_info('Fri') # the group that visited the least things shortest_group = kp.groupby('group_id').size().argmin() # the group that visited the most things longest_group = kp.groupby('group_id').size().argmax() # the largest group largest_group = groups['size'].argmax() df = kp[kp['group_id'] == longest_group] im = data.read_image('grey') plt.imshow(im, extent=[0, 100, 0, 100]) cmap = plt.get_cmap('rainbow') for i in range(len(df) - 1): amt = i / (len(df) - 2) color = np.array(cmap(amt)) color[:-1] *= 0.8 prev = df.iloc[i] next = df.iloc[i + 1] # tdiff = next.Timestamp - prev.Timestamp # size = 0.001 * (tdiff / np.timedelta64(1, 's')) # text = '{} -> {}'.format(prev.place_id, next.place_id) arrowprops = { 'arrowstyle': 'simple', # 'mutation_scale': size,
def prep_func(f, x, y): x = read_image(x) x = decode_png(x) x = resize(x, img_height, img_width) return f, x, y
def __init__(self): super(HeatMapWidget, self).__init__() self.im = data.read_image('Grey') self.init_ui()
def detect(): source, weights, view_img, imgsz,save_vidio,detect_image,detect_vidio,webcam,dist_thres_lim = opt.source, opt.weights, opt.view_img,\ opt.img_size,opt.save_vidio,opt.detect_img,opt.detect_vidio,\ opt.detect_webcam,opt.dist_thres_lim set_logging() device = select_device(opt.device) half = device.type != 'cpu' # half precision only supported on CUDA # Load model model = attempt_load(weights, map_location=device) # load FP33 model stride = int(model.stride.max()) # model stride imgsz = check_img_size(imgsz, s=stride) # check img_size if half: model.half() # to FP16 pipe = rs.pipeline() cfg = rs.config() if not webcam: cfg.enable_device_from_file(file_name=source) cfg.enable_stream(rs.stream.depth, 848, 480, rs.format.z16, 30) cfg.enable_stream(rs.stream.color, 848, 480, rs.format.rgb8, 30) pipe.start(cfg) a = 0 names = model.module.names if hasattr(model, 'module') else model.names colors = [[random.randint(0, 255) for _ in range(3)] for _ in names] # Run inference if device.type != 'cpu': model( torch.zeros(1, 3, imgsz, imgsz).to(device).type_as( next(model.parameters()))) # run once start = time.time() colorizer = rs.colorizer() hole_filling = rs.hole_filling_filter() try: while True: if detect_image: im0s, depth, colorized_depth = detects_img( pipe, hole_filling, colorizer) if detect_vidio: im0s, depth, colorized_depth = detects_vidio( pipe, hole_filling, colorizer) img = read_image(im0s) img = torch.from_numpy(img).to(device) img = img.half() if half else img.float() # uint8 to fp16/32 img /= 255.0 # 0 - 255 to 0.0 - 1.0 if img.ndimension() == 3: img = img.unsqueeze(0) # Inference # t1 = time_synchronized() pred = model(img, augment=opt.augment)[0] # Apply NMS pred = non_max_suppression(pred, opt.conf_thres, opt.iou_thres, classes=opt.classes, agnostic=opt.agnostic_nms) all_coords = [] # t3 = time.time() for _, det in enumerate(pred): if len(det): det = det.cpu() # Rescale boxes from img_size to im0 size det[:, :4] = scale_coords(img.shape[2:], det[:, :4], im0s.shape).round() for *xyxy, conf, cls in reversed(det): depth_temp = depth[int(xyxy[1]):int(xyxy[3]), int(xyxy[0]):int(xyxy[2])].astype(float) depth_temp = depth_temp * 0.001 dist, _, _, _ = cv2.mean(depth_temp) dist_temp = Decimal(dist).quantize(Decimal('0.000')) y_mid = (int(xyxy[1]) + int(xyxy[3])) / 2 x_mid = (xyxy[0] + xyxy[2]) / 2 len_x = np.cumsum( depth[int(y_mid), int(xyxy[0]):int(xyxy[2])])[-1] * 0.00165 len_y = np.cumsum(depth[int(xyxy[1]):int(xyxy[3]), int(x_mid)])[-1] * 0.00165 label1 = str(dist_temp) + "m" + str( int(len_x)) + "mm" + ',' + str(int(len_y)) + "mm" all_coords.append(xyxy) label = f'{names[int(cls)]} {conf:.2f}' plot_one_box(xyxy, im0s, label=label, color=colors[int(cls)], line_thickness=3) plot_dots_on_people(xyxy, im0s) plot_one_box(xyxy, colorized_depth, label=label1, color=colors[int(cls)], line_thickness=2) distancing_all(all_coords, im0s, depth=depth, dist_thres_lim=dist_thres_lim) images = np.hstack((cv2.cvtColor(im0s, cv2.COLOR_RGB2BGR), cv2.cvtColor(colorized_depth, cv2.COLOR_RGB2BGR))) a += 1 # if a %1 ==0: print("\r>>>FPS:{:.2f}<<< ".format(a / (time.time() - start)), end="") if view_img: cv2.imshow("RealSense", images) # t6 = time.time() # print("cv2imshow() time :" + str(t6 - t4)) # 按下“q”键停止q if cv2.waitKey(1) & 0xFF == ord('q'): # cv2.destroyAllWindows() break if detect_image: cv2.imwrite("out_img.png", images) break if save_vidio: if a != 600: fps, w, h = 30, images.shape[1], images.shape[0] # save_path += '.mp4' if a == 1: vid_writer = cv2.VideoWriter( save_vidio + "test.mp4", cv2.VideoWriter_fourcc(*'mp4v'), fps, (w, h)) vid_writer.write(images) # print("\r video [{}/{}] save path{} ".format(a,300,save_vidio),end="") else: print(" Done,vidio save to{}, time{:.2f}".format( save_vidio, time.time() - start)) # vid_writer.release() cv2.destroyAllWindows() break # t7 =time.time() # print('cv2.waitKey()' + str(t7 - t6)) finally: # pipe.stop() pass
def on_pick(event): # print('pick') c.fill(0) c[event.ind] = 1 draw(axs) draw_paths(event.ind) fig.canvas.draw() if timecube: image = Image.open(data.get_image_path()) image = image.resize((100, 100)) im = np.asarray(image) / 255 im_x, im_y = np.ogrid[0:im.shape[0], 0:im.shape[1]] else: im = data.read_image() files = data.read_manifold('Fri', 'category_timespans') c = np.zeros(len(files.items()[0][1]), 'int64') df = data.read_data('Fri') all_ids = df['id'].unique() all_ids.sort() gs = gridspec.GridSpec(3, 3, width_ratios=[2, 1, 1]) fig = plt.figure() if timecube: map_ax = fig.add_subplot(gs[:, 0], projection='3d') map_ax.view_init(10, 30) else: map_ax = fig.add_subplot(gs[:, 0]) draw_map_image()