From 0e430960e3ef9404fbb50d7de263172f82e6955d Mon Sep 17 00:00:00 2001 From: buyi1128 <1422236959@qq.com> Date: Fri, 24 May 2019 15:44:27 +0800 Subject: [PATCH] =?UTF-8?q?=E9=BB=84=E8=89=B2=E6=8C=87=E7=A4=BA=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Algorithm/Remember.py | 8 ---- Algorithm/others/colorIndicator.py | 19 ++++++++ Algorithm/pressure/digitPressure.py | 74 +++++++++++++++++++++++------ Interface.py | 30 ++++-------- TestServiceSample.py | 17 +++++-- ocr_config/KWH.json | 2 +- 6 files changed, 102 insertions(+), 48 deletions(-) delete mode 100644 Algorithm/Remember.py create mode 100644 Algorithm/others/colorIndicator.py diff --git a/Algorithm/Remember.py b/Algorithm/Remember.py deleted file mode 100644 index 7989b5a..0000000 --- a/Algorithm/Remember.py +++ /dev/null @@ -1,8 +0,0 @@ -def remember(image, info): - """ - remember value for exam - :param image: ROI image - :param info: information for this meter - :return: value - """ - return info["result"] diff --git a/Algorithm/others/colorIndicator.py b/Algorithm/others/colorIndicator.py new file mode 100644 index 0000000..9344ec9 --- /dev/null +++ b/Algorithm/others/colorIndicator.py @@ -0,0 +1,19 @@ +import cv2 +import numpy as np + +from Algorithm.utils.Finder import meterFinderByTemplate, meterFinderBySIFT + +def colorIndicator(ROI, info): + res = False + image = meterFinderBySIFT(ROI, info) + HSV = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) + # color = [np.array([26, 43, 46]), np.array([34, 255, 255])] + color = [np.array([11, 43, 46]), np.array([34, 255, 255])] + Lower = color[0] + Upper = color[1] + mask = cv2.inRange(HSV, Lower, Upper) + upmask = mask[int(0.25*mask.shape[0]):int(0.5*mask.shape[0]), :] + upmask = cv2.bitwise_and(np.ones(upmask.shape, np.uint8), upmask) + if np.sum(upmask) / upmask.shape[0]*upmask.shape[1] > 0.2: + res = True + return res \ No newline at end of file diff --git a/Algorithm/pressure/digitPressure.py b/Algorithm/pressure/digitPressure.py index 69e46e6..7762893 100644 --- a/Algorithm/pressure/digitPressure.py +++ b/Algorithm/pressure/digitPressure.py @@ -11,6 +11,8 @@ def digitPressure(image, info): + # cv2.imshow("img ", image) + # cv2.waitKey(0) template = meterFinderBySIFT(image, info) # 存储图片 @@ -27,6 +29,7 @@ def digitPressure(image, info): try: os.mkdir("storeDigitData/thresh/" + str(i)) os.mkdir("storeDigitData/rgb/" + str(i)) + savePicture(template, info) except IOError: continue @@ -60,7 +63,6 @@ def rgbRecognize(template, info): # 网络初始化 MyNet = newNet(if_rgb=True) myRes = [] - imgNum = int((len(os.listdir("storeDigitData/")) - 1) / 3) for i in range(len(heightSplit)): split = widthSplit[i] myNum = "" @@ -73,9 +75,6 @@ def rgbRecognize(template, info): num = MyNet.recognizeNet(img) myNum = myNum + num - # 存储图片 - cv2.imwrite("storeDigitData/rgb/{}/{}_{}{}_p{}.bmp".format( - num, imgNum, i, j, num), img) myRes.append(myNum) if ifShow: @@ -105,6 +104,62 @@ def bitRecognize(template, info): # 灰度图 gray = cv2.cvtColor(dst, cv2.COLOR_BGR2GRAY) + # 针对不同的数字表类型进行不同的增强 + if info["digitType"] != "TTC": + Blur = cv2.GaussianBlur(gray, (5, 5), 0) + Hist = cv2.equalizeHist(Blur) + thresh = cv2.adaptiveThreshold(Hist, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV, 15, 11) + else: + thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, block, param) + if ifOpen == "close": + p = cv2.getStructuringElement(cv2.MORPH_RECT, (2, 2)) + thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, p) + + # 网络初始化 + MyNet = newNet(if_rgb=False) + myRes = [] + + for i in range(len(heightSplit)): + split = widthSplit[i] + myNum = "" + for j in range(len(split) - 1): + if "decimal" in info.keys() and j == info["decimal"][i]: + myNum += "." + continue + # 得到分割的图片区域 + img = thresh[heightSplit[i][0]:heightSplit[i][1], split[j]:split[j + 1]] + rgb_ = dst[heightSplit[i][0]:heightSplit[i][1], split[j]:split[j + 1]] + num = MyNet.recognizeNet(img) + myNum = myNum + num + + myRes.append(myNum) + + if ifShow: + cv2.imshow("rec", dst) + cv2.imshow("template", template) + print(myRes) + cv2.waitKey(0) + cv2.destroyAllWindows() + return myRes + +def savePicture(template, info): + template = cv2.GaussianBlur(template, (3, 3), 0) + + # 读取标定信息 + widthSplit = info["widthSplit"] + heightSplit = info["heightSplit"] + + # 将info中的参数加入代码中 + block = info["thresh"]["block"] + param = info["thresh"]["param"] + ifOpen = info["ifopen"] + + # 由标定点得到液晶区域 + dst = boxRectifier(template, info) + + # 灰度图 + gray = cv2.cvtColor(dst, cv2.COLOR_BGR2GRAY) + # 针对不同的数字表类型进行不同的增强 if info["digitType"] != "TTC": Blur = cv2.GaussianBlur(gray, (5, 5), 0) @@ -124,7 +179,7 @@ def bitRecognize(template, info): # 网络初始化 MyNet = newNet(if_rgb=False) - myRes = [] + imgNum = int((len(os.listdir("storeDigitData/")) - 1) / 3) for i in range(len(heightSplit)): @@ -137,7 +192,6 @@ def bitRecognize(template, info): # 得到分割的图片区域 img = thresh[heightSplit[i][0]:heightSplit[i][1], split[j]:split[j + 1]] rgb_ = dst[heightSplit[i][0]:heightSplit[i][1], split[j]:split[j + 1]] - num = MyNet.recognizeNet(img) myNum = myNum + num @@ -146,12 +200,4 @@ def bitRecognize(template, info): num, imgNum, i, j, num), img) cv2.imwrite("storeDigitData/rgb/{}/{}_{}{}_p{}.bmp".format( num, imgNum, i, j, num), rgb_) - myRes.append(myNum) - if ifShow: - cv2.imshow("rec", dst) - cv2.imshow("template", template) - print(myRes) - cv2.waitKey(0) - cv2.destroyAllWindows() - return myRes diff --git a/Interface.py b/Interface.py index e577d70..7d01940 100644 --- a/Interface.py +++ b/Interface.py @@ -14,16 +14,15 @@ from Algorithm.pressure.normalPressure import normalPressure from Algorithm.pressure.colorPressure import colorPressure + +from Algorithm.onoff.contactStatus import contactStatus from Algorithm.onoff.onoffIndoor import onoffIndoor from Algorithm.onoff.onoffOutdoor import onoffOutdoor from Algorithm.onoff.onoffBatteryScreen import onoffBattery from Algorithm.onoff.readyStatus import readyStatus from Algorithm.onoff.springStatus import springStatus -from Algorithm.onoff.contactStatus import contactStatus -from Algorithm.others.colordetect import colordetect -from Algorithm.others.Cabinet_indicator import indicatorimg -from Algorithm.others.Knob_status import knobstatus +from Algorithm.others.colorIndicator import colorIndicator from configuration import * @@ -78,7 +77,7 @@ def getInfo(ID): info["type"] = digitPressure elif info["type"] == "normalPressure": info["type"] = normalPressure - elif info["type"] == "contact": + elif info["type"] == "contactStatus": info["type"] = contactStatus elif info["type"] == "colorPressure": info["type"] = colorPressure @@ -100,19 +99,14 @@ def getInfo(ID): info["type"] = onoffBattery elif info["type"] == "videoDigit": info["type"] = videoDigit - elif info["type"] == "ready": + elif info["type"] == "readyStatus": info["type"] = readyStatus - elif info["type"] == "spring": + elif info["type"] == "springStatus": info["type"] = springStatus - elif info["type"] == "colordetect": - info["type"] = colordetect - elif info["type"] == "cabinetindicator": - info["type"] = indicatorimg - elif info["type"] == "Knob": - info["type"] = knobstatus + elif info["type"] == "colorIndicator": + info["type"] = colorIndicator else: info["type"] = None - info["template"] = cv2.imread(templatePath + "/" + ID + ".jpg") if info["digitType"] != "False": info.update(json.load(open(os.path.join("ocr_config", info["digitType"] + ".json")))) @@ -145,11 +139,7 @@ def meterReader(recognitionData, meterIDs): # cv2.waitKey(0) if x != 0 or y != 0 or w != 0 or h != 0: ROI = recognitionData[y:y + h, x:x + w] - else: - ROI = recognitionData - try: results.append(meterReaderCallBack(ROI, info)) - except AttributeError: - print("Error in ", ID) - results = [0] + else: + results.append(meterReaderCallBack(recognitionData, info)) return results \ No newline at end of file diff --git a/TestServiceSample.py b/TestServiceSample.py index 3558d65..9faf616 100644 --- a/TestServiceSample.py +++ b/TestServiceSample.py @@ -33,24 +33,29 @@ def startClient(results): def codecov(imgPath): images = os.listdir(imgPath) - config = os.listdir(configPath) for im in images: + if im.startswith(".DS"): + continue image = cv2.imread(imgPath + "/" + im) print(im) pos = im.split(".")[0].split("-") - cfg = im.split(".")[0]+"_1" + cfg = "rcs0_1" # im.split(".")[0]+"_1" # for i in range(1, 6): # cfg = pos[0] + "-" + pos[1] + "_" + str(i) # if cfg + ".json" in config: # receive2 = meterReader(image, [cfg]) # print(cfg, receive2) + if image.shape[0] > 600: + shrink = image.shape[0] / 600 + image = cv2.resize(image, (0, 0), fx=1 / shrink, fy=1 / shrink) + print("image shrink: ", image.shape) receive2 = meterReader(image, [cfg]) print(cfg, receive2) print("codecov done") -def testVideo(): +def videoTest(): video_path = "info/20190128/IMAGES/video_" for file in os.listdir(video_path): if file.startswith(".DS"): @@ -83,8 +88,10 @@ def testVideo(): # codecov("info/20190410/IMAGES/Pic") # codecov("info/20190410/IMAGES/Pic_2") - codecov("info/20190515/IMAGES/image") - # codecov("info/20190514/image/") + + # codecov("info/20190523/image/") + codecov("info/split/image/") + # # codecov("info/20190416/IMAGES/image") diff --git a/ocr_config/KWH.json b/ocr_config/KWH.json index 02487e2..63844da 100644 --- a/ocr_config/KWH.json +++ b/ocr_config/KWH.json @@ -5,7 +5,7 @@ }, "widthSplit": [ [19,37,56,77,95,96], - [19,37,56,77,95,96] + [19,37,56,77,78,96] ], "heightSplit": [ [28,49],