0
votes

I have a bunch of images say 200 in a folder I want to crop all of them. I know the logic to loop through the images in the folder and perform cropping. I wrote a code for cropping of the single image but for multiple images, when I tried to loop through them it showing error.

This is for the single image. This is working fine

import numpy as np
import cv2
import os

count=0
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
img = cv2.imread('C:\\Users\\Sasidhar Mankala\\Desktop\\pythonproject\\DataBase creation\\Images\\85.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
    roi_gray = gray[y:y+h, x:x+w]
    roi_color = img[y:y+h+8, x:x+w+8]
    crop = img[y-8: y+h+8 , x-8: x+w+8]
    image_name = str(count)+'.png'
    image_path = os.path.join('C:\\Users\\Sasidhar Mankala\\Desktop\\pythonproject\\DataBase creation\\Cropped_Images',image_name)
    count += 1

cv2.imwrite(image_path,crop)
cv2.waitKey(0)
cv2.destroyAllWindows()

For the multiple images, I got this far but this growing error

import numpy as np
import cv2
import os

count=0
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

directory = 'C:\\Users\\Sasidhar Mankala\\Desktop\\pythonproject\\DataBase creation\\Images'

for images in directory:
    gray = cv2.cvtColor(images, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)
    for (x,y,w,h) in faces:
        # img = cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
        roi_gray = gray[y:y+h, x:x+w]
        roi_color = images[y:y+h+8, x:x+w+8]
        crop = images[y-8: y+h+8 , x-8: x+w+8]
        image_name = str(count)+'.png'
        image_path = os.path.join('C:\\Users\\Sasidhar Mankala\\Desktop\\pythonproject\\DataBase creation\\Cropped_Images',image_name)
    count += 1

cv2.imwrite(image_path,crop)
cv2.waitKey(0)
cv2.destroyAllWindows()

the error showing is

Traceback (most recent call last): File "c:\Users\Sasidhar Mankala\Desktop\pythonproject\DataBase creation\crop.py", line 11, in gray = cv2.cvtColor(images, cv2.COLOR_BGR2GRAY) TypeError: Expected Ptr<cv::UMat> for argument 'src'

I think this is happening is because of cvtColor is expecting a specific path but I don't understand how to do that.

Please help me with this. Thanks in advance

2

2 Answers

0
votes

You've to iterate on the images in your directory (feel free to substitute jpg if you have another format

import glob
for images in glob.glob(''.join([directory, r"\*.jpg"])):
    # Your code...

In your previous code you were iterating over a string

0
votes

The problem is this line for images in directory: You are not getting the path for each image.

To get the path for each image do:

import os

path_to_images = 'C:/Users/Sasidhar Mankala/Desktop/pythonproject/DataBase reation/Images/'

for imgName in os.listdir(path_to_images):
   imgPath = path_to_images + imgName

os.listdir(directory) returns a list of all the filenames in that directory

Another error is in the input to cvtColor. It takes an image input not an image path. So:

image = cv2.imread(imgPath) 
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

You were also trying to manipulate a path. Notice how in roi_color now is using the imported image and in your code it was trying to use what was supposed to be the image path (images).

   faces = face_cascade.detectMultiScale(gray, 1.3, 5)
     for (x,y,w,h) in faces:
        # img = cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
        roi_gray = gray[y:y+h, x:x+w]
        roi_color = image[y:y+h+8, x:x+w+8]
        crop = image[y-8: y+h+8 , x-8: x+w+8]
        image_name = str(count)+'.png'
        image_path = os.path.join('C:\\Users\\Sasidhar Mankala\\Desktop\\pythonproject\\DataBase creation\\Cropped_Images',image_name)
    count += 1

cv2.imwrite(image_path,crop)
cv2.waitKey(0)
cv2.destroyAllWindows()