Jordan Sclancky
Jordan Sclancky

Reputation: 13

Is there a method to save images in subfolder created during execution in OpenCV

This is my first attempt at making a python project with OpenCV. I want to know if there is a way to store images captured by OpenCV into sub-folders.

Currently when running the ImageCapture function,it captures photos and stores it in the same folder for all users.

for (x,y,w,h) in faces:
     cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)        
     sampleNum=sampleNum+1
     cv2.imwrite("TrainingImage\ "+name +"."+Id +'.'+ str(sampleNum) + ".jpg", gray[y:y+h,x:x+w])

Name and ID are given by the user before calling the ImageCapture function.Is there any way in which it can store images into a sub folder with the name that the user has given?

Upvotes: 1

Views: 877

Answers (1)

GPrathap
GPrathap

Reputation: 7820

Creating folders are not a job of OpenCV, you can use the programming language to do it which you use.

In your case, I guess this is what you are looking for.

import os, errno
import cv2

for (x,y,w,h) in faces:
    cv2.rectangle(frame,(x,y),(x+w,y+h),(255,0,0),2)        
    sampleNum=sampleNum+1
    try:
        if not os.path.exists("TrainingImage/" + name):
            os.makedirs("TrainingImage/" + name)
        cv2.imwrite("TrainingImage/"+ name +"/" + name + "." + str(Id) +'.'+ str(sampleNum) + ".jpg", gray[y:y+h,x:x+w])
    except OSError as e:
        if e.errno != errno.EEXIST:
            raise

Upvotes: 2

Related Questions