mirror of
https://github.com/sipeed/MaixCDK.git
synced 2026-09-15 06:03:02 -05:00
optimize ppocr
This commit is contained in:
@@ -82,7 +82,7 @@ namespace maix::nn
|
||||
/**
|
||||
* convert box point to a list type.
|
||||
* @return list type, element is int type, value [x1, y1, x2, y2, x3, y3, x4, y4].
|
||||
* @maixpy maix.nn.OCR_Box.y4
|
||||
* @maixpy maix.nn.OCR_Box.to_list
|
||||
*/
|
||||
std::vector<int> to_list()
|
||||
{
|
||||
@@ -112,8 +112,8 @@ namespace maix::nn
|
||||
* @maixpy maix.nn.OCR_Object.__init__
|
||||
* @maixcdk maix.nn.OCR_Object.OCR_Object
|
||||
*/
|
||||
OCR_Object(const nn::OCR_Box &box, const std::vector<int> &idx_list, const std::vector<std::string> &char_list, float score = 0, const std::vector<nn::OCR_Box> &char_boxes = std::vector<nn::OCR_Box>())
|
||||
:box(box), score(score), idx_list(idx_list), char_boxes(char_boxes), _char_list(char_list)
|
||||
OCR_Object(const nn::OCR_Box &box, const std::vector<int> &idx_list, const std::vector<std::string> &char_list, float score = 0, const std::vector<int> &char_pos = std::vector<int>())
|
||||
:box(box), score(score), idx_list(idx_list), char_pos(char_pos), _char_list(char_list)
|
||||
{
|
||||
_chars.clear();
|
||||
for(const auto &c : _char_list)
|
||||
@@ -144,6 +144,12 @@ namespace maix::nn
|
||||
*/
|
||||
std::vector<int> idx_list;
|
||||
|
||||
/**
|
||||
* Chars' position relative to left
|
||||
* @maixpy maix.nn.OCR_Object.char_pos
|
||||
*/
|
||||
std::vector<int> char_pos;
|
||||
|
||||
/**
|
||||
* Get OCR_Object's charactors, return a string type.
|
||||
* @return All charactors in string type.
|
||||
@@ -179,12 +185,6 @@ namespace maix::nn
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* All charactors' boxes, list type, element is nn.OCR_Box type.
|
||||
* @maixpy maix.nn.OCR_Object.char_boxes
|
||||
*/
|
||||
std::vector<nn::OCR_Box> char_boxes;
|
||||
|
||||
/**
|
||||
* OCR_Object info to string
|
||||
* @return OCR_Object info string
|
||||
@@ -229,9 +229,9 @@ namespace maix::nn
|
||||
* @throw Throw exception if no memory
|
||||
* @maixpy maix.nn.OCR_Objects.add
|
||||
*/
|
||||
nn::OCR_Object &add(const nn::OCR_Box &box, const std::vector<int> &idx_list, const std::vector<std::string> &char_list, float score = 0, const std::vector<nn::OCR_Box> &char_boxes = std::vector<nn::OCR_Box>())
|
||||
nn::OCR_Object &add(const nn::OCR_Box &box, const std::vector<int> &idx_list, const std::vector<std::string> &char_list, float score = 0, const std::vector<int> &char_pos = std::vector<int>())
|
||||
{
|
||||
OCR_Object *obj = new OCR_Object(box, idx_list, char_list, score, char_boxes);
|
||||
OCR_Object *obj = new OCR_Object(box, idx_list, char_list, score, char_pos);
|
||||
if(!obj)
|
||||
throw err::Exception(err::ERR_NO_MEM);
|
||||
objs.push_back(obj);
|
||||
|
||||
@@ -362,7 +362,7 @@ namespace maix::nn
|
||||
{
|
||||
return new nn::OCR_Objects();
|
||||
}
|
||||
nn::OCR_Objects *res = _post_process(img, outputs, img.width(), img.height(), fit, char_box);
|
||||
nn::OCR_Objects *res = _post_process(img, outputs, img.width(), img.height(), fit);
|
||||
delete outputs;
|
||||
if(res == NULL)
|
||||
{
|
||||
@@ -381,7 +381,7 @@ namespace maix::nn
|
||||
* 4 points postiion, format: [x1, y1, x2, y2, x3, y3, x4, y4], point 1 at the left-top, point 2 right-top...
|
||||
* @param char_box Calculate every charactor's box, default false, if true then you can get charactor's box by nn.OCR_Object's char_boxes attribute.
|
||||
*/
|
||||
nn::OCR_Object *recognize(image::Image &img, const std::vector<int> &box_points = std::vector<int>(), bool char_box = false)
|
||||
nn::OCR_Object *recognize(image::Image &img, const std::vector<int> &box_points = std::vector<int>())
|
||||
{
|
||||
nn::OCR_Box box(0, 0, img.width(), 0, img.width(), img.height(), 0, img.height());
|
||||
bool crop = false;
|
||||
@@ -399,13 +399,13 @@ namespace maix::nn
|
||||
}
|
||||
std::vector<int> idx_list;
|
||||
std::vector<std::string> char_list;
|
||||
std::vector<nn::OCR_Box> char_boxes;
|
||||
nn::OCR_Object *obj = new nn::OCR_Object(box, idx_list, char_list, 1.0, char_boxes);
|
||||
std::vector<int> char_pos;
|
||||
nn::OCR_Object *obj = new nn::OCR_Object(box, idx_list, char_list, 1.0, char_pos);
|
||||
if(!obj)
|
||||
{
|
||||
throw err::Exception(err::ERR_NO_MEM);
|
||||
}
|
||||
_recognize(img, box, obj->idx_list, char_list, obj->char_boxes, char_box, crop);
|
||||
_recognize(img, box, obj->idx_list, char_list, obj->char_pos, crop);
|
||||
obj->update_chars(char_list);
|
||||
return obj;
|
||||
}
|
||||
@@ -572,9 +572,9 @@ namespace maix::nn
|
||||
return err::ERR_NONE;
|
||||
}
|
||||
|
||||
nn::OCR_Objects *_post_process(image::Image &img, tensor::Tensors *outputs, int img_w, int img_h, maix::image::Fit fit, bool char_box);
|
||||
nn::OCR_Objects *_post_process(image::Image &img, tensor::Tensors *outputs, int img_w, int img_h, maix::image::Fit fit);
|
||||
|
||||
void _recognize(image::Image &img, const nn::OCR_Box &box, std::vector<int> &idx_list, std::vector<std::string> &char_list, std::vector<nn::OCR_Box> &char_boxes, bool char_box, bool crop);
|
||||
void _recognize(image::Image &img, const nn::OCR_Box &box, std::vector<int> &idx_list, std::vector<std::string> &char_list, std::vector<int> &char_pos, bool crop);
|
||||
|
||||
// void _get_layer_objs(std::vector<nn::Object> &objs, tensor::Tensor &output, int layer_i, int layer_num)
|
||||
// {
|
||||
|
||||
@@ -34,7 +34,7 @@ namespace maix::nn
|
||||
h = h - y;
|
||||
}
|
||||
|
||||
nn::OCR_Objects *PP_OCR::_post_process(image::Image &img, tensor::Tensors *outputs, int img_w, int img_h, maix::image::Fit fit, bool char_box)
|
||||
nn::OCR_Objects *PP_OCR::_post_process(image::Image &img, tensor::Tensors *outputs, int img_w, int img_h, maix::image::Fit fit)
|
||||
{
|
||||
nn::OCR_Objects *objects = new nn::OCR_Objects();
|
||||
// int layer_num = outputs->size();
|
||||
@@ -80,8 +80,8 @@ namespace maix::nn
|
||||
nn::OCR_Box box(boxes[i][0][0], boxes[i][0][1], boxes[i][1][0], boxes[i][1][1], boxes[i][2][0], boxes[i][2][1], boxes[i][3][0], boxes[i][3][1]);
|
||||
std::vector<int> idxes;
|
||||
std::vector<std::string> chars;
|
||||
std::vector<nn::OCR_Box> char_boxes;
|
||||
objects->add(box, idxes, chars, scores[i], char_boxes);
|
||||
std::vector<int> char_pos;
|
||||
objects->add(box, idxes, chars, scores[i], char_pos);
|
||||
}
|
||||
delete tmp_img;
|
||||
|
||||
@@ -95,7 +95,7 @@ namespace maix::nn
|
||||
// _get_external_box(boxes[i], x, y, w, h, shape[3], shape[2]);
|
||||
std::vector<std::string> char_list;
|
||||
nn::OCR_Object &obj = objects->at(i);
|
||||
_recognize(img, obj.box, obj.idx_list, char_list, obj.char_boxes, char_box, true);
|
||||
_recognize(img, obj.box, obj.idx_list, char_list, obj.char_pos, true);
|
||||
obj.update_chars(char_list);
|
||||
}
|
||||
break;
|
||||
@@ -104,7 +104,7 @@ namespace maix::nn
|
||||
return objects;
|
||||
}
|
||||
|
||||
void PP_OCR::_recognize(image::Image &img, const nn::OCR_Box &box, std::vector<int> &idx_list, std::vector<std::string> &char_list, std::vector<nn::OCR_Box> &char_boxes, bool char_box, bool crop)
|
||||
void PP_OCR::_recognize(image::Image &img, const nn::OCR_Box &box, std::vector<int> &idx_list, std::vector<std::string> &char_list, std::vector<int> &char_pos, bool crop)
|
||||
{
|
||||
cv::Mat img_src(img.height(), img.width(), CV_8UC3, img.data());
|
||||
cv::Mat *std_img = &img_src;
|
||||
@@ -203,6 +203,7 @@ namespace maix::nn
|
||||
{
|
||||
idx_list.push_back(max_idxes[i] - 1);
|
||||
char_list.push_back(labels[max_idxes[i] - 1]);
|
||||
char_pos.push_back(i);
|
||||
}
|
||||
last_idx = max_idxes[i];
|
||||
}
|
||||
|
||||
@@ -33,8 +33,8 @@ int _main(int argc, char *argv[])
|
||||
display::Display disp = display::Display();
|
||||
|
||||
int font_size = 20;
|
||||
image::load_font("sourcehansans", "/maixapp/share/font/SourceHanSansCN-Regular.otf", font_size);
|
||||
image::set_default_font("sourcehansans");
|
||||
image::load_font("ppocr", "/maixapp/share/font/ppocr_keys_v1.ttf", font_size);
|
||||
image::set_default_font("ppocr");
|
||||
|
||||
if (argc >= 3)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user