要解决的整个问题是读取这些能量计显示的数字。 An Image of the energy meter. 然后我需要能够在 android 应用程序中实现所有这些。 我要做的是首先通过回归找到包含数字的黑色矩形的位置。然后尝试通过另一个网络读取数字。
1- 我是否选择了解决这个问题的正确途径?
2- 我下面的网络出了什么问题?
我有一个包含 78 张图像的小型数据集,如上图所示。 为了预测矩形的位置,我使用 500*200 大小的滑动窗口从原始图像中裁剪了一些小图像。我有将近 10,000 张大小为 500*200 的图像。使用网络的第一个版本,我获得了高精度和低损失。但问题是我对任何输入都得到了相同的考试输出。我尝试了不同的东西并接受了再培训,但没有运气。但是最后一个网络没有准确性。
这就是我加载数据的方式:
def load_train_data(self):
data = np.empty((0, 20, 50, 1), int)
labels = np.empty((0, 8), int)
files = glob.glob(self.dataset_path + '\\train\\*.jpg')
print('{} train files found'.format(len(files)))
print('loading files...')
for i in range(len(files)):
image = Image.open(files[i]).convert('L')
data = np.append(data, [np.array(image).reshape((20, 50, 1)).astype('float32') / 255], axis=0)
labels = np.append(labels, [np.array(self.decode_file_name(files[i]))], axis=0)
return (data, labels)
def decode_file_name(self, file_name):
arr = file_name.split('\\')
name = arr[len(arr) - 1];
name_parts = name[0:len(name) - 4].split("_")
if len(name_parts) == 11:
temp = [int(name_parts[3]), int(name_parts[4]), int(name_parts[5]), int(name_parts[6]),
int(name_parts[7]), int(name_parts[8]), int(name_parts[9]), int(name_parts[10])]
else:
temp = [0, 0, 0, 0, 0, 0, 0, 0]
return temp
这是模型:
def build_model():
m = tf.keras.Sequential([
tf.keras.layers.Conv2D(64, kernel_size=10, activation=tf.keras.activations.relu, input_shape=(20, 50, 1),
data_format='channels_last'),
tf.keras.layers.Conv2D(32, kernel_size=5, activation=tf.keras.activations.relu),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(100, activation=tf.keras.activations.relu),
# tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(8, activation=tf.keras.activations.linear)
])
m.compile(optimizer=tf.keras.optimizers.Adam(lr=0.0001), loss=tf.keras.losses.mean_absolute_percentage_error,
metrics=['accuracy'])
return m
最后是拟合函数:
model = build_model()
history = model.fit(x_train, y_train, epochs=10, validation_data=(x_test, y_test))
这是上述网络的结果:
更新
我在加载数据集的过程中发现了一个错误并修复了它。我重新训练了网络,但无论我如何更改网络,准确率都低于 40%。
最佳答案
下面是使用OpenCV获取黑色矩形ROI的方法
- 将图像转换为灰度和高斯模糊
- Canny 边缘检测
- 执行形态学操作以平滑图像
- 使用最小阈值区域查找轮廓并进行过滤
- 用所需的矩形创建 mask
- 提取投资返回率
Canny边缘检测
变形关闭
找到轮廓并使用最小阈值区域进行过滤以隔离矩形,然后绘制到蒙版上
从这里,我们找到边界矩形,然后使用 Numpy 切片提取
结果
import cv2
import numpy as np
image = cv2.imread('1.jpg')
result = image.copy()
mask = np.zeros(result.shape, dtype=np.uint8)
blur = cv2.GaussianBlur(image, (3,3), 0)
gray = cv2.cvtColor(blur, cv2.COLOR_BGR2GRAY)
canny = cv2.Canny(gray, 120, 255, 1)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3))
close = cv2.morphologyEx(canny, cv2.MORPH_CLOSE, kernel)
cnts = cv2.findContours(close, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
min_area = 10000
for c in cnts:
area = cv2.contourArea(c)
if area > min_area:
cv2.drawContours(mask, [c], -1, (255,255,255), -1)
mask = cv2.cvtColor(mask, cv2.COLOR_BGR2GRAY)
result[mask==0] = (255,255,255)
mask_canny = cv2.Canny(result, 120, 255, 1)
cnts = cv2.findContours(mask_canny, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
x,y,w,h = cv2.boundingRect(c)
ROI = result[y:y+h, x:x+w]
cv2.imwrite("ROI.png", ROI)
cv2.rectangle(result, (x, y), (x + w, y + h), (36,255,12), 2)
cv2.imshow('canny', canny)
cv2.imshow('close', close)
cv2.imshow('image', image)
cv2.imshow('mask', mask)
cv2.imshow('result', result)
cv2.waitKey(0)
关于python - 使用 keras 对图像进行 convnet 回归 - 精度不能提高超过 40%,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57483565/