我目前正在研究一个Darknet/YOLO项目,该项目使用python中的opencv从实时流接收图像中检测对象。为了检测对象,opencv图像,它只是一个形状为(height, width, color_channels)
的numpy数组,必须转换为Darknet(用c编写)可以读取的格式(在Darknet中定义的具有类型为*float的数据属性的image类)。为此,我用python编写了以下代码:
h, w, c = input_frame.shape
# create a flattened image and normalize by devision by 255.
# NOTE transpose(2, 0, 1) permutes the axes from 0,1,2 to 2,0,1 (clockwise cycle)
flattened_image = input_frame.transpose(2, 0, 1).flatten().astype(np.float32)/255.
# create a C type pointer
c_float_p = ctypes.POINTER(ctypes.c_float) # define LP_c_float type
c_float_p_frame = flattened_image.ctypes.data_as(c_float_p) # cast to LP_c_float
# create empty C_IMAGE type object and then set data to c_float_p_frame
C_IMAGE_frame = dn.make_image(w, h, c)
C_IMAGE_frame.data = c_float_p_frame
(注意dn
是暗网接口,并在上面的某个地方导入,但这不是问题,所以它不是很重要)
def np_image_to_c_IMAGE(input_frame):
"""
parameters
==========
input_frame: ndarray (opencv image)
returns
==========
C_IMAGE_frame: C IMAGE object (implimented in darknet)
converts a numpy image (w x h x c dim ndarray) to a C type IMAGE
defined in darknet. Returns a pointer.
"""
h, w, c = input_frame.shape
# create a flattened image and normalize by devision by 255.
# NOTE transpose(2, 0, 1) permutes the axes from 0,1,2 to 2,0,1 (clockwise cycle)
flattened_image = input_frame.transpose(2, 0, 1).flatten().astype(np.float32)/255.
# create a C type pointer
c_float_p = ctypes.POINTER(ctypes.c_float) # define LP_c_float type
c_float_p_frame = flattened_image.ctypes.data_as(c_float_p) # cast to LP_c_float
# create empty C_IMAGE type object and then set data to c_float_p_frame
C_IMAGE_frame = dn.make_image(w, h, c)
C_IMAGE_frame.data = c_float_p_frame
return C_IMAGE_frame
我最初非常困惑,为什么我的代码是创建段错误,但我运行了一些调试测试,发现以下问题:访问C_IMAGE_frame.data[0]
(即只是读出第一个值)内的函数,我得到一个浮点数,就像人们所期望的,但如果我做同样的返回C_IMAGE_frame后,像这样:
#opencv get image and other code...
C_IMAGE = np_image_to_C_IMAGE(opencv_image)
print(C_IMAGE.data[0])
python引发段错误。我检查了是否所有的指针都"返回"了。我看到一些指针重赋魔法发生了
def np_image_to_C_IMAGE(input_frame):
# rest of function...
print(C_IMAGE_frame) # output: <lib.darknet.IMAGE object at 0x0000021F24F6EDC0>
print(C_IMAGE_frame.data) # output: <lib.darknet.LP_c_float object at 0x0000021F24F6EBC0>
print(C_IMAGE_frame.data[0]) # output: 0.0
return C_IMAGE_frame
# after C_IMAGE is returned in script
C_IMAGE = np_image_to_C_IMAGE(opencv_image)
print(C_IMAGE) # output: <lib.darknet.IMAGE object at 0x0000021F24F6EDC0>
print(C_IMAGE.data) # output: <lib.darknet.LP_c_float object at 0x0000021F24F6BAC0>
print(C_IMAGE.data[0] # raises Segmentation fault
注意data
指针0x0000021F24F6EBC0
变成了0x0000021F24F6BAC0
,所以当然它会出现分段故障,但为什么会发生这种情况呢?我怎样才能避免这种情况呢?这是蟒蛇内部的诡计还是别的什么?我的意思是,如果我在python中返回一些东西,我期望它是我传递给return
的确切对象,但也许python ctypes打破了一些东西或有一些有趣的实现需要一个解决方案?现在我将代码粘贴回内联到我的分析脚本中,所以我的脚本再次工作,但我对为什么这首先发生以及如何解决它非常感兴趣。
编辑我添加了一个最小可重复的示例:
from ctypes import *
import numpy as np
class IMAGE(Structure):
_fields_ = [("w", c_int),
("h", c_int),
("c", c_int),
("data", POINTER(c_float))]
img = np.zeros((1080, 1920, 3)) # h, w, c array = opencv image analogon
def np_image_to_c_IMAGE(input_frame):
h, w, c = input_frame.shape
flattened_image = input_frame.transpose(2, 0, 1).flatten().astype(np.float32)/255.
c_float_p = POINTER(c_float) # define LP_c_float type
c_float_p_frame = flattened_image.ctypes.data_as(c_float_p) # cast to LP_c_float
C_IMAGE_frame = IMAGE(w, h, c, c_float_p_frame)
print(C_IMAGE_frame)
print(C_IMAGE_frame.data)
return C_IMAGE_frame
C_IMAGE = np_image_to_c_IMAGE(img)
print(C_IMAGE)
print(C_IMAGE.data)
输出:
# within function
<__main__.IMAGE object at 0x7fc7f618ff40>
<__main__.LP_c_float object at 0x7fc7f49b1040>
# after return
<__main__.IMAGE object at 0x7fc7f618ff40>
<__main__.LP_c_float object at 0x7fc800777f40>
将数据指针存储在IMAGE
中不会保留对图像数据的引用。一旦flattened_image
和c_float_p_frame
离开作用域,数据就被释放。在图像中存储一个额外的引用,以防止数据被释放:
from ctypes import *
import numpy as np
class IMAGE(Structure):
_fields_ = [("w", c_int),
("h", c_int),
("c", c_int),
("data", POINTER(c_float))]
img = np.zeros((1080, 1920, 3))
def np_image_to_c_IMAGE(input_frame):
h, w, c = input_frame.shape
flattened_image = input_frame.transpose(2, 0, 1).flatten().astype(np.float32)/255.
c_float_p = POINTER(c_float)
c_float_p_frame = flattened_image.ctypes.data_as(c_float_p)
C_IMAGE_frame = IMAGE(w,h,c,c_float_p_frame)
C_IMAGE_frame.ref = c_float_p_frame # extra reference to data stored
print(C_IMAGE_frame)
print(C_IMAGE_frame.data)
print(cast(C_IMAGE_frame.data,c_void_p)) # the pointer value
print(C_IMAGE_frame.data.contents) # data valid
return C_IMAGE_frame
C_IMAGE = np_image_to_c_IMAGE(img)
print(C_IMAGE)
print(C_IMAGE.data)
print(cast(C_IMAGE.data,c_void_p)) # pointer is the same, but contents freed if no ref.
print(C_IMAGE.data.contents) # crashes here if extra reference not kept.
输出(注意实际存储的指针值是相同的,但如果C_IMAGE_frame.ref
行被注释掉,最终打印将崩溃):
<__main__.IMAGE object at 0x000001A8B8B5CBC0>
<__main__.LP_c_float object ddat 0x000001A8B8B5CC40>
c_void_p(1824215363648)
c_float(0.0)
<__main__.IMAGE object at 0x000001A8B8B5CBC0>
<__main__.LP_c_float object at 0x000001A8B8B5CC40>
c_void_p(1824215363648)
c_float(0.0)
不是很优雅,我不确定为什么将c_float_p_frame
存储在IMAGE.data
中不足以保持引用,但将其存储在IMAGE.ref
中无需深入ctypes
的核心。