I use opencv to open a camera, it shows face area croping in another window at once, so i have 2 windows in the same time. So i would like to show image and get array at once. Below is my script
import numpy as np
import cv2
# load clasifier from file
face_cascade = cv2.CascadeClassifier('cascades\haarcascade_frontalface_default.xml')
img = cv2.VideoCapture(0)
while(1):
#Read images
h,f=img.read()
gray = cv2.cvtColor(f, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
# create rectangle
cv2.rectangle(f,(x,y),(x+w,y+h),(255,255,255),)
# crop face area
roi_gray = gray[y:y+h, x:x+w]
# showing crop face area
cv2.imshow('crop',roi_gray)
#create window camera which name is img
cv2.imshow('img',f)
key = cv2.waitKey(200)
if key in [27, ord('Q'), ord('q')]:
break
How to get array from cv2.imshow('crop',roi_gray)? Thank you
Related
I am trying to display a live video feed from camera. When I run the program, ret returns true but cv2.imshow() displays a placeholder image. Any help would be greatly appreciated.
import numpy as np
from cv2 import cv2
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
#initialize video from the webcam
video = cv2.VideoCapture(0)
print(cv2.VideoCapture(0).isOpened()) # ->returns True
while True:
# ret tells if the camera works properly. Frame is an actual frame from the video feed
ret, frame= video.read()
if ret ==True:
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Detect the faces
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
# Draw the rectangle around each face
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
# Display
cv2.imshow('img', frame)
if cv2.waitKey(30) & 0xff==27:
break
video.release()
cv2.destroyAllWindows()
cv2.imshow('img', frame) opens the following window.
So, I checked whether the Camera permission is allowed and it seems it already is. I am using MacOS Big Sur (version 11.6).
hi guys I want to implement (zoom in and zoom out) like digital camera to the detected faces while real-time capturing using opencv, is there is any way I can do it without just cropping the frame then display it.
here is my code ... ,,,
import cv2
# Load the cascade
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
# To capture video from webcam.
cap = cv2.VideoCapture(0)
# To use a video file as input
# cap = cv2.VideoCapture('filename.mp4')
while True:
# Read the frame
_, img = cap.read()
# Convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Detect the faces
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
# Draw the rectangle around each face
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)
# Display
cv2.imshow('img', img)
# Stop if escape key is pressed
k = cv2.waitKey(30) & 0xff
if k==27:
break
# Release the VideoCapture object
cap.release()
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
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
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()
I have a image as follows:
On this image I am running a face detection code as follows:
More details incase you want to run this code are here
Face detector:
import cv2
# Load the cascade
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
# Read the input image
img = cv2.imread('5.jpg')
# Convert into grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Detect faces
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
# Draw rectangle around the faces
for (x, y, w, h) in faces:
cv2.rectangle(img, (x-20, y-70), (x + w + 50, y + h + 50), (255, 0, 0), 2)
crop_img = img[y:y+h, x:x+w]
cv2.imwrite("cropped.jpg", crop_img)
As a output of this code image I get
What I want is:
In the output I want the entire image but with the face removed. How to achieve this result ?
Just use numpy to overwrite the pixels:
img[y1:y1+h, x1:x1+w, 0] = 0
img[y1:y1+h, x1:x1+w, 1] = 0
img[y1:y1+h, x1:x1+w, 2] = 0
note that numpy has a strange indexing system that rows are first, columns second (opposite to common indexing)
I have some code which will draw a rectangle using cv2.rectangle function based on other parameters. This is part of a face detection program. The rectangle is shown on screen within an image using imshow. However, due to the variable nature of the rectangle's dimensions, there are situations when the rectangle is not visible. After the line of code which draws the rectangle in the image, how do I detect if the rectangle is visible or not. I want this so that I can find when a face is not detected. This is my code:
import numpy as np
import cv2
# multiple cascades: https://github.com/Itseez/opencv/tree/master/data/haarcascades
# https://github.com/Itseez/opencv/blob/master/data/haarcascades/haarcascade_frontalface_default.xml
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
# https://github.com/Itseez/opencv/blob/master/data/haarcascades/haarcascade_eye.xml
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
cap = cv2.VideoCapture(0)
while 1:
ret, img = cap.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
cv2.rectangle(img,(x + (w / 4), y + (h / 4)),(x+(3 * w / 4),y + (3 * h / 4)),(255,0,0),2)
cv2.imshow('img',img)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
cap.release()
cv2.destroyAllWindows()
Thanks!
Add print len(faces) in the while loop. If its zero, then no face detected. No rectangle.