很快啊-检测网图是不是网页截图v2.0

from joblib import Parallel, delayed
import cv2
import os
import shutil
import pytesseract
import numpy as np
from skimage.color import rgb2gray
from tqdm import tqdm

# 设置OCR引擎(如果仍然使用)
pytesseract.pytesseract.tesseract_cmd = r'/usr/bin/tesseract'

def is_webpage_screenshot_optimized(image_path):
    # 读取图像,并检查是否读取成功
    img = cv2.imread(image_path)
    if img is None:
        print(f"Failed to load image: {image_path}")
        return False  # 或者返回一个默认的结果,取决于你的逻辑
    
    # 缩放图像
    img = cv2.resize(img, None, fx=0.5, fy=0.5)
    
    # 转换为灰度
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    
    # 边缘检测
    edges = cv2.Canny(gray, 50, 150, apertureSize=3)
    lines = cv2.HoughLinesP(edges, 1, np.pi/180, 100, minLineLength=100, maxLineGap=10)
    line_count = 0 if lines is None else len(lines)
    
    # OCR(如果仍然需要)
    # text = pytesseract.image_to_string(gray, lang='eng')
    # 使用简单的文本检测替代复杂的OCR
    text = ""
    
    # 颜色分析
    img_small = cv2.resize(img, (100, 100))  # 缩小图像以加速颜色分析
    img_flat = img_small.reshape((-1, 3))
    unique_colors = len(np.unique(img_flat, axis=0))
    
    # 字体和布局分析(简化版本)
    font_layout_score = 0.5  # 假设一个固定的分数,或者使用更简单的算法
    
    # 综合评估
    return line_count > 10 and len(text) > 100 and unique_colors < 128 and font_layout_score > 0.5

# 设置路径
source_dir = "images"
dest_dir = "non_webpage_images"

# 创建目标文件夹
if not os.path.exists(dest_dir):
    os.makedirs(dest_dir)

# 获取所有图片文件的列表
image_paths = [os.path.join(root, file) for root, dirs, files in os.walk(source_dir) 
            for file in files if file.lower().endswith(('.png', '.jpg', '.jpeg'))]
# 并行处理
results = Parallel(n_jobs=-1)(delayed(is_webpage_screenshot_optimized)(path) for path in tqdm(image_paths, desc="Processing Images"))

# 移动非网页截图
for path, result in zip(image_paths, results):
    if not result:
        shutil.move(path, dest_dir)
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值