将变量值附加到cv2.imwrite函数

时间:2019-04-10 19:24:42

标签: python opencv

我想将字符串变量的值附加到cv2.imwrite()函数中

我已经编写了一个代码来从视频中提取帧,并检查目标对象是否存在于帧中。当前,在其中找到目标的帧将另存为frame0,frame1等。下面是我的相同代码

def target_non_target(input_frames_folder,model_file):

    count=0
    folders = glob(input_frames_folder)

    img_list = []

    for folder in folders:
        for f in glob(folder+"/*.jpg"):
            img_list.append(f)


    for i in range(len(img_list)):
        print(img_list[i])
        v1=os.path.basename(img_list[i])
        print(v1)
        img_name = os.path.splitext(v1)[0]
        print(img_name)
        image = cv2.imread(img_list[i])
        orig = image.copy()

        image = cv2.resize(image, (28, 28))
        image = image.astype("float") / 255.0
        image = img_to_array(image)
        image = np.expand_dims(image, axis=0)


        print("[INFO] loading network...")
        model = load_model(model_file)


        (non_target, target) = model.predict(image)[0]

        if target > non_target:
            label = "Target"

        else:
            label = "Non Target"

        probab = target if target > non_target else non_target
        label = "{}: {:.2f}%".format(label, probab * 100)


        output = imutils.resize(orig, width=400)
        cv2.putText(output, label, (10, 25),  cv2.FONT_HERSHEY_SIMPLEX,0.7, (0, 255, 0), 2)

        if target > non_target:
            cv2.imwrite("C:\\Python35\\target_non_target\\Target_images_new\\frame%d.jpg"%count,orig)
            count += 1
    #cv2.imshow("Output", output)
        cv2.waitKey(0)



frames_folder = ("C:\\Python36\\videos\\videos_new\\*")

model = ("C:\\Python35\\target_non_target\\target_non_target.model")

target_check = target_non_target(frames_folder,model)

当前将找到目标对象的帧保存为frame0,frame1等。我想使用该图像的原始名称保存该框架,该图像位于变量IMG_NAME中。我如何将其附加到cv2.imwrite()函数中。我以前的几次尝试都给我这个错误

  

TypeError:在字符串格式化期间并非所有参数都已转换

1 个答案:

答案 0 :(得分:1)

使用cv2.imwrite()保存框架时,您需要更改字符串格式。当前,您的格式为%d,它在尝试将整数作为字符串时尝试使用整数。您可以使用%s或使用内置的format()函数。

更改

cv2.imwrite("C:\\Python35\\target_non_target\\Target_images_new\\frame%d.jpg"%count,orig)

cv2.imwrite("C:\\Python35\\target_non_target\\Target_images_new\\frame{}.jpg".format(img_name),orig)