File size: 5,319 Bytes
3bda1a9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | import os
import cv2
#from paddleocr import PaddleOCRVL
import time
import csv
####
from rule_11 import is_fontsize_percentage_large_image
#from rule_14 import is_text_design_uncoordinated_image
from rule_16 import is_guide_button_with_yolo_image
'''
from rule_2 import is_low_hue_diversity_image
from rule_3 import is_promotion_image
from rule_4 import is_low_value_image
from rule_8 import is_promo_with_yolo_image
from rule_9 import is_subject_too_large_image
from rule_10 import is_outside_safe_area_image
'''
# 假定 ocr 及 is_promotion_image 已初始化/定义
from paddleocr import PaddleOCR
from ultralytics import YOLO
ocr = PaddleOCR(
# ==== ① 文档方向 + 纠偏预处理 ====
use_textline_orientation=False,
use_doc_orientation_classify=False,
use_doc_unwarping=False,
# ==== ② 检测模型 ====
text_detection_model_name="PP-OCRv5_server_det",
text_detection_model_dir="model/paddleocr/det/PP-OCRv5_server_det_infer",
#r"model/paddleocr/det/ch_PP-OCRv3_det_infer_new"
# ==== ③ 识别模型 ====
text_recognition_model_name="PP-OCRv5_server_rec",
text_recognition_model_dir="model/paddleocr/rec/PP-OCRv5_server_rec_infer",
#r"model/paddleocr/rec/ch_PP-OCRv3_rec_infer_new"
# ==== ⑦ 语言 ====
lang="ch"
)
yolo_model = YOLO("model/runs/detect/train8/weights/best.pt")
def main(input_folder):
count_total = 0
count_is_fontsize_percentage_large=0
count_is_text_design_uncoordinated =0
count_is_guide_button_with_yolo=0
'''
count_promo = 0
count_low_value=0
count_low_hue_diversity=0
count_is_promo_with_yolo=0
count_is_subject_too_large=0
count_is_outside_safe_area=0
'''
results_list = []
for filename in os.listdir(input_folder):
if not filename.lower().endswith(('.jpg', '.jpeg', '.png', '.bmp','.webp','.jfif')):
continue
image_path = os.path.join(input_folder, filename)
img = cv2.imread(image_path)
if img is None:
print(f"[警告] 图片读取失败: {filename}")
continue
count_total += 1
img_result = {
"filename": filename,
"is_fontsize_percentage_large": None,
"is_text_design_uncoordinated": None,
}
'''
'''
is_fontsize_percentage_large= is_fontsize_percentage_large_image(img,ocr)
img_result["is_fontsize_percentage_large"] = is_fontsize_percentage_large
if is_fontsize_percentage_large:
count_is_fontsize_percentage_large += 1
result_text = "文字-占比过大"
else:
result_text = "文字-占比正常"
#print(f"文件: {filename}\t判断: {result_text}")
print(f"文件: {os.path.join(input_folder,filename)}\n判断: {result_text}")
'''
is_text_design_uncoordinated = is_text_design_uncoordinated_image(img,ocr)
img_result["is_text_design_uncoordinated"] = is_text_design_uncoordinated
if is_text_design_uncoordinated:
count_is_text_design_uncoordinated += 1
result_text = "文字搭配不协调"
else:
result_text = "文字搭配协调"
#print(f"文件: {filename}\t判断: {result_text}")
print(f"文件: {os.path.join(input_folder,filename)}\n判断: {result_text}")
is_guide_button_with_yolo = is_guide_button_with_yolo_image(img,model=yolo_model,idx=count_total,ocr=ocr)
img_result["is_text_design_uncoordinated"] = is_guide_button_with_yolo
if is_guide_button_with_yolo:
count_is_guide_button_with_yolo += 1
result_text = "存在引导按钮"
else:
result_text = "不存在"
#print(f"文件: {filename}\t判断: {result_text}")
print(f"文件: {os.path.join(input_folder,filename)}\n判断: {result_text}")
'''
results_list.append(img_result)
# 输出统计信息
if count_total > 0:
percentage = round(count_is_fontsize_percentage_large / count_total * 100, 2)
print(f"\n统计:总图片数={count_total},\
文字占比大={count_is_fontsize_percentage_large}|{round(count_is_fontsize_percentage_large / count_total * 100, 2)}%,\
文字搭配不协调={count_is_text_design_uncoordinated}|{round(count_is_text_design_uncoordinated/ count_total * 100, 2)}%,\
存在引导按钮={count_is_guide_button_with_yolo}|{round(count_is_guide_button_with_yolo/ count_total * 100, 2)}%,\
end")
else:
print("未检测到有效图片。")
output_csv = "image_rule_results.csv"
with open(output_csv, "w", newline='', encoding="utf-8") as csvfile:
fieldnames = [
"filename",
"is_fontsize_percentage_large",
"is_text_design_uncoordinated",
"is_guide_button_with_yolo",
]
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for row in results_list:
writer.writerow(row)
print(f"\n已保存统计表:{output_csv}")
if __name__ == '__main__':
input_folder = "stage2/hw_v2/文字占比" # 你的图片文件夹路径
main(input_folder)
|