Read all images from folder and detect faces, crop and save to new folder
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I'm trying to build a model in which it will read all the images from a given folder and detects the face, crop and save cropped face to a new folder!
Can anyone help me with code as I am receiving an error:
cv2.imshow(str(img) , img)
TypeError: mat is not a numpy array, neither a scalar
code:
import glob
import cv2
import sys
while 1 :
filename = input("Enter the file name in which images are present =")
for img in glob.glob(filename+'/*.*'):
#try :
var_img = cv2.imread(img)
cv2.imshow(str(img) , var_img)
def detect_face(img):
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
face_cascade = cv2.CascadeClassifier('opencv-files/lbpcascade_frontalface.xml')
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=5);
if (len(faces) == 0):
return None, None
(x, y, w, h) = faces[0]
return gray[y:y+w, x:x+h], faces[0]
cv2.imshow(str(img) , img)
cv2.waitKey(0)
cv2.destroyAllWindows()
python opencv image-processing mat
add a comment |
I'm trying to build a model in which it will read all the images from a given folder and detects the face, crop and save cropped face to a new folder!
Can anyone help me with code as I am receiving an error:
cv2.imshow(str(img) , img)
TypeError: mat is not a numpy array, neither a scalar
code:
import glob
import cv2
import sys
while 1 :
filename = input("Enter the file name in which images are present =")
for img in glob.glob(filename+'/*.*'):
#try :
var_img = cv2.imread(img)
cv2.imshow(str(img) , var_img)
def detect_face(img):
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
face_cascade = cv2.CascadeClassifier('opencv-files/lbpcascade_frontalface.xml')
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=5);
if (len(faces) == 0):
return None, None
(x, y, w, h) = faces[0]
return gray[y:y+w, x:x+h], faces[0]
cv2.imshow(str(img) , img)
cv2.waitKey(0)
cv2.destroyAllWindows()
python opencv image-processing mat
add a comment |
I'm trying to build a model in which it will read all the images from a given folder and detects the face, crop and save cropped face to a new folder!
Can anyone help me with code as I am receiving an error:
cv2.imshow(str(img) , img)
TypeError: mat is not a numpy array, neither a scalar
code:
import glob
import cv2
import sys
while 1 :
filename = input("Enter the file name in which images are present =")
for img in glob.glob(filename+'/*.*'):
#try :
var_img = cv2.imread(img)
cv2.imshow(str(img) , var_img)
def detect_face(img):
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
face_cascade = cv2.CascadeClassifier('opencv-files/lbpcascade_frontalface.xml')
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=5);
if (len(faces) == 0):
return None, None
(x, y, w, h) = faces[0]
return gray[y:y+w, x:x+h], faces[0]
cv2.imshow(str(img) , img)
cv2.waitKey(0)
cv2.destroyAllWindows()
python opencv image-processing mat
I'm trying to build a model in which it will read all the images from a given folder and detects the face, crop and save cropped face to a new folder!
Can anyone help me with code as I am receiving an error:
cv2.imshow(str(img) , img)
TypeError: mat is not a numpy array, neither a scalar
code:
import glob
import cv2
import sys
while 1 :
filename = input("Enter the file name in which images are present =")
for img in glob.glob(filename+'/*.*'):
#try :
var_img = cv2.imread(img)
cv2.imshow(str(img) , var_img)
def detect_face(img):
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
face_cascade = cv2.CascadeClassifier('opencv-files/lbpcascade_frontalface.xml')
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=5);
if (len(faces) == 0):
return None, None
(x, y, w, h) = faces[0]
return gray[y:y+w, x:x+h], faces[0]
cv2.imshow(str(img) , img)
cv2.waitKey(0)
cv2.destroyAllWindows()
python opencv image-processing mat
python opencv image-processing mat
edited Jan 4 at 4:36
Dale Burrell
3,47952655
3,47952655
asked Jan 3 at 10:25
Eswar kumar EshuEswar kumar Eshu
21
21
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
It looks like you're trying to imshow the filename instead of the actual array. glob.glob
returns a list of filenames so your img
that you're trying to imshow is just a string. You need to read in the image first before you imshow it. You did that in this line: var_img = cv2.imread(img)
so that means your array is var_img
. But later you tried to imshow again using just img
. You can only imshow var_img
which is an array and not img
which is a string.
add a comment |
try this
import glob
import cv2
import sys
import os
def detect_face(img):
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
face_cascade = cv2.CascadeClassifier('opencvfiles/lbpcascade_frontalface.xmlv')
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=5)
return faces
filename = input("Enter the file name in which images are present =")
for img in glob.glob(filename+'/*.*'):
var_img = cv2.imread(img)
face = detect_face(var_img)
print(face)
if (len(face) == 0):
continue
for(ex, ey, ew, eh) in face:
crop_image = var_img[ey:ey+eh, ex:ex+ew]
cv2.imshow("cropped", crop_image)
cv2.waitKey(0)
cv2.imwrite(os.path.join("outputs/",str(img)),crop_image)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54020379%2fread-all-images-from-folder-and-detect-faces-crop-and-save-to-new-folder%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
It looks like you're trying to imshow the filename instead of the actual array. glob.glob
returns a list of filenames so your img
that you're trying to imshow is just a string. You need to read in the image first before you imshow it. You did that in this line: var_img = cv2.imread(img)
so that means your array is var_img
. But later you tried to imshow again using just img
. You can only imshow var_img
which is an array and not img
which is a string.
add a comment |
It looks like you're trying to imshow the filename instead of the actual array. glob.glob
returns a list of filenames so your img
that you're trying to imshow is just a string. You need to read in the image first before you imshow it. You did that in this line: var_img = cv2.imread(img)
so that means your array is var_img
. But later you tried to imshow again using just img
. You can only imshow var_img
which is an array and not img
which is a string.
add a comment |
It looks like you're trying to imshow the filename instead of the actual array. glob.glob
returns a list of filenames so your img
that you're trying to imshow is just a string. You need to read in the image first before you imshow it. You did that in this line: var_img = cv2.imread(img)
so that means your array is var_img
. But later you tried to imshow again using just img
. You can only imshow var_img
which is an array and not img
which is a string.
It looks like you're trying to imshow the filename instead of the actual array. glob.glob
returns a list of filenames so your img
that you're trying to imshow is just a string. You need to read in the image first before you imshow it. You did that in this line: var_img = cv2.imread(img)
so that means your array is var_img
. But later you tried to imshow again using just img
. You can only imshow var_img
which is an array and not img
which is a string.
answered Jan 3 at 22:11
makaylamakayla
11
11
add a comment |
add a comment |
try this
import glob
import cv2
import sys
import os
def detect_face(img):
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
face_cascade = cv2.CascadeClassifier('opencvfiles/lbpcascade_frontalface.xmlv')
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=5)
return faces
filename = input("Enter the file name in which images are present =")
for img in glob.glob(filename+'/*.*'):
var_img = cv2.imread(img)
face = detect_face(var_img)
print(face)
if (len(face) == 0):
continue
for(ex, ey, ew, eh) in face:
crop_image = var_img[ey:ey+eh, ex:ex+ew]
cv2.imshow("cropped", crop_image)
cv2.waitKey(0)
cv2.imwrite(os.path.join("outputs/",str(img)),crop_image)
add a comment |
try this
import glob
import cv2
import sys
import os
def detect_face(img):
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
face_cascade = cv2.CascadeClassifier('opencvfiles/lbpcascade_frontalface.xmlv')
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=5)
return faces
filename = input("Enter the file name in which images are present =")
for img in glob.glob(filename+'/*.*'):
var_img = cv2.imread(img)
face = detect_face(var_img)
print(face)
if (len(face) == 0):
continue
for(ex, ey, ew, eh) in face:
crop_image = var_img[ey:ey+eh, ex:ex+ew]
cv2.imshow("cropped", crop_image)
cv2.waitKey(0)
cv2.imwrite(os.path.join("outputs/",str(img)),crop_image)
add a comment |
try this
import glob
import cv2
import sys
import os
def detect_face(img):
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
face_cascade = cv2.CascadeClassifier('opencvfiles/lbpcascade_frontalface.xmlv')
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=5)
return faces
filename = input("Enter the file name in which images are present =")
for img in glob.glob(filename+'/*.*'):
var_img = cv2.imread(img)
face = detect_face(var_img)
print(face)
if (len(face) == 0):
continue
for(ex, ey, ew, eh) in face:
crop_image = var_img[ey:ey+eh, ex:ex+ew]
cv2.imshow("cropped", crop_image)
cv2.waitKey(0)
cv2.imwrite(os.path.join("outputs/",str(img)),crop_image)
try this
import glob
import cv2
import sys
import os
def detect_face(img):
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
face_cascade = cv2.CascadeClassifier('opencvfiles/lbpcascade_frontalface.xmlv')
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=5)
return faces
filename = input("Enter the file name in which images are present =")
for img in glob.glob(filename+'/*.*'):
var_img = cv2.imread(img)
face = detect_face(var_img)
print(face)
if (len(face) == 0):
continue
for(ex, ey, ew, eh) in face:
crop_image = var_img[ey:ey+eh, ex:ex+ew]
cv2.imshow("cropped", crop_image)
cv2.waitKey(0)
cv2.imwrite(os.path.join("outputs/",str(img)),crop_image)
answered Jan 4 at 8:25
gkslsahingkslsahin
627
627
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54020379%2fread-all-images-from-folder-and-detect-faces-crop-and-save-to-new-folder%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown