Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Src/readyv1.0 #74

Merged
merged 3 commits into from
Apr 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions Interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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 *

Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -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))

Expand Down
22 changes: 19 additions & 3 deletions TestServiceSample.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -50,6 +65,7 @@ def codecov():

print("codecov done")


def testVideo():
video_path = ("video_")
config = os.listdir("config")
Expand All @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions algorithm/Common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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])
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
Binary file added algorithm/onoff/frozen_east_text_detection.pb
Binary file not shown.
148 changes: 148 additions & 0 deletions algorithm/onoff/readyStatus.py
Original file line number Diff line number Diff line change
@@ -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
}
Binary file added image/onoff1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/onoff2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/onoff3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/onoff4.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/onoff5.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/onoff6.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added image/onoff7.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 26 additions & 0 deletions info/20190410/config/onoff1_1.json
Original file line number Diff line number Diff line change
@@ -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
}
26 changes: 26 additions & 0 deletions info/20190410/config/onoff2_1.json
Original file line number Diff line number Diff line change
@@ -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
}
26 changes: 26 additions & 0 deletions info/20190410/config/onoff3_1.json
Original file line number Diff line number Diff line change
@@ -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
}
26 changes: 26 additions & 0 deletions info/20190410/config/onoff4_1.json
Original file line number Diff line number Diff line change
@@ -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
}
Loading