当前位置: 首页 > news >正文

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

在这里插入图片描述
同时还 计算出坐标及字体大小

在这里插入图片描述

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_cvdef 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 = wpixel_height = htext_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_positionsdef 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)
http://www.dtcms.com/a/339977.html

相关文章:

  • RocketMq消费者动态订阅topic
  • 【PyTorch项目实战】OpenNMT本地机器翻译框架 —— 支持本地部署和自定义训练
  • 千里马招标网站的核心技术分析
  • qwen2.5vl(1): 环境安装及运行
  • 二维图像处理(完整版2)
  • iOS安全和逆向系列教程 第22篇:iOS应用网络安全与通信保护
  • 自学python第10天
  • 路由器最大传输速率测试
  • VS Code 终端完全指南
  • Pandas中数据清理、连接数据以及合并多个数据集的方法
  • 仿新浪微博 typecho 主题模板源码
  • 1. AutoSAR 技术学习
  • Spring AOP核心原理与实战指南
  • 任务十一 搜索页面开发
  • Incredibuild 新增 Unity 支持:击破构建时间过长的痛点
  • AutoSarAP状态管理的状态机能否理解成C++的类?
  • 电视系统:开启视听新时代
  • 一个多功能的文件分享工具--zdir手动部署教程
  • 垂直领域大模型构建:法律行业“类ChatGPT”系统的训练与落地
  • el-table合并单元格
  • 接口自动化测试大全(python+pytest+allure)
  • Angular极速入门
  • 【CUDA教程--3】通过简单的矩阵运算入门CUDA
  • cursor+mcp-clickhouse进行数据分析
  • Spring循环依赖源码调试详解,用两级缓存代替三级缓存
  • JB4-9-任务调度
  • 网络通信基础:从数据链路层到传输层
  • 如何解决pip安装报错ModuleNotFoundError: No module named ‘paramiko’问题
  • Leetcode 3652. Best Time to Buy and Sell Stock using Strategy
  • 【20250819】mathtype的使用