2025-08-19利用opencv检测图片中文字及图片的坐标

同时还 计算出坐标及字体大小

bash 复制代码
from cnocr import CnOcr
import cv2
import numpy as np
from PIL import Image, ImageDraw, ImageFont

# 初始化 cnocr
ocr = CnOcr()


def put_chinese_text(image, text, position, font_size=20, color=(0, 255, 0)):
    """
    在图像上绘制中文文本
    """
    # 将OpenCV的BGR图像转换为RGB图像
    image_pil = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))

    # 设置字体,需要系统中有支持中文的字体
    try:
        # Windows系统常用字体
        font = ImageFont.truetype("simhei.ttf", font_size)
    except:
        try:
            # 尝试其他常见中文字体
            font = ImageFont.truetype("arial.ttf", font_size)
        except:
            # 如果找不到特定字体,使用默认字体
            font = ImageFont.load_default()

    # 创建绘图对象
    draw = ImageDraw.Draw(image_pil)

    # 绘制文本
    draw.text(position, text, font=font, fill=color)

    # 转换回OpenCV格式
    image_cv = cv2.cvtColor(np.asarray(image_pil), cv2.COLOR_RGB2BGR)

    return image_cv


def detect_text_positions(image_path):
    """检测图片中的文字位置"""
    # 加载图片
    image = cv2.imread(image_path)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    # 使用 cnocr 检测文字
    results = ocr.ocr(gray)  # 使用 ocr.ocr 而不是 ocr.ocr_for_single_line

    # 保存文字框
    text_positions = []
    for result in results:
        text = result['text']
        bbox = result['position']
        x_min, y_min = bbox[0]
        x_max, y_max = bbox[2]
        x, y, w, h = int(x_min), int(y_min), int(x_max - x_min), int(y_max - y_min)

        # 计算文字的像素宽度和高度
        pixel_width = w
        pixel_height = h

        text_positions.append({
            "text": text,
            "bbox": (x, y, w, h),
            "pixel_width": pixel_width,
            "pixel_height": pixel_height
        })

        # 在图片上绘制文字框
        cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
        # 使用支持中文的函数绘制文本
        image = put_chinese_text(image, text, (x, y - 20), font_size=15, color=(0, 255, 0))

    return image, text_positions


def main(image_path):
    """主函数:检测文字位置"""
    # 检测文字
    text_image, text_positions = detect_text_positions(image_path)

    # 显示结果
    cv2.imshow("Detected Text", text_image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

    # 打印文字位置及尺寸信息
    print("文字位置:")
    for text_pos in text_positions:
        print(f"文本: {text_pos['text']}, 坐标: {text_pos['bbox']}, "
              f"像素宽度: {text_pos['pixel_width']}, 像素高度: {text_pos['pixel_height']}")

    return {"text_positions": text_positions}


if __name__ == "__main__":
    image_path = "6.png"  # 替换为你的图片路径
    results = main(image_path)
相关推荐
chenchihwen37 分钟前
AI代码开发宝库系列:Function Call
人工智能·python·1024程序员节·dashscope
FreeBuf_38 分钟前
微软Copilot被用于窃取OAuth令牌,AI Agent成为攻击者帮凶
人工智能·microsoft·copilot
学slam的小范39 分钟前
ROS跑ORB-SLAM3遇见的问题总结
人工智能·机器人·自动驾驶
coding消烦员1 小时前
新版 vscode 去除快捷键 Ctrl+I 显示 Copilot 的 AI 对话框
人工智能·vscode·copilot
周杰伦_Jay1 小时前
【自动驾驶开源仿真平台】Carla、AirSim、Udacity self-driving-car-sim、Apollo、Autoware。
人工智能·机器学习·自动驾驶
牛奶还是纯的好2 小时前
双目测距实战5-立体矫正
人工智能·3d
无风听海2 小时前
神经网络之窗口大小对词语义向量的影响
人工智能·深度学习·神经网络
sali-tec2 小时前
C# 基于halcon的视觉工作流-章52-生成标定板
开发语言·图像处理·人工智能·算法·计算机视觉
IT古董2 小时前
【第五章:计算机视觉-项目实战之推荐/广告系统】2.粗排算法-(4)粗排算法模型多目标算法(Multi Task Learning)及目标融合
人工智能·算法·1024程序员节
newxtc3 小时前
【江苏政务服务网-注册_登录安全分析报告】
人工智能·安全·yolo·政务·1024程序员节·安全爆破