Skip to content

Commit

Permalink
fix flameshot-org#702 add OCR with tesseract
Browse files Browse the repository at this point in the history
  • Loading branch information
Raul Sampedro committed Jan 27, 2023
1 parent 05c3b87 commit 9af54b3
Show file tree
Hide file tree
Showing 15 changed files with 164 additions and 1 deletion.
2 changes: 2 additions & 0 deletions data/graphics.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,10 @@
<file>img/material/white/move_up.svg</file>
<file>img/material/white/delete.svg</file>
<file>img/material/black/apps.svg</file>
<file>img/material/black/ocr.svg</file>
<file>img/material/black/image.svg</file>
<file>img/material/white/apps.svg</file>
<file>img/material/white/ocr.svg</file>
<file>img/material/white/image.svg</file>
</qresource>
</RCC>
8 changes: 8 additions & 0 deletions data/img/material/black/ocr.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions data/img/material/white/ocr.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 27 additions & 0 deletions src/config/generalconf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ GeneralConf::GeneralConf(QWidget* parent)
initAntialiasingPinZoom();
initUploadHistoryMax();
initUndoLimit();
initTesseractPath();
initUploadClientSecret();
initPredefinedColorPaletteLarge();
initShowSelectionGeometry();
Expand Down Expand Up @@ -545,6 +546,32 @@ void GeneralConf::initUploadHistoryMax()
vboxLayout->addWidget(m_uploadHistoryMax);
}

void GeneralConf::initTesseractPath()
{
auto* box = new QGroupBox(tr("Tesseract path (for OCR)"));
box->setFlat(true);
m_layout->addWidget(box);

auto* vboxLayout = new QVBoxLayout();
box->setLayout(vboxLayout);

m_tesseractPath = new QLineEdit(this);
QString foreground = this->palette().windowText().color().name();
m_tesseractPath->setStyleSheet(
QStringLiteral("color: %1").arg(foreground));
m_tesseractPath->setText(ConfigHandler().tesseractBinPath());
connect(m_tesseractPath,
SIGNAL(editingFinished()),
this,
SLOT(tesseractPathEdited()));
vboxLayout->addWidget(m_tesseractPath);
}

void GeneralConf::tesseractPathEdited()
{
ConfigHandler().setTesseractBinPath(m_tesseractPath->text());
}

void GeneralConf::initUploadClientSecret()
{
auto* box = new QGroupBox(tr("Imgur Application Client ID"));
Expand Down
3 changes: 3 additions & 0 deletions src/config/generalconf.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ private slots:
void exportFileConfiguration();
void resetConfiguration();
void togglePathFixed();
void tesseractPathEdited();
void uploadClientKeyEdited();
void useJpgForClipboardChanged(bool checked);
void setSaveAsFileExtension(QString extension);
Expand Down Expand Up @@ -87,6 +88,7 @@ private slots:
void initUploadWithoutConfirmation();
void initUseJpgForClipboard();
void initUploadHistoryMax();
void initTesseractPath();
void initUploadClientSecret();
void initSaveLastRegion();
void initShowSelectionGeometry();
Expand Down Expand Up @@ -118,6 +120,7 @@ private slots:
QPushButton* m_resetButton;
QCheckBox* m_saveAfterCopy;
QLineEdit* m_savePath;
QLineEdit* m_tesseractPath;
QLineEdit* m_uploadClientKey;
QPushButton* m_changeSaveButton;
QCheckBox* m_screenshotPathFixedCheck;
Expand Down
1 change: 1 addition & 0 deletions src/core/capturerequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class CaptureRequest
PIN = 16,
UPLOAD = 32,
ACCEPT_ON_SELECT = 64,
OCR = 128,
};

CaptureRequest(CaptureMode mode,
Expand Down
24 changes: 24 additions & 0 deletions src/core/flameshot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <QDebug>
#include <QDesktopWidget>
#include <QFile>
#include <QProcess>
#include <QMessageBox>
#include <QThread>
#include <QTimer>
Expand Down Expand Up @@ -349,6 +350,29 @@ void Flameshot::exportCapture(QPixmap capture,
<< selection.x() << "+" << selection.y() << "\n";
}

if (tasks & CR::OCR) {
QProcess *tesseractProcess = new QProcess();

tesseractProcess->setProcessChannelMode(QProcess::MergedChannels);
tesseractProcess->start(ConfigHandler().tesseractBinPath(), QStringList() << "stdin" << "stdout");

capture.save(tesseractProcess, "PNG");
tesseractProcess->closeWriteChannel();

AbstractLogger::info() << QObject::tr("Running OCR with tesseract");

if(!tesseractProcess->waitForFinished()){
QMessageBox messageBox;
QString errorMessage = tr("Error running tesseract OCR from ") + ConfigHandler().tesseractBinPath() + "\n" + tesseractProcess->errorString();
messageBox.critical(0, tr("Error"), errorMessage);
messageBox.setFixedSize(500,200);
}else{
QString stdout = tesseractProcess->readAllStandardOutput();
FlameshotDaemon::copyToClipboard(stdout);
}

}

if (tasks & CR::PRINT_RAW) {
QByteArray byteArray;
QBuffer buffer(&byteArray);
Expand Down
1 change: 1 addition & 0 deletions src/tools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ target_sources(
target_sources(flameshot PRIVATE rectangle/rectangletool.h rectangle/rectangletool.cpp)
target_sources(flameshot PRIVATE redo/redotool.h redo/redotool.cpp)
target_sources(flameshot PRIVATE save/savetool.h save/savetool.cpp)
target_sources(flameshot PRIVATE ocr/ocrtool.h ocr/ocrtool.cpp)
target_sources(flameshot PRIVATE accept/accepttool.h accept/accepttool.cpp)
target_sources(flameshot PRIVATE invert/inverttool.h invert/inverttool.cpp)
target_sources(flameshot PRIVATE selection/selectiontool.h selection/selectiontool.cpp)
Expand Down
1 change: 1 addition & 0 deletions src/tools/capturetool.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class CaptureTool : public QObject
TYPE_SIZEDECREASE = 21,
TYPE_INVERT = 22,
TYPE_ACCEPT = 23,
TYPE_OCR = 24,
};
Q_ENUM(Type);

Expand Down
55 changes: 55 additions & 0 deletions src/tools/ocr/ocrtool.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: 2017-2019 Alejandro Sirgo Rica & Contributors

#include "ocrtool.h"
#include "src/utils/screenshotsaver.h"
#include <QApplication>
#include <QPainter>
#include <QStyle>
#if defined(Q_OS_MACOS)
#include "src/widgets/capture/capturewidget.h"
#include <QWidget>
#endif

OcrTool::OcrTool(QObject* parent)
: AbstractActionTool(parent)
{}

bool OcrTool::closeOnButtonPressed() const
{
return true;
}

QIcon OcrTool::icon(const QColor& background, bool inEditor) const
{
Q_UNUSED(inEditor)
return QIcon(iconPath(background) + "ocr.svg");
}

QString OcrTool::name() const
{
return tr("OCR");
}

CaptureTool::Type OcrTool::type() const
{
return CaptureTool::TYPE_OCR;
}

QString OcrTool::description() const
{
return tr("OCR the capture");
}

CaptureTool* OcrTool::copy(QObject* parent)
{
return new OcrTool(parent);
}

void OcrTool::pressed(CaptureContext& context)
{
emit requestAction(REQ_CLEAR_SELECTION);
emit requestAction(REQ_CAPTURE_DONE_OK);
context.request.addTask(CaptureRequest::OCR);
emit requestAction(REQ_CLOSE_GUI);
}
27 changes: 27 additions & 0 deletions src/tools/ocr/ocrtool.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// SPDX-License-Identifier: GPL-3.0-or-later
// SPDX-FileCopyrightText: 2017-2019 Alejandro Sirgo Rica & Contributors

#pragma once

#include "abstractactiontool.h"

class OcrTool : public AbstractActionTool
{
Q_OBJECT
public:
explicit OcrTool(QObject* parent = nullptr);

bool closeOnButtonPressed() const override;

QIcon icon(const QColor& background, bool inEditor) const override;
QString name() const override;
QString description() const override;

CaptureTool* copy(QObject* parent = nullptr) override;

protected:
CaptureTool::Type type() const override;

public slots:
void pressed(CaptureContext& context) override;
};
2 changes: 2 additions & 0 deletions src/tools/toolfactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "line/linetool.h"
#include "marker/markertool.h"
#include "move/movetool.h"
#include "ocr/ocrtool.h"
#include "pencil/penciltool.h"
#include "pin/pintool.h"
#include "pixelate/pixelatetool.h"
Expand Down Expand Up @@ -61,6 +62,7 @@ CaptureTool* ToolFactory::CreateTool(CaptureTool::Type t, QObject* parent)
if_TYPE_return_TOOL(TYPE_SIZEINCREASE, SizeIncreaseTool);
if_TYPE_return_TOOL(TYPE_SIZEDECREASE, SizeDecreaseTool);
if_TYPE_return_TOOL(TYPE_INVERT, InvertTool);
if_TYPE_return_TOOL(TYPE_OCR, OcrTool);
if_TYPE_return_TOOL(TYPE_ACCEPT, AcceptTool);
default:
return nullptr;
Expand Down
2 changes: 2 additions & 0 deletions src/utils/confighandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ static QMap<class QString, QSharedPointer<ValueHandler>>
// drawFontSize, remember to update ConfigHandler::toolSize
OPTION("copyOnDoubleClick" ,Bool ( false )),
OPTION("uploadClientSecret" ,String ( "313baf0c7b4d3ff" )),
OPTION("tesseractBinPath" ,String ( "tesseract" )),
OPTION("showSelectionGeometry" , BoundedInt (0,5,4)),
OPTION("showSelectionGeometryHideTime", LowerBoundedInt (0, 3000))
};
Expand All @@ -142,6 +143,7 @@ static QMap<QString, QSharedPointer<KeySequence>> recognizedShortcuts = {
SHORTCUT("TYPE_SAVE" , "Ctrl+S" ),
SHORTCUT("TYPE_ACCEPT" , "Return" ),
SHORTCUT("TYPE_EXIT" , "Ctrl+Q" ),
SHORTCUT("TYPE_OCR" , ),
SHORTCUT("TYPE_IMAGEUPLOADER" , ),
#if !defined(Q_OS_MACOS)
SHORTCUT("TYPE_OPEN_APP" , "Ctrl+O" ),
Expand Down
1 change: 1 addition & 0 deletions src/utils/confighandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ class ConfigHandler : public QObject
CONFIG_GETTER_SETTER(squareMagnifier, setSquareMagnifier, bool)
CONFIG_GETTER_SETTER(copyOnDoubleClick, setCopyOnDoubleClick, bool)
CONFIG_GETTER_SETTER(uploadClientSecret, setUploadClientSecret, QString)
CONFIG_GETTER_SETTER(tesseractBinPath, setTesseractBinPath, QString)
CONFIG_GETTER_SETTER(saveLastRegion, setSaveLastRegion, bool)
CONFIG_GETTER_SETTER(showSelectionGeometry, setShowSelectionGeometry, int)
// SPECIAL CASES
Expand Down
3 changes: 2 additions & 1 deletion src/widgets/capture/capturetoolbutton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ static std::map<CaptureTool::Type, int> buttonTypeOrder

{ CaptureTool::TYPE_SIZEINCREASE, 22 },
{ CaptureTool::TYPE_SIZEDECREASE, 23 },
{ CaptureTool::TYPE_OCR, 24 },
};

int CaptureToolButton::getPriorityByButton(CaptureTool::Type b)
Expand All @@ -175,7 +176,7 @@ QList<CaptureTool::Type> CaptureToolButton::iterableButtonTypes = {
CaptureTool::TYPE_MOVESELECTION, CaptureTool::TYPE_UNDO,
CaptureTool::TYPE_REDO, CaptureTool::TYPE_COPY,
CaptureTool::TYPE_SAVE, CaptureTool::TYPE_EXIT,
CaptureTool::TYPE_IMAGEUPLOADER,
CaptureTool::TYPE_IMAGEUPLOADER, CaptureTool::TYPE_OCR,
#if !defined(Q_OS_MACOS)
CaptureTool::TYPE_OPEN_APP,
#endif
Expand Down

0 comments on commit 9af54b3

Please sign in to comment.