diff --git a/Interface.py b/Interface.py index dcba3f3..fc08a79 100644 --- a/Interface.py +++ b/Interface.py @@ -9,7 +9,6 @@ from algorithm.highlightDigitMeter import highlightDigit from algorithm.videoDigit import videoDigit - from algorithm.arrest.countArrester import countArrester from algorithm.arrest.doubleArrester import doubleArrester @@ -20,6 +19,7 @@ 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 configuration import * @@ -96,13 +96,14 @@ def getInfo(ID): info["type"] = onoffBattery elif info["type"] == "videoDigit": info["type"] = videoDigit + elif info["type"] == "ready": + info["type"] = readyStatus 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")))) + info.update(json.load(open(os.path.join("ocr_config", info["digitType"] + ".json")))) return info @@ -135,7 +136,7 @@ def meterReader(recognitionData, meterIDs): ROI = recognitionData[y:y + h, x:x + w] # results[ID] = meterReaderCallBack(ROI, info) results.append(meterReaderCallBack(ROI, info)) - # else: + # else: # results[ID] = meterReaderCallBack(recognitionData, info) results.append(meterReaderCallBack(recognitionData, info)) diff --git a/TestServiceSample.py b/TestServiceSample.py index 4704e60..dbd97a3 100644 --- a/TestServiceSample.py +++ b/TestServiceSample.py @@ -32,11 +32,26 @@ def startClient(results): results.append(True) +def testReadyStatus(): + imgPath = "image" + configPath = "info/20190410/config" + images = os.listdir(imgPath) + config = os.listdir(configPath) + for im in images: + filename, extention = os.path.splitext(im.lower()) + if extention == '.jpg' or extention == '.png': + image = cv2.imread(imgPath + "/" + im) + for i in range(1, 6): + cfg = filename + "_" + str(i) + if cfg + ".json" in config: + receive2 = meterReader(image, [cfg]) + print(cfg, receive2) + + def codecov(): images = os.listdir("image") config = os.listdir("config") - for im in images: image = cv2.imread(imgPath + "/" + im) print(im) @@ -50,6 +65,7 @@ def codecov(): print("codecov done") + def testVideo(): video_path = ("video_") config = os.listdir("config") @@ -72,9 +88,9 @@ def testVideo(): # clientProcess.start() # clientProcess.join() # serverProcess.terminate() - + # testReadyStatus() codecov() - testVideo() + # testVideo() # # for i in range(20): # serverProcess = multiprocessing.Process(target=startServer) diff --git a/algorithm/Common.py b/algorithm/Common.py index b553a6a..a662cf4 100644 --- a/algorithm/Common.py +++ b/algorithm/Common.py @@ -138,8 +138,9 @@ def meterFinderBySIFT(image, info): # for debug # templateBlurred = cv2.drawKeypoints(templateBlurred, templateKeyPoint, templateBlurred) # imageBlurred = cv2.drawKeypoints(imageBlurred, imageKeyPoint, imageBlurred) - # cv2.imshow("template", templateBlurred) + # # cv2.imshow("template", templateBlurred) # cv2.imshow("image", imageBlurred) + # cv2.waitKey(0) # match bf = cv2.BFMatcher() @@ -148,7 +149,6 @@ def meterFinderBySIFT(image, info): # The first one is better than the second one good = [[m] for m, n in matches if m.distance < 0.8 * n.distance] - # distance matrix templatePointMatrix = np.array([list(templateKeyPoint[p[0].queryIdx].pt) for p in good]) imagePointMatrix = np.array([list(imageKeyPoint[p[0].trainIdx].pt) for p in good]) @@ -301,7 +301,7 @@ def calPointerValueByOuterPoint(cls, startPoint, endPoint, centerPoint, pointerP # print(startPoint, endPoint, centerPoint, pointerPoint, startValue, totalValue) angleRange = cls.calAngleClockwise(startPoint, endPoint, centerPoint) angle = cls.calAngleClockwise(startPoint, pointerPoint, centerPoint) - value = angle / angleRange * (totalValue-startValue) + startValue + value = angle / angleRange * (totalValue - startValue) + startValue if value > totalValue or value < startValue: return startValue if angle > np.pi + angleRange / 2 else totalValue return value @@ -437,7 +437,7 @@ def scanPointer(meter, info): degree = AngleFactory.calPointerValueByOuterPoint(start, end, center, outerPoint, startVal, endVal) # small value to zero - if degree-startVal < 0.05 * (endVal-startVal): + if degree - startVal < 0.05 * (endVal - startVal): degree = startVal if ifShow: diff --git a/algorithm/onoff/frozen_east_text_detection.pb b/algorithm/onoff/frozen_east_text_detection.pb new file mode 100644 index 0000000..5702180 Binary files /dev/null and b/algorithm/onoff/frozen_east_text_detection.pb differ diff --git a/algorithm/onoff/readyStatus.py b/algorithm/onoff/readyStatus.py new file mode 100644 index 0000000..b41da2e --- /dev/null +++ b/algorithm/onoff/readyStatus.py @@ -0,0 +1,148 @@ +import numpy as np +import time +import cv2 +from algorithm.Common import meterFinderBySIFT, meterFinderByTemplate + + +def isDark(img): + gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) + r, c = gray_img.shape[:2] + # dark num + dark_sum = 0 + # dark ration + dark_prop = 0 + # pixels n7m of gray image + piexs_sum = r * c + for row in gray_img: + for colum in row: + if colum < 120: + dark_sum += 1 + dark_prop = dark_sum / (piexs_sum) + # print("dark_sum:" + str(dark_sum)) + # print("piexs_sum:" + str(piexs_sum)) + # print("dark_prop=dark_sum/piexs_sum:" + str(dark_prop)) + if dark_prop >= 0.70: + return True + else: + return False + + # def hist(pic_path): + # img = cv2.imread(pic_path, 0) + # hist = cv2.calcHist([img], [0], None, [256], [0, 256]) + # plt.subplot(121) + # plt.imshow(img, 'gray') + # plt.xticks([]) + # plt.yticks([]) + # plt.title("Original") + # plt.subplot(122) + # plt.hist(img.ravel(), 256, [0, 256]) + # plt.show() + # + + +def readyStatus(img, info): + template = info['template'] + image = meterFinderByTemplate(img, info['template']) + # if image is dark enough, do gamma correction for enhancing dark details + if isDark(image): + max = np.max(image) + image = np.power(image / float(max), 1/3) * max + image = image.astype(np.uint8) + # cv2.imshow('Gamma', image) + # cv2.waitKey(0) + t_shape = template.shape[:2] + # image = meterFinderBySIFT(img, info) + orig = image.copy() + # minimum probability required to inspect a region + min_confidence = 0.5 + # load the input image and grab the image dimensions + (H, W) = image.shape[:2] + # path to input EAST text detector + model_name = 'frozen_east_text_detection.pb' + # set the new width and height and then determine the ratio in change + # for both the width and height + # image width should be multiple of 32, so do height + newW = (t_shape[0]) // 32 * 32 + newH = (t_shape[1]) // 32 * 32 + rW = W / float(newW) + rH = H / float(newH) + # resize the image and grab the new image dimensions + image = cv2.resize(image, (newW, newH)) + (H, W) = image.shape[:2] + # define the two output layer names for the EAST detector model that + # we are interested -- the first is the output probabilities and the + # second can be used to derive the bounding box coordinates of text + layerNames = [ + "feature_fusion/Conv_7/Sigmoid", + "feature_fusion/concat_3"] + # load the pre-trained EAST text detector + # print("[INFO] loading EAST text detector...") + net = cv2.dnn.readNet(model_name) + # construct a blob from the image and then perform a forward pass of + # the model to obtain the two output layer sets + blob = cv2.dnn.blobFromImage(image, 1.0, (W, H), + (123.68, 116.78, 103.94), swapRB=True, crop=False) + start = time.time() + net.setInput(blob) + (scores, geometry) = net.forward(layerNames) + end = time.time() + # show timing information on text prediction + # print("[INFO] text detection took {:.6f} seconds".format(end - start)) + # grab the number of rows and columns from the scores volume, then + # initialize our set of bounding box rectangles and corresponding + # confidence scores + (numRows, numCols) = scores.shape[2:4] + rects = [] + confidences = [] + # loop over the number of rows + for y in range(0, numRows): + # extract the scores (probabilities), followed by the geometrical + # data used to derive potential bounding box coordinates that + # surround text + scoresData = scores[0, 0, y] + xData0 = geometry[0, 0, y] + xData1 = geometry[0, 1, y] + xData2 = geometry[0, 2, y] + xData3 = geometry[0, 3, y] + anglesData = geometry[0, 4, y] + + # loop over the number of columns + for x in range(0, numCols): + # if our score does not have sufficient probability, ignore it + if scoresData[x] < min_confidence: + continue + + # compute the offset factor as our resulting feature maps will + # be 4x smaller than the input image + (offsetX, offsetY) = (x * 4.0, y * 4.0) + + # extract the rotation angle for the prediction and then + # compute the sin and cosine + angle = anglesData[x] + cos = np.cos(angle) + sin = np.sin(angle) + + # use the geometry volume to derive the width and height of + # the bounding box + h = xData0[x] + xData2[x] + w = xData1[x] + xData3[x] + + # compute both the starting and ending (x, y)-coordinates for + # the text prediction bounding box + endX = int(offsetX + (cos * xData1[x]) + (sin * xData2[x])) + endY = int(offsetY - (sin * xData1[x]) + (cos * xData2[x])) + startX = int(endX - w) + startY = int(endY - h) + + # add the bounding box coordinates and probability score to + # our respective lists + rects.append((startX, startY, endX, endY)) + confidences.append(scoresData[x]) + if len(rects) > 0: + return { + 'value': True + } + else: + return { + 'value': False + } diff --git a/image/onoff1.jpg b/image/onoff1.jpg new file mode 100755 index 0000000..04303b4 Binary files /dev/null and b/image/onoff1.jpg differ diff --git a/image/onoff2.png b/image/onoff2.png new file mode 100755 index 0000000..3f0abb2 Binary files /dev/null and b/image/onoff2.png differ diff --git a/image/onoff3.jpg b/image/onoff3.jpg new file mode 100755 index 0000000..8632ffe Binary files /dev/null and b/image/onoff3.jpg differ diff --git a/image/onoff4.jpg b/image/onoff4.jpg new file mode 100755 index 0000000..fad5aae Binary files /dev/null and b/image/onoff4.jpg differ diff --git a/image/onoff5.jpg b/image/onoff5.jpg new file mode 100755 index 0000000..2ab7a4c Binary files /dev/null and b/image/onoff5.jpg differ diff --git a/image/onoff6.jpg b/image/onoff6.jpg new file mode 100755 index 0000000..feebf27 Binary files /dev/null and b/image/onoff6.jpg differ diff --git a/image/onoff7.jpg b/image/onoff7.jpg new file mode 100755 index 0000000..25e9618 Binary files /dev/null and b/image/onoff7.jpg differ diff --git a/info/20190410/config/onoff1_1.json b/info/20190410/config/onoff1_1.json new file mode 100644 index 0000000..d0ebcc3 --- /dev/null +++ b/info/20190410/config/onoff1_1.json @@ -0,0 +1,26 @@ +{ + "name": "test", + "type": "ready", + "ROI": { + "x": 428, + "y": 741, + "w": 152, + "h": 153 + }, + "startPoint": { + "x": 23, + "y": 149 + }, + "endPoint": { + "x": 148, + "y": 35 + }, + "centerPoint": { + "x": 147, + "y": 150 + }, + "startValue": 0, + "totalValue": 12, + "digitType": "False", + "result": 0.0 +} \ No newline at end of file diff --git a/info/20190410/config/onoff2_1.json b/info/20190410/config/onoff2_1.json new file mode 100644 index 0000000..6a85a0d --- /dev/null +++ b/info/20190410/config/onoff2_1.json @@ -0,0 +1,26 @@ +{ + "name": "test", + "type": "ready", + "ROI": { + "x": 171, + "y": 47, + "w": 90, + "h": 90 + }, + "startPoint": { + "x": 23, + "y": 149 + }, + "endPoint": { + "x": 148, + "y": 35 + }, + "centerPoint": { + "x": 147, + "y": 150 + }, + "startValue": 0, + "totalValue": 12, + "digitType": "False", + "result": 0.0 +} \ No newline at end of file diff --git a/info/20190410/config/onoff3_1.json b/info/20190410/config/onoff3_1.json new file mode 100644 index 0000000..2ecdea1 --- /dev/null +++ b/info/20190410/config/onoff3_1.json @@ -0,0 +1,26 @@ +{ + "name": "test", + "type": "ready", + "ROI": { + "x": 527, + "y": 1085, + "w": 237, + "h": 197 + }, + "startPoint": { + "x": 23, + "y": 149 + }, + "endPoint": { + "x": 148, + "y": 35 + }, + "centerPoint": { + "x": 147, + "y": 150 + }, + "startValue": 0, + "totalValue": 12, + "digitType": "False", + "result": 0.0 +} \ No newline at end of file diff --git a/info/20190410/config/onoff4_1.json b/info/20190410/config/onoff4_1.json new file mode 100644 index 0000000..6bfb7f2 --- /dev/null +++ b/info/20190410/config/onoff4_1.json @@ -0,0 +1,26 @@ +{ + "name": "test", + "type": "ready", + "ROI": { + "x": 507, + "y": 662, + "w": 172, + "h": 203 + }, + "startPoint": { + "x": 23, + "y": 149 + }, + "endPoint": { + "x": 148, + "y": 35 + }, + "centerPoint": { + "x": 147, + "y": 150 + }, + "startValue": 0, + "totalValue": 12, + "digitType": "False", + "result": 0.0 +} \ No newline at end of file diff --git a/info/20190410/config/onoff5_1.json b/info/20190410/config/onoff5_1.json new file mode 100644 index 0000000..9a6547d --- /dev/null +++ b/info/20190410/config/onoff5_1.json @@ -0,0 +1,26 @@ +{ + "name": "test", + "type": "ready", + "ROI": { + "x": 584, + "y": 1098, + "w": 302, + "h": 257 + }, + "startPoint": { + "x": 23, + "y": 149 + }, + "endPoint": { + "x": 148, + "y": 35 + }, + "centerPoint": { + "x": 147, + "y": 150 + }, + "startValue": 0, + "totalValue": 12, + "digitType": "False", + "result": 0.0 +} \ No newline at end of file diff --git a/info/20190410/config/onoff6_1.json b/info/20190410/config/onoff6_1.json new file mode 100644 index 0000000..5c63530 --- /dev/null +++ b/info/20190410/config/onoff6_1.json @@ -0,0 +1,26 @@ +{ + "name": "test", + "type": "ready", + "ROI": { + "x": 594, + "y": 732, + "w": 287, + "h": 235 + }, + "startPoint": { + "x": 23, + "y": 149 + }, + "endPoint": { + "x": 148, + "y": 35 + }, + "centerPoint": { + "x": 147, + "y": 150 + }, + "startValue": 0, + "totalValue": 12, + "digitType": "False", + "result": 0.0 +} \ No newline at end of file diff --git a/info/20190410/config/onoff7_1.json b/info/20190410/config/onoff7_1.json new file mode 100644 index 0000000..a348d6e --- /dev/null +++ b/info/20190410/config/onoff7_1.json @@ -0,0 +1,26 @@ +{ + "name": "test", + "type": "ready", + "ROI": { + "x": 490, + "y": 898, + "w": 237, + "h": 164 + }, + "startPoint": { + "x": 23, + "y": 149 + }, + "endPoint": { + "x": 148, + "y": 35 + }, + "centerPoint": { + "x": 147, + "y": 150 + }, + "startValue": 0, + "totalValue": 12, + "digitType": "False", + "result": 0.0 +} \ No newline at end of file diff --git a/info/20190410/template/onoff1_1.jpg b/info/20190410/template/onoff1_1.jpg new file mode 100755 index 0000000..3112b0f Binary files /dev/null and b/info/20190410/template/onoff1_1.jpg differ diff --git a/info/20190410/template/onoff2_1.jpg b/info/20190410/template/onoff2_1.jpg new file mode 100755 index 0000000..527a863 Binary files /dev/null and b/info/20190410/template/onoff2_1.jpg differ diff --git a/info/20190410/template/onoff3_1.jpg b/info/20190410/template/onoff3_1.jpg new file mode 100755 index 0000000..d14b0b5 Binary files /dev/null and b/info/20190410/template/onoff3_1.jpg differ diff --git a/info/20190410/template/onoff4_1.jpg b/info/20190410/template/onoff4_1.jpg new file mode 100755 index 0000000..a29c433 Binary files /dev/null and b/info/20190410/template/onoff4_1.jpg differ diff --git a/info/20190410/template/onoff5_1.jpg b/info/20190410/template/onoff5_1.jpg new file mode 100755 index 0000000..10ff269 Binary files /dev/null and b/info/20190410/template/onoff5_1.jpg differ diff --git a/info/20190410/template/onoff6_1.jpg b/info/20190410/template/onoff6_1.jpg new file mode 100755 index 0000000..1e3be5d Binary files /dev/null and b/info/20190410/template/onoff6_1.jpg differ diff --git a/info/20190410/template/onoff7_1.jpg b/info/20190410/template/onoff7_1.jpg new file mode 100755 index 0000000..bbf8a30 Binary files /dev/null and b/info/20190410/template/onoff7_1.jpg differ