多线程检测图像是不是网页截图v3.0

# python data/utils/select_none_web_v2.py
# 多线程检测是不是网页截图

import cv2
import pytesseract
import numpy as np
import os
import shutil
from concurrent.futures import ThreadPoolExecutor, as_completed
from tqdm import tqdm

# 设置Tesseract路径
pytesseract.pytesseract.tesseract_cmd = r'/usr/bin/tesseract'

# 图像预处理和特征提取
def extract_features(image_path):
    img = cv2.imread(image_path)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    
    # 文本检测
    text = pytesseract.image_to_string(gray)
    text_len = len(text)
    
    # 颜色分布特征
    color_hist = cv2.calcHist([gray], [0], None, [256], [0,256])
    color_hist = color_hist.flatten()
    
    # 计算总像素数
    total_pixels = np.prod(img.shape[:2])
    
    return text_len, color_hist, total_pixels

# 筛选规则
def is_webpage_screenshot(image_path, min_text_length=100, max_text_density=0.05):
    text_len, _, total_pixels = extract_features(image_path)
    
    # 文本长度阈值
    if text_len < min_text_length:
        return False
    
    # 颜色密度阈值,假设文本区域颜色分布较为集中
    text_density = text_len / total_pixels
    
    if text_density > max_text_density:
        return False
    
    return True

# 设置路径
source_dir = "data/aigc_0"
dest_dir = "data/aigc_0_non"

# 确保目标目录存在
if not os.path.exists(dest_dir):
    os.makedirs(dest_dir)

# 应用筛选规则到未知图片,并移动非网页截图
image_paths = [os.path.join(source_dir, filename) for filename in os.listdir(source_dir)]

# 自定义线程数
num_threads = 10  # 根据你的系统资源进行调整

# 使用ThreadPoolExecutor进行多线程处理
with ThreadPoolExecutor(max_workers=num_threads) as executor:
    futures = {executor.submit(is_webpage_screenshot, path): path for path in image_paths}
    
    # 使用tqdm显示进度
    for future in tqdm(as_completed(futures), total=len(image_paths), desc="Processing images"):
        path = futures[future]
        try:
            result = future.result()
        except Exception as e:
            print(f"An error occurred while processing {path}: {e}")
        else:
            if not result:
                shutil.move(path, dest_dir)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

之群害马

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值