无法使用opencv预先加载预先训练的细分网络

时间:2020-05-31 11:33:05

标签: python numpy opencv

我无法加载预先训练的segnet模型和权重,并且仅在使用较新版本的opencv而不安装caffe的情况下才能在演示映像上运行它。有人遇到过这样的问题吗? 下面是我的代码,之后就是我得到的错误。

import numpy as np
import argparse

import time
import cv2


model_path = r"D:\Work\SegNet\bayesian_segnet_camvid.caffemodel"
prototxt_path = r"D:\Work\SegNet\bayesian_segnet_camvid.prototxt"
classes_path = r"D:\Work\SegNet\classes.txt"
input_img_path = r"D:\Work\SegNet\frame_0.jpg"
#resized_img_path = r""
colors_path = r"D:\Work\SegNet\colors.txt"
output_img_path = r"D:\Work\SegNet\frame_res_0.jpg"

# load the class label names
classes = open(classes_path).read().strip().split("\n")

# if a colors file was supplied, load it from disk
# otherwise, we need to randomly generate RGB colors for each class
colors = open(colors_path).read().strip().split("\n")
colors = [np.array(c.split(",")).astype("int") for c in colors]
colors = np.array(colors, dtype="uint8")


# initialize the legend visualization
legend = np.zeros(((len(classes) * 25) + 25, 300, 3), dtype="uint8")

# loop over the class names + colors
for (i, (className, color)) in enumerate(zip(classes, colors)):
    # draw the class name + color on the legend
    color = [int(c) for c in color]
    cv2.putText(legend, className, (5, (i * 25) + 17),
        cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
    cv2.rectangle(legend, (100, (i * 25)), (300, (i * 25) + 25),
        tuple(color), -1)

# load our serialized model from disk
print("[INFO] loading model...")
net = cv2.dnn.readNetFromCaffe(prototxt_path, model_path)

# load the input image, resize it, and construct a blob from it,
# but keeping in mind that the original input image dimensions
# segnet 360, 480
image = cv2.imread(input_img_path)
#image = imutils.resize(image, width=args["width"])
image = cv2.resize(image, (480, 360))
blob = cv2.dnn.blobFromImage(image, 1 / 255.0, (480, 360), 0,
    swapRB=True, crop=False)

# perform a forward pass using the segmentation model
net.setInput(blob)
start = time.time()
output = net.forward()
end = time.time()

# show the amount of time inference took
print("[INFO] inference took {:.4f} seconds".format(end - start))

# infer the total number of classes along with the spatial dimensions
# of the mask image via the shape of the output array
(numClasses, height, width) = output.shape[1:4]

# our output class ID map will be num_classes x height x width in
# size, so we take the argmax to find the class label with the
# largest probability for each and every (x, y)-coordinate in the
# image
classMap = np.argmax(output[0], axis=0)

# given the class ID map, we can map each of the class IDs to its
# corresponding color
mask = colors[classMap]

# resize the mask and class map such that its dimensions match the
# original size of the input image (we're not using the class map
# here for anything else but this is how you would resize it just in
# case you wanted to extract specific pixels/classes)
mask = cv2.resize(mask, (image.shape[1], image.shape[0]),
    interpolation=cv2.INTER_NEAREST)
classMap = cv2.resize(classMap, (image.shape[1], image.shape[0]),
    interpolation=cv2.INTER_NEAREST)

# perform a weighted combination of the input image with the mask to
# form an output visualization
output = ((0.4 * image) + (0.6 * mask)).astype("uint8")

cv2.imwrite(output_img_path, output)

# show the input and output images
cv2.imshow("Legend", legend)
cv2.imshow("Input", image)
cv2.imshow("Output", output)
cv2.waitKey(0)

错误是这个: OpenCV(4.2.0)C:\ projects \ opencv-python \ opencv \ modules \ dnn \ src \ caffe \ caffe_io.cpp:1156:错误:(-2:未指定错误)失败:ReadProtoFromTextFile(param_file,param)。无法解析功能'cv :: dnn :: ReadNetParamsFromTextFileOrDie'中的NetParameter文件:D:\ Work \ SegNet \ bayesian_segnet_camvid.prototxt

0 个答案:

没有答案