Python imutils.rotate_bound() Examples

The following are 9 code examples of imutils.rotate_bound(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module imutils , or try the search function .
Example #1
Source File: vision.py    From Gap with Apache License 2.0 6 votes vote down vote up
def rotate(self, degree):
        """ rotate the image """
        if not isinstance(degree, int):
            raise ValueError("Degree must be an integer")

        if degree <= -360 or degree >= 360:
            raise ValueError("Degree must be between -360 and 360")

        # rotate the image
        rotated = imutils.rotate_bound(self._imgdata, degree)

        # resize back to expected dimensions
        if degree not in [0, 90, 180, 270, -90, -180, -270]:
            # resize takes only height x width
            shape = (self._imgdata.shape[0], self._imgdata.shape[1])
            rotated = cv2.resize(rotated, shape, interpolation=cv2.INTER_AREA)
        return rotated 
Example #2
Source File: convert_fddb_to_json.py    From detectron-self-train with MIT License 5 votes vote down vote up
def load_vid(vid_file,rotation=None):
    vid = []
    if rotation is None:
        rotation = _ffmpeg_extract_rotation(vid_file)
    videogen = cv2.VideoCapture(vid_file)
    while True:
        ret,im = videogen.read()
        if not ret:
            break
        im = imutils.rotate_bound(im,rotation-360)
        vid.append(im)
    videogen.release()
    return vid 
Example #3
Source File: convert_fddb_to_json.py    From detectron-self-train with MIT License 5 votes vote down vote up
def load_vid(vid_file,rotation=None):
    vid = []
    if rotation is None:
        rotation = _ffmpeg_extract_rotation(vid_file)
    videogen = cv2.VideoCapture(vid_file)
    while True:
        ret,im = videogen.read()
        if not ret:
            break
        im = imutils.rotate_bound(im,rotation-360)
        vid.append(im)
    videogen.release()
    return vid 
Example #4
Source File: fast_convert_fddb_to_json.py    From detectron-self-train with MIT License 5 votes vote down vote up
def save_vid_frames(vid_name,vid_file_path,frame_list,save_det_dir,rotation=None):
    vid = load_vid(vid_file_path,rotation=rotation)
    for frame_id_str in frame_list:
        frame_id = int(frame_id_str)
        frame = imutils.rotate_bound(vid[frame_id],rotation-360)
        cv2.imwrite(os.path.join(save_det_dir,vid_name+'_'+frame_id_str+'.jpg'),frame)
    del(vid) 
Example #5
Source File: main_dlib.py    From snapchat-filters-opencv with MIT License 5 votes vote down vote up
def apply_sprite(image, path2sprite, w, x, y, angle, ontop=True):
    sprite = cv2.imread(path2sprite, -1)
    # print sprite.shape
    sprite = rotate_bound(sprite, angle)
    (sprite, y_final) = adjust_sprite2head(sprite, w, y, ontop)
    image = draw_sprite(image, sprite, x, y_final)


# points are tuples in the form (x,y)
# returns angle between points in degrees 
Example #6
Source File: dog.py    From voice-enabled-chatbot with MIT License 5 votes vote down vote up
def apply_sprite(image, path2sprite,w,x,y, angle, ontop = True):
    sprite = cv2.imread(path2sprite,-1)
    #print sprite.shape
    sprite = rotate_bound(sprite, angle)
    (sprite, y_final) = adjust_sprite2head(sprite, w, y, ontop)
    image = draw_sprite(image,sprite,x, y_final)

#points are tuples in the form (x,y)
# returns angle between points in degrees 
Example #7
Source File: read_xml_correct_rotation_left.py    From Label-Annotation-VOC-Pascal with MIT License 5 votes vote down vote up
def rotate_file_image(filename, Alpha):
    img=cv2.imread(filename,1)
    dst=imutils.rotate_bound(img,Alpha)
    w_n,h_n,d_n=dst.shape
    file_rotate_image=str(Alpha)+filename
    Alpha=-1*Alpha
    cv2.imwrite(file_rotate_image,dst)
    file_annotate=filename.replace("png", "xml")
    file_annotate_rotate=file_rotate_image.replace("png", "xml")
    copy_file(file_annotate,file_annotate_rotate)
    rotate_xml(file_annotate_rotate, Alpha,w_n, h_n) 
Example #8
Source File: read_xml_correct_rotation.py    From Label-Annotation-VOC-Pascal with MIT License 5 votes vote down vote up
def rotate_file_image(filename, Alpha):
    img=cv2.imread(filename,1)
    dst=imutils.rotate_bound(img,Alpha)
    w_n,h_n,d_n=dst.shape
    file_rotate_image=str(Alpha)+filename
    cv2.imwrite(file_rotate_image,dst)
    file_annotate=filename.replace("png", "xml")
    file_annotate_rotate=file_rotate_image.replace("png", "xml")
    copy_file(file_annotate,file_annotate_rotate)
    rotate_xml(file_annotate_rotate, Alpha,w_n, h_n) 
Example #9
Source File: solver.py    From captcha-middleware with GNU General Public License v3.0 4 votes vote down vote up
def applyOcr(imgUrl):
    """
    Open an image and read its letters.
    :param imgUrl: The URL for a CAPTCHA image.
    :return: The string in the imgae.
    """
    response = urlopen("file://" + imgUrl)
    img = np.asarray(bytearray(response.read()), dtype="uint8")
    gray_img = cv2.imdecode(img, cv2.IMREAD_GRAYSCALE)
    # if it's black on white:
    gray_img = 255 - gray_img
    _, mask = cv2.threshold(gray_img, THRESHOLD, 255, cv2.THRESH_BINARY)
    mask, contours, hierarchy = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    letters = {}  # x coordinate ↦ predicted letter
    for c, contour in enumerate(contours):
        rect = cv2.minAreaRect(contour)  # Find a rotated bounding box
        # rect is a tuple: ((corner 1, corner2), angle)
        angle = adjustAngle(rect[-1])
        # Draw this contour
        letter_mask = np.zeros(mask.shape, dtype="uint8")
        letter_mask = cv2.drawContours(letter_mask, [contour], contourIdx=0, color=255,
                                       thickness=-1)
        segmented_img = cv2.bitwise_and(gray_img, gray_img, mask=letter_mask)  # Applying mask
        dark_spun = imutils.rotate_bound(segmented_img, -angle)
        """
        We could crop the letter, but Tesseract doesn't seem to like such tight margins.
        brightPixels = cv2.findNonZero(dark_spun)
        x, y, width, height = cv2.boundingRect(brightPixels)
        cropped = darkSpun[y:y+height, x:x+width]
        """
        colored = cv2.cvtColor(dark_spun, cv2.COLOR_GRAY2RGB)
        colored = 255 - colored
        pil_image = Image.fromarray(colored)
        char_result = image_to_string(pil_image, config="--psm 10 --oem 0")
        if char_result is not None and len(char_result) > 0:
            moments = cv2.moments(contour)
            xCentre = int(moments["m10"]/moments["m00"])
            while xCentre in letters:
                xCentre += 1  # Avoid key clash
            letters[xCentre] = char_result.upper()
            if logger.getEffectiveLevel() <= logging.DEBUG:
                scipy.misc.imsave(char_result + ".jpg", pil_image)
        else:
            logger.debug("No result for character %d", c)
            if logger.getEffectiveLevel() <= logging.DEBUG:
                scipy.misc.imsave("unknown letter {c}.jpg".format(c=c), pil_image)
    # Adjust letters based on X axis
    word_solution = ""
    for xCentre in sorted(letters.keys()):
        word_solution += letters[xCentre]
    logger.debug("OCR saw %s", word_solution)
    return word_solution