GGzet
GGzet

Reputation: 319

Save images in loop with different names

I have a problem with saving cropped images in a loop. My code:

def run(self, image_file):
    print(image_file)
    cap = cv2.VideoCapture(image_file)
    while(cap.isOpened()):
        ret, frame = cap.read()
        if ret == True:
            img = frame
            min_h = int(max(img.shape[0] / self.min_height_dec, self.min_height_thresh))
            min_w = int(max(img.shape[1] / self.min_width_dec, self.min_width_thresh))
            gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
            faces = self.face_cascade.detectMultiScale(gray, 1.3, minNeighbors=5, minSize=(min_h, min_w))

            images = []
            for i, (x, y, w, h) in enumerate(faces):
                images.append(self.sub_image('%s/%s-%d.jpg' % (self.tgtdir, self.basename, i + 1), img, x, y, w, h))
            print('%d faces detected' % len(images))

            for (x, y, w, h) in faces: 
                self.draw_rect(img, x, y, w, h)
                # Fix in case nothing found in the image
            outfile = '%s/%s.jpg' % (self.tgtdir, self.basename)
            cv2.imwrite(outfile, img)
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
        else:
            break
    cap.release()
    cv2.destroyAllWindows()
    return images, outfile

I have a loop for every frame with cropping on faces. The problem is that for every cropped image and picture it gives the same name, and in the end I have faces only from the last frame. How should I fix this code to save all cropped faces and images?

Upvotes: 5

Views: 29474

Answers (5)

Hutch
Hutch

Reputation: 173

plt.savefig('/content/drive/MyDrive/Colab Notebooks/res_data/dimers/'+str(yname)+'_'+str(xname)+'_'+str(dimername), bbox_inches='tight' )
            
you can save as :                
      
/content/drive/MyDrive/Colab Notebooks/res_data/dimers/zeta_beta_GA.png

Upvotes: 0

nima farhadi
nima farhadi

Reputation: 698

i think this will helpful...

import cv2

vid = cv2.VideoCapture("video.mp4")
d = 0
ret, frame = vid.read()

while ret:
    ret, frame = vid.read()
    filename = "images/file_%d.jpg"%d
    cv2.imwrite(filename, frame)
    d+=1

this will save every frame with different name.

Upvotes: 2

user8630637
user8630637

Reputation: 21

import numpy as np
import cv2

cap = cv2.VideoCapture(0)
i = 0

while(True):
    # Capture frame-by-frame
    i = i +1
    ret, frame = cap.read()

gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

# Display the resulting frame
cv2.imshow('frame',frame)
**cv2.imwrite("template {0}.jpg".format(i),gray)**


if cv2.waitKey(0) & 0xFF == ord('q'):
    break

cap.release()
cv2.destroyAllWindows()

--- Code by rohbhot

Upvotes: 2

Colwin
Colwin

Reputation: 2685

You are saving each file with the same name. Hence you are overwriting previous saved images

outfile = '%s/%s.jpg' % (self.tgtdir, self.basename)

Change the line to this to add a random string in the name

outfile = '%s/%s.jpg' % (self.tgtdir, self.basename + str(uuid.uuid4()))

You will need to import uuid at the top of your file too

Upvotes: 7

ZdaR
ZdaR

Reputation: 22954

You may use the datetime module to get the current time with milliseconds precision, which would avoid the name conflicts, to assign the name before saving the image as:

from datetime import datetime

outfile = '%s/%s.jpg' % (self.tgtdir, self.basename + str(datetime.now()))
cv2.imwrite(outfile, img)

You can also use other techniques such as uuid4 to get unique random id for each frame, but since the name would be random it would cumbersome on some platforms to display them in sorted orders, So I think using timestamp in name would get your job done.

Upvotes: 2

Related Questions