Toggle navigation
Toggle navigation
This project
Loading...
Sign in
kykint
/
TELEGRAMBOT
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
kykint
2019-06-05 21:12:09 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
e80eb82c98e58c3c0a7d81368c1c46b481fa0aea
e80eb82c
1 parent
d01801a3
Implement OCR using tesseract
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
61 additions
and
0 deletions
app.js
package-lock.json
package.json
app.js
View file @
e80eb82
...
...
@@ -17,6 +17,12 @@ const languagedetect_api_url = 'https://openapi.naver.com/v1/papago/detectLangs'
const
db
=
require
(
'./db'
);
db
.
init
();
// Tesseract module for image recognition
const
tesseract
=
require
(
'node-tesseract-ocr'
);
// fs module for saving/removing image file upon/after recognition
const
fs
=
require
(
'fs'
);
// /echo [whatever]
bot
.
onText
(
/
\/
echo
(
.+
)
/
,
(
msg
,
match
)
=>
{
// 'msg' is the received Message from Telegram
...
...
@@ -184,6 +190,55 @@ bot.onText(/^\/(t|translate)($| ((.|\n)+))/, (msg, match) => {
}
});
// When an image file is received, temporarily save it in current directory
// and use tesseract to recognize its text. The texts are then translated
// and sent to the user.
bot
.
on
(
'photo'
,
(
msg
)
=>
{
const
user
=
msg
.
from
;
const
chatId
=
msg
.
chat
.
id
;
const
photo
=
msg
.
photo
[
2
];
const
photoId
=
photo
.
file_id
;
// Download the image, which will later be deleted to avoid git detection
bot
.
downloadFile
(
photoId
,
'.'
).
then
(
function
(
filePath
)
{
console
.
log
(
'Image downloaded: '
,
filePath
);
// Begin tesseract OCR
tesseract
.
recognize
(
filePath
).
then
(
function
(
text
)
{
console
.
log
(
'OCR result: '
,
text
);
// Translate the result
translate
(
user
,
text
).
then
(
function
(
result
)
{
// Send recognized text to user
bot
.
sendMessage
(
chatId
,
result
);
// Delete the image
fs
.
unlink
(
filePath
,
function
(
error
)
{
if
(
error
)
{
console
.
log
(
'Error deleting image: '
,
error
);
}
else
{
console
.
log
(
'Successfully deleted file '
,
filePath
);
}
});
});
}).
catch
(
function
(
error
)
{
// OCR failed
console
.
log
(
'OCR error: '
,
error
);
// Delete the image
fs
.
unlink
(
filePath
,
function
(
error
)
{
if
(
error
)
{
console
.
log
(
'Error deleting image: '
,
error
);
}
else
{
console
.
log
(
'Successfully deleted file '
,
filePath
);
}
});
});
}).
catch
(
function
(
error
)
{
// Image download failed
console
.
log
(
'Image download error: '
,
error
);
});
});
// /l(anguage)
// Let user select the language he wants his message to translate to.
// When triggered, bot will send an inline keyboard message with a list
...
...
package-lock.json
View file @
e80eb82
...
...
@@ -417,6 +417,11 @@
}
}
},
"node-tesseract-ocr"
:
{
"version"
:
"0.1.0"
,
"resolved"
:
"https://registry.npmjs.org/node-tesseract-ocr/-/node-tesseract-ocr-0.1.0.tgz"
,
"integrity"
:
"sha512-gUp+fy8Wiy2ZZQiSe33ApfyaBPOOlTz8nrHjZqWGAX4l06lW01uO6ABA5KWBhO3B1DmWl1Y9T1aLasfXYpm2Dg=="
},
"oauth-sign"
:
{
"version"
:
"0.9.0"
,
"resolved"
:
"https://registry.npmjs.org/oauth-sign/-/oauth-sign-0.9.0.tgz"
,
...
...
package.json
View file @
e80eb82
...
...
@@ -12,6 +12,7 @@
"dependencies"
:
{
"mongodb"
:
"^3.2.6"
,
"node-telegram-bot-api"
:
"^0.30.0"
,
"node-tesseract-ocr"
:
"^0.1.0"
,
"request"
:
"^2.88.0"
}
}
...
...
Please
register
or
login
to post a comment