互感器铭牌图像识别系统
互感器铭牌图像识别系统
下面是一个完整的互感器铭牌图像识别系统,使用Python实现。该系统能够自动识别互感器铭牌图像中的关键信息,包括型号、序列号、额定参数等。
系统设计思路
互感器铭牌识别流程:
- 图像预处理:增强图像质量,提高识别准确率
- 文本区域检测:定位铭牌中的文字区域
- OCR识别:提取文字内容
- 信息解析:从识别结果中提取关键参数
- 结果验证:检查识别结果的合理性
import cv2
import numpy as np
import pytesseract
import re
import os
import matplotlib.pyplot as plt
from PIL import Image
from skimage import exposure, filters
from difflib import SequenceMatcher
from datetime import datetime
import json# 设置Tesseract路径(根据实际安装路径修改)
# pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'class TransformerPlateRecognizer:def __init__(self):self.recognized_data = {"model": None,"serial_number": None,"rated_voltage": None,"rated_current": None,"accuracy_class": None,"manufacturer": None,"production_date": None,"frequency": None,"insulation_level": None}self.common_manufacturers = ["ABB", "Siemens", "Schneider", "GE", "Mitsubishi", "Toshiba", "Hitachi", "Hyundai"]self.debug = Falseself.image_path = Noneself.preprocessed_image = Nonedef preprocess_image(self, image_path):"""图像预处理:增强对比度、降噪、二值化等"""self.image_path = image_path# 读取图像img = cv2.imread(image_path)if img is None:raise ValueError(f"无法读取图像: {image_path}")# 转换为灰度图gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)# 对比度增强 - CLAHEclahe = cv2.createCLAHE(clipLimit=3.0, tileGridSize=(8, 8))enhanced = clahe.apply(gray)# 降噪 - 非局部均值去噪denoised = cv2.fastNlMeansDenoising(enhanced, None, h=10, templateWindowSize=7, searchWindowSize=21)# 自适应阈值二值化binary = cv2.adaptiveThreshold(denoised, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)# 形态学操作 - 开运算去除小噪点kernel = np.ones((1, 1), np.uint8)processed = cv2.morphologyEx(binary, cv2.MORPH_OPEN, kernel)self.preprocessed_image = processedreturn processeddef detect_text_regions(self):"""检测文本区域"""if self.preprocessed_image is None:raise RuntimeError("请先预处理图像")# 使用轮廓检测找到文本区域contours, _ = cv2.findContours(self.preprocessed_image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)# 筛选出可能是文本的区域text_regions = []for contour in contours:x, y, w, h = cv2.boundingRect(contour)# 过滤掉太小的区域if w < 15 or h < 15:continue# 过滤掉太宽或太高的区域(可能是线条)if w > 5 * h or h > 3 * w:continuetext_regions.append((x, y, w, h))# 合并重叠的区域merged_regions = []text_regions = sorted(text_regions, key=lambda r: r[1]) # 按y坐标排序for region in text_regions:x, y, w, h = regionmerged = Falsefor i, merged_region in enumerate(merged_regions):mx, my, mw, mh = merged_region# 检查是否重叠if (x < mx + mw and x + w > mx and y < my + mh and y + h > my):# 合并区域nx = min(x, mx)ny = min(y, my)nw = max(x + w, mx + mw) - nxnh = max(y + h, my + mh) - nymerged_regions[i] = (nx, ny, nw, nh)merged = Truebreakif not merged:merged_regions.append(region)# 按从上到下、从左到右排序merged_regions = sorted(merged_regions, key=lambda r: (r[1], r[0]))# 绘制检测到的区域(调试用)if self.debug:debug_img = cv2.cvtColor(self.preprocessed_image, cv2.COLOR_GRAY2BGR)for (x, y, w, h) in merged_regions:cv2.rectangle(debug_img, (x, y), (x + w, y + h), (0, 255, 0), 2)plt.figure(figsize=(12, 8))plt.imshow(cv2.cvtColor(debug_img, cv2.COLOR_BGR2RGB))plt.t