コード例 #1
0
def test(path):
    model = Model()
    model.to("cuda:0")
    model.eval()
    checkpoint = torch.load("./model.pth")
    model.load_state_dict(checkpoint["model"])
    img = np.array(Image.open(path).resize([448, 448]))[np.newaxis]
    img = np.transpose(img, axes=[0, 3, 1, 2]) / 255
    img = torch.tensor(img, dtype=torch.float32).to("cuda:0")
    preds = model(img).cpu().detach().numpy()
    cell_h, cell_w = IMG_H / S, IMG_W / S
    x, y = np.meshgrid(range(S), range(S))
    preds_xywhs = []
    for i in range(B):
        preds_x = (preds[0, :, :, i * 4] + x) * cell_w
        preds_y = (preds[0, :, :, i * 4 + 1] + y) * cell_h
        preds_w = preds[0, :, :, i * 4 + 2] * IMG_W
        preds_h = preds[0, :, :, i * 4 + 3] * IMG_H
        preds_xywh = np.dstack((preds_x, preds_y, preds_w, preds_h))
        preds_xywhs.append(preds_xywh)
    preds_xywhs = np.dstack(preds_xywhs)
    preds_xywhs = np.reshape(preds_xywhs, [-1, 4])
    preds_class = preds[0, :, :, 10:]
    preds_class = np.reshape(preds_class, [-1, 20])
    preds_c = preds[0, :, :, 8:10]
    preds_c = np.reshape(preds_c, [-1, 1])
    max_arg = np.argmax(preds_c, axis=0)
    print("max confidence: %f" % (preds_c[max_arg]))
    max_arg_ = np.argmax(preds_class[int(max_arg // 2)])
    print("class confidence: %f" % (preds_class[max_arg // 2, max_arg_]))
    print("class category: %s" % (CLASSES[int(max_arg_)]))
    Image.fromarray(
        np.uint8(
            draw_bboxes(np.array(Image.open(path).resize([448, 448])),
                        preds_xywhs[max_arg[0]:max_arg[0] + 1]))).show()
コード例 #2
0
ファイル: scan.py プロジェクト: mindyh/WalklistScanner
def get_diffed_response_codes(cur_rc: Image,
                              list_dir: str) -> Tuple[Image, Image]:
    cur_rc = cur_rc.threshold()

    # Load the reference response codes, bold and not bold version.
    ref_rc = Image.from_file(list_dir + utils.RESPONSE_CODES_IMAGE_FILENAME,
                             Rotation.NONE)
    ref_rc = ref_rc.threshold()

    bold_ref_rc = Image.from_file(
        list_dir + utils.BOLD_RESPONSE_CODES_IMAGE_FILENAME, Rotation.NONE)
    bold_ref_rc = bold_ref_rc.threshold(bold_ref_rc)

    # Align and diff against both reference images.
    aligned_rc = cur_rc.align_to(ref_rc)
    bold_aligned_rc = cur_rc.align_to(bold_ref_rc)

    diff = aligned_rc.diff_against(ref_rc)
    bold_diff = bold_aligned_rc.diff_against(bold_ref_rc)

    # Count how many white pixels are in each diff.
    white_pixels = diff.numWhitePixels()
    bold_white_pixels = bold_diff.numWhitePixels()

    # The one with the least white pixels should be the correct image.
    if white_pixels < bold_white_pixels:
        return diff, aligned_rc
    else:
        return bold_diff, bold_aligned_rc
コード例 #3
0
ファイル: scan.py プロジェクト: mindyh/WalklistScanner
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
コード例 #4
0
 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])
コード例 #5
0
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})
コード例 #6
0
    def send_img(self, img_file):
        img = image.import_img(img_file, .5, self.__max_img_width,
                self.__max_img_height)

        # convert the image to binary string...
        send_d = b""
        val = 0
        shift = 7
        cnt = 0
        packages = 0
        for y in range(img.shape[0]):
            for x in range(img.shape[1]):
                if img[y][x] == 0:
                    val += (1 << shift)
                shift -= 1

                if shift == -1:
                    send_d += self.__com.int_conv(val, 1)
                    shift = 7
                    val = 0
                    cnt += 1
                if cnt == (int)(self.__max_data_length/8):
                    self.__send_px(send_d)
                    packages += 1
                    send_d = b""
                    cnt = 0
        if cnt > 0:
            self.__send_px(send_d)

        print(str(packages) + " packages send")
コード例 #7
0
    def send_img(self, img_file):
        img = image.import_img(img_file, .5, self.__max_img_width,
                               self.__max_img_height)

        # convert the image to binary string...
        send_d = b""
        val = 0
        shift = 7
        cnt = 0
        packages = 0
        for y in range(img.shape[0]):
            for x in range(img.shape[1]):
                if img[y][x] == 0:
                    val += (1 << shift)
                shift -= 1

                if shift == -1:
                    send_d += self.__com.int_conv(val, 1)
                    shift = 7
                    val = 0
                    cnt += 1
                if cnt == (int)(self.__max_data_length / 8):
                    self.__send_px(send_d)
                    packages += 1
                    send_d = b""
                    cnt = 0
        if cnt > 0:
            self.__send_px(send_d)

        print(str(packages) + " packages send")
コード例 #8
0
ファイル: app.py プロジェクト: SilvioGiancola/ImageHandler
    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
コード例 #9
0
ファイル: loading.py プロジェクト: klu1211/pytorch-toolbox
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
コード例 #10
0
    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)
コード例 #11
0
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})
コード例 #12
0
ファイル: scan.py プロジェクト: mindyh/WalklistScanner
def centers_to_responses(centers: List[Point],
                         response_codes: List[ResponseCode],
                         aligned_response_codes: Image) -> List[ResponseCode]:
    DISTANCE_THRESH = 35
    SPLIT_DIST_THRESH = 5

    selected_responses = []
    for center in centers:
        (closest_code, closest_dist) = None, 9999999
        (second_code, second_dist) = None, 9999999
        for code in response_codes:
            if utils.__DEBUG__:
                cv2.circle(aligned_response_codes.raw_image,
                           code.coords.to_tuple(),
                           4, (255, 0, 0),
                           thickness=3)

            dist = center.calc_distance(code.coords)

            if dist < DISTANCE_THRESH:
                if dist < closest_dist:
                    closest_code = code
                    closest_dist = dist
                elif dist < DISTANCE_THRESH:
                    second_code = code
                    second_dist = dist

        # The case of only appending the closest point.
        if (closest_code and not second_code) or \
           (closest_code and second_code and (second_dist - closest_dist > SPLIT_DIST_THRESH)):
            selected_responses.append(closest_code)
        # The split is too close to call, add both closest responses.
        elif (closest_code and second_code
              and (second_dist - closest_dist <= SPLIT_DIST_THRESH)):
            selected_responses.append(closest_code)
            selected_responses.append(second_code)

    if utils.__DEBUG__:
        aligned_response_codes.show()
    return selected_responses
コード例 #13
0
ファイル: app.py プロジェクト: SilvioGiancola/ImageHandler
    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
コード例 #14
0
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
コード例 #15
0
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
コード例 #16
0
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
コード例 #17
0
ファイル: app.py プロジェクト: SilvioGiancola/ImageHandler
    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
コード例 #18
0
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
コード例 #19
0
ファイル: scan.py プロジェクト: mindyh/WalklistScanner
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
コード例 #20
0
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
コード例 #21
0
ファイル: scan.py プロジェクト: mindyh/WalklistScanner
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
コード例 #22
0
ファイル: scan.py プロジェクト: mindyh/WalklistScanner
def manual_review(
        response_bounding_box: BoundingBox, page: Image,
        circled_responses: List[ResponseCode], voter_id,
        response_codes: List[ResponseCode]) -> Tuple[bool, List[ResponseCode]]:
    user_verdict = None
    top_margin = 50

    # init response_image
    responses_image = page.get_roi(response_bounding_box).add_border(
        top=top_margin)

    # get list of responses
    response_pairs = []
    if circled_responses:
        for resp in circled_responses:
            response_pairs.append('Q{}: {}'.format(resp.question_number,
                                                   resp.value))

            # add dots in the center of each highlighted response code
            cv2.circle(responses_image.raw_image,
                       (resp.coords.x, resp.coords.y + top_margin), 6,
                       (0, 0, 255), -1)

        # convert to a string
        response_string = ", ".join(response_pairs)
    else:
        response_string = 'None'

    # annotate the response image
    responses_question = "Responses: {}. Is this correct? (y|n|c|m|h|g|l)".format(
        response_string)

    # keep looping until the y or n key is pressed
    while True:
        # display the image and wait for a keypress
        key = responses_image.show(title="manual review",
                                   message=responses_question,
                                   resize=False)

        # if the 'y' key is pressed, user approves
        if key == ord("y"):
            user_verdict = True
            print('{}: Correct scan!'.format(voter_id))
            break

        # if the 'n' key is pressed, user rejects
        elif key == ord("n"):
            user_verdict = False
            print('{}: Incorrect scan :('.format(voter_id))
            circled_responses = get_correct_responses(response_codes)
            break

        # if the 'c' key is pressed, user rejects, correct answer is none
        elif key == ord("c"):
            user_verdict = False
            print('{}: Incorrect scan :('.format(voter_id))
            circled_responses = get_correct_responses(response_codes, 'c')
            break

        # if the 'm' key is pressed, user rejects, correct answer is MAT
        elif key == ord("m"):
            user_verdict = False
            print('{}: Incorrect scan :('.format(voter_id))
            circled_responses = get_correct_responses(response_codes, 'm')
            break

        # if the 'h' key is pressed, user rejects, correct answer is NH
        elif key == ord("h"):
            user_verdict = False
            print('{}: Incorrect scan :('.format(voter_id))
            circled_responses = get_correct_responses(response_codes, 'h')
            break

        # if the 'g' key is pressed, user rejects, correct answer is GTD
        elif key == ord("g"):
            user_verdict = False
            print('{}: Incorrect scan :('.format(voter_id))
            circled_responses = get_correct_responses(response_codes, 'g')
            break

        # if the 'l' key is pressed, user rejects, correct answer is MAT + NH
        elif key == ord("l"):
            user_verdict = False
            print('{}: Incorrect scan :('.format(voter_id))
            circled_responses = get_correct_responses(response_codes, 'l')
            break

    # close window
    cv2.destroyAllWindows()

    # create empty list if None
    if circled_responses is None:
        circled_responses = []

    print('correct_responses:')
    print([code.value for code in circled_responses])

    return user_verdict, circled_responses
コード例 #23
0
def main() -> None:
    args = parse_args()
    check_for_errors(args)

    if args["skip_bold"]:
        args["bold_page_number"] = None
        args["bold_line_number"] = None

    rotate_dir = utils.map_rotation(args["rotate_dir"])
    retval = ingest_clean_page(args["clean"], args["page_number"], rotate_dir,
                               args["bold_page_number"],
                               args["bold_line_number"])
    if not retval:
        print("Could not ingest the clean page.")
        return

    list_id, clean_page = retval

    num_pages = ingest_walklist(list_id, args["walklist"])
    list_dir = utils.get_list_dir(list_id)

    response_codes: List[ResponseCode] = []
    if not args["skip_markup"]:
        response_codes = markup_response_codes(clean_page, list_id,
                                               args["line_number"])
    else:
        response_codes = utils.load_response_codes(list_id)

    bounding_box = get_response_codes_bounding_box(response_codes, clean_page)

    # Normalize the response code coords
    if not args["skip_markup"]:
        for code in response_codes:
            code.coords = Point(code.coords.x - bounding_box.top_left.x,
                                code.coords.y - bounding_box.top_left.y)
        # Save out the ResponseCodes themselves.
        save_response_codes(list_id, response_codes)

        # Save out the response code image.
        rc_image_path = utils.get_list_dir(
            list_id) + utils.RESPONSE_CODES_IMAGE_FILENAME
        rc_roi = clean_page.get_roi(bounding_box)
        rc_roi.save_to_file(rc_image_path)

        # Save out the response code bounding box.
        utils.save_ref_boxes(list_dir,
                             {"response_codes": bounding_box.to_list()})

    if not args["skip_bold"]:
        # Save out the bold response code image.
        list_dir = utils.get_list_dir(list_id)
        rc_roi = Image.from_file(
            list_dir + utils.RESPONSE_CODES_IMAGE_FILENAME, Rotation.NONE)
        bold_rc_roi = Image.from_file(
            list_dir + utils.BOLD_RESPONSE_CODES_IMAGE_FILENAME, Rotation.NONE)
        aligned_bold_rc_roi = bold_rc_roi.align_to(rc_roi)

        if utils.__DEBUG__:
            aligned_bold_rc_roi.show()

        bold_rc_image_path = list_dir + utils.BOLD_RESPONSE_CODES_IMAGE_FILENAME
        aligned_bold_rc_roi.save_to_file(bold_rc_image_path)

    print("Saved out reference response codes.")
    print("Done, now run scan.py")
コード例 #24
0
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

# добавлем родительский каталог в пути посика модуле
# чтобы был виден модуль utils
import os
import sys
sys.path = [os.pardir] + sys.path

# чтобы можно было загрузить изображение по частям
from utils import Image

# чтобы перемешать части изображения
import random

orig = Image.open('cat.png')

img = Image.new(orig.x_size, orig.y_size)

parts = [orig[i] for i in range(len(orig))]
# parts = [image for image in orig]

random.shuffle(parts)

for i in range(len(orig)):
    img[i] = parts[i]

img.save('cat_break.png')
img.show('cat_break')
コード例 #25
0
ファイル: app.py プロジェクト: SilvioGiancola/ImageHandler
class ImageHandlerApp(QMainWindow, Ui_ImageViewer):
    def __init__(self):
        # Explaining super is out of the scope of this article
        # So please google it if you're not familar with it
        # Simple reason why we use it here is that it allows us to
        # access variables, methods etc in the design.py file
        super().__init__()
        self.setupUi(self)  # This is defined in design.py file automatically

        fh = logging.FileHandler('log.log')  # create File Logger
        ch = logging.StreamHandler()  # create Stream Logger
        gh = GuiLogger(self.textBrowser_Logging)  # create GUI Logger
        formatter = logging.Formatter(
            '[%(asctime)s][%(levelname)s] %(message)s')
        fh.setFormatter(formatter)
        ch.setFormatter(formatter)
        gh.setFormatter(formatter)
        logging.getLogger().addHandler(fh)
        logging.getLogger().addHandler(ch)
        logging.getLogger().addHandler(gh)
        logging.getLogger().setLevel(logging.DEBUG)

        self.model = QStandardItemModel(self.listView_listImages)
        self.currentImage = None

        self.listView_listImages.doubleClicked.connect(self.on_item_changed)
        self.actionDetect_Seeds.triggered["bool"].connect(self.detectSeeds)
        self.actionClassify_Seeds.triggered["bool"].connect(self.classifySeeds)
        self.pushButton_addImages.clicked["bool"].connect(self.addImages)
        self.pushButton_removeImage.clicked["bool"].connect(
            self.removeCurrentImage)
        self.pushButton_addFolder.clicked["bool"].connect(self.addFolder)
        self.pushButton_removeAllImages.clicked["bool"].connect(
            self.removeAllImages)

    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 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 addImages(self, trigger=False, fileNames=None):
        try:
            if fileNames is None:
                fileNames, _ = QFileDialog.getOpenFileNames(
                    self, "Open Image", "", "Image Files (*.png *.jpg *.bmp)")
            if fileNames is []:
                return

            for fileName in fileNames:
                self.addImage(fileName=fileName)
        except:
            error = str(sys.exc_info()[0])
            function = str(inspect.stack()[0][3])
            logging.error(f"Exception caught on {function}: {error}")

        return

    def addFolder(self, trigger=False, folder=None):
        try:
            if folder is None:
                folder = QFileDialog.getExistingDirectory(
                    self, "Select Folder", "")
            if folder is "":
                return
            valid_images = [".jpg", ".gif", ".png", ".tga"]
            for f in os.listdir(folder):
                ext = os.path.splitext(f)[1]
                if ext.lower() not in valid_images:
                    continue

                self.addImage(fileName=os.path.join(folder, f))
        except:
            error = str(sys.exc_info()[0])
            function = str(inspect.stack()[0][3])
            logging.error(f"Exception caught on {function}: {error}")

        return

    def removeCurrentImage(self, trigger=False):
        try:
            # for item in model.findItems('something'):
            if (self.currentImage is None):
                logging.error("Select an image to remove.")
                return
            self.model.removeRow(self.currentImage.row())
            self.listView_listImages.setModel(self.model)
            self.showImage()
        except:
            error = str(sys.exc_info()[0])
            function = str(inspect.stack()[0][3])
            logging.error(f"Exception caught on {function}: {error}")

    def removeAllImages(self, trigger=False):
        try:
            print(self.model.rowCount())
            self.model.removeRows(0, self.model.rowCount())
            self.listView_listImages.setModel(self.model)
            self.showImage()
        except:
            error = str(sys.exc_info()[0])
            function = str(inspect.stack()[0][3])
            logging.error(f"Exception caught on {function}: {error}")

    def on_item_changed(self, index):
        try:
            print("on_item_changed")
            self.currentImage = self.model.itemFromIndex(index)
            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 detectSeeds(self):
        try:
            if (self.currentImage is None):
                logging.error("Select an image first.")
                return
            resultimage, good_cnt = detectSeeds(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 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 showImage(self, Image=None):
        try:
            if Image is None:
                self.currentImage = None
                self.label.setText("Image")
                return

            pixmap01 = QPixmap.fromImage(Image.QT)
            pixmap_image = QPixmap(pixmap01)
            self.label.setPixmap(pixmap_image)
            self.label.setAlignment(QtCore.Qt.AlignCenter)
            self.label.setScaledContents(True)
            self.label.setMinimumSize(100, 100)
        except:
            error = str(sys.exc_info()[0])
            function = str(inspect.stack()[0][3])
            logging.error(f"Exception caught on {function}: {error}")

        return
コード例 #26
0
ファイル: feature.py プロジェクト: gmelchett/terra
 def __init__(self, image_name, forest_size):
     Image.__init__(self, image_name)
     self.forest_size = forest_size
コード例 #27
0
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)
コード例 #28
0
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,
        )
コード例 #29
0
 def __init__(self, image_name, forest_size):
     Image.__init__(self, image_name)
     self.forest_size = forest_size
コード例 #30
0
ファイル: terrain.py プロジェクト: gmelchett/terra
 def __init__(self, image_name):
     Image.__init__(self, image_name)
     self.size = (TERRAIN_WIDTH, TERRAIN_HEIGHT)
     self.items = (self.surface.get_width() / self.size[0],
                   self.surface.get_height() / self.size[1])
コード例 #31
0
ファイル: terrain.py プロジェクト: gmelchett/terra
 def __init__(self, image_name):
     Image.__init__(self, image_name)
     self.size = (TERRAIN_WIDTH, TERRAIN_HEIGHT)
     self.items = (self.surface.get_width()/self.size[0], self.surface.get_height()/self.size[1])
コード例 #32
0
        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,
コード例 #33
0
class FolderIconChooser(Gtk.Window, GObject.GObject, Thread):
    """
        FolderIcon Chooser Class
    """
    __gsignals__ = {
        'selected': (GObject.SIGNAL_RUN_FIRST, None, (str, )),
        'loaded': (GObject.SIGNAL_RUN_FIRST, None, ()),
    }

    def __init__(self, folders):
        GObject.GObject.__init__(self)
        Thread.__init__(self)
        Gtk.Window.__init__(self)
        self.connect("delete-event", self._close_window)
        # Here i assume that all folders got the same icon...
        self._folders = folders
        self.model = []
        self._flowbox = Gtk.FlowBox()

        # Threading stuff
        self.setDaemon(True)
        self.run()

        # Window configurations
        self.set_default_size(650, 500)
        self.set_size_request(650, 500)
        self.set_resizable(True)
        self.set_position(Gtk.WindowPosition.CENTER_ON_PARENT)
        self.connect("key-press-event", self._on_key_press)

        # Widgets & Accelerators
        self._build_header_bar()
        self._build_content()
        self._setup_accels()

    def emit(self, *args):
        # Use idle_add to make it possible to use emit within a Thread
        GLib.idle_add(GObject.GObject.emit, self, *args)

    def run(self):
        """Threading run method."""
        # Load the completion entries
        self.model = []
        # List all the places icons
        theme = Gtk.IconTheme.get_default()
        folders = theme.list_icons('Places')
        folders.sort()
        # Fill in the model (str: icon path, pixbuf)
        theme = Gtk.IconTheme.get_default()
        for folder in folders:
            pixbuf = load_pixbuf(theme, folder)
            if pixbuf:
                self.model.append({"path": folder, "pixbuf": pixbuf})
        self.emit("loaded")
        return False

    def do_loaded(self):
        """loaded signal handler."""
        for entry in self.model:
            child = FolderBox(entry["path"], entry["pixbuf"])
            self._flowbox.add(child)

    def _build_header_bar(self):
        """Setup window headerbar."""
        # Header bar
        headerbar = Gtk.HeaderBar()
        headerbar_container = Gtk.Box(orientation=Gtk.Orientation.VERTICAL,
                                      spacing=3)

        title = Gtk.Label()
        title.set_text(_("Icon Chooser"))
        title.get_style_context().add_class("title")
        headerbar_container.pack_start(title, False, False, 0)

        subtitle = Gtk.Label()
        subtitle.get_style_context().add_class("subtitle")
        subtitle_text = ", ".join(self._folders)
        subtitle.set_text(subtitle_text)
        subtitle.set_ellipsize(Pango.EllipsizeMode.END)
        subtitle.set_tooltip_text(subtitle_text)
        subtitle.props.max_width_chars = 30
        headerbar_container.pack_start(subtitle, False, False, 0)

        headerbar.set_custom_title(headerbar_container)
        headerbar.set_show_close_button(False)

        # Search Button
        self._search_btn = Gtk.ToggleButton()
        search_icn = Gio.ThemedIcon(name="system-search-symbolic")
        search_img = Gtk.Image.new_from_gicon(search_icn, Gtk.IconSize.BUTTON)
        self._search_btn.set_image(search_img)

        # Cancel Button
        cancel_button = Gtk.Button()
        cancel_button.set_label(_("Cancel"))
        cancel_button.connect("clicked", self._close_window)

        headerbar.pack_start(cancel_button)
        headerbar.pack_end(self._search_btn)
        self.set_titlebar(headerbar)

    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 _setup_accels(self):
        """Setup accels."""
        self._accels = Gtk.AccelGroup()
        self.add_accel_group(self._accels)

        key, mod = Gtk.accelerator_parse("Escape")
        self._accels.connect(key, mod, Gtk.AccelFlags.VISIBLE,
                             self._close_window)

        key, mod = Gtk.accelerator_parse("Return")
        self._accels.connect(key, mod, Gtk.AccelFlags.VISIBLE, self._do_select)

        key, mod = Gtk.accelerator_parse("<Control>F")
        self._accels.connect(key, mod, Gtk.AccelFlags.VISIBLE,
                             self._toggle_search)

    def _get_selected_icon(self):
        """Return the selected icon name."""
        icon_name = self._flowbox.get_selected_children()[0].name
        return icon_name

    def _on_update_preview(self, *args):
        icon_name = self._get_selected_icon()
        self._preview.set_icon(icon_name)

    def _do_select(self, *args):
        self.emit("selected", self._get_selected_icon())

    def _on_key_press(self, window, event):
        self._search_bar.handle_event(event)

    def _on_search(self, *args):
        """On search signal handler."""
        data = self._search_entry.get_text().strip()
        self._flowbox.set_filter_func(self._filter_func, data, True)

    def _close_window(self, *args):
        """Handle the destroy/delete-event signal."""
        # Hide the search bar when the user hits Escape
        is_cancel_btn = not isinstance(args[0], Gtk.AccelGroup)
        if self._search_bar.get_search_mode() and not is_cancel_btn:
            self._search_bar.set_search_mode(False)
        else:
            self.destroy()

    def _toggle_search(self, *args):
        """Toggle the search bar."""
        self._search_bar.set_search_mode(
            not self._search_bar.get_search_mode())

    def _filter_func(self, row, data, notify_destroy):
        """Filter func used to filter FlowBoxChild's."""
        folder_icon_name = row.name
        if data:
            split_data = data.split(" ")
            found = True
            for string in split_data:
                found = string.lower() in folder_icon_name
            return found
        else:
            return True