Reputation: 203
I want to append a value of a string variable into cv2.imwrite()
function
I have made a code to extract frames from video and check if my target object is present in the frame or not. Currently the frames in which the target is found are saved as frame0, frame1 and so on. Below is my code for the same
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)
Currently the frames in which the target object is found are saved as frame0, frame1,etc. I want to save the frame with the original name of the image which comes in the variable IMG_NAME. How can i append it to the cv2.imwrite()
function. My few previous attempts gave me this error
TypeError: not all arguments converted during string formatting
Upvotes: 2
Views: 4639
Reputation: 46600
You need to change your string formatting when you save a frame with cv2.imwrite()
. Currently your formatting is %d
which is trying to use a integer when it should be a string. You can either use %s
or use the built in format() function.
Change
cv2.imwrite("C:\\Python35\\target_non_target\\Target_images_new\\frame%d.jpg"%count,orig)
to
cv2.imwrite("C:\\Python35\\target_non_target\\Target_images_new\\frame{}.jpg".format(img_name),orig)
Upvotes: 2