Skip to content

Instantly share code, notes, and snippets.

@junfenglx
Created January 15, 2016 06:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save junfenglx/ed966fb7ea41ed2a13a9 to your computer and use it in GitHub Desktop.
Save junfenglx/ed966fb7ea41ed2a13a9 to your computer and use it in GitHub Desktop.
使用tesseract-ocr和opencv识别视频中文字
#!/bin/bash
g++ -std=c++11 `pkg-config --libs tesseract opencv lept` $1 -o ${1}.out
#include <iostream>
#include <fstream>
#include <string>
#include <opencv2/opencv.hpp>
#include <time.h>
#include <tesseract/baseapi.h>
#include <leptonica/allheaders.h>
using namespace std;
using namespace cv;
const char* get_text_from_frame(tesseract::TessBaseAPI *myOCR, Mat edges);
void process_image(Mat& frame, Mat& out);
int main(int argc, char * argv[]) {
// [1]
tesseract::TessBaseAPI *myOCR =
new tesseract::TessBaseAPI();
// [2]
printf("Tesseract-ocr version: %s\n",
myOCR->Version());
printf("Leptonica version: %s\n",
getLeptonicaVersion());
// [3]
if (myOCR->Init(NULL, "eng")) {
fprintf(stderr, "Could not initialize tesseract.\n");
exit(1);
}
std::string filename = argv[1];
cv::VideoCapture cap(filename);
if (!cap.isOpened())
return -1;
// open output file
std::string outfile = argv[2];
std::fstream fs;
fs.open(outfile);
if(!fs.is_open())
{
fs.clear();
fs.open(outfile, std::ios::out); //Create file.
fs.close();
fs.open(outfile);
}
//创建窗口
// cvNamedWindow("bilibili_2015_top100", 1);
Mat title, score;
//显示视屏
time_t last_time = time(NULL);
const char* s;
for(int i=1 ;; ++i)
{
Mat frame;
cap >> frame;
if (frame.empty())
break;
Mat title_frame = frame.rowRange(980, 1040).colRange(75, 400);
Mat score_frame = frame.rowRange(940, 995).colRange(1620, 1860);
process_image(title_frame, title);
process_image(score_frame, score);
// imshow("title", title);
// imshow("score", score);
// if(waitKey(30) >= 0) break;
time_t this_time = time(NULL);
if(this_time != last_time)
{
last_time = this_time;
cout << "Frame_" << i << endl;
fs << "Frame_" << i << endl;
s = get_text_from_frame(myOCR, title);
fs << "Id: " << s << endl;
printf("Id: %s\n", s);
s = get_text_from_frame(myOCR, score);
fs << "Score: " << s << endl;
printf("Score: %s\n", s);
}
}
return 0;
}
void process_image(Mat& frame, Mat& out) {
const static double thres = 50;
const static double color = 255;
cvtColor(frame, out, cv::COLOR_RGB2GRAY);
// GaussianBlur(out, out, Size(7,7), 1.5, 1.5);
// Canny(out, out, 0, 30, 3);
// cv::bitwise_not(out, out);
cv::threshold(out, out, thres, color, CV_THRESH_BINARY);
}
const char* get_text_from_frame(tesseract::TessBaseAPI *myOCR, Mat edges) {
//cout << "width: " << edges.size().width << ", height: " << edges.size().height <<
// ",channels: " << edges.channels() << ", step1: " << edges.step1() << endl;
myOCR->SetImage(edges.data, edges.size().width, edges.size().height, edges.channels(), edges.step1());
myOCR->Recognize(NULL);
const char* out = myOCR->GetUTF8Text();
return out;
}
#include <string>
#include <opencv2/opencv.hpp>
#include <time.h>
#include <tesseract/baseapi.h>
#include <leptonica/allheaders.h>
using namespace std;
using namespace cv;
int main(int argc, char * argv[]) {
// [1]
tesseract::TessBaseAPI *myOCR =
new tesseract::TessBaseAPI();
// [2]
printf("Tesseract-ocr version: %s\n",
myOCR->Version());
printf("Leptonica version: %s\n",
getLeptonicaVersion());
// [3]
if (myOCR->Init(NULL, "eng")) {
fprintf(stderr, "Could not initialize tesseract.\n");
exit(1);
}
std::string filename = argv[1];
Mat frame = cv::imread(filename);
Mat edges;
//创建窗口
cv::namedWindow("bilibili_2015_top100", 1);
//显示视屏
double thres = 50;
double color = 255;
frame = frame.rowRange(980, 1040).colRange(75, 410);
// frame = frame.rowRange(940, 995).colRange(1620, 1860);
cvtColor(frame, edges, cv::COLOR_BGR2GRAY);
// GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);
// Canny(edges, edges, 0, 30, 3);
// cv::bitwise_or(edges, edges);
cv::threshold(edges, edges, thres, color, CV_THRESH_BINARY);
imshow("edges", edges);
// myOCR->SetRectangle(0,0,pFrame->width,pFrame->height);
cout << "width: " << edges.size().width << ", height: " << edges.size().height <<
",channels: " << edges.channels() << ", step1: " << edges.step1() << endl;
myOCR->SetImage(edges.data, edges.size().width, edges.size().height, edges.channels(), edges.step1());
myOCR->Recognize(NULL);
const char* out = myOCR->GetUTF8Text();
// std::string s(out);
cout << out << std::endl;
if(waitKey(6000) >= 0)
return 0;
return 0;
}
#include <cstdio>
#include <cctype>
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
const char prefix[] = "http://www.bilibili.com/video/av";
int cnt;
void ReadURL() {
char ch;
while ((ch = getchar()) != EOF) {
if (std::tolower(ch) == 'a') break;
}
if (getchar() == EOF) exit(0);
ch = getchar();
if (!isdigit(ch)) {
// not valid Id
return;
}
printf("%s", prefix);
printf("%c", ch);
while((ch = getchar()) != EOF) {
if (!isdigit(ch)) break;
printf("%c", ch);
}
printf("\n");
}
int main(int argc, char* argv[]) {
if (argc != 3) {
std::cerr << "help: " << argv[0] << " in_file out_file";
std::cerr << std::endl;
exit(1);
}
freopen(argv[1], "r", stdin);
freopen(argv[2], "w", stdout);
while (1)
ReadURL();
}
@junfenglx
Copy link
Author

using uniq removes duplicates

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment