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

发布于:2024-08-01 ⋅ 阅读:(111) ⋅ 点赞:(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)

网站公告

今日签到

点亮在社区的每一天
去签到