Example #1
0
    def getWord(self, img_file):
        ##基于exif信息文字朝向检测
        img_exif = Image.open(img_file)
        file_name = os.path.basename(img_file)
        file_path = os.path.dirname(img_file)
        img = cv2.imread(img_file)
        is_exif = False
        try:
            for orientation in ExifTags.TAGS.keys():
                if ExifTags.TAGS[orientation] == 'Orientation': break
            exif = dict(img_exif._getexif().items())
            if exif[orientation] == 3:
                img = Image.fromarray(img).transpose(Image.ROTATE_180)
                is_exif = True
            elif exif[orientation] == 8:
                img = Image.fromarray(img).transpose(Image.ROTATE_270)
                is_exif = True
            elif exif[orientation] == 6:
                img = Image.fromarray(img).transpose(Image.ROTATE_90)
                is_exif = True
        except:
            pass

        # print (img_file)
        # print (img)
        angle = 0
        if img is not None:
            ##基于tensorflow文字朝向检测
            if adjust and is_exif is False:
                angle = self.angle_detect_dnn(img=np.copy(img))
                if angle == 90:
                    img = Image.fromarray(img).transpose(Image.ROTATE_270)
                elif angle == 180:
                    img = Image.fromarray(img).transpose(Image.ROTATE_180)
                elif angle == 270:
                    img = Image.fromarray(img).transpose(Image.ROTATE_90)

        #霍夫矫正
            if is_exif is False:
                angle = rectifyImgAngle(img_file)
                img = cv2.imread(img_file)

            image = np.array(img)
            # image =  cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
            data, drawUrl = text_ocr(image, scale, maxScale, TEXT_LINE_SCORE,
                                     file_path, file_name)

            res = {
                'data': data,
                'errCode': 0,
                'drawUrl': drawUrl,
                'angle': angle
            }
        else:
            res = {'data': [], 'errCode': 3, 'drawUrl': '', 'angle': angle}
        return res
Example #2
0
def job(uid, url, imgString, iscut, isclass, billModel, ip):
    now = get_now()
    if url is not None:
        img = read_url_img(url)
    elif imgString is not None:
        img = base64_to_PIL(imgString)
    else:
        img = None

    if img is not None:
        image = np.array(img)
        image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
        data = text_ocr(image, scale, maxScale, TEXT_LINE_SCORE)

        res = {'data': data, 'errCode': 0}
    else:
        res = {'data': [], 'errCode': 3}
    return res
Example #3
0
import json
import os
from PIL import Image
import numpy as np
from config import scale,maxScale,TEXT_LINE_SCORE
from pprint import pprint
import cv2
from dnn.main import text_ocr

img = Image.open(os.environ["OCR_IMAGE_PATH"]).convert('RGB')
image = np.array(img)
image =  cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
data = text_ocr(image,scale,maxScale,TEXT_LINE_SCORE)

print(json.dumps(data))
Example #4
0
import sys
sys.path.append('/Users/admin/Downloads/darknet-ocr-master')
from dnn.main import text_ocr

for f in files:
    if f in done or f in known_problem:
        print(f, "done", file=sys.stderr)
        continue

    try:
        print(f + "\t", end="")
        sys.stdout.flush()
        # print(f, file=sys.stderr)
        image = cv2.imread(f)
        image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
        p = 100
        image = 255 - cv2.copyMakeBorder(255 - image, p, p, p, p,
                                         cv.BORDER_CONSTANT, (0, 0, 0))
        # cv2.imshow('',image);cv.waitKey(0)
        # print(image,scale,maxScale,TEXT_LINE_SCORE)
        data = text_ocr(image, 200, 300, 0.0)

        o = ''.join([x['text'] for x in data])

        print(o)
        print(f + "\t" + o, file=sys.stderr)
        sys.stdout.flush()

    except:
        print(f, "fail", file=sys.stderr)
def job(path):
    img = cv2.imread(path)
    image = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
    data = text_ocr(image, scale_d, maxScale, TEXT_LINE_SCORE)
    res = {'data':data,'errCode':0}
    print(res)