def run(self): self.keepRunning = True self.worstTime = 0.0 self.meanTime = 0.0 self.counter = 0 """ Image Processor is the context for the image processing algorithms we are developing. We can plug and play with any of the implemented algorithms by dorpping them into the Image Processor constructor or using the setAlgorithm method. """ self.imgProcessor = ImageProcessor(BasicAlgorithm(self.sharedData)) while self.keepRunning: e1 = cv2.getTickCount() self.imgProcessor.processImage() e2 = cv2.getTickCount() t = (e2 - e1) / cv2.getTickFrequency() self.meanTime += t self.counter += 1 if t > self.worstTime: self.worstTime = t print("Worst img processsing time took: " + str(self.worstTime)) #TODO Debugging only print("Mean img processsing time took: " + str(self.meanTime / self.counter))
def reload(self): """Overrides because we need to reset img_flattened, add panel markers and remove dots""" ImageProcessor.reload(self) self._img_flattened = False self.__clicks = [] self._make_panel_dots()
def __init__(self, master: Tk, panels_in: Dict[str, List[Tuple[int, int]]], save_to: str): """ :param master: The root for this Tk window :param panels_in: a dictionary whose keys are absolute paths to the images and whose values are a list of pixel cords of panels in said images. :param save_to: The path to the folder to save images to. """ self.__panels_in = panels_in self._img_flattened = False self.__num_saved = 0 self.__dim = None self.__clicks = [] self.__saved = dict() # Toolbar toolbar = Frame(master) reload_button = Button(toolbar, text="Reload", command=self.reload) save_button = Button(toolbar, text="Save", command=self.save) next_button = Button(toolbar, text="Next", command=self.next_file) reload_button.pack(side=LEFT, padx=2, pady=2) save_button.pack(side=LEFT, padx=2, pady=2) next_button.pack(side=LEFT, padx=2, pady=2) toolbar.pack(side=TOP, fill=X) ImageProcessor.__init__(self, master, list(panels_in.keys()), save_to) self.movable_image.canvas.bind('<Button-1>', self.left_mouse_down) self._master.after(200, self.reload)
def _extract_training_images_from_path(self, csv_data_path, raw_dimensions, csv_image_col, csv_label_col): """ Extracts training images from csv file found in user-supplied directory path :param csv_data_path: path to directory containing image data csv file supplied by user """ dataLoader = DataLoader(from_csv=True, target_labels=[0,1,2,3,4,5,6], datapath=csv_data_path, image_dimensions=raw_dimensions, csv_label_col=csv_label_col, csv_image_col=csv_image_col) images, labels = dataLoader.get_data() imageProcessor = ImageProcessor(images, target_dimensions=self.target_dimensions) images = imageProcessor.process_training_data() self.train_images = images self.y_train = labels if self.verbose: print(images.shape) print(labels.shape)
def render(self, context): resolved_options = dict( zip([str(opt) for opt in self.options.keys()], [self.options[v].resolve(context) for v in self.options]) ) size = [int(z.resolve(context)) for z in self.size] path = str(self.path.resolve(context)) if not path: raise TemplateSyntaxError("Provided image path is empty") if not os.path.isabs(path): path = os.path.join(settings.MEDIA_ROOT, path) try: processor = ImageProcessor() processor.add_filter(Image.Image.thumbnail, size, resample=Image.ANTIALIAS) return "%s%s" % ( settings.THUMBNAIL_PREFIX, os.path.basename(self.thumbnails_cache.get_image_file(processor, path)), ) except (IOError, OSError), e: if not settings.FAIL_SILENTLY: raise e return ""
def fit(self, features, labels, validation_split, batch_size=10, epochs=20): """ Trains the neural net on the data provided. :param features: Numpy array of training data. :param labels: Numpy array of target (label) data. :param validation_split: Float between 0 and 1. Percentage of training data to use for validation :param batch_size: :param epochs: Max number of times to train on input data. """ self.regression_model.compile(loss="mean_squared_error", optimizer="rmsprop", metrics=["accuracy"]) self.regression_model.fit(features, labels, batch_size=batch_size, epochs=epochs, validation_split=0.0, shuffle=True) regression_predictions = self.regression_model.predict(features) imageProcessor = ImageProcessor() features, labels = imageProcessor._get_time_delay_training_data( regression_predictions, regression_predictions) self.model.compile(optimizer="RMSProp", loss="cosine_proximity", metrics=["accuracy"]) self.model.fit( features, labels, batch_size=batch_size, epochs=epochs, validation_split=validation_split, callbacks=[ReduceLROnPlateau(), EarlyStopping(patience=3)])
def draw_comparison_image(processor: ImageProcessor, save_folder_path: str) -> None: annotator = processor.annotator annotator.set_save_location(os.path.join(save_folder_path, processor.name)) lines: list = processor.get_list_of_lines() for line in lines: sorted_vertices: list = line['bounding_box'] box_color = (0, 0, 0) # black annotator.draw_polygon(sorted_vertices, box_color) words: list = processor.get_list_of_words() for word in words: sorted_vertices: list = word['bounding_box'] start_color = (0, 200, 0) # green end_color = (0, 0, 230) # red annotator.draw_line(sorted_vertices[0], sorted_vertices[3], start_color, 2) annotator.draw_line(sorted_vertices[1], sorted_vertices[2], end_color, 2) annotator.save_annotated_image_to_file()
def __init__(self, *args, **kwargs): # Create camera and start stream self.cam = cam.CameraStream().start() # Create user interface self.userinterface = gui.UserInterface(self) # Add RUN function to button 1 to start Scan self.start_page = self.userinterface.frames[ self.userinterface.StartPage] self.start_page.add_function_to_button1(self.run) self.shot_frame = [[] for x in range(200)] # Create Stepper and add function to button 4 self.stepper = step.Stepper("/dev/ttyACM0") self.start_page.add_function_to_button4(self.stepper.moveBack) self.stepper_set = 0 self.start_page.add_function_to_button2(self.stepper.moveStep) # Create image processor self.img_proc = ImageProcessor()
def moveWithCV(showMovie=False): ip = ImageProcessor() vid = VideoStream(src=0).start() time.sleep(2.0) while True: frame = vid.read() if frame is None: break frame = imutils.resize(frame, width=600) if showMovie: if ip.contour is not None: cv2.drawContours(frame, [ip.contour], 0, (0, 255, 0), 3) x, y = ip.getPositionFromFrame(frame) if showMovie: cv2.putText(frame, "x: {}, y: {}".format(math.floor(x), math.floor(y)), (100, 50), cv2.FONT_HERSHEY_SIMPLEX, 2, (255, 255, 255), 3) cv2.imshow("Test", frame) key = cv2.waitKey(1) & 0xFF if key == ord("q"): robo.chill() break
from imageprocessor import ImageProcessor from neuralnets import TransferLearningNN verbose = True target_dimensions = (128, 128) raw_dimensions = (48, 48) target_labels = [0,1,2,3,4,5,6] model_name = 'inception_v3' print('Loading data...') csv_file_path = "image_data/sample.csv" dataLoader = DataLoader(from_csv=True, target_labels=target_labels, datapath=csv_file_path, image_dimensions=raw_dimensions, csv_label_col=0, csv_image_col=1) images, labels = dataLoader.get_data() if verbose: print('raw image shape: ' + str(images.shape)) print('Processing data...') imageProcessor = ImageProcessor(images, target_dimensions=target_dimensions, rgb=True) images = imageProcessor.process_training_data() if verbose: print ('processed image shape: ' + str(images.shape)) print('--------------- Inception-V3 Model -------------------') print('Initializing neural network with InceptionV3 base model...') model = TransferLearningNN(model_name=model_name, target_labels=target_labels) print('Training model...') print('numLayers: ' + str(len(model.model.layers))) model.fit(images, labels, 0.15)
SetForegroundWindow, ShowWindow) from imageprocessor import ImageProcessor from HLMVModel import HLMVModel if __name__ == '__main__': number_of_images = 24 # Y rotations vertical_rotations = 1 # X rotations # Initial parameters. Mostly, you won't need to set these. model = HLMVModel({ 'rotation': None, 'translation': None, 'rotation_offset': None, 'vertical_offset': None }) ip = ImageProcessor(number_of_images, vertical_rotations) def enum_callback(hwnd, _): """ Focus and maximise HLMV then compute the cropping boundary based on its resulting size """ if GetWindowText(hwnd)[:7] == 'models\\': SetForegroundWindow(hwnd) ShowWindow(hwnd, SW_MAXIMIZE) rect = GetWindowRect(hwnd) global crop_boundary crop_boundary = ( rect[0] + 10, # Left edge <-> image left rect[1] + 51, # Top edge <-> image top rect[2] - 10, # Left edge <-> image right
def copy(args): image_processor = ImageProcessor(args.dir, args.save_dir, process=False) image_processor.process_dir()
def process_dir(args): image_processor = ImageProcessor(args.dir, args.save_dir) image_processor.process_dir()
from neuralnets import TransferLearningNN from skimage import color, io import numpy as np from matplotlib import pyplot as plt verbose = True target_dimensions = (128, 128) raw_dimensions = (48, 48) target_labels = [0, 1, 2, 3, 4, 5, 6] model_name = 'inception_v3' print('Extracting training data...') csv_file_path = "image_data/sample.csv" imageProcessor = ImageProcessor(from_csv=True, target_labels=target_labels, datapath=csv_file_path, target_dimensions=target_dimensions, raw_dimensions=raw_dimensions, csv_label_col=0, csv_image_col=1) features, labels = imageProcessor.get_training_data() print('--------------- Inception-V3 Model -------------------') print('Initializing neural network with InceptionV3 base model...') model = TransferLearningNN(model_name=model_name, target_labels=target_labels) print('Training model...') print('numLayers: ' + str(len(model.model.layers))) model.fit(features, labels, 0.15)
from imageprocessor import ImageProcessor from neuralnets import TimeDelayNN from featureextractor import FeatureExtractor # ------------- PARAMETERS -------------- # verbose = True target_dimensions = (128, 128) raw_dimensions = (48, 48) target_labels = [0, 1, 2, 3] print('--------------- Regression + TimeDelayNN Model -------------------') print('Collecting data...') root_directory = 'image_data/sample_directory' imageProcessor = ImageProcessor(from_csv=False, target_labels=target_labels, datapath=root_directory, target_dimensions=target_dimensions, raw_dimensions=None) images, labels = imageProcessor.get_training_data() print('images shape: ' + str(images.shape)) print('labels shape: ' + str(labels.shape)) print('Extracting features...') featureExtractor = FeatureExtractor(images, return_2d_array=False) featureExtractor.add_feature('hog', { 'orientations': 8, 'pixels_per_cell': (16, 16), 'cells_per_block': (1, 1) }) features = featureExtractor.extract() print("features shape: " + str(features.shape))
class BabyScannerApp(): def __init__(self, *args, **kwargs): # Create camera and start stream self.cam = cam.CameraStream().start() # Create user interface self.userinterface = gui.UserInterface(self) # Add RUN function to button 1 to start Scan self.start_page = self.userinterface.frames[ self.userinterface.StartPage] self.start_page.add_function_to_button1(self.run) self.shot_frame = [[] for x in range(200)] # Create Stepper and add function to button 4 self.stepper = step.Stepper("/dev/ttyACM0") self.start_page.add_function_to_button4(self.stepper.moveBack) self.stepper_set = 0 self.start_page.add_function_to_button2(self.stepper.moveStep) # Create image processor self.img_proc = ImageProcessor() def restart(self): global stopFlag stopFlag = True def run(self): global count global stopFlag self.start_page.start_cam_button.config(state='disabled') self.start_page.update_idletasks() stopFlag = True if stopFlag is True: # Set interval of 90ms for calling this function again after_id = self.userinterface.after(90, self.run) #If stepper hasnt been called to move, call to move if self.stepper_set is 0: step_status = self.stepper.moveStep() self.stepper_set = 1 print(step_status) if "MOVE CMD" in step_status: print("Stepper is moving...") self.stepper_set = 2 # If stepper asnwered to the move call, start taking pictures with interval if self.stepper_set is 2: # Update gui self.start_page.label.config( text="Retrieving frame %d of 140" % (count)) self.start_page.update_idletasks() # Store frame from camera self.shot_frame[count] = self.cam.read() count = count + 1 # 140 is the max count of frames until stepper is near the end point if count == 140: self.stepper_set = 0 self.userinterface.after_cancel(after_id) self.process() def process(self): global stopFlag stopFlag = False global count x = 0 # Check if there is already a coordinates file, if so: delete to make a new one coord_filename = "data/coordinates.csv" try: os.remove(coord_filename) except OSError: pass my_file = open(coord_filename, "w") # Lists to store every coordinate in z_coord = [[] for x in range(200)] x_coord = [0] * 200 y_coord = [[] for x in range(200)] # Go through all photo frames for n in list(range(count)): # Add 3 millimeters after every photo x_coord[n] = x x = x + 3 # Update Gui with status self.start_page.label.config(text="Processing image %d of 140" % (n + 1)) self.start_page.update_idletasks() # Get XYZ coords image = self.shot_frame[n] x_coord[n], y_coord[n], z_coord[n] = self.img_proc.getXYZ(image, n) # Write XYZ coords to CSV file for h in range(0, 256, 1): my_file.write( str(x_coord[n]) + ',' + str(y_coord[n][h]) + ',' + str(z_coord[n][h]) + '\n') print("Processing image %d of 130\r" % (n)) print("\n") print("Scanning done") # Update GUI with button states self.start_page.start_cam_button.config(state='normal') self.start_page.but3_cam.config(state='normal') self.start_page.update_idletasks() self.start_page.label.config(text="done - idle") self.start_page.update_idletasks() # Reset the taken frames count for next scan count = 0
# I.e., the idea is to create separate image processors when concurrent pipelines # are desired (e.g., look for faces AND pink elephants at the same time), and place # the exclusive options into a single processor (e.g., look for faces OR pink elephants) faces = Faces() # NOTE: NOTE: NOTE # # Reminder that each image processor should process exactly one vision pipeline # at a time (it can be selectable in the future) and that the same vision # pipeline should NOT be sent to different image processors as this is simply # confusing and can cause some comingling of data (depending on how the vision # pipeline was defined... we can't control the use of object-specific internals # being run from multiple threads... so don't do it!) bucketProcessor = ImageProcessor(bucketCam, faces).start() print("Waiting for ImageProcessors to start...") while (bucketProcessor.isStopped() == True): time.sleep(0.001) print("ImageProcessors appear online!") # Continue feeding display or streams in foreground told to stop fps = FrameRate() # Keep track of display rate TODO: Thread that too! fps.start() # Loop forever displaying the images for initial testing # # NOTE: NOTE: NOTE: NOTE: # cv2.imshow in Linux relies upon X11 binding under the hood. These binding are NOT inherently thread