Skip to content
This repository has been archived by the owner on Jan 20, 2024. It is now read-only.

Commit

Permalink
[maix] fix config size & tobytes to jpg bmp png.
Browse files Browse the repository at this point in the history
  • Loading branch information
junhuanchen committed May 24, 2022
1 parent a5dd449 commit 334cca7
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 27 deletions.
12 changes: 6 additions & 6 deletions ext_modules/_maix_display/example/test_maix_r329display.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,26 @@

from _maix_display import Display

__fastview__ = Display()
__show__ = Display()

canvas = Image.new("RGB", (240, 240), "#ff1b50")
__fastview__ = Display()
__show__ = Display()
mk = canvas.tobytes()
__fastview__.draw(mk, 240, 240)
__show__.draw(mk, 240, 240)

canvas = Image.new("RGB", (240, 240), "#501bff")
mk = canvas.tobytes()
__fastview__.draw(mk, 240, 240)
__show__.draw(mk, 240, 240)

'''
import _maix_camera
from _maix_display import Display
cam = _maix_camera.Camera(240,240,0)
__fastview__ = Display(240, 240)
__show__ = Display(240, 240)
while True:
img_bytes = cam.read()
if img_bytes[0]:
__fastview__.draw(img_bytes[1],240,240)
__show__.draw(img_bytes[1],240,240)
'''
26 changes: 24 additions & 2 deletions ext_modules/_maix_image/_maix_image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -423,11 +423,33 @@ int maix_image::_save(std::string file_path, std::string format)
return 0;
}

py::bytes maix_image::_tobytes()
py::bytes maix_image::_tobytes(std::string format, std::vector<int> params)
{
if (NULL == this->_img)
return py::bytes();
return py::bytes((const char *)this->_img->data, this->_maix_image_size);

if (format == "rgb")
return py::bytes((const char *)this->_img->data, this->_maix_image_size);

std::vector<uchar>encoded_buffer;
cv::Mat cv_is_bgr, rgb(this->_img->height, this->_img->width, CV_8UC3, this->_img->data);
cv::cvtColor(rgb, cv_is_bgr, cv::COLOR_RGB2BGR);

if (format == "jpg") {
if (params.size() == 0) params.push_back(CV_IMWRITE_JPEG_QUALITY), params.push_back(95);
cv::imencode(".jpeg", cv_is_bgr, encoded_buffer, params);
return py::bytes((const char *)encoded_buffer.data(), encoded_buffer.size());
}
if (format == "png") {
if (params.size() == 0) params.push_back(CV_IMWRITE_PNG_COMPRESSION), params.push_back(3);
cv::imencode(".png", cv_is_bgr, encoded_buffer, params);
return py::bytes((const char *)encoded_buffer.data(), encoded_buffer.size());
}
if (format == "bmp") {
cv::imencode(".bmp", cv_is_bgr, encoded_buffer, params);
return py::bytes((const char *)encoded_buffer.data(), encoded_buffer.size());
}
return py::bytes();
}

maix_image &maix_image::_resize(int dst_w, int dst_h, int func, int padding, std::vector<int> size)
Expand Down
2 changes: 1 addition & 1 deletion ext_modules/_maix_image/include/maix_image.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class maix_image : virtual public any_image, public maix_vision, public maix_cus
int len__();
void _show();
int _save(std::string file_path, std::string format);
py::bytes _tobytes();
py::bytes _tobytes(std::string format, std::vector<int> params);
size_t img_pointer();
maix_image &_new(std::vector<int> size, std::vector<int> color, std::string mode);
maix_image &_load(py::object data, std::vector<int> size, std::string mode);
Expand Down
2 changes: 1 addition & 1 deletion ext_modules/_maix_image/py_maix_image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ PYBIND11_MODULE(_maix_image, mo)
.def("clear", &maix_image::_clear)
.def("delete", &maix_image::_delete)
.def("save", &maix_image::_save, py::arg("path"), py::arg("format") = "jpeg")
.def("tobytes", &maix_image::_tobytes)
.def("tobytes", &maix_image::_tobytes, py::arg("format")="rgb", py::arg("params") = std::vector<int>{})
.def("resize", &maix_image::_resize, py::arg("w") = 0, py::arg("h") = 0, py::arg("func") = 1, py::arg("padding") = 1, py::arg("size") = std::vector<int>{0, 0})
.def("draw_line", &maix_image::_draw_line, py::arg("x1"), py::arg("y1"), py::arg("x2"), py::arg("y2"), py::arg("color") = std::vector<int>{127, 127, 127}, py::arg("thickness") = 1)
.def("draw_cross", &maix_image::_draw_cross, py::arg("x"), py::arg("y"), py::arg("c"), py::arg("size") = 5, py::arg("thickness") = 1)
Expand Down
4 changes: 2 additions & 2 deletions ext_modules/_maix_vivo/_maix_vivo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ class _v83x_vivo
init(vi_w, vi_h, ai_w, ai_h, vo_dir, ai_dir);
}

int resize(int w, int h, int i)
int config(int w, int h, int i)
{
if (this->inited && this->vi[i]->width != w && this->vi[i]->height != h)
{
Expand Down Expand Up @@ -438,6 +438,6 @@ PYBIND11_MODULE(_maix_vivo, m)
.def(pybind11::init<int, int, int, int, int, int>(),
py::arg("vi_w") = 240, py::arg("vi_h") = 240, py::arg("ai_w") = 224, py::arg("ai_h") = 224, py::arg("vo_dir") = 0, py::arg("ai_dir") = 0)
.def("get", &_v83x_vivo::get, py::arg("show") = false, py::arg("more") = false)
.def("resize", &_v83x_vivo::resize, py::arg("w") = 240, py::arg("h") = 240, py::arg("i") = 1)
.def("config", &_v83x_vivo::config, py::arg("w") = 240, py::arg("h") = 240, py::arg("i") = 0)
.def("set", &_v83x_vivo::set);
}
17 changes: 8 additions & 9 deletions maix/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,16 @@ def config(self, size=None, _ai_size=(224, 224)):
if size == None:
size = (display.width(), display.height())
super(V831VivoMaixVideo, self).config(size)
print('[camera] config input size(%d, %d)' %
(self.width(), self.height()))
if self.cam == None:
display.__fastview__ = self.cam = _v83x_vivo(display.width(), display.height(), _ai_size[0], _ai_size[1], vo_dir = self._vo_dir, ai_dir = self._ai_dir)
display.__show__ = self.cam = _v83x_vivo(size[0], size[1], _ai_size[0], _ai_size[1], vo_dir = self._vo_dir, ai_dir = self._ai_dir)
def __new_draw__(img):
if isinstance(img, bytes):
display.__fastview__.set(img)
display.__show__.set(img)
display.__draw__ = __new_draw__
else:
self.cam.resize(size[0], size[1])
print('[camera] config input size(%d, %d)' %
(self.width(), self.height()))
self.cam.config(size[0], size[1])

def read(self, video_num=0, show=False, skip_frame=8):
if self.cam == None:
Expand Down Expand Up @@ -104,12 +104,11 @@ def config(self, size=None, video=0, horizontal=1, vertical=1):
from maix import display
size = (display.width(), display.height())
super(SpMaixVideo, self).config(size)
if self.cam == None:
self.cam = Camera(self.width(), self.height(), video, horizontal, vertical)
else:
pass
print('[camera] config input size(%d, %d, %d)' %
(self.width(), self.height(), video))
if self.cam:
del self.cam
self.cam = Camera(self.width(), self.height(), video, horizontal, vertical)

def read(self):
if self.cam == None:
Expand Down
12 changes: 6 additions & 6 deletions maix/display.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ def config(size=(240, 240)):
_width, _height = size[0], size[1]

try:
__fastview__ = None
__show__ = None
from _maix_display import Display
__fastview__ = Display()
config(size=(__fastview__.width, __fastview__.height))
__show__ = Display()
config(size=(__show__.width, __show__.height))
def __draw__(img):
global __fastview__, _width, _height
global __show__, _width, _height
if isinstance(img, bytes):
__fastview__.draw(img, _width, _height)
__show__.draw(img, _width, _height)
from maix import camera
except ModuleNotFoundError as e:
pass
Expand Down Expand Up @@ -60,5 +60,5 @@ def show(img=None, box=(0, 0), local_show=True, remote_show=True):
img = img.tobytes()
except ImportError as e:
pass
if __fastview__:
if __show__:
__draw__(img)
26 changes: 26 additions & 0 deletions tests/general/usage_desktop.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,29 @@
print(t)
print(type(t))
display.show(t)

# from maix import camera, display
# t = camera.capture()
# print(t)
# print(type(t))
# display.show(t)

# camera.camera.config((640, 480))
# t = camera.capture()
# print(t)
# print(type(t))
# display.show(t)

# from maix import camera, display
# t = camera.capture().draw_rectangle(30, 50, 130, 140, color=(255, 50, 50), thickness=-1)

# print(len(t.tobytes()))

# with open("t.bmp", "wb") as f:
# f.write(t.tobytes("bmp"))

# with open("t.jpg", "wb") as f:
# f.write(t.tobytes("jpg"))

# with open("t.png", "wb") as f:
# f.write(t.tobytes("png", (16, 3)))

0 comments on commit 334cca7

Please sign in to comment.