IS Tim
-
使用的技术
- OpenCV
- PaddleOCR
- Python 3
-
OpenCV
安装依赖pip install opencv-python
图片红色矩形框识别
def opencv_paddle(): """ opencv识别红色矩形框,paddle识别文字 :return: """ # 加载图像 img = cv2.imread("file/17119755916493.png") # 转换为HSV色彩空间 hsv_img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) # 定义红色的HSV范围 lower_red = (0, 43, 46) upper_red = (10, 255, 255) # 这个范围可能需要根据实际情况调整 mask = cv2.inRange(hsv_img, lower_red, upper_red) # 使用形态学操作去除噪声,如膨胀与腐蚀 kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3)) mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel) # 找到轮廓 contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # 遍历轮廓,筛选出矩形轮廓 rectangles = [] for cnt in contours: x, y, w, h = cv2.boundingRect(cnt) if w > 50 and h > 30: # 设定最小宽度和高度阈值 rectangles.append((x, y, w, h)) # 从原图像中裁剪出红色矩形区域 cropped_images = [] for rect in rectangles: x, y, w, h = rect cropped = img[y:y + h, x:x + w] cropped_images.append(cropped)
-
PaddleOCR
安装依赖pip install paddlepaddle pip install paddleocr
图片识别
""" paddle 图片文字识别 :return: """ ocr = PaddleOCR(use_angle_cls=True, use_gpu=False) # 使用CPU预加载,不用GPU text = ocr.ocr("file/17119755916493.png", cls=True) # 打开图片文件 print(text) # 打印所有文本信息 for t in text: print(t[1][0])
-
完整实例
from paddleocr import PaddleOCR import cv2 from PIL import Image import paddleocr import numpy as np def opencv(): """ paddle 图片文字识别 :return: """ ocr = PaddleOCR(use_angle_cls=True, use_gpu=False) # 使用CPU预加载,不用GPU text = ocr.ocr("file/17119755916493.png", cls=True) # 打开图片文件 print(text) # 打印所有文本信息 for t in text: print(t[1][0]) def opencv_paddle(): """ opencv识别红色矩形框,paddle识别文字 :return: """ # 加载图像 img = cv2.imread("file/17119755916493.png") # 转换为HSV色彩空间 hsv_img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) # 定义红色的HSV范围 lower_red = (0, 43, 46) upper_red = (10, 255, 255) # 这个范围可能需要根据实际情况调整 mask = cv2.inRange(hsv_img, lower_red, upper_red) # 使用形态学操作去除噪声,如膨胀与腐蚀 kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3)) mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel) # 找到轮廓 contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # 遍历轮廓,筛选出矩形轮廓 rectangles = [] for cnt in contours: x, y, w, h = cv2.boundingRect(cnt) if w > 50 and h > 30: # 设定最小宽度和高度阈值 rectangles.append((x, y, w, h)) # 从原图像中裁剪出红色矩形区域 cropped_images = [] for rect in rectangles: x, y, w, h = rect cropped = img[y:y + h, x:x + w] cropped_images.append(cropped) # 初始化PaddleOCR ocr = paddleocr.PaddleOCR(use_angle_cls=True, lang='ch') # 如果是中文 # 对每个裁剪出的矩形框进行文字识别 for cropped in cropped_images: # 将OpenCV格式转换为PIL格式 pil_image = Image.fromarray(cv2.cvtColor(cropped, cv2.COLOR_BGR2RGB)) # 将PIL.Image转换为numpy数组 np_image = np.array(pil_image) # 进行文字识别 result = ocr.ocr(np_image) # 输出识别的文字 for line in result: # 坐标,文字 print(line) # 或者你可以保存每个裁剪区域并分别识别 # for i, crop in enumerate(cropped_images): # cv2.imwrite(f'crop_{i}.jpg', crop) # # 然后针对每个保存的文件运行PaddleOCR识别 if __name__ == '__main__': opencv_paddle()