具有全局变量的功能的序列化

时间:2019-02-25 13:01:29

标签: python pickle dill

我正在尝试使用包含全局变量的dill序列化一个函数。我得到Segmentation fault (core dumped)

import dill
import base64
from imutils.video import VideoStream
import cv2
import imutils

args ={
    "prototxt": "MobileNetSSD_deploy.prototxt.txt",
    "model": "MobileNetSSD_deploy.caffemodel",
    "confidence":0.2
}
net = cv2.dnn.readNetFromCaffe(args["prototxt"], args["model"])
def detect(frame):
    f1 = imutils.resize(frame,width=500)
    global net
    blob = cv2.dnn.blobFromImage(cv2.resize(f1,(300,300)), 0.007843,(300,300), 127.5 )
    net.setInput(blob)
    detections = net.forward()
    return detections
s = base64.b64encode(dill.dumps(detect,recurse=True))
print(s)

在另一个文件中,它接收到该函数的标准化版本,这是第一个函数的输出

import dill
import base64
from imutils.video import VideoStream
import cv2
import imutils
args ={
    "prototxt": "MobileNetSSD_deploy.prototxt.txt",
    "model": "MobileNetSSD_deploy.caffemodel",
    "confidence":0.2
}
net = cv2.dnn.readNetFromCaffe(args["prototxt"], args["model"])

b = # searialized function string
f = base64.b64decode(b)
func = dill.loads(f)
frame = cv2.imread("1.jpg")
print(func(frame))

环境,库在两个系统上都设置相同。调试代码后,我发现反序列化后创建的函数中不存在变量net

有什么可能的方法来对此进行序列化?

更新:运行故障处理程序给出了此输出

Fatal Python error: Segmentation fault

Current thread 0x00007f4147647700 (most recent call first):
  File "/home/sandeep/Huawei/real-time-object-detection/migration_test1.py", line 17 in detect
  File "migration_test2.py", line 17 in <module>
Segmentation fault (core dumped)

line 17 in migration_test1.py represents the `line net.setInput(blob)`
line 17 in migration_test2.py represents the `line print(func(frame))`

0 个答案:

没有答案