def video_loop(self): try: # keep looping over frames until we are instructed to stop while not self.stopVideo.is_set(): # grab the frame from the video stream _, frame = self.vc.read() frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # image processing is done by caffe before feeding to CNN results = segment(frame, pad=self.padding) class_map = utils.get_pixel_map(results) class_map = Image.fromarray(class_map) class_map = ImageTk.PhotoImage(class_map) # if the panel is not None, we need to initialize it if self.panel is None: self.panel = tk.Label(image=class_map) self.panel.image = class_map self.panel.pack(side="left", padx=10, pady=10) # otherwise, simply update the panel else: self.panel.configure(image=class_map) self.panel.image = class_map except RuntimeError: print("[INFO] caught a RuntimeError")
def segment( self, map_type ): # consider renaming to avoid confusion with gpu_segment.segment """ Function for performing and plotting segmentation :return: """ # Check if there are any Canvas objects in GUI children and destroy them for child in list(self.root.children.values()): if child.widgetName == 'canvas': child.destroy() try: # convert current frame to RGB for processing by 'gpu_segment' frame = cv2.cvtColor(self.frame, cv2.COLOR_BGR2RGB) # image processing is done by caffe before feeding to CNN results = segment(frame, pad=self.padding, caffemodel=self.model) engine = utils.PlottingEngine() engine.set_colormap( map_type=map_type, freq=1000) # Have colormap set by button in GUI in future pixels, colorbar, _ = engine.process(results) image = Image.fromarray(pixels) image = ImageTk.PhotoImage(image) colorbar = FigureCanvasTkAgg(colorbar, master=self.root) colorbar.get_tk_widget().pack(side="right", padx=10, pady=10) # Update panel with segmented image. self.panel.configure(image=image) self.panel.image = image self.root.update_idletasks() except RuntimeError: print("[INFO] caught a RuntimeError")
# Script to test GPU segmentation with pyramidal method. import sys import numpy as np from material_segmentation import minc_plotting as minc_plot from material_segmentation.gpu_segment import segment import skimage image_path = sys.argv[1] # path to image to be segmented # Equivalent to caffe.io.load_image(image_path) orig_image = skimage.img_as_float(skimage.io.imread( image_path, as_grey=False)).astype(np.float32) padding = 0 results = segment(orig_image, pad=padding) # minc_plot.plot_probability_maps(av_prob_maps, results) minc_plot.plot_class_map(results) minc_plot.plot_confidence_map(results)
if __name__ == "__main__": log.basicConfig(format="[ %(levelname)s ] %(message)s", level=log.INFO, stream=sys.stdout) # Configure logging args = build_argparser().parse_args() if args.input == 'cam': input_stream = 0 else: input_stream = args.input assert os.path.isfile(args.input), "Specified input file doesn't exist" cap = cv2.VideoCapture(input_stream) window_result = cv2.namedWindow("class map") window_frame = cv2.namedWindow("frame") while cap.isOpened(): ret, frame = cap.read() # convert current frame to RGB for processing by 'gpu_segment' RGBframe = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) results = segment(RGBframe, args.padding, args.model) class_map = utils.get_pixel_map(results) cv2.imshow('class map', class_map) cv2.imshow('frame', frame) cv2.waitKey(10)
parser.add_argument("-i", "--image", type=str, help="path to image to be classified") parser.add_argument("--caffemodel", type=str, default="models/minc-googlenet-conv.caffemodel", help="path to caffemodel file") parser.add_argument( "--padding", "-p", type=int, default=0, help="number of pixels to pad image with before segmenting") parser.add_argument("--path", type=str, default="plots") args = parser.parse_args() image = caffe.io.load_image( args.image ) # must load image using caffe.io.load_image(). note this outputs RGB image if args.mode == "segment": output = gpu_segment.segment(image, caffemodel=args.caffemodel, pad=args.padding) else: # Classify output = gpu_segment.classify(image, caffemodel=args.caffemodel) minc_plotting.plot_output(output)