Skip to content

Commit fa4ca12

Browse files
committed
fixed numpy different with opencv
1 parent 537dfec commit fa4ca12

File tree

4 files changed

+32
-31
lines changed

4 files changed

+32
-31
lines changed

Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,14 @@ out/nvjpeg-test: out/nvjpeg-test.o
4646
# out/${PYTHON_LIB_NAME}: out/nvjpeg-python.o
4747
# gcc --shared -fPIC -o out/${PYTHON_LIB_NAME} out/nvjpeg-python.o -L${CUDA_PATH}/lib64 -lnvjpeg -lcudart -L${PYTHON_LIB_PATH} -lpython${PYTHON_VERSION}m ${CFLAGS}
4848
python-interface:
49-
${PYTHON_BIN} setup.py build_ext
49+
${PYTHON_BIN} setup.py build
5050

5151
clean:
5252
rm -Rf out build dist pynvjpeg.egg-info
5353

54+
release: clean python-interface
55+
${PYTHON_BIN} setup.py sdist
56+
${PYTHON_BIN} -m twine upload dist/*
57+
5458
# install: out/${PYTHON_LIB_NAME}
5559
# cp -f out/${PYTHON_LIB_NAME} ${PYTHON_DYNLOAD_PATH}

nvjpeg-python.c

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ NvJpegPythonImage* NvJpegPython_createImage(int width, int height, int nComponen
6363
imgdesc->img.channel[i] = pBuffer + (width*height*i);
6464
imgdesc->img.pitch[i] = width;
6565
}
66+
imgdesc->img.pitch[0] = width*3;
6667

6768
imgdesc->height = height;
6869
imgdesc->width = width;
@@ -86,6 +87,7 @@ NvJpegPythonImage* NvJpegPython_createImageFromHost(int width, int height, const
8687
imgdesc->img.channel[i] = pBuffer + (width*height*i);
8788
imgdesc->img.pitch[i] = width;
8889
}
90+
imgdesc->img.pitch[0] = width*3;
8991
cudaMemcpy(imgdesc->img.channel[0], data, width*height*3, cudaMemcpyHostToDevice);
9092

9193
imgdesc->height = height;
@@ -114,7 +116,7 @@ NvJpegPythonImage* NvJpegPython_decode(NvJpegPythonHandle* handle, const unsigne
114116
nvjpegGetImageInfo(nv_handle, jpegData, length, &nComponent, &subsampling, widths, heights);
115117

116118
NvJpegPythonImage* imgdesc = NvJpegPython_createImage(widths[0], heights[0], nComponent, subsampling);
117-
int nReturnCode = nvjpegDecode(nv_handle, nv_statue, jpegData, length, NVJPEG_OUTPUT_BGR, &(imgdesc->img), NULL);
119+
int nReturnCode = nvjpegDecode(nv_handle, nv_statue, jpegData, length, NVJPEG_OUTPUT_BGRI, &(imgdesc->img), NULL);
118120

119121
if(nReturnCode != 0)
120122
{
@@ -162,7 +164,7 @@ NvJpegJpegData* NvJpegPython_encode(NvJpegPythonHandle* handle, NvJpegPythonImag
162164
nvjpegEncoderParamsSetOptimizedHuffman(nv_enc_params, 1, NULL);
163165
nvjpegEncoderParamsSetSamplingFactors(nv_enc_params, img->subsampling, NULL);
164166

165-
nvjpegEncodeImage(nv_handle, nv_enc_state, nv_enc_params, &(img->img), NVJPEG_INPUT_BGR, img->width, img->height, NULL);
167+
nvjpegEncodeImage(nv_handle, nv_enc_state, nv_enc_params, &(img->img), NVJPEG_INPUT_BGRI, img->width, img->height, NULL);
166168

167169
size_t length;
168170
nvjpegEncodeRetrieveBitstream(nv_handle, nv_enc_state, NULL, &length, NULL);
@@ -293,26 +295,13 @@ static PyObject* NvJpeg_decode(NvJpeg* Self, PyObject* Argvs)
293295
NvJpegPythonImage* img = NvJpegPython_decode(m_handle, (const unsigned char*)jpegData, len);
294296

295297
unsigned char* data = NvJpegPythonImage2HostMemory(img);
296-
npy_intp dims[1] = {(npy_intp)((img->width)*(img->height))*3};
297-
PyObject* temp = PyArray_SimpleNewFromData(1, dims, NPY_UINT8, data);
298+
299+
npy_intp dims[3] = {(npy_intp)(img->height), (npy_intp)(img->width), 3};
300+
PyObject* temp = PyArray_SimpleNewFromData(3, dims, NPY_UINT8, data);
301+
298302
PyArray_ENABLEFLAGS((PyArrayObject*) temp, NPY_ARRAY_OWNDATA);
299-
// free(data);
300-
301-
PyObject* shape = Py_BuildValue("(i,i,i)", 3, img->height, img->width);
302303
NvJpegPython_destoryImage(&img);
303-
PyObject* rtn = PyArray_Reshape((PyArrayObject*)temp, shape);
304-
Py_DECREF(shape);
305-
Py_DECREF(temp);
306-
temp = rtn;
307-
308-
rtn = PyArray_SwapAxes((PyArrayObject*)temp, 0, 2);
309-
Py_DECREF(temp);
310-
temp = rtn;
311-
312-
rtn = PyArray_SwapAxes((PyArrayObject*)temp, 0, 1);
313-
Py_DECREF(temp);
314-
// Py_INCREF(rtn);
315-
return rtn;
304+
return temp;
316305
}
317306

318307
static PyObject* NvJpeg_encode(NvJpeg* Self, PyObject* Argvs)
@@ -339,6 +328,7 @@ static PyObject* NvJpeg_encode(NvJpeg* Self, PyObject* Argvs)
339328
}
340329

341330
NvJpegPythonHandle* m_handle = (NvJpegPythonHandle*)Self->m_handle;
331+
342332
NvJpegPythonImage* img = NvJpegPython_createImageFromHost(PyArray_DIM(vecin, 1), PyArray_DIM(vecin, 0), (const unsigned char*)PyArray_BYTES(vecin), 3);
343333
NvJpegJpegData* data = NvJpegPython_encode(m_handle, img, quality);
344334

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
from distutils.core import setup, Extension
1111
setup(name='pynvjpeg',
12-
version='0.0.3',
12+
version='0.0.4',
1313
ext_modules=[Extension('nvjpeg', ['nvjpeg-python.c'])],
1414
author="Usingnet",
1515
author_email="developer@usingnet.com",

tests/test.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,24 @@
1111

1212
from nvjpeg import NvJpeg
1313

14-
fp = open(os.path.join(os.path.dirname(__file__), "test.jpg"), "rb")
14+
nj = NvJpeg()
15+
16+
image_file = os.path.join(os.path.dirname(__file__), "test.jpg")
17+
18+
fp = open(image_file, "rb")
1519
img = fp.read()
1620
fp.close()
21+
nj_np = nj.decode(img)
22+
cv_np = cv2.imread(image_file)
1723

18-
nj = NvJpeg()
19-
img_np = nj.decode(img)
20-
21-
cv2.imshow("Demo", img_np)
24+
cv2.imshow("NvJpeg Decode Image", nj_np)
25+
cv2.imshow("OpenCV Decode Image", cv_np)
2226
cv2.waitKey(0)
2327

24-
jpg = nj.encode(img_np)
25-
fp = open(os.path.join(os.path.dirname(__file__), "out", "python-test.jpg"), "wb")
26-
fp.write(jpg)
27-
fp.close()
28+
nj_jpg = nj.encode(cv_np)
29+
30+
fp = open(os.path.join(os.path.dirname(__file__), "out", "python-nvjpeg-test.jpg"), "wb")
31+
fp.write(nj_jpg)
32+
fp.close()
33+
34+
cv2.imwrite(os.path.join(os.path.dirname(__file__), "out", "python-opencv-test.jpg"), cv_np)

0 commit comments

Comments
 (0)