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

Commit

Permalink
[update] Don't want to explain.
Browse files Browse the repository at this point in the history
  • Loading branch information
junhuanchen committed Jan 13, 2021
1 parent 8549af8 commit 0a225cd
Show file tree
Hide file tree
Showing 15 changed files with 745 additions and 145 deletions.
59 changes: 59 additions & 0 deletions ext_modules/_maix/_maix.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,39 @@
#define _VERSION_ "0.1"
#define _NAME_ "_maix"

#include "jpeglib.h"
#include "string.h"

PyDoc_STRVAR(_maix_doc, "MaixPy Python3 library.\n");

static PyObject *_maix_help() {
return PyUnicode_FromString(_maix_doc);
}

PyDoc_STRVAR(_maix_rgb2jpg_doc, "rgb2jpg()\n\nConvert image(rgb888) bytes data to jpeg image bytes.\n");
static PyObject *PyJpegCompress(char *inData, int width, int height, int channels, int color_space, int quality);
static PyObject *_maix_rgb2jpg(PyObject *self, PyObject *args)
{
PyObject *bytes = NULL;
PyObject *inRGB = NULL;

int width = 1, height = 1;
int channels = 3, color_space = JCS_RGB, quality = 75;

if (!PyArg_ParseTuple(args, "Oii|iii", &inRGB,
&width, &height, &channels, &color_space, &quality))
return NULL;

char *rgb_data = PyBytes_AS_STRING(inRGB);

bytes = PyJpegCompress(rgb_data, width, height, channels, color_space, quality);

return bytes;
}

static PyMethodDef _maix_methods[] = {
{"help", (PyCFunction)_maix_help, METH_NOARGS, _maix_doc},
{"rgb2jpg", (PyCFunction)_maix_rgb2jpg, METH_VARARGS, _maix_rgb2jpg_doc},
{NULL}
};

Expand Down Expand Up @@ -54,3 +79,37 @@ PyMODINIT_FUNC PyInit__maix(void)
return module;
}

static PyObject *PyJpegCompress(char *inData, int width, int height, int channels, int color_space, int quality)
{
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
long unsigned int outSize = 0;
uint8_t *outbuffer = NULL;
JSAMPROW row_pointer[1];

cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);
jpeg_mem_dest(&cinfo, &outbuffer, &outSize);
cinfo.image_width = width;
cinfo.image_height = height;
cinfo.input_components = channels; // 3 / 1
cinfo.in_color_space = color_space; // JCS_RGB / JCS_GRAYSCALE
jpeg_set_defaults(&cinfo);
jpeg_set_quality(&cinfo, quality, TRUE); // default 75
jpeg_start_compress(&cinfo, TRUE);
int row_stride = width * 3;
while (cinfo.next_scanline < cinfo.image_height)
{
row_pointer[0] = (uint8_t *)&inData[cinfo.next_scanline * row_stride];
(void)jpeg_write_scanlines(&cinfo, row_pointer, 1);
}
jpeg_finish_compress(&cinfo);

PyObject *bytes = PyBytes_FromStringAndSize((const char *)outbuffer, outSize);

if (NULL != outbuffer)
free(outbuffer), outbuffer = NULL;

jpeg_destroy_compress(&cinfo);
return bytes;
}
4 changes: 2 additions & 2 deletions ext_modules/_maix/example/test__maix.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
def test_import():
import _maix
print(_maix.help())
tmp = _maix.Camera()
print(tmp.rgb2jpg(b"\xff"*640*480*3))
tmp = _maix.rgb2jpg(b"\xff"*4*6*3, 4, 6)
print(len(tmp))
60 changes: 0 additions & 60 deletions ext_modules/_maix/pyCamera.c
Original file line number Diff line number Diff line change
@@ -1,45 +1,6 @@

#include "_maix.h"

#include "jpeglib.h"

#include "string.h"

static PyObject *PyJpegCompress(char *inData, int width, int height, int channels, int color_space, int quality)
{
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
long unsigned int outSize = 0;
uint8_t *outbuffer = NULL;
JSAMPROW row_pointer[1];

cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);
jpeg_mem_dest(&cinfo, &outbuffer, &outSize);
cinfo.image_width = width;
cinfo.image_height = height;
cinfo.input_components = channels; // 3 / 1
cinfo.in_color_space = color_space; // JCS_RGB / JCS_GRAYSCALE
jpeg_set_defaults(&cinfo);
jpeg_set_quality(&cinfo, quality, TRUE); // default 75
jpeg_start_compress(&cinfo, TRUE);
int row_stride = width;
while (cinfo.next_scanline < cinfo.image_height)
{
row_pointer[0] = (uint8_t *)&inData[cinfo.next_scanline * row_stride];
(void)jpeg_write_scanlines(&cinfo, row_pointer, 1);
}
jpeg_finish_compress(&cinfo);

PyObject *bytes = PyBytes_FromStringAndSize((const char *)outbuffer, outSize);

if (NULL != outbuffer)
free(outbuffer), outbuffer = NULL;

jpeg_destroy_compress(&cinfo);
return bytes;
}

/* Macros needed for Python 3 */
#ifndef PyInt_Check
#define PyInt_Check PyLong_Check
Expand Down Expand Up @@ -139,29 +100,8 @@ static PyObject *Camera_str(PyObject *object)
return dev_desc;
}

PyDoc_STRVAR(Camera_rgb2jpg_doc, "rgb2jpg()\n\nConvert image(rgb888) bytes data to jpeg image bytes.\n");
static PyObject *Camera_rgb2jpg(CameraObject *self, PyObject *args)
{
PyObject *bytes = NULL;
PyObject *inRGB = NULL;

int width = self->width, height = self->height;
int channels = 3, color_space = JCS_RGB, quality = 75;

if (!PyArg_ParseTuple(args, "O|iiiii", &inRGB,
&width, &height, &channels, &color_space, &quality))
return NULL;

char *rgb_data = PyBytes_AS_STRING(inRGB);

bytes = PyJpegCompress(rgb_data, width, height, channels, color_space, quality);

return bytes;
}

static PyMethodDef Camera_methods[] = {

{"rgb2jpg", (PyCFunction)Camera_rgb2jpg, METH_VARARGS, Camera_rgb2jpg_doc},
{"close", (PyCFunction)Camera_close, METH_NOARGS, Camera_close_doc},
{"__enter__", (PyCFunction)Camera_enter, METH_NOARGS, NULL},
{"__exit__", (PyCFunction)Camera_exit, METH_NOARGS, NULL},
Expand Down
74 changes: 45 additions & 29 deletions maix/Video.py
Original file line number Diff line number Diff line change
@@ -1,64 +1,80 @@

class VideoCapture():
class MaixVideo():

def __init__(self, source="/dev/video", size=(640, 480)):
self.source = source
def __init__(self, size=(640, 480)):
self.width, self.height = size

def write(self):
pass # for file

def read(self):
return b'\x00\xFF\x00' * (self.width * self.height)
return b'\xFF\x00\x00' * (self.width * self.height)

def capture(self):
from PIL import Image
return Image.frombytes("RGB", (self.width, self.height), self.read())
return Image.frombytes(
"RGB", (self.width, self.height), self.read())

def close(self):
pass # for file


camera = VideoCapture()
camera = MaixVideo()

try:
# use libmaix on v831
from libmaix import Camera

class V831VideoCapture(VideoCapture):
class V831MaixVideo(MaixVideo):

def __init__(self, source="/sipeed/v831", size=(480, 360)):
def __init__(self, source="/v831", size=(480, 360)):
super(V831MaixVideo, self).__init__(size)
self.source = source
self.width, self.height = size
self.cam = Camera(self.width, self.height)

def read(self):
return self.cam.read() # bytes
return self.cam.read()

def __del__(self):
self.cam.close()

camera = V831VideoCapture()
camera = V831MaixVideo()
except Exception as e:
pass

try:
from cv2 import VideoCapture

class CvMaixVideo(MaixVideo):

def __init__(self, source=0, size=(640, 480)):
super(CvMaixVideo, self).__init__(size)
self.source = source
self.cam = VideoCapture(0)

def read(self):
ret, frame = self.cam.read()
if ret:
bgr = frame[..., ::-1] # bgr2rgb
return bgr.tobytes() # bytes
return None

def __del__(self):
self.cam.release()

camera = CvMaixVideo()
except Exception as e:
pass

if __name__ == '__main__':
'''
def test_cv():
import cv2
cap = cv2.VideoCapture(0)
print(cap)
while True:
ret, frame = cap.read()
print(ret, type(frame))
if frame is not None:
cv2.imshow("Video", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
# test_cv()
'''
import display
# display.clear((255, 0, 0))
display.clear((255, 0, 0))
display.show(camera.capture())
# tmp = camera.read()
# import _maix
# frame = _maix.rgb2jpg(camera.rgbbuf, camera.width, camera.height)
# print(len(frame) // 1024, camera.width, camera.height)
# from PIL import Image
# from io import BytesIO
# img = Image.open(BytesIO(frame))
# img.show()

5 changes: 3 additions & 2 deletions maix/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from .Video import camera
from .import display
from .import display, rpycs

__all__ = ['display', 'Video', 'camera', 'rpycs']

__all__ = ['display', 'Video', 'camera']
Loading

0 comments on commit 0a225cd

Please sign in to comment.