=== 小学算式OCR训练系统 ===
字符集: 0123456789+-×÷=Xx
类别数: 18
加载数据集: math_problems
错误使用 tranorc11 (第 39 行)
数据集结构错误!\n正确结构示例:\nmath_problems/\n├── 2+3=5/\n│ ├── img1.png\n│ └── img2.png\n├── 10-4=6/\n└── ...\n每个子文件夹名称应是算式标签
这是现在的代码提示,下面我会给出原来的math_problems数据图片生成代码,请根据上面的要求,修改生成代码,从而提供符合要求的数据
% === 图像生成代码 ===
outputDir = 'math_problems';
if ~exist(outputDir, 'dir')
mkdir(outputDir);
end
numImages = 100;
dataTable = table('Size', [numImages, 5], ...
'VariableTypes', {'string', 'string', 'string', 'string', 'string'}, ...
'VariableNames', {'Filename', 'Problem', 'CorrectResult', 'Operator', 'CorrectFlag'});
fontSize = 80;
horizontalPadding = 50;
verticalPadding = 80;
bgColor = [1, 1, 1];
textColor = [0, 0, 0];
fig = figure('Visible', 'off', 'Color', bgColor, ...
'Position', [100, 100, 800, 600], ...
'InvertHardcopy', 'off');
ax = axes('Parent', fig, 'Position', [0, 0, 1, 1], ...
'XLim', [0, 1], 'YLim', [0, 1], 'Visible', 'off');
hold(ax, 'on');
for imgIdx = 1:numImages
filename = sprintf('%04d.png', imgIdx);
dataTable.Filename(imgIdx) = string(filename);
opType = randi(4);
switch opType
case 1 % 加法
a = randi([10, 90]);
b = randi([1, 100-a]);
result = a + b;
eq = sprintf('%d + %d = ', a, b);
operator = 'addition';
case 2 % 减法
a = randi([20, 100]);
b = randi([1, a-1]);
result = a - b;
eq = sprintf('%d - %d = ', a, b);
operator = 'subtraction';
case 3 % 乘法
a = randi([1, 10]);
b = randi([1, 10]);
result = a * b;
eq = sprintf('%d × %d = ', a, b);
operator = 'multiplication';
case 4 % 除法
divisor = randi([2, 10]);
quotient = randi([1, 10]);
dividend = divisor * quotient;
result = quotient;
eq = sprintf('%d ÷ %d = ', dividend, divisor);
operator = 'division';
end
if rand() > 0.5
displayedResult = result;
isCorrect = "1";
else
minError = max(1, round(result * 0.8));
maxError = round(result * 1.2);
possibleErrors = setdiff(minError:maxError, result);
if isempty(possibleErrors)
possibleErrors = [result-1, result+1];
end
errorIdx = randi(length(possibleErrors));
displayedResult = possibleErrors(errorIdx);
isCorrect = "0";
end
fullEq = sprintf('%s%d', eq, displayedResult);
textWidth = length(fullEq) * fontSize * 0.6;
imgWidth = textWidth + 2*horizontalPadding;
imgHeight = verticalPadding + fontSize + verticalPadding/2;
set(fig, 'Position', [100, 100, imgWidth, imgHeight]);
cla(ax);
text(ax, 0.5, 0.5, fullEq, ...
'FontSize', fontSize, ...
'FontWeight', 'normal', ...
'Color', textColor, ...
'HorizontalAlignment', 'center', ...
'VerticalAlignment', 'middle');
dataTable.Problem(imgIdx) = string(fullEq);
dataTable.CorrectResult(imgIdx) = string(result);
dataTable.Operator(imgIdx) = operator;
dataTable.CorrectFlag(imgIdx) = isCorrect;
% 按运算符分类保存图像
opDir = fullfile(outputDir, operator);
if ~exist(opDir, 'dir')
mkdir(opDir);
end
imwrite(getframe(fig).cdata, fullfile(opDir, filename));
end
writetable(dataTable, fullfile(outputDir, 'math_problems.xlsx'));
close(fig);
fprintf('生成完成!\n');
最新发布