コード例 #1
0
def getAlignedImage(img):
    # for i in range(50, 100):
    #     for j in range(200, 255):
    #         binary_img = getCanny(img, j, i, 3, 0)
    #         print(i, j)
    #         show(binary_img, 10)
    binary_img = utils.getCanny(img, 20, 50, 3, 0)
    # show(binary_img, 0)
    max_contour, max_area = utils.findMaxContour(binary_img)
    h, w = img.shape[:2]
    bg = np.zeros((h, w, 3), np.uint8)
    bg.fill(0)
    cv2.drawContours(bg, max_contour, -1, (0, 0, 255), 1)

    bg = utils.getCanny(bg, 30, 30, 7, 0)
    lines = cv2.HoughLinesP(bg, 1, np.pi / 180, 100, maxLineGap=50)
    # 画出边框线
    # for i in range(len(lines)):
    #     for x1, y1, x2, y2 in lines[i]:
    #         cv2.line(img, (x1, y1), (x2, y2), (112, 255, 0), 2)
    #         show(img,0)
    # 得到四个角坐标
    point = utils.getCornerPoint(lines)
    print(point)
    # boxes = adaPoint(point, ratio)
    boxes = np.trunc(point)
    boxes = utils.orderPoints(boxes)
    warped = utils.warpImage(img, boxes)
    utils.show(warped, 0)
    return warped
コード例 #2
0
def justify_front(path):
    print(path)
    img = cv2.imdecode(np.fromfile(path, dtype=np.uint8), 1)

    # for i in range(100, 200):
    #     for j in range(50, 200):
    #         binary_img = utils.getCanny(img, i, j, 15, 0)
    #         print(i, j)
    #         utils.show(binary_img, 1)

    binary_img = utils.getCanny(img, 100, 150, 15, 0)
    # utils.show(binary_img, 0)
    contours, hierarchy = cv2.findContours(binary_img, cv2.RETR_CCOMP,
                                           cv2.CHAIN_APPROX_NONE)
    flag = False
    for i in range(len(contours)):
        x, y, w, h = cv2.boundingRect(contours[i])
        area = w * h
        if (area > 20000) & (abs(w - h) < 20):
            if (x > 400) & (y > 270):
                print(path)
                flag = True
            # print(x, y, w, h)
            # img = cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 2)
            # utils.show(img, 0)

    if flag:
        img = utils.rotate_image(img, 180)
        utils.show(img, 1)
        cv2.imencode('.jpg', img)[1].tofile(path)
コード例 #3
0
def getExpDateCrop(img):
    # img = getAlignedImage(img)
    back = False
    image_grey = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    front_face_detected = frontFaceClassifier.detectMultiScale(image_grey, scaleFactor=1.2,
                                                               minNeighbors=5, flags=cv2.CASCADE_SCALE_IMAGE)

    if len(front_face_detected) != 0:
        back = True
    binary_img = utils.getCanny(img, 100, 200, 20, 0)

    # for i in range(180,200):
    #     for j in range(350,400):
    #         binary_img = utils.getCanny(img, j, i, 15, 0)
    #         print(i, j)
    #         utils.show(binary_img, 1)

    # utils.show(binary_img, 0)
    contours, hierarchy = cv2.findContours(binary_img, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_NONE)

    recList = []
    for i in range(len(contours)):
        x, y, w, h = cv2.boundingRect(contours[i])
        area = w * h
        if (area > 1000) & (h < 80) & (y > 300) & (x > 280) & (x < 400):
            recList.append(x)
            recList.append(y)
            recList.append(w)
            recList.append(h)
            # cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 3)  # 绿色
            # utils.show(img, 0)

    # 对轮廓坐标进行排序
    if len(recList) != 0:
        recArray = np.array(recList).reshape(-1, 4)
        idex = np.lexsort([recArray[:, 0], recArray[:, -1]])
        sorted_data = recArray[idex, :]
        # print(sorted_data)
        # utils.show(img, 0)
        # 找到期限的坐标
        # for i in range(len(sorted_data)):
        x, y, w, h = sorted_data[0]
        print(x, y, w, h)
        # cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 3)  # 红色
        # utils.show(img, 0)
        return img[y:y + h, x:x + w], back
    else:
        print("?????")
        return "", back
コード例 #4
0
def crop_main(img_path):
    # base_path=img_path.split("/")
    img = cv2.imdecode(np.fromfile(img_path, dtype=np.uint8), 1)
    img = utils.img_resize(img, 900)
    binary_img = utils.getCanny(img, 20, 50, 3, 0)
    # max_contour, max_area = utils.findMaxContour(binary_img)
    contours, _ = cv2.findContours(binary_img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
    for i in range(len(contours)):
        area = cv2.contourArea(contours[i])
        rotate = False
        if area > 5000:
            x, y, w, h = cv2.boundingRect(contours[i])
            if w > h:
                if (w / h < 1) | (w / h > 2):
                    break
                else:
                    print(w, h)
            else:
                if (h / w < 1) | (h / w > 2):
                    break
                else:
                    print(w, h)
                    rotate = True
            image = img[y - 10:y + h + 10, x - 10:x + w + 10]
            print(image.shape)
            if rotate:
                image = utils.rotate_image(image, 90)
            image = cv2.resize(image, (856, 540), interpolation=cv2.INTER_CUBIC)
            print(image.shape)
            if len(image.shape) == 3:
                image_grey = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
            else:
                image_grey = image

            front_face_detected = frontFaceClassifier.detectMultiScale(image_grey, scaleFactor=1.2,
                                                                       minNeighbors=5, flags=cv2.CASCADE_SCALE_IMAGE)

            if len(front_face_detected) != 0:
                for x, y, w, h in front_face_detected:
                    if x < 856 / 2:
                        image = utils.rotate_image(image, 180)
                    # cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 255), 2)
            utils.show(image, 1)
            name = img_path.split("\\")[1]
            # 把框出来的身份证图片另存为
            cv2.imencode('.jpg', image)[1].tofile(
                "E:/KingT/staff/test/all身份证/" + name + "_" + str(i) + img_path.split("\\")[2])
コード例 #5
0
def detect_two(path1, path2):
    global null_list
    print("=====detect two=====")
    print(path1)
    print(path2)
    null_flag = False
    img1 = cv2.imdecode(np.fromfile(path1, dtype=np.uint8), 1)
    img2 = cv2.imdecode(np.fromfile(path2, dtype=np.uint8), 1)
    img_list = []
    for img in [img1, img2]:

        # img = utils.img_resize(img)
        # for i in range(0, 100):
        #     for j in range(0, 100):
        #         binary_img = utils.getCanny(img, i, j, 6, 0)
        #         print(i, j)
        #         utils.show(binary_img, 10)

        binary_img = utils.getCanny(img, 20, 60, 20, 0)
        # utils.show(utils.img_resize(binary_img), 0)
        contours, _ = cv2.findContours(binary_img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
        for i in range(len(contours)):
            rotate = False
            x, y, w, h = cv2.boundingRect(contours[i])
            # print(x, y, w, h)
            # m = cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 2)
            # utils.show(utils.img_resize(m), 0)
            if (w * h > 25000) & ((w > 275) | (h > 275)) & (x > 0) & (y > 0):
                if w > h:
                    if (w / h < 1) | (w / h > 2):
                        break
                    else:
                        print(w, h)
                else:
                    if (h / w < 1) | (h / w > 2):
                        break
                    else:
                        print(w, h)
                        rotate = True
                if (y - 10 > 0) & (x - 10 > 0):
                    image = img[y - 10:y + h + 10, x - 10:x + w + 10]
                else:
                    image = img[y:y + h, x:x + w]
                if rotate:
                    image = utils.rotate_image(image, 90)
                image = cv2.resize(image, (856, 540), interpolation=cv2.INTER_CUBIC)
                # utils.show(image, 0)
                img_list.append(image)

    while len(img_list) < 2:
        print(str(len(img_list)), path1, path2)
        print("null")
        null_flag = True
        null_list.append(path1)
        img_list.append(np.zeros((540, 856, 3), dtype=np.uint8))

    fname = path1.split("\\")[1]
    out_folder_path = "E:/KingT/staff/result/身份证null/" + fname
    if null_flag:
        pic_name1 = path1.split("\\")[2]
        pic_name2 = path2.split("\\")[2]
        if not os.path.exists(out_folder_path):
            os.mkdir(out_folder_path)
        # shutil.copy(path1, out_folder_path + "/" + pic_name1)
        # shutil.copy(path2, out_folder_path + "/" + pic_name2)
    else:
        if os.path.exists(out_folder_path):
            # ...
            shutil.rmtree(out_folder_path)
    # 检测人脸
    for i in range(2):
        image_grey = cv2.cvtColor(img_list[0], cv2.COLOR_BGR2GRAY)
        front_face_detected = frontFaceClassifier.detectMultiScale(image_grey, scaleFactor=1.25,
                                                                   minNeighbors=5,
                                                                   flags=cv2.CASCADE_SCALE_IMAGE)
        if len(front_face_detected) != 0:
            for x, y, w, h in front_face_detected:
                # print(x, y, w, h)
                if (w > 120) & (x > 300):
                    # cv2.rectangle(img_list[0], (x, y), (x + w, y + h), (0, 255, 255), 2)
                    # utils.show(img_list[1], 200)
                    # utils.show(img_list[0], 200)
                    return img_list[1], img_list[0]
                else:
                    if i == 1:
                        return img_list[0], img_list[1]
                    else:
                        img_list[0] = utils.rotate_image(img_list[0], 180)

        else:
            return img_list[0], img_list[1]
コード例 #6
0
def recognize_concat(front_path, back_path, concat):
    if concat:
        front = cv2.imdecode(np.fromfile(front_path, dtype=np.uint8), 1)
        back = cv2.imdecode(np.fromfile(back_path, dtype=np.uint8), 1)
        front = cv2.resize(front, (856, 540), interpolation=cv2.INTER_CUBIC)
        back = cv2.resize(back, (856, 540), interpolation=cv2.INTER_CUBIC)

        bg = np.ones((1110, 876, 3), dtype=np.uint8) * 255
        bg[10:550, 10:866] = front
        bg[560:1100, 10:866] = back
        output_path = "E:/KingT/staff/result/身份证new/" + front_path.split(
            "_")[0].split("\\")[1] + ".jpg"
        # cv2.imencode('.jpg', bg)[1].tofile(output_path)

    global number_list
    global exp_date_list
    global non_detect_list
    for path in [front_path, back_path]:
        if path == "":
            break
        print(path)
        name = path.split("-")[1]
        img = cv2.imdecode(np.fromfile(path, dtype=np.uint8), 1)
        img = cv2.resize(img, (856, 540), interpolation=cv2.INTER_CUBIC)

        # for i in range(180,240):
        #     for j in range(100, 500):
        #         binary_img = utils.getCanny(img, i, j, 15, 0)
        #         print(i, j)
        #         utils.show(binary_img, 1)

        binary_img = utils.getCanny(img, 180, 170, 15, 0)
        contours, hierarchy = cv2.findContours(binary_img, cv2.RETR_CCOMP,
                                               cv2.CHAIN_APPROX_NONE)
        global nonList
        recList = []
        flag1 = False
        for i in range(len(contours)):
            x, y, w, h = cv2.boundingRect(contours[i])
            if (x > 100) & (x < 500) & (w > 300) & (y > 300) & (h > 30) & (
                    y < 500):
                recList.append(x)
                recList.append(y)
                recList.append(w)
                recList.append(h)
        recArray = np.array(recList).reshape(-1, 4)
        idex = np.lexsort([recArray[:, 2], recArray[:, 1]])
        # 从大到小排
        idex = np.flip(idex)
        sorted_data = recArray[idex, :]
        for data in sorted_data:
            x, y, w, h = data[0:4]
            if (h < 70) & (w > 200) & (w < 600) & (y + h < 520) & (y + h >
                                                                   420):
                print(path)
                print("find1: %d %d %d %d" % (x, y, w, h))

                crop = img[y:y + h, x:x + w]
                imagegray = cv2.cvtColor(crop, cv2.COLOR_RGB2GRAY)

                # for i in range(300):
                #     retval, imagebin = cv2.threshold(imagegray, i, 255, cv2.THRESH_BINARY+cv2.THRESH_TRUNC)
                #     utils.show(imagebin, 1)
                #     text = pytesseract.image_to_string(imagebin, lang='eng')
                #     print(i, text)

                retval, imagebin = cv2.threshold(
                    imagegray, 20, 255, cv2.THRESH_BINARY + cv2.THRESH_TRUNC)
                text = pytesseract.image_to_string(imagebin, lang='eng')

                if len(text) == 0:
                    text = pytesseract.image_to_string(crop, lang='eng')

                if len(text) != 0:
                    if path.find("front") != -1:
                        text = text.replace("=", "-").replace("~", "-").replace(" ", "") \
                            .replace(",", ".").replace(":", ".").replace("::", ".").replace("+", ".").replace(";", ".")
                        exp_date_list.append([name, text])
                    else:
                        text = text.replace("$", "3").replace(" ", "")
                        number_list.append([name, text])
                    print(text)
                else:
                    non_detect_list.append(path)
                print()
                # 红色 第一次找到
                img = cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255),
                                    2)
                flag1 = True
                utils.show(img, 1)
                break
        if not flag1:
            utils.show(binary_img, 1)

            # for i in range(100, 500):
            #     for j in range(0, 900):
            #         binary_img = utils.getCanny(img, i, j, 15, 0)
            #         print(i, j)
            #         utils.show(binary_img, 1)

            binary_img = utils.getCanny(img, 200, 300, 18, 0)
            contours, hierarchy = cv2.findContours(binary_img, cv2.RETR_CCOMP,
                                                   cv2.CHAIN_APPROX_NONE)
            recList = []
            flag2 = False
            for i in range(len(contours)):
                x, y, w, h = cv2.boundingRect(contours[i])
                if (x > 100) & (x < 500) & (w > 300) & (y > 300) & (h > 30) & (
                        y < 500):
                    recList.append(x)
                    recList.append(y)
                    recList.append(w)
                    recList.append(h)
            recArray = np.array(recList).reshape(-1, 4)
            idex = np.lexsort([recArray[:, 2], recArray[:, 1]])
            # 从大到小排
            idex = np.flip(idex)
            sorted_data = recArray[idex, :]
            for data in sorted_data:
                x, y, w, h = data[0:4]
                if (h < 70) & (w > 200) & (w < 600) & (y + h < 520) & (y + h >
                                                                       420):
                    print(path)
                    print("find2: %d %d %d %d" % (x, y, w, h))

                    crop = img[y:y + h, x:x + w]
                    imagegray = cv2.cvtColor(crop, cv2.COLOR_RGB2GRAY)
                    retval, imagebin = cv2.threshold(
                        imagegray, 20, 255,
                        cv2.THRESH_BINARY + cv2.THRESH_TRUNC)
                    text = pytesseract.image_to_string(imagebin, lang='eng')

                    if len(text) == 0:
                        text = pytesseract.image_to_string(crop, lang='eng')

                    if len(text) != 0:
                        if path.find("front") != -1:
                            text = text.replace("=", "-").replace("~", "-").replace(" ", "") \
                                .replace(",", ".").replace(":", ".").replace("::", ".").replace("+", ".").replace(";",
                                                                                                                  ".")
                            exp_date_list.append([name, text])
                        else:
                            text = text.replace("$", "3").replace(" ", "")
                            number_list.append([name, text])
                        print(text)
                    else:
                        non_detect_list.append(path)
                    # 绿色 第二次找到
                    img = cv2.rectangle(img, (x, y), (x + w, y + h),
                                        (0, 255, 0), 2)
                    flag2 = True
                    utils.show(img, 1)
                    break
                else:
                    print(x, y, w, h)
                    # 蓝色 找到 但是不对
                    img = cv2.rectangle(img, (x, y), (x + w, y + h),
                                        (255, 0, 0), 2)
                    utils.show(img, 1)
            if not flag2:
                nonList.append(path)
                print(nonList)
                utils.show(binary_img, 1)