IndexError using OpenCV imwrite - python

I am working on a face detection script with OpenCV.
This is my code:
# Draw a rectangle around the faces
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
if k%256 == 32:
# SPACE pressed
print(my_random_string(9)) # For example, D9E50C
img_name = "face_{}.png".format(img_counter)
cv2.imwrite(img_name, frame[[[(x, y), (x+w, y+h), (0, 255, 0), 2]]])
print("{} written!".format(img_name))
img_counter += 1
# Display the resulting frame
cv2.imshow('FaceDetection', frame)
But I'm getting the following error:
This is screenshot for the error
> c:\Users\Bakri\Desktop\FaceDetection-master\FaceDetection.py:39: FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated; use `arr[tuple(seq)]` instead of `arr[seq]`. In the future this will be interpreted as an array index, `arr[np.array(seq)]`, which will result either in an error or a different result.
cv2.imwrite(img_name, frame[[[(x, y), (x+w, y+h), (0, 255, 0), 2]]])
Traceback (most recent call last):
File "C:\Users\Bakri\AppData\Local\Programs\Python\Python36\lib\runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "c:\Users\Bakri\Desktop\FaceDetection-master\FaceDetection.py", line 39, in <module>
cv2.imwrite(img_name, frame[[[(x, y), (x+w, y+h), (0, 255, 0), 2]]])
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
Press any key to continue . . .
I searched the Stack Overflow but I can't find any relevant questions/answers.

ok here is full code that save captured images from camera
It saves all camera window (face and all body)
I want it to save only face that covered with rectangle
import uuid
import cv2
import sys
faceCascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
video_capture = cv2.VideoCapture('http://192.168.43.1:8080/video')
img_counter = 0
def my_random_string(string_length=10):
"""Returns a random string of length string_length."""
random = str(uuid.uuid4()) # Convert UUID format to a Python string.
random = random.upper() # Make all characters uppercase.
random = random.replace("-","") # Remove the UUID '-'.
return random[0:string_length] # Return the random string.
while True:
# Capture frame-by-frame
ret, frame = video_capture.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
k = cv2.waitKey(1)
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.5,
minNeighbors=5,
minSize=(30, 30),
flags=cv2.CASCADE_SCALE_IMAGE
)
# Draw a rectangle around the faces
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
if k%256 == 32:
# SPACE pressed
print(my_random_string(9)) # For example, D9E50C
img_name = "face_{}.png".format(img_counter)
#cv2.imwrite(img_name, frame)
cv2.imwrite(img_name, frame[[[(x, y), (x+w, y+h), (0, 255, 0), 2]]])
print("{} written!".format(img_name))
img_counter += 1
# Display the resulting frame
cv2.imshow('FaceDetection', frame)
if k%256 == 27: #ESC Pressed
break
elif k%256 == 32:
# SPACE pressed
print('')
#print(my_random_string(9)) # For example, D9E50C
#img_name = "facedetect_webcam_{}.png".format(img_counter)
#cv2.imwrite(img_name, frame)
#print("{} written!".format(img_name))
#img_counter += 1
# When everything is done, release the capture
video_capture.release()
cv2.destroyAllWindows()
this line save all image from camera:
cv2.imwrite(img_name, frame)
i try to save only rectangle image by this code:
cv2.imwrite(img_name, frame[[[(x, y), (x+w, y+h), (0, 255, 0), 2]]])
but am still getting that error

Related

Trying to print the center position of a square

I have tried to use cv2.putText and it appears to show the position based on the the top right of the window and not the actual center of the image. It will probably be an obvious fix since I just started using opencv
import os
import numpy as np
font = cv2.FONT_HERSHEY_SIMPLEX
org = (50, 50)
fontScale = 1
color = (255, 0, 0)
radius = 3
thickness = 2
cascPath=os.path.dirname(cv2.__file__)+"/data/haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascPath)
video_capture = cv2.VideoCapture(0)
while (True):
ret, frames = video_capture.read()
gray = cv2.cvtColor(frames, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(30, 30),
flags=cv2.CASCADE_SCALE_IMAGE
)
for (x, y, w, h) in faces:
cv2.rectangle(frames, (x, y), (x+w, y+h), (0, 255, 0), 2)
text = (x+w//2), (y+h//2)
cv2.circle(frames, (cx, cy), radius, (255, 0, 0), -1)
cv2.putText(frames, str(text), org, font, fontScale, color, thickness)
cv2.imshow('Video', frames)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
video_capture.release()
cv2.destroyAllWindows()
With the cv2.getTextSize() function, you can calculate the pixel size of the text you will put and subtract it from the text's position. In this way, the text will be right on the center.
text = (x+w//2), (y+h//2)
text_size,t = cv2.getTextSize(text=str(text), fontFace=cv2.FONT_HERSHEY_DUPLEX, fontScale=1, thickness=1)
text_size_x,text_size_y = text_size
text_pos = (x+w//2)-(text_size_x//2), (y+h//2)+(text_size_y//2)
Here is a working code
import os
import numpy as np
import cv2
font = cv2.FONT_HERSHEY_SIMPLEX
org = (50, 50)
fontScale = 1
color = (255, 0, 0)
radius = 3
thickness = 2
cascPath=os.path.dirname(cv2.__file__)+"/data/haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascPath)
video_capture = cv2.VideoCapture(0)
while (True):
ret, frames = video_capture.read()
gray = cv2.cvtColor(frames, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(30, 30),
flags=cv2.CASCADE_SCALE_IMAGE
)
for (x, y, w, h) in faces:
cv2.rectangle(frames, (x, y), (x+w, y+h), (0, 255, 0), 2)
text = (x+w//2), (y+h//2)
text_size,t = cv2.getTextSize(text=str(text), fontFace=cv2.FONT_HERSHEY_DUPLEX, fontScale=1, thickness=1)
text_size_x,text_size_y = text_size
text_pos = (x+w//2)-(text_size_x//2), (y+h//2)+(text_size_y//2)
#cv2.circle(frames, (cx, cy), radius, (255, 0, 0), -1)
cv2.putText(frames, str(text), text_pos, font, fontScale, color, thickness)
cv2.imshow('Video', frames)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
video_capture.release()
cv2.destroyAllWindows()

type error when running program

I am trying to create a program than can detect faces and predict its gender on the live stream. but I get this error message when running the program
Traceback (most recent call last):
File "text.py", line 54, in <module>
cus_ana.prediction()
File "text.py", line 31, in prediction
gender = gender_classifier()
TypeError: __call__() missing 1 required positional argument: 'inputs'
This is the code so far
class CusAnalytics():
def __init__(self, cascade, gender):
self.cascade = cascade
self.gender = gender
def gender_classifier(self):
classifier = self.gender
gender = classifier.predict(np.expand_dims(cv2.resize(cropped, (198, 198)), axis=0))
gender = np.where(gender.flatten() < 0.5, "Female", "Male")
def prediction(self):
cam = cv2.VideoCapture(0)
while True:
ret, frame = cam.read()
frame = cv2.flip(frame, 1)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = self.cascade.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=5, minSize=(150, 150))
for (x, y, w, h) in faces:
cropped = cv2.resize(frame[y:y+h, x:x+w], (198,198))
gender = gender_classifier()
if gender[0] == 'Male':
cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
cv2.rectangle(frame, (x-1, y+h), (x+w+1, y+h+50), (255, 0, 0), -1)
cv2.rectangle(frame, (x-1, y), (x+w+1, y-50), (255, 0, 0), -1)
else:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 0, 255), 2)
cv2.rectangle(frame, (x-1, y+h), (x+w+1, y+h+50), (0, 0, 255), -1)
cv2.rectangle(frame, (x-1, y), (x+w+1, y-50), (0, 0, 255), -1)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cv2.imshow('Stream', frame)
cascadePath = "../../../MODEL/TRAINED MODELS/FACE_DETECTION/haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascadePath)
gender_classifier_path = '../../../MODEL/TRAINED MODELS/GENDER_CLASSIFICATION/best_gender_classifier.h5'
gender_classifier = load_model(gender_classifier_path)
cus_ana = CusAnalytics(faceCascade, gender_classifier)
cus_ana.prediction()
The code works fine without the implementation of OOPS. I am new to object oriented programming and am trying to learn with this project.
Thank you in advance

Unable to detect face and eye with OpenCV in Python

This code is to detect face and eyes using webcam but getting this error
Traceback (most recent call last):
File "D:/Acads/7.1 Sem/BTP/FaceDetect-master/6.py", line 28, in <module>
eyes = eyeCascade.detectMultiScale(roi)
NameError: name 'roi' is not defined
but when i use this code do detect faces and eyes in a image its working properly without any error
import matplotlib
import matplotlib.pyplot as plt
import cv2
import sys
import numpy as np
import os
faceCascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eyeCascade= cv2.CascadeClassifier('haarcascade_eye.xml')
video_capture = cv2.VideoCapture(0)
while True:
# Capture frame-by-frame
ret, frame = video_capture.read()
faces = faceCascade.detectMultiScale(frame)
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
roi = frame[y:y+h, x:x+w]
eyes = eyeCascade.detectMultiScale(roi)
for (ex,ey,ew,eh) in eyes:
cv2.rectangle(roi,(ex,ey),(ex+ew,ey+eh), 255, 2)
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
video_capture.release()
cv2.destroyAllWindows()
I think it is just a problem of indentation.
roi is out of scope when you go out of the faces loop.
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
roi = frame[y:y+h, x:x+w]
# eyes detection runs for each face
eyes = eyeCascade.detectMultiScale(roi)
for (ex,ey,ew,eh) in eyes:
cv2.rectangle(roi,(ex,ey),(ex+ew,ey+eh), 255, 2)

Haar- Cascade face detection OpenCv

I used the following code to detect a face using Haar cascade classifiers provided by OpenCv Python. But the faces are not detected and the square around the face is not drawn. How to solve this?
import cv2
index=raw_input("Enter the index No. : ")
cascPath = "haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascPath)
cap = cv2.VideoCapture(0)
cont=0
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=10,
minSize=(30, 30),
flags = cv2.cv.CV_HAAR_SCALE_IMAGE
)
for (x, y, w, h) in faces:
#cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
# Display the resulting frame
cv2.imshow('frame',frame)
inpt=cv2.waitKey(1)
if inpt & 0xFF == ord('q'):
break
elif inpt & 0xFF == ord('s') :
#name='G:\XCODRA\Integrated_v_01\EigenFaceRecognizer\img2'+index+"."+(str(cont))+".png"
name='IC_image\\'+index+"."+(str(cont))+".png"
resized = cv2.resize(gray,None,fx=200, fy=200, interpolation = cv2.INTER_AREA)
img=cv2.equalizeHist(resized)
cv2.imwrite(name,img)
print cont
cont+=1
Use the full path for the classifier.

OpenCV +Python take sample automatically

I want to take some sample of face image from webcam. I want to take about 8 samples. So when i press button "s", it will take 8 samples. So i try to make looping like this:
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(30, 30),
flags=cv2.cv.CV_HAAR_SCALE_IMAGE
)
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
if cv2.waitKey(1) & 0xFF == ord('s'):
ROI = frame[y:y+h,x:x+w]
nameUser = raw_input("Input yourname: ")
for i in range(0,8):
i_string = str(i)
cv2.imwrite(nameUser+i_string+'.jpg',ROI)
time.sleep(3)
But 8 image that i got all is exactly same. There is a way to make program take sample face but not exactly same?
i think, it's just a logic problem, try to re-organize it:
recording = 0 # use it as a flag/counter
while(True):
ok, frame= cap.read();
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY);
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(30, 30),
flags=cv2.cv.CV_HAAR_SCALE_IMAGE
)
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
if cv2.waitKey(1) & 0xFF == ord('s'):
recording = 8
nameUser = raw_input("Input yourname: ")
if recording >= 0 && len(faces)>0:
ROI = frame[y:y+h,x:x+w]
i_string = str(recording)
cv2.imwrite(nameUser+i_string+'.jpg',ROI)
recording -= 1
time.sleep(3)

Categories