def get_circled_responses(response_bounding_box: BoundingBox, response_codes: List[ResponseCode], page, list_dir) -> Tuple[List[ResponseCode], bool]: cur_response_codes = page.get_roi(response_bounding_box) diff, aligned_response_codes = get_diffed_response_codes( cur_response_codes, list_dir) # crop pixels to account for the alignment algo introducing whitespace raw_diff = diff.raw_image raw_diff = raw_diff[20:, 0:-10] raw_diff = cv2.medianBlur(raw_diff, 5) raw_diff = Image(raw_diff).threshold().grayscale().raw_image raw_diff = cv2.dilate(raw_diff, np.ones((5, 5), np.uint8), iterations=2) raw_diff = cv2.medianBlur(raw_diff, 5) raw_diff = cv2.medianBlur(raw_diff, 5) if utils.__DEBUG__: Image(raw_diff).show() contour_centers, has_error = get_circle_centers(raw_diff) circled_responses: List = [] if not has_error: circled_responses = centers_to_responses(contour_centers, response_codes, aligned_response_codes) print("Error? ", has_error) return circled_responses, has_error
def data(self): image, gray_image = self.__canvas_image() hashable = array_to_tuple(image) if hashable not in self.__image_cache: binary_image = cv2.Laplacian(gray_image[0:240, 0:205], cv2.CV_8UC1) dilated_image = cv2.dilate(binary_image, np.ones((6, 6))) _, thresh = cv2.threshold(dilated_image, constants.BINARY_THRESHOLD, 255, cv2.THRESH_BINARY) components = cv2.connectedComponentsWithStats(thresh, constants.CONNECTIVITY, cv2.CV_32S) centers = components[3] retval, threshold = cv2.threshold(thresh, 200, 255, cv2.THRESH_BINARY_INV) self.__image_cache[hashable] = threshold return Image(image, threshold) return Image(image, self.__image_cache[hashable])
def load_images_and_labels(root_data_path): hot_dog_paths, not_hot_dog_paths = load_data_paths(root_data_path) hot_dog_labels, not_hot_dog_labels = create_labels(hot_dog_paths, not_hot_dog_paths) images = [Image(path) for path in hot_dog_paths + not_hot_dog_paths] labels = [Label(value) for value in hot_dog_labels + not_hot_dog_labels] return images, labels
def triangulateTwoImages(filename1, filename2, projections_file=None, cv=False): ''' Read images from absolute filepaths and detect features, then triangulate. ''' img1 = Image(filename1) img1.detect_features() img2 = Image(filename2) img2.detect_features() pts1, pts2, matches = CVFuncs.findMatchesKnn(img1, img2, filter=True) ''' Find K ''' K = img1.K ''' Get essential or fundamental matrix ''' E, mask = CVFuncs.findEssentialMat(pts1, pts2, K) ''' Get R and T (using artificial ones for now) ''' points, r, t, newMask = CVFuncs.recoverPose(E, pts1, pts2, K) ''' Draw image projections using R and T ''' if projections_file: draw.drawProjections(pts1, pts2, K.matrix, r, t, projections_file) ''' Triangulate and draw points ''' if cv: triangulated = CVFuncs.cvTriangulate(pts1, pts2, K.matrix, r, t) else: triangulated = CVFuncs.discreteTriangulate(pts1, pts2, K.matrix, r, t) # triangulated = draw.transformPointsToViewingCoordinates(triangulated) return triangulated, r, t
def detect_image(): filepaths = save_files(request.files) output_paths = [] for file in filepaths: image = Image(file) image.detect_features() output_filename = os.path.join(OUTPUT_IMAGE_PATH, str(uuid.uuid4()) + "_kps.jpg") output = image.draw_keypoints(output_filename, orientation=True, gray=True) output_paths.append(output_filename[output_filename.find("/static"):]) print output_paths return json.dumps({"output_paths": output_paths})
def classifySeeds(self): try: if (self.currentImage is None): logging.error("Select an image first.") return resultimage = classifySeeds(self.currentImage.CV) self.showImage(Image(cv_img=resultimage)) except: error = str(sys.exc_info()[0]) function = str(inspect.stack()[0][3]) logging.error(f"Exception caught on {function}: {error}") return
def docker_image_with_cartridge(cartridge_cmd, tmpdir, project_with_cartridge, request, docker_client): project = project_with_cartridge cmd = [cartridge_cmd, "pack", "docker", project.path] process = subprocess.run(cmd, cwd=tmpdir) assert process.returncode == 0, \ "Error during creating of docker image" image_name = find_image(docker_client, project.name) assert image_name is not None, "Docker image isn't found" request.addfinalizer(lambda: delete_image(docker_client, image_name)) image = Image(image_name, project) return image
def docker_image(tmpdir, light_project, request, docker_client): project = light_project cmd = [os.path.join(basepath, "cartridge"), "pack", "docker", project.path] process = subprocess.run(cmd, cwd=tmpdir) assert process.returncode == 0, \ "Error during creating of docker image" image_name = find_image(docker_client, project.name) assert image_name is not None, "Docker image isn't found" request.addfinalizer(lambda: delete_image(docker_client, image_name)) image = Image(image_name, project) return image
def addImage(self, trigger=False, fileName=None): try: if fileName is None: fileName, _ = QFileDialog.getOpenFileName( self, "Open Image", "", "Image Files (*.png *.jpg *.bmp)") if fileName is None or fileName is "": return self.model.appendRow(Image(path=fileName)) self.listView_listImages.setModel(self.model) except: error = str(sys.exc_info()[0]) function = str(inspect.stack()[0][3]) logging.error(f"Exception caught on {function}: {error}") return
def openImage(self, trigger=False, fileName=None): try: if fileName is None: fileName, _ = QFileDialog.getOpenFileName( self, "Open Image", "", "Image Files (*.png *.jpg *.bmp)") if fileName is None or fileName is "": return self.currentImage = Image(path=fileName) self.showImage(self.currentImage) except: error = str(sys.exc_info()[0]) function = str(inspect.stack()[0][3]) logging.error(f"Exception caught on {function}: {error}") return
def docker_image(cartridge_cmd, session_tmpdir, session_light_project, request, docker_client): project = session_light_project add_runtime_requirements_file(project) cmd = [cartridge_cmd, "pack", "docker", project.path] process = subprocess.run(cmd, cwd=session_tmpdir) assert process.returncode == 0, \ "Error during creating of docker image" image_name = find_image(docker_client, project.name) assert image_name is not None, "Docker image isn't found" request.addfinalizer(lambda: delete_image(docker_client, image_name)) image = Image(image_name, project) return image
def extract_barcode_info(barcode, image: Image) -> Optional[Tuple[BoundingBox, str]]: data = barcode.data.decode("utf-8") voter_id = re.sub(r'\W+', '', data) # remove any non-alphanumeric characters # check if it's a valid voter_id id_regex_match = re.match(r'\w{10,}CA', voter_id) if id_regex_match: voter_id = id_regex_match.group(0) if utils.__DEBUG__: print('Voter ID: {}'.format(voter_id)) else: print('Invalid voter id {}, skipping.'.format(voter_id)) return None voter_id = voter_id[:-2] # remove the CA at the end (x, y, w, h) = barcode.rect barcode_bb = BoundingBox(Point(x, y), Point(x + w, y + h)) # draw image if utils.__DEBUG__: markup_image = image.raw_image # extract the the barcode pts = np.array([[[x, y] for (x, y) in barcode.polygon]], np.int32) cv2.polylines(markup_image, pts, True, (0, 0, 255), 2) # cv2.rectangle(image, (x, y), (x + w, y + h), (0, 0, 255), 2) # the barcode data is a bytes object so if we want to draw it on # our output image we need to convert it to a string first # draw the barcode data and barcode type on the image text = "{}".format(data) cv2.putText(markup_image, text, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2) Image(markup_image).show() # print the barcode type and data to the terminal print("[INFO] Found barcode: {}".format(barcode)) return barcode_bb, voter_id
def _build_content(self): """"Setup window content widges.""" container = Gtk.Box(orientation=Gtk.Orientation.VERTICAL) # Search bar self._search_bar = Gtk.SearchBar() self._search_bar.set_show_close_button(True) self._search_btn.bind_property("active", self._search_bar, "search-mode-enabled", 1) self._search_entry = Gtk.SearchEntry() self._search_entry.set_width_chars(60) self._search_entry.connect("search-changed", self._on_search) self._search_bar.add(self._search_entry) self._search_bar.connect_entry(self._search_entry) container.pack_start(self._search_bar, False, False, 0) # Preview image self._preview = Image() self._default_icon = get_default_icon(self._folders[0]) self._preview.set_icon(self._default_icon) scrolled = Gtk.ScrolledWindow() scrolled.set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC) self._flowbox.connect("child-activated", self._do_select) self._flowbox.connect("selected-children-changed", self._on_update_preview) self._flowbox.set_valign(Gtk.Align.START) self._flowbox.set_row_spacing(0) self._flowbox.set_activate_on_single_click(False) self._flowbox.set_min_children_per_line(4) self._flowbox.set_max_children_per_line(12) self._flowbox.set_selection_mode(Gtk.SelectionMode.SINGLE) scrolled.add(self._flowbox) container.pack_start(self._preview, False, False, 0) container.pack_start(scrolled, True, True, 0) self.add(container)
def match_images(): filepaths = save_files(request.files) output_paths = [] imList = [] for imageLocation in filepaths: image1 = Image(imageLocation) image1.detect_features() imList.append(image1) print imList for x in range(0, len(imList)): for y in range(x + 1, len(imList)): points1, points2, matches = CVFuncs.findMatchesKnn(imList[x], imList[y], filter=True, ratio=True) print points1 output_filename = os.path.join(OUTPUT_IMAGE_PATH, str(uuid.uuid4()) + "_match.jpg") CVFuncs.drawMatches(imList[x], imList[y], matches, output_filename) output_paths.append(output_filename[output_filename.find("/static"):]) print output_paths return json.dumps({"output_paths": output_paths})
def docker_image_print_environment(cartridge_cmd, tmpdir, project_without_dependencies, request, docker_client): project = project_without_dependencies replace_project_file(project, 'init.lua', INIT_PRINT_ENV_FILEPATH) cmd = [ cartridge_cmd, "pack", "docker", "--tag", project.name, project.path, ] process = subprocess.run(cmd, cwd=tmpdir) assert process.returncode == 0, \ "Error during creating of docker image" image_name = find_image(docker_client, project.name) assert image_name is not None, "Docker image isn't found" request.addfinalizer(lambda: delete_image(docker_client, image_name)) image = Image(image_name, project) return image
def get_circle_centers(raw_diff: np.array) -> Tuple[List[Point], bool]: # find contours in the thresholded image _, contours, hierarchy = cv2.findContours(raw_diff.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # loop over the contours, find the good ones and get the center of them raw_diff = cv2.cvtColor(raw_diff, cv2.COLOR_GRAY2BGR) mask = np.ones(raw_diff.shape[:2], dtype="uint8") * 255 contour_centers = [] has_error = False for c in contours: # Remove the contours too small to be circles area = cv2.contourArea(c) hull = cv2.convexHull(c) # compute the center of the contour M = cv2.moments(hull) center = Point(int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"])) if area < CONTOUR_LOWER_THRESH: if utils.__DEBUG__: cv2.drawContours(mask, [c], -1, 0, -1) continue elif area > CONTOUR_UPPER_THRESH: has_error = True else: cv2.circle(raw_diff, center.to_tuple(), 7, (0, 0, 255), -1) contour_centers.append(center) # show the image if utils.__DEBUG__: cv2.drawContours(raw_diff, [c], -1, (0, 255, 0), 3) raw_diff = cv2.bitwise_and(raw_diff, raw_diff, mask=mask) cv2.drawContours(raw_diff, [hull], -1, (0, 255, 0), 3) Image(raw_diff).show() return contour_centers, has_error
y_label = ["noncat", "cat"][y] imgs = [ _Image(fname) for fname in listdir_imgs( os.path.join(args.dataset, dataset_type, y_label)) ] for img in tqdm(imgs, unit=" image", total=len(imgs), disable=False): x = args.method(img) x.append(y) ans.append(x) ans = np.asarray(ans, dtype=np.float) np.random.shuffle(ans) return ans.T args = get_args() _Image = lambda fname: Image(fname, args.sigma, args.kernel_size, args. threshold) if (args.dataset == os.path.join( "..", "dataset")) and (not os.path.isdir(args.dataset)): url = "https://llpinokio-ia.herokuapp.com/cats.zip" print("downloading dataset...") download_and_extract_zip(url, args.dataset) print("preprocesing training") X_train = get_dataset_type("train") print("preprocesing evaluation") X_eval = get_dataset_type("evaluation") model_train = LogisticRegression(*X_train, alpha=args.alpha, normalize=args.normalize) model_eval = LogisticRegression(*X_eval,
def main(args): if len(args) < 2: print_help() exit() parser = argparse.ArgumentParser() parser.add_argument('mode', default=None, type=str) parser.add_argument('manual_identifier', default=None, nargs='?', type=str) parser.add_argument('-i', default=[], action='append', nargs='?', type=str) parser.add_argument('-f', default=None, type=str) parser.add_argument('-o', default='output.jpg', type=str) parser.add_argument('--scene_output', default=None, type=str) #type=argparse.FileType('w')) parser.add_argument('--projection_output', default=None, type=str) #type=argparse.FileType('w')) parser.add_argument('--silent', action='store_true') parser.add_argument('--cv', action='store_true') args = parser.parse_args(args[1:]) # current_dir = os.path.dirname(os.path.realpath(__file__)) current_dir = os.getcwd() if args.f: files = filter( lambda x: any([i in x.lower() for i in ACCEPTED_FILETYPES]), os.listdir(args.f)) files = map(lambda x: os.path.join(args.f, x), files) args.i += files args.i = map(lambda x: os.path.join(current_dir, x), args.i) mode = args.mode if mode == 'detect': if not args.silent: print 'Detecting images: {}'.format(", ".join(args.i)) print 'Outputting to: {}'.format(args.o) # if there is more than one output image if len(args.i) > 1: for x in range(len(args.i)): image = Image(args.i[x]) image.detect_features() output = image.draw_keypoints(CVFuncs.addPostToPath(args.o, x), orientation=True, gray=True) else: image = Image(args.i[0]) image.detect_features() output = image.draw_keypoints(args.o, orientation=True, gray=True) elif mode == 'match': if not args.silent: print 'Matching images: {}'.format(", ".join(args.i)) print 'Outputting to: {}'.format(args.o) imList = [] for imageLocation in args.i: image1 = Image(imageLocation) image1.detect_features() imList.append(image1) for x in range(0, len(imList)): for y in range(x + 1, len(imList)): points1, points2, matches = CVFuncs.findMatchesKnn(imList[x], imList[y], filter=True, ratio=True) # if there is more than one output image if len(imList) > 2: CVFuncs.drawMatches( imList[x], imList[y], matches, CVFuncs.addPostToPath(args.o, str(x) + "-" + str(y))) else: CVFuncs.drawMatches(imList[x], imList[y], matches, args.o) elif mode == 'triangulate': if not args.silent: print 'Triangulating images: {}'.format(args.i) if args.scene_output: print 'Outputting scene to: {}'.format(args.scene_output) if args.projection_output: print 'Outputting projections to: {}'.format( args.projection_output) track = TrackCreator(args.i) track.triangulateImages(scene_file=args.scene_output, projections_file=args.projection_output, silent=args.silent) elif mode == 'manual_pts': manual_location = args.manual_identifier triangulateManual.triangulateManualAndOutput( args.i[0], args.i[1], manual_location, output_file=args.scene_output, projections_file=args.projection_output)