Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

一直提示图片文件上传不合法 #1

Open
liuyan-design opened this issue May 4, 2024 · 1 comment
Open

一直提示图片文件上传不合法 #1

liuyan-design opened this issue May 4, 2024 · 1 comment

Comments

@liuyan-design
Copy link

0.0.0.0:80被占用使用的127.0.0.0:8000
再上传数据的时候就一直提示图片不合法

@lingskr
Copy link

lingskr commented May 4, 2024

修改views.py为:
# result_list = []
# for i in range(len(result)):
#     result_list.append(result[i][-1][0])
    result_list = []
    for item in result:
        if item and item[-1]:  # 确保item和item[-1]不为None
            result_list.append(item[-1][0])

修改infer.py为:

from paddleocr import PaddleOCR, draw_ocr
import os
from .settings import BASE_DIR
from PIL import Image
# 模型路径下必须含有model和params文件
ocr = PaddleOCR(det_model_dir=os.path.join(BASE_DIR, "det_db"),
                use_angle_cls=True)


# def electric_meter(img_path):
#     print(img_path)
#     img_path = os.path.join(img_path)
#     result = ocr.ocr(img_path, cls=True)
#     for line in result:
#         print(line)
#
#     # 显示结果
#     print("可视化")
#     image = Image.open(img_path).convert('RGB')
#     boxes = [line[0] for line in result]
#     txts = [line[1][0] for line in result]
#     scores = [line[1][1] for line in result]
#     im_show = draw_ocr(image, boxes, txts, scores)
#     im_show = Image.fromarray(im_show)
#     im_show.save(os.path.join(BASE_DIR, "rec_result", os.path.basename(img_path)))
#     # im_show.show()
#     return result

def electric_meter(img_path):
    print(f"Processing image: {img_path}")
    # 确保 img_path 是有效的文件路径
    if not os.path.exists(img_path):
        print("Image path does not exist.")
        return None

    try:
        result = ocr.ocr(img_path, cls=True)
        # 检查结果是否为空
        if not result:
            print("No OCR result.")
            return None

        for line in result:
            print(line)

        # 显示结果
        print("可视化")
        image = Image.open(img_path).convert('RGB')
        boxes = [line[0] for line in result if line]
        txts = [line[1][0] for line in result if line]
        scores = [line[1][1] for line in result if line]
        im_show = draw_ocr(image, boxes, txts, scores)
        im_show = Image.fromarray(im_show)
        save_path = os.path.join(BASE_DIR, "rec_result", os.path.basename(img_path))
        im_show.save(save_path)
        print(f"Result saved to: {save_path}")
        return result
    except Exception as e:
        print(f"An error occurred: {e}")
        return None

# electric_meter(r"C:\Users\boyif\Desktop\paddle\electric\M2021\IMG_20210724_132657.jpg")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants