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

Commit

Permalink
[camera] fix re-config size.
Browse files Browse the repository at this point in the history
  • Loading branch information
junhuanchen committed May 31, 2022
1 parent 8ca6db4 commit 63c4ec2
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 41 deletions.
32 changes: 17 additions & 15 deletions ext_modules/_maix_vivo/_maix_vivo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,13 +301,13 @@ class _v83x_vivo
// // save buffer used default 3 3
// extern uint8_t libmaix_cam_vovi_bufmax, libmaix_cam_voui_bufmax;
// libmaix_cam_vovi_bufmax = 3, libmaix_cam_voui_bufmax = 3;
// printf("[_v83x_vivo] %d %d %d %d %d %d \r\n", vi_w, vi_h, ai_w, ai_h, vo_dir, ai_dir);
init(vi_w, vi_h, ai_w, ai_h, vo_dir, ai_dir);
}

int config(int w, int h, int i)
int cfg(int w, int h, int i)
{
if (this->inited && this->vi[i]->width != w && this->vi[i]->height != h)
// printf("[cfg] %d %d %d \r\n", this->inited, (this->vi[i]->width != w), (this->vi[i]->height != h));
if ((this->inited) && ((this->vi[i]->width != w) || (this->vi[i]->height != h)))
{
if (NULL != this->vi[i])
libmaix_cam_destroy(&this->vi[i]);
Expand All @@ -334,11 +334,6 @@ class _v83x_vivo
{
if (!this->inited)
{
// printf("libmaix_camera_module_init %s:%s\r\n", __FILE__, __FUNCTION__);
libmaix_camera_module_init();
// printf("libmaix_image_module_init %s:%s\r\n", __FILE__, __FUNCTION__);
libmaix_image_module_init();

this->ai_dir = ai_dir;
this->vo_dir = this->vo_dir = vo_dir;

Expand Down Expand Up @@ -416,11 +411,6 @@ class _v83x_vivo
libmaix_image_destroy(&this->yuv2rgb);
if (NULL != this->vo)
libmaix_vo_destroy(&this->vo);
// usleep(200000); // wait 1s to deinit
// printf("libmaix_image_module_deinit %s:%s\r\n", __FILE__, __FUNCTION__);
libmaix_image_module_deinit();
// printf("libmaix_camera_module_deinit %s:%s\r\n", __FILE__, __FUNCTION__);
libmaix_camera_module_deinit();
this->inited = false;
}
return this->inited;
Expand All @@ -432,12 +422,24 @@ class _v83x_vivo
}
};

static struct _maix_vivo
{
_maix_vivo() {
libmaix_camera_module_init();
libmaix_image_module_init();
}
~_maix_vivo() {
libmaix_image_module_deinit();
libmaix_camera_module_deinit();
}
} global_maix_vivo;

PYBIND11_MODULE(_maix_vivo, m)
{
pybind11::class_<_v83x_vivo>(m, "_v83x_vivo")
.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)
py::arg("vi_w") = 240, py::arg("vi_h") = 240, py::arg("ai_w") = 192, py::arg("ai_h") = 128, py::arg("vo_dir") = 0, py::arg("ai_dir") = 0)
.def("get", &_v83x_vivo::get, py::arg("show") = false, py::arg("more") = false)
.def("config", &_v83x_vivo::config, py::arg("w") = 240, py::arg("h") = 240, py::arg("i") = 0)
.def("cfg", &_v83x_vivo::cfg, py::arg("w") = 240, py::arg("h") = 240, py::arg("i") = 0)
.def("set", &_v83x_vivo::set);
}
30 changes: 30 additions & 0 deletions ext_modules/_maix_vivo/example/_maix_vivo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

from maix import camera

print(camera.camera.cam.cfg(240, 240, 0))
img = camera.camera.cam.get()
print(len(img[0]))

print(camera.camera.cam.cfg(320, 240, 0))
img = camera.camera.cam.get()
print(len(img[0]))

# camera.config(size=(240, 240))

# camera.config(size=(320, 240))

from _maix_vivo import _v83x_vivo
cam = _v83x_vivo(320, 240)

print(cam.cfg(240, 240, 0))
frame = cam.get()
print(len(frame))
if len(frame) > 0:
print(len(frame[0]))

print(cam.cfg(320, 240, 0))
frame = cam.get()
print(len(frame))
if len(frame) > 0:
print(len(frame[0]))

7 changes: 5 additions & 2 deletions maix/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def capture(self, pillow=False):
try:
if pillow == False:
from maix import image
# print("self._width, self._height", self._width, self._height)
return image.Image().load(tmp, (self._width, self._height), "RGB")
except Exception as e:
pass
Expand Down Expand Up @@ -69,7 +70,7 @@ def __new_draw__(img):
display.__show__.set(img)
display.__draw__ = __new_draw__
else:
self.cam.config(size[0], size[1])
self.cam.cfg(size[0], size[1])

def read(self, video_num=0, show=False, skip_frame=8):
if self.cam == None:
Expand Down Expand Up @@ -163,14 +164,16 @@ def __del__(self):
# camera = MaixVideo()
raise e

''' # 20220531 dls fix it!!!!
def _del_config(*args, **kwargs):
# or use camera.camera.config if you knew what you were doing.
print("[camera.config] is deprecated, use [image.resize] for images.")
'''

# registered interface
capture = camera.capture
read = camera.read
config = _del_config
config = camera.config
height = camera.height
width = camera.width
close = camera.close
22 changes: 22 additions & 0 deletions tests/general/uasge_evdev.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from maix import event, display, image
from select import select

dev = event.InputDevice("/dev/input/event1")
while True:
r, w, x = select([dev], [], [], 0) # if r == 0 or set 0 will read() raise BlockingIOError
if r:
img = display.as_image()
img.draw_rectangle(0, 0, 240, 240, color=(255, 0, 0), thickness=-1)
x, y = 0, 0
for data in dev.read():
if (data.type == 0x03):
print(' type: ', data.type, ' code: ', hex(data.code), ' value: ', hex(data.value))
if data.code == 0x35: # ABS_MT_POSITION_X
x = data.value
if data.code == 0x36: # ABS_MT_POSITION_Y
y = data.value
if data.code == 0x30: # ABS_MT_TOUCH_MAJOR
tmp = "(%d, %d, %d)" % (x, y, hex(data.value))
w, h = image.get_string_size(tmp)
img.draw_string(120 - w // 2, 120, tmp)
display.show(img)
70 changes: 46 additions & 24 deletions tests/general/usage_desktop.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,59 @@
from maix import camera, display

camera.config(size=(240, 240))
img = camera.capture()
print(img)
img.save("240x240.jpg")
display.show(img)

camera.config(size=(320, 240))
img = camera.capture()
print(img)
img.save("320x240.jpg")
display.show(img)

for i in range(10):
camera.config(size=(240, 240))
for i in range(10):
img = camera.capture()
display.show(img)
camera.config(size=(320, 240))
for i in range(10):
img = camera.capture()
display.show(img)

display.config((416, 416))
# camera.config is remove.
camera.camera.config((640, 480), 0, 1, 0)

while True:
t = camera.capture()
print(t)
print(type(t))
display.show(t)
# while True:
# t = camera.capture()
# print(t)
# print(type(t))
# display.show(t)

# from maix import camera, display
# t = camera.capture()
# 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)
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)
from maix import camera, display
t = camera.capture().draw_rectangle(30, 50, 130, 140, color=(255, 50, 50), thickness=-1)

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

# with open("t.bmp", "wb") as f:
# f.write(t.tobytes("bmp"))
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.jpg", "wb") as f:
f.write(t.tobytes("jpg"))

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

0 comments on commit 63c4ec2

Please sign in to comment.