mian03.py 4.61 KB
import glob
import os
from math import sqrt

import cv2
import numpy as np
from paddleocr import PaddleOCR, draw_ocr
from PIL import Image
from pyzbar.pyzbar import decode

# load model
# Paddleocr目前支持中英文、英文、法语、德语、韩语、日语,可以通过修改 lang参数进行切换
# lang参数依次为`ch`, `en`, `french`, `german`, `korean`, `japan`
ocr = PaddleOCR(
    use_angle_cls=True,
    # use_angle_cls=False,
    lang="ch",
    # use_gpu=False,
    use_gpu=True,
    use_tensorrt=True,
    det_algorithm="DB",
    # det_algorithm="EAST",
    det_db_score_mode="slow",
    # use_space_char=False, #不可用
    det_model_dir="ch_ppocr_server_v4.0_det_infer/",
    cls_model_dir="ch_ppocr_mobile_v4.0_cls_infer/",
    rec_model_dir="ch_ppocr_server_v4.0_rec_infer/",
    det_db_thresh=0.1,
    det_db_box_thresh=0.1,
    det_db_unclip_ratio=2.0,
    max_batch_size=20,
    use_dilation=True,
    # rec_algorithm="SVTR_LCNet",
)

# load dataset
# img_path = "./08631508_2.jpg"
# img_path = "./image/baidu_image/test1.jpg"
# img_path = "./train/threecode/file_1704852415_6623.jpg"
# 构建文件匹配模式
file_pattern = os.path.join("./train/threecode", "*")

# 获取文件夹下的所有文件
file_list = glob.glob(file_pattern)


def flatten_list(nested_list):
    flattened_list = []
    for item in nested_list:
        if isinstance(item, list):
            flattened_list.extend(flatten_list(item))
        elif isinstance(item, tuple):
            for value in item:
                if type(value) in (int, float):
                    if value > 1.0:
                        flattened_list.append(value)
                else:
                    flattened_list.append(value)
        else:
            # flattened_list.append(item)
            pass
    return flattened_list


# 打印文件列表
for image_path in file_list:
    original_image = Image.open(image_path)

    # 放大图片
    scale_factor = 5  # 放大倍数
    # scaled_image = original_image.resize(
    #     (original_image.width * scale_factor, original_image.height * scale_factor)
    # )

    dst = original_image.resize(
        (original_image.width * scale_factor, original_image.height * scale_factor)
    )

    # 裁剪放大后的图片
    # crop_left = scaled_image.width // 2 - original_image.width // 2  # 裁剪左上角 x 坐标
    # crop_top = scaled_image.height // 2 - original_image.height // 2  # 裁剪左上角 y 坐标
    # # crop_width = original_image.width  # 裁剪宽度
    # # crop_height = original_image.height  # 裁剪高度

    # crop_width = original_image.width * 3 / 4  # 裁剪宽度
    # crop_height = original_image.height * 3 / 4  # 裁剪高度

    # dst = scaled_image.crop(
    #     (crop_left, crop_top, crop_left + crop_width, crop_top + crop_height)
    # )

    # if os.path.exists(image_path.replace("train", "validation")):
    #     os.remove(image_path.replace("train", "validation"))
    # dst.save(image_path.replace("train", "validation"))

    # original_image = cv2.imread(img_path)

    # # 获取原始图像尺寸
    # height, width, _ = original_image.shape
    # print(height, width)

    # # 将图像放大两倍
    # scaled_image = cv2.resize(original_image, (2 * width, 2 * height))

    # # 计算裁剪区域的起始坐标
    # x = width // 2  # 起始横坐标
    # y = height // 2  # 起始纵坐标.
    # print(y + height, x + width)
    # cropped_image = scaled_image[y : y + height, x : x + width]  # 进行裁剪
    # dst = cv2.cvtColor(cropped_image, cv2.COLOR_BGR2RGB)  # 将图像从BGR转换为RGB
    # cv2.imshow("Image", dst)
    # result = ocr.ocr(image_path, cls=True)
    # 检测条形码
    # dst = cv2.imread(image_path)
    # barcodes = decode(dst)

    # # 遍历条形码区域并进行遮盖
    # for barcode in barcodes:
    #     x, y, w, h = barcode.rect
    #     cv2.rectangle(np.asarray(dst), (x, y), (x + w, y + h), (0, 0, 0), -1)

    # if os.path.exists(image_path.replace("train", "validation")):
    #     os.remove(image_path.replace("train", "validation"))
    # dst.save(image_path.replace("train", "validation"))

    # result = ocr.ocr(image_path, cls=True)
    result = ocr.ocr(np.asarray(dst), cls=True)
    # result = ocr.ocr(image_path.replace("train", "validation"), cls=True)
    for line in result:
        print(line)
        # print(line[0][1][0])

    result_list = flatten_list(result)
    print("result_list", result_list)
    with open("./log06.txt", "a+", encoding="utf-8") as file:
        if result_list is not None:
            file.write(image_path + "----" + str(result_list) + "\n")
        else:
            file.write(image_path + "----" + "NULL" + "\n")