Ejemplo n.º 1
0
def _swt(pil_img):
    pil_img = pil_img.resize((pil_img.size[0] * 4, pil_img.size[1] * 4),
                             PIL.Image.ANTIALIAS)
    pil_img = pillowfight.swt(pil_img, pillowfight.SWT_OUTPUT_BW_TEXT)
    pil_img = pil_img.resize(
        (int(pil_img.size[0] / 4), int(pil_img.size[1] / 4)),
        PIL.Image.ANTIALIAS)
    return pil_img
Ejemplo n.º 2
0
    def test_swt(self):
        with tempfile.NamedTemporaryFile(suffix='.jpg') as tmpfile:
            in_img = PIL.Image.open("tests/data/crappy_background.jpg")
            out_img = pillowfight.swt(
                in_img, output_type=pillowfight.SWT_OUTPUT_ORIGINAL_BOXES)
            in_img.close()

            # beware of JPG compression
            out_img.save(tmpfile.name)
            out_img.close()
            out_img = PIL.Image.open(tmpfile.name)

        expected_img = PIL.Image.open("tests/data/crappy_background_swt.jpg")
        self.assertEqual(out_img.tobytes(), expected_img.tobytes())
        expected_img.close()
Ejemplo n.º 3
0
    def test_swt2(self):
        with tempfile.NamedTemporaryFile(suffix='.jpg') as tmpfile:
            in_img = PIL.Image.open("tests/data/black_border_problem.jpg")
            out_img = pillowfight.swt(
                in_img, output_type=pillowfight.SWT_OUTPUT_ORIGINAL_BOXES)
            in_img.close()

            # beware of JPG compression
            self.assertEqual(out_img.mode, "RGB")
            out_img.save(tmpfile.name)
            out_img.close()
            out_img = PIL.Image.open(tmpfile.name)

        expected_img = PIL.Image.open(
            "tests/data/black_border_problem_swt.jpg")
        self.assertEqual(out_img.tobytes(), expected_img.tobytes())
        expected_img.close()
Ejemplo n.º 4
0
    CNN_model = ConvolutionNN()

    # load ground truth
    with open(args.gtText, 'r') as gtfile:
        gt_data = gtfile.read().replace('\n', '')

    # preprocess, GaussianBlur and segmentation
    img = cv2.imread(args.image)
    # blurred_img = cv2.GaussianBlur(img, (5, 5), 0)
    # cv2.imwrite('blurred'+args.image, blurred_img)

    # Need to run SWT algorithm to get rid of non text
    # with blurred image, it will neglect trivia part, but it will also degrade performance
    # PIL_img = Image.open('blurred'+args.image)
    PIL_img = Image.open(args.image)
    PIL_no_text_img = pf.swt(PIL_img, output_type=pf.SWT_OUTPUT_ORIGINAL_BOXES)
    to_extract_img = PIL_to_cv_img(PIL_no_text_img)
    # to_extract_img = cv2.imread(args.image, 1)

    if DEBUG:
        cv2.imshow('img to extract texts', to_extract_img)
        cv2.waitKey(0)
        cv2.destroyAllWindows()

    extracted_string = ''
    region_imgs, region_coords = extract_regions(to_extract_img)
    for i in range(len(region_imgs)):
        lines = extract_words(region_imgs[i])
        region_text_block = ''

        for words in lines:
Ejemplo n.º 5
0
    im_with_keypoints = cv2.drawKeypoints(img, keypoints, np.array([]), (0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)

# Show keypoints
    cv2.imshow("Keypoints", im_with_keypoints)
    cv2.waitKey(1)

LIVE_STREAM = "http://192.168.77.81:8080/video"

camera_stream = WebcamVideoStream(src=LIVE_STREAM).start()


framecount = 0
while camera_stream.stream.isOpened():
    framecount += 1
    frame = cv2.cvtColor(camera_stream.read(), cv2.COLOR_BGR2GRAY)
    img = Image.fromarray(frame)
    img_out = pillowfight.swt(img, output_type=pillowfight.SWT_OUTPUT_ORIGINAL_BOXES)
    print(tesserocr.image_to_text(img))

    cv2.imshow("", frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()


#image = cv2.imread("img1.jpg")
#image = cv2.resize(image,(320,240))
#captch_ex(image)
Ejemplo n.º 6
0
 def readImageText(self, imagePath, language="all"):
     image = Image.open(imagePath)
     image = image.convert("RGB")
     image = pillowfight.swt(
         image, output_type=pillowfight.SWT_OUTPUT_ORIGINAL_BOXES)
     return self.extractText(image, language)
Ejemplo n.º 7
0
import sys
import PIL
import pillowfight
import pytesseract

img_in = PIL.Image.open(sys.argv[1])
if_natural = sys.argv[2]
if int(if_natural):
    img_out = pillowfight.swt(
        img_in, output_type=pillowfight.SWT_OUTPUT_GRAYSCALE_TEXT)
    img_out.save("results/beforeT", "PNG")
else:
    img_out = img_in

config = '-c tessedit_write_images=1'
img_str = pytesseract.image_to_string(img_out, lang='eng', config=config)

print(">>>>>>>>>>>>>>>>>>>>>>>>")
print(img_str)
print("<<<<<<<<<<<<<<<<<<<<<<<<")
Ejemplo n.º 8
0
from pillowfight import swt
from PIL import Image
import cv2
import numpy

input_image = Image.open("test.jpg")
output_img = swt(input_image)
im = numpy.asarray(output_img)
cv2.imwrite('ouut.jpg', im)
print(output_img)
Ejemplo n.º 9
0
def main():
    parser = argparse.ArgumentParser(description='Replace texts in picture.')
    parser.add_argument(
        '--ori', help='Original Picture Folder Path', required=True)
    parser.add_argument(
        '--ref', help='Reference Picture Folder Path', required=True)
    parser.add_argument(
        '--out', '-o', help='Output folder Path', default='result', metavar='result')
    parser.add_argument('--max-feature', type=int,
                        help='Max feature for aliging', default=5000, metavar='5000')
    parser.add_argument('--good-match-percent', type=float,
                        help='Good match percent for aliging', default=0.01, metavar='0.01')
    parser.add_argument('--detect-margin', type=int,
                        help='margin for detecting replace texts, larger is slower', default=40, metavar='40')
    parser.add_argument('--replace-margin', type=int,
                        help='margin for actually replaced texts', default=20, metavar='20')
    args = parser.parse_args()

    orifiles = [os.path.join(args.ori, fi) for fi in os.listdir(args.ori)
                if fi.endswith('.jpg') or fi.endswith('.png')]
    reffiles = [os.path.join(args.ref, fi) for fi in os.listdir(args.ref)
                if fi.endswith('.jpg') or fi.endswith('.png')]

    i = 0

    for ref_filename, im_filename in zip(orifiles, reffiles):
        # Read reference image
        print("Reading reference image : ", ref_filename)
        im_reference = cv2.imread(ref_filename, cv2.IMREAD_COLOR)

        # Read image to be aligned
        print("Reading image to align : ", im_filename)
        im = cv2.imread(im_filename, cv2.IMREAD_COLOR)

        print("Aligning images ...")
        # Registered image will be resotred in im_aligned.
        # The estimated homography will be stored in h.
        im_aligned, _ = align_images(
            im, im_reference, args.max_feature, args.good_match_percent)

        # Write aligned image to disk.
        aligned_filename = "aligned.jpg"
        print("Saving aligned image : ", aligned_filename)
        cv2.imwrite(aligned_filename, im_aligned)

        swt_filename = "mask.jpg"
        print("Applying swt filter ...")
        imp_aligned = Image.open(aligned_filename)
        impswt = pillowfight.swt(
            imp_aligned, output_type=pillowfight.SWT_OUTPUT_BW_TEXT)
        imswt = cv2.cvtColor(np.array(impswt), cv2.COLOR_RGB2BGR)
        print("Saving swt filtered image : ", swt_filename)
        cv2.imwrite(swt_filename, imswt)

        swt_filename_ref = "mask_ref.jpg"
        print("Applying swt filter to ref ...")
        imp_ref = Image.open(ref_filename)
        impswt_ref = pillowfight.swt(
            imp_ref, output_type=pillowfight.SWT_OUTPUT_BW_TEXT)
        imswt_ref = cv2.cvtColor(np.array(impswt_ref), cv2.COLOR_RGB2BGR)
        print("Saving swt filtered image : ", swt_filename_ref)
        cv2.imwrite(swt_filename_ref, imswt_ref)

        result_filename = os.path.join(args.out, str(i)+".jpg")
        print("Replacing texts ...")
        imswt_gray = cv2.cvtColor(imswt, cv2.COLOR_BGR2GRAY)
        imswt_ref_gray = cv2.cvtColor(imswt_ref, cv2.COLOR_BGR2GRAY)
        im_result = replace_images(
            im_reference, im_aligned, imswt_gray, imswt_ref_gray,
            args.detect_margin, args.replace_margin)
        print("Saving result image : ", result_filename)
        cv2.imwrite(result_filename, im_result)

        im_bound = bound(imswt_gray, imswt_ref_gray)
        cv2.imwrite("bound.jpg", im_bound)
        i = i+1
Ejemplo n.º 10
0
"""
Created on Fri Jan 18 20:09:20 2019

@author: Savitoj
"""

import pillowfight
from PIL import Image
from PIL import ImageOps
import cv2
import numpy
import matplotlib.pyplot as plt

input_image = Image.open("New-Folder/img (48).jpg")
#input_image = ImageOps.invert(input_image)
output_img = pillowfight.swt(input_image)
im = numpy.asarray(output_img)

cv2.imwrite("out_img.jpg", im)
mser = cv2.MSER_create()
gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
for row in range(len(gray)):
    for col in range(len(gray[0])):
        if row == 0 or col == 0 or row == len(gray) - 1 or col == len(
                gray[0]) - 1:
            continue

        btm = gray[row + 1][col]
        top = gray[row - 1][col]
        lft = gray[row][col + 1]
        right = gray[row][col - 1]
Ejemplo n.º 11
0
def plate_detected(area, mode, h, w, pre):

    output_list = list()

    # очистка по предположительному размеру плейта
    # сейчас идет расчет относительно размера найденой фичи (лицо, рост, профиль)
    # возможно лучше сделать относительно предпологаемой области нахождения плейта
    '''if mode == 'profiles' or mode == 'faces':
        # параметры для расчета относительно размера фичи
        min_expected_width = h / 4
        min_expected_height = h / 4
        max_expected_width = h * 1.5
        max_expected_height = h * 1.5
        
    elif mode == 'fullbodys':
        min_expected_width = h
        min_expected_height = h
        max_expected_width = h
        max_expected_height = h'''

    # относительно рамера предпологаемой области плейта
    # одинаковые значения для всех фичь, так как размер предпологаемой области почти одинаков
    min_expected_width = int(w / 6)
    min_expected_height = int(h / 15)
    max_expected_width = int(w)
    max_expected_height = int(h / 3)

    # cv2.imshow('area', area)
    # cv2.waitKey(0)
    new = Image.fromarray(area)

    img_out = pillowfight.swt(
        new, output_type=pillowfight.SWT_OUTPUT_ORIGINAL_BOXES)
    # img_out.show()
    img_out = np.array(img_out)
    img_out = cv2.cvtColor(img_out, cv2.COLOR_BGR2GRAY)
    # cv2.imshow('img_out', img_out)
    # cv2.waitKey(0)

    # черные блоки областей возвращаемх pillowfight.swt
    area_plate_block = cv2.threshold(img_out, 254, 255,
                                     cv2.THRESH_BINARY_INV)[1]
    # cv2.imshow('black_box', area_plate_block)
    # cv2.waitKey(0)

    contours, hierarchy = cv2.findContours(area_plate_block, cv2.RETR_TREE,
                                           cv2.CHAIN_APPROX_SIMPLE)
    # отрисовка контуров черных блоков из pillowfight.swt
    # arr = np.zeros([area.shape[0], area.shape[1]])
    # arr = cv2.drawContours(arr, contours, -1, 255)
    # cv2.imshow('black_box_contour', arr)
    # cv2.waitKey(0)

    # структура contour[0][0][0] : первый элемент - индес точки(против чавовой начиная с левой верхней)
    # второй - всегда 0, третий - 0->x 1->y
    for contour in contours:
        if len(contour) == 4:
            width = contour[3][0][0] - contour[0][0][0]
            height = contour[1][0][1] - contour[0][0][1]

            if (min_expected_width < width < max_expected_width)\
                    and (min_expected_height < height < max_expected_height):
                y1 = contour[0][0][1]
                y2 = contour[1][0][1]
                x1 = contour[0][0][0]
                x2 = contour[3][0][0]

                width_extension = 12
                height_extension = 8

                if y1 - height_extension < 0:
                    y1 = 0
                else:
                    y1 = y1 - height_extension

                if y2 + height_extension > area.shape[0]:
                    y2 = area.shape[0]
                else:
                    y2 = y2 + height_extension

                if x1 - width_extension < 0:
                    x1 = 0
                else:
                    x1 = x1 - width_extension

                if x2 + width_extension > area.shape[1]:
                    x2 = area.shape[1]
                else:
                    x2 = x2 + width_extension

                expected_plate = area[y1:y2, x1:x2]

                output_list.append(plate_symbol_detected(expected_plate, pre))

    clear_output_list = [x for x in output_list if x is not None]
    if len(clear_output_list) > 0:
        print(clear_output_list)
        return clear_output_list
    else:
        return None
Ejemplo n.º 12
0
def SWT(Img, name):
    #img_in = PIL.Image.open(name+extensao)
    img_out = pillowfight.swt(
        Img, output_type=pillowfight.SWT_OUTPUT_GRAYSCALE_TEXT)
    img_out.save(path + name + " - 7SWT" + extensao)
Ejemplo n.º 13
0
def main_func(image_path):
	#start
	PATH_ORIGINAL = image_path
	originalPath = PATH_ORIGINAL
	imgSkewed = Image.open(originalPath)
	imgSwt = pillowfight.swt(imgSkewed, output_type=pillowfight.SWT_OUTPUT_BW_TEXT)
	imgSwt.save(PATH_SWT)
	# saved skewed Image
	# imageCopied = convertPilImageToCv2Image(imgSwt)
	image = convertPilImageToCv2Image(imgSwt)

	results, image = east(image)

	# TODO
	rect = results[-4][0]
	# import pdb
	# pdb.set_trace()

	roi = image[rect[1]-40:rect[3]+40, rect[0]-40:rect[2]+40]
	if roi==[]:
		roi = image[rect[1]-4:rect[3], rect[0]-4:rect[2]]
	# cv2.imwrite(PATH_CROP_AFTER_EAST, roi)
	# roi = image[rect[1]-4:rect[3], rect[0]-4:rect[2]]
	image = roi

	# for rect in results[-2]:
		# roi = image[rect[1]-40:rect[3]+40, rect[0]-40:rect[2]+40]
		# cv2.imwrite('crop.jpg',roi)
		# break

	#################################################################################################

	# convert the image to grayscale and flip the foreground
	# and background to ensure foreground is now "white" and
	# the background is "black"
	gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
	gray = cv2.bitwise_not(gray)
	 
	# threshold the image, setting all foreground pixels to
	# 255 and all background pixels to 0
	thresh = cv2.threshold(gray, 0, 255,
		cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]

	# grab the (x, y) coordinates of all pixel values that
	# are greater than zero, then use these coordinates to
	# compute a rotated bounding box that contains all
	# coordinates
	coords = np.column_stack(np.where(thresh > 0))
	angle = cv2.minAreaRect(coords)[-1]
	 
	# the `cv2.minAreaRect` function returns values in the
	# range [-90, 0); as the rectangle rotates clockwise the
	# returned angle trends to 0 -- in this special case we
	# need to add 90 degrees to the angle
	if angle < -45:
		angle = -(90 + angle)
	 
	# otherwise, just take the inverse of the angle to make
	# it positive
	else:
		angle = -angle

	# print("angle " + str(angle))

	# rotate the image to deskew it
	(h, w) = image.shape[:2]
	center = (w // 2, h // 2)
	M = cv2.getRotationMatrix2D(center, angle, 1.0)
	img = cv2.imread(PATH_ORIGINAL)
	(h, w) = img.shape[:2]
	rotated = cv2.warpAffine(img, M, (w, h),
		flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_REPLICATE)

	# # draw the correction angle on the image so we can validate it
	# cv2.putText(rotated, "Angle: {:.2f} degrees".format(angle),
	# 	(10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
	 
	# show the output image
	# print("[INFO] angle: {:.3f}".format(angle))
	# cv2.imshow("Input", image)
	# cv2.imshow("Rotated", rotated)
	# cv2.imwrite(PATH_ORIGINAL_ROTATED,rotated)
	# cv2.waitKey(0)
	#################################################################
	image = convertCv2ImageToPilImage(rotated)
	image.save(PATH_300_DPI_IMAGE,dpi=(300,300))

	results, image = east(convertPilImageToCv2Image(image))

	# print(results)
	######################################################################################################33\

	imgSkewed = Image.open(PATH_300_DPI_IMAGE)
	# imgSwt = pillowfight.swt(imgSkewed, output_type=pillowfight.SWT_OUTPUT_BW_TEXT)
	imgSkewed.save(PATH_SWT)

	cmd = "tesseract " + PATH_SWT + " test -l eng --psm 11 --oem 1" 

	returned_value = os.system(cmd)
	  # returns the exit code in unix
	text = open("test.txt", "r")
	text1 = str(text.read())
	print(text.read())
	return (text1)
Ejemplo n.º 14
0
import sys
import pillowfight
import pyocr
import pyocr.builders

tools = pyocr.get_available_tools()
if len(tools) == 0:
    print("No OCR tool found")
    sys.exit(1)
# The tools are returned in the recommended order of usage
tool = tools[0]
print("Will use tool '%s'" % (tool.get_name()))
# Ex: Will use tool 'libtesseract'

langs = tool.get_available_languages()
print("Available languages: %s" % ", ".join(langs))
lang = 'eng'
print("Will use lang '%s'" % (lang))
# Ex: Will use lang 'fra'
# Note that languages are NOT sorted in any way. Please refer
# to the system locale settings for the default language
# to use.
img_in = PIL.Image.open(sys.argv[1])
img_out = pillowfight.swt(img_in, output_type=pillowfight.SWT_OUTPUT_BW_TEXT)
txt = tool.image_to_string(Image.open(sys.argv[1]),
                           lang=lang,
                           builder=pyocr.builders.TextBuilder())
print(">>>>>>>>>>>>>>")
print(txt)
print("<<<<<<<<<<<<<<")
out = img.resize(tuple(i * 2 for i in img.size), PIL.Image.LANCZOS)
process = True
if process:
    out = pillowfight.ace(img, slope=10, limit=1000, samples=100, seed=None)

    # Canny edge detection
    # out = pillowfight.canny(out)

    # Gaussian blur
    # out = pillowfight.gaussian(out, sigma=2.0, nb_stddev=5)

    # sobel
    # out = pillowfight.sobel(out)

    # Stroke Width Transform
    # SWT_OUTPUT_BW_TEXT = 0  # default
    # SWT_OUTPUT_GRAYSCALE_TEXT = 1
    # SWT_OUTPUT_ORIGINAL_BOXES = 2
    out = pillowfight.swt(out, output_type=2)

    #unpaper
    out = pillowfight.unpaper_blackfilter(out)
    out = pillowfight.unpaper_blurfilter(out)
    out = pillowfight.unpaper_border(out)
    # out = pillowfight.unpaper_grayfilter(out) #adds white rectangles?
    out = pillowfight.unpaper_masks(out)
    out = pillowfight.unpaper_noisefilter(out)

print(pytesseract.image_to_string(out))

out.save("out.png")