Skip to content

Commit 1e8f9d5

Browse files
committed
[image] fix open file fail.
1 parent 30c1e38 commit 1e8f9d5

File tree

3 files changed

+32
-27
lines changed

3 files changed

+32
-27
lines changed

ext_modules/_maix_image/_maix_image.cpp

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -262,13 +262,18 @@ maix_image &maix_image::_open_file(std::string path)
262262
this->v_close();
263263
libmaix_image_t *tmp_img = NULL;
264264
libmaix_err_t err = libmaix_cv_image_open_file(&tmp_img, path.c_str());
265-
if (LIBMAIX_ERR_NOT_EXEC == err)
265+
if (LIBMAIX_ERR_NOT_EXEC == err) // maybe is bytes jpg.
266266
{
267267
try
268268
{
269269
// maybe is jpg or bmp bytes
270270
cv::Mat tmp(1, path.size(), CV_8U, (char *)path.data());
271271
cv::Mat image = cv::imdecode(tmp, cv::IMREAD_COLOR);
272+
if (image.empty()) {
273+
printf("[image.open] %s file does not exist\r\n", path.c_str());
274+
this->v_close();
275+
return *this;
276+
}
272277
cv::cvtColor(image, image, cv::ColorConversionCodes::COLOR_BGR2RGB);
273278
tmp_img = libmaix_image_create(image.cols, image.rows, LIBMAIX_IMAGE_MODE_RGB888, LIBMAIX_IMAGE_LAYOUT_HWC, NULL, true);
274279
memcpy(tmp_img->data, image.data, tmp_img->width * tmp_img->height * 3);
@@ -298,7 +303,7 @@ py::object maix_image::_to_py(std::string im)
298303
{
299304
if (NULL == this->_img)
300305
{
301-
py::print("no img");
306+
py::print("[image] is empty !");
302307
return py::none();
303308
}
304309
if (im == "maix_image")
@@ -381,7 +386,7 @@ void maix_image::_show()
381386
{
382387
if (NULL == this->_img)
383388
{
384-
py::print("no img");
389+
py::print("[image] is empty !");
385390
return;
386391
}
387392
auto _maix_display = py::module::import("_maix_display");
@@ -480,7 +485,7 @@ maix_image &maix_image::_resize(int dst_w, int dst_h, int func, int padding, std
480485
{
481486
if (NULL == this->_img)
482487
{
483-
py::print("no img");
488+
py::print("[image] is empty !");
484489
return *this;
485490
}
486491
if (dst_w == 0 && dst_h == 0)
@@ -547,7 +552,7 @@ maix_image &maix_image::_draw_line(int x1, int y1, int x2, int y2, std::vector<i
547552
{
548553
if (NULL == this->_img)
549554
{
550-
py::print("no img");
555+
py::print("[image] is empty !");
551556
return *this;
552557
}
553558
libmaix_cv_image_draw_line(this->_img, x1, y1, x2, y2, MaixColor(color[0], color[1], color[2]), thickness);
@@ -558,7 +563,7 @@ maix_image &maix_image::_draw_cross(int x, int y, int c, int size, int thickness
558563
{
559564
if (NULL == this->_img)
560565
{
561-
py::print("no img");
566+
py::print("[image] is empty !");
562567
return *this;
563568
}
564569
image_t img = {};
@@ -574,7 +579,7 @@ maix_image &maix_image::_draw_rectangle(int x1_x, int y1_y, int x2_w, int y2_h,
574579
{
575580
if (NULL == this->_img)
576581
{
577-
py::print("no img");
582+
py::print("[image] is empty !");
578583
return *this;
579584
}
580585
if (is_xywh)
@@ -587,7 +592,7 @@ maix_image &maix_image::_draw_circle(int x, int y, int r, std::vector<int> color
587592
{
588593
if (NULL == this->_img)
589594
{
590-
py::print("no img");
595+
py::print("[image] is empty !");
591596
return *this;
592597
}
593598
libmaix_cv_image_draw_circle(this->_img, x, y, r, MaixColor(color[0], color[1], color[2]), thickness);
@@ -598,7 +603,7 @@ maix_image &maix_image::_draw_ellipse(int cx, int cy, int rx, int ry, double ang
598603
{
599604
if (NULL == this->_img)
600605
{
601-
py::print("no img");
606+
py::print("[image] is empty !");
602607
return *this;
603608
}
604609
libmaix_cv_image_draw_ellipse(this->_img, cx, cy, rx, ry, angle, startAngle, endAngle, MaixColor(color[0], color[1], color[2]), thickness);
@@ -609,7 +614,7 @@ maix_image &maix_image::_draw_string(int x, int y, std::string str, double scale
609614
{
610615
if (NULL == this->_img)
611616
{
612-
py::print("no img");
617+
py::print("[image] is empty !");
613618
return *this;
614619
}
615620
libmaix_cv_image_draw_string(this->_img, x, y, str.c_str(), scale, MaixColor(color[0], color[1], color[2]), thickness);
@@ -620,7 +625,7 @@ maix_image &maix_image::_rotate(double angle, int adjust)
620625
{
621626
if (NULL == this->_img)
622627
{
623-
py::print("no img");
628+
py::print("[image] is empty !");
624629
return *this;
625630
}
626631
libmaix_image_t *tmp = NULL;
@@ -636,7 +641,7 @@ maix_image &maix_image::_flip(int flip)
636641
{
637642
if (NULL == this->_img)
638643
{
639-
py::print("no img");
644+
py::print("[image] is empty !");
640645
return *this;
641646
}
642647
libmaix_cv_image_flip(this->_img, flip);
@@ -647,7 +652,7 @@ maix_image &maix_image::_convert(std::string mode)
647652
{
648653
if (NULL == this->_img)
649654
{
650-
py::print("no img");
655+
py::print("[image] is empty !");
651656
return *this;
652657
}
653658
libmaix_image_t *tmp = libmaix_image_create(this->_img->width, this->_img->height, any_cast<libmaix_image_mode_t>(this->py_to_pram[this->get_to(mode)][0]), LIBMAIX_IMAGE_LAYOUT_HWC, NULL, true);
@@ -703,7 +708,7 @@ maix_image *maix_image::_draw_crop(int x, int y, int w, int h)
703708
{
704709
if (NULL == this->_img)
705710
{
706-
py::print("no img");
711+
py::print("[image] is empty !");
707712
maix_image *tmp_img = new maix_image();
708713
return tmp_img;
709714
}
@@ -734,7 +739,7 @@ maix_image &maix_image::_draw_image(py::object data, int x, int y, double alpha)
734739
{
735740
if (NULL == this->_img)
736741
{
737-
py::print("no img");
742+
py::print("[image] is empty !");
738743
return *this;
739744
}
740745
if (py_input_maix_image != this->get_img_type(data))
@@ -765,7 +770,7 @@ maix_image &maix_image::_set_pixel(int x, int y, std::vector<int> color)
765770
{
766771
if (NULL == this->_img)
767772
{
768-
py::print("no img");
773+
py::print("[image] is empty !");
769774
return *this;
770775
}
771776
libmaix_image_color_t colot_tmp;
@@ -801,7 +806,7 @@ maix_image &maix_image::_hist_eq(bool adaptive, float clip_limit, maix_image &ma
801806
{
802807
if (NULL == this->_img)
803808
{
804-
py::print("no img");
809+
py::print("[image] is empty !");
805810
return *this;
806811
}
807812

@@ -841,7 +846,7 @@ maix_image &maix_image::_gamma_corr(float gamma, float contrast, float brightnes
841846
{
842847
if (NULL == this->_img)
843848
{
844-
py::print("no img");
849+
py::print("[image] is empty !");
845850
return *this;
846851
}
847852
image_t img = {};
@@ -859,7 +864,7 @@ maix_image &maix_image::_lens_corr(float strength, float zoom, float x_corr, flo
859864
{
860865
if (NULL == this->_img)
861866
{
862-
py::print("no img");
867+
py::print("[image] is empty !");
863868
return *this;
864869
}
865870

@@ -880,7 +885,7 @@ maix_image &maix_image::_mean(const int ksize, bool threshold, int offset, bool
880885
{
881886
if (NULL == this->_img)
882887
{
883-
py::print("no img");
888+
py::print("[image] is empty !");
884889
return *this;
885890
}
886891

@@ -923,7 +928,7 @@ py::list maix_image::_imlib_get_statistics(std::vector<int> roi_src, std::vector
923928

924929
if (NULL == this->_img)
925930
{
926-
py::print("no img");
931+
py::print("[image] is empty !");
927932
return py::none();
928933
}
929934

@@ -1066,7 +1071,7 @@ maix_image &maix_image::_imlib_rotation_corr(float x_rotation, float y_rotation,
10661071
{
10671072
if (NULL == this->_img)
10681073
{
1069-
py::print("no img");
1074+
py::print("[image] is empty !");
10701075
return *this;
10711076
}
10721077

@@ -1093,7 +1098,7 @@ maix_image &maix_image::_opencv_Canny(double threshold1, double threshold2, int
10931098
{
10941099
if (NULL == this->_img)
10951100
{
1096-
py::print("no img");
1101+
py::print("[image] is empty !");
10971102
return *this;
10981103
}
10991104
if (this->_maix_image_type == "RGB")
@@ -1178,9 +1183,9 @@ py::dict maix_image::_imlib_find_template(maix_image &template_src, float arg_th
11781183

11791184
maix_image *maix_image::_warp_affine(std::vector<int> points, int w, int h)
11801185
{
1181-
if (NULL == this->_img && points.size() < 4)
1186+
if (NULL == this->_img || points.size() < 3)
11821187
{
1183-
py::print("no img");
1188+
py::print("[image] is empty !");
11841189
maix_image *tmp_img = new maix_image();
11851190
return tmp_img;
11861191
}

ext_modules/_maix_image/py_maix_image.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ PYBIND11_MODULE(_maix_image, mo)
119119
.def("get_pixel", &maix_image::_get_pixel, py::arg("x"), py::arg("y"))
120120
.def("set_pixel", &maix_image::_set_pixel, py::arg("x"), py::arg("y"), py::arg("color"))
121121
.def("Canny", &maix_image::_opencv_Canny, py::arg("threshold1") = 10, py::arg("threshold2") = 100, py::arg("apertureSize") = 3, py::arg("L2gradient") = false)
122-
.def("affine",&maix_image::_warp_affine, py::arg("points"), py::arg("w"), py::arg("h"))
122+
.def("crop_affine",&maix_image::_warp_affine, py::arg("points"), py::arg("w"), py::arg("h"))
123123

124124
// maix_image继承maix_vision方法
125125
.def("get_blob_lab", &maix_image::get_blob_color_max, py::arg("roi") = std::vector<int>{0, 0, 0, 0}, py::arg("critical") = 0, py::arg("co") = 0)

0 commit comments

Comments
 (0)