How can I make a delay in python without affecting video camera? - python

I want to set a delay just before my for loop gets executed. However doing so freezes my webcam.
while name != "welcome":
status, img = webcam.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = load.detectMultiScale(gray, 1.3, 5)
cv2.imshow("DEMO", img)
cv2.waitKey(10)
time.sleep(10)
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
id, conf = demo(gray[y:y + h, x:x + w])

Related

Python OpenCV Face Detection method returns NoneType object when not detecting any face

When I run this program, everything works fine if there is a detectable face in front of the camera. The second it lose the face, program crashes and gives an error like this:
Traceback (most recent call last):
File "C:\Users\oguzs\PycharmProiects\DroneDeneme\FaceTracking.py", line 77, in <module>
img, info = findFace(img)
TypeError: cannot unpack non-iterable NoneType object
Here is my face detection code:
def findFace(img):
faceCascade = cv2.CascadeClassifier("Resources/haarcascade_frontalface_default.xml")
imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(imgGray, scaleFactor=1.1, minNeighbors=3, flags=cv2.CASCADE_SCALE_IMAGE)
myFaceListC = []
myFaceListArea = []
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 2)
cx = x + w // 2
cy = y + h // 2
area = w * h
cv2.circle(img, (cx, cy), 5, (0, 255, 0), cv2.FILLED)
myFaceListC.append([cx, cy])
myFaceListArea.append(area)
if len(myFaceListArea) != 0:
i = myFaceListArea.index(max(myFaceListArea))
return img, [myFaceListC[i], myFaceListArea[i]]
else:
return img, [[0, 0], 0]
This is where i call findFace()
cap = cv2.VideoCapture(0)
while True:
_, img = cap.read()
img = cv2.resize(img, (w, h))
img, info = findFace(img)
cv2.imshow("Output", img)
cv2.waitKey(1)
I just started to learn openCV so any advice would be great :)
take a look at the code below. BTW i recommend you to use newer and better one
import numpy as np
import cv2 as cv
def findFace(img, faceCascade):
imgGray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(imgGray, scaleFactor=1.1, minNeighbors=3, flags=cv.CASCADE_SCALE_IMAGE)
myFaceListC = []
myFaceListArea = []
if len(faces) == 0:
return img, [[0, 0], 0]
for (x, y, w, h) in faces:
cv.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 2)
cx = x + w // 2
cy = y + h // 2
area = w * h
cv.circle(img, (cx, cy), 5, (0, 255, 0), cv.FILLED)
myFaceListC.append([cx, cy])
myFaceListArea.append(area)
if len(myFaceListArea) != 0:
i = myFaceListArea.index(max(myFaceListArea))
return img, [myFaceListC[i], myFaceListArea[i]]
else:
return img, [[0, 0], 0]
def main():
cascade = cv.CascadeClassifier(cv.samples.findFile("haarcascades/haarcascade_frontalface_alt.xml"))
cam = cv.VideoCapture(0)
while True:
_ret, img = cam.read()
img, info = findFace(img,cascade)
cv.imshow('facedetect', img)
if cv.waitKey(5) == 27:
break
print('Done')
if __name__ == '__main__':
main()
cv.destroyAllWindows()
The error says that findFace returned nothing.
If you look carefully, you'll see that indeed findFace may not return anything if the loop loops over an empty list. That happens when no faces were found. That situation should have been expected. Not all pictures contain faces.
Programming requires figuring out the meaning and causes of error messages. How you fix that is up to you. Perhaps check for the condition (list is empty), or simply return a default value after the loop, i.e. when there was no return issued in the loop.

Make eye tracker that only scans the upper 1/2 of my face

My original idea can be seen in the code below but it's still detecting eyes on the lower half of my face. The goal is to make it only scan the upper half of my face, therefore weeding out incorrect matches.
import cv2
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
cap = cv2.VideoCapture(0) # sets up webcam
while 1: # capture frame, converts to greyscale, looks for faces
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: # draws box around face
cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
roi_gray = gray[y:y + h, x:x + w]
roi_color = img[y:y + h, x:x + w]
half_point = y
print("half point: " + str(half_point))
eyes = eye_cascade.detectMultiScale(roi_gray) # looks for eyes
for (ex, ey, ew, eh) in eyes: # draws boxes around eyes
check_point = ey
print("check_point: " + str(check_point))
if check_point > half_point:
pass
else:
cv2.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 2)
cv2.imshow('img', img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
I only modified the line 15 and 16
cv2.rectangle(img, (x, y), (x + w, y + int(h / 2)), (255, 0, 0), 2)
roi_gray = gray[y:y + int(h / 2), x:x + w]
The full code:
import cv2
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
cap = cv2.VideoCapture(0) # sets up webcam
while 1: # capture frame, converts to greyscale, looks for faces
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: # draws box around face
cv2.rectangle(img, (x, y), (x + w, y + int(h / 2)), (255, 0, 0), 2) #Modified
roi_gray = gray[y:y + int(h / 2), x:x + w] #Modified
roi_color = img[y:y + h, x:x + w]
half_point = y
print("half point: " + str(half_point))
eyes = eye_cascade.detectMultiScale(roi_gray) # looks for eyes
for (ex, ey, ew, eh) in eyes: # draws boxes around eyes
check_point = ey
print("check_point: " + str(check_point))
if check_point > half_point:
pass
else:
cv2.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 2)
cv2.imshow('img', img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
But, I recommend to use dlib insead. It's more reliable.
Here is my example:
import numpy as np
import cv2
import dlib
cap = cv2.VideoCapture(0)
predictor = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
detector = dlib.get_frontal_face_detector()
def draw_on_frame(eye):
coordinates = np.array([])
for i in eye:
x = landmarks.part(i).x
y = landmarks.part(i).y
cv2.circle(frame, (x, y), 3, (0, 0, 255), -1)
coordinates = np.append(coordinates, [x, y])
x1, y1, w1, h1 = cv2.boundingRect(coordinates.reshape(-1, 2).astype(int))
cv2.rectangle(frame, (x1, y1), (x1 + w1, y1 + h1), (0, 255, 0), 1)
return x1, y1, w1, h1
while (cap.isOpened()):
ret, frame = cap.read()
if ret == True:
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = detector(gray)
for face in faces:
landmarks = predictor(gray, face)
left_eye = range(36, 42)
right_eye = range(42, 48)
left = draw_on_frame(left_eye)
right = draw_on_frame(right_eye)
roi_left = frame[left[1]:left[1]+left[3], left[0]:left[0]+left[2]]
roi_right = frame[right[1]:right[1] + right[3], right[0]:right[0] + right[2]]
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
cap.release()
cv2.destroyAllWindows()

Opencv+arduino(face tracking) problems

This is a face tracker made with Opencv and Arduino, but something went wrong, this code will automatically stop after running.
My code is in this url:https://pastebin.com/Zeb2FMqc
def detectjob():
while True:
ret, img = cap.read()
if ret:
cv2.namedWindow("img", cv2.WINDOW_AUTOSIZE)
cv2.resizeWindow('img', 500, 500)
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, y), (x + w, y + h), (0, 255, 0), 5)
roi_gray = gray[y:y + h, x:x + w]
roi_color = img[y:y + h, x:x + w]
arr = {y: y + h, x: x + w}
xx = int(x + (x + h)) / 2
print(xx)
yy = int(y + (y + w)) / 2
print(yy)
data = "X{0:f}Y{0:f}".format(xx, yy) + 'c'
print(data)
arduino.write(data.encode())
cv2.imshow('img', img)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
cap.release()
cv2.destroyAllWindows()

opencv TypeError: 'int' object is not iterable

I am trying to detect a face using OpenCV. I have a file recognizer.py as follows:
import cv2
faceDetect = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
cam = cv2.VideoCapture(0)
rec = cv2.face.createLBPHFaceRecognizer()
rec.load('recognizer/trainningData.yml')
id = 0
font = cv2.FONT_HERSHEY_SIMPLEX
while True:
ret, img = cam.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = faceDetect.detectMultiScale(gray, 1.3, 5)
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 2)
id, conf = rec.predict(gray[y:y + h, x:x + w])
cv2.putText(img, str(id), (x, y + h), font, 255, (255, 0, 0))
cv2.imshow("Face", img)
if cv2.waitKey(1) == ord('q'):
break
cam.release()
cv2.destroyAllWindows()
When I am trying to run this code, the program is running successfully and opening a camera window.
But whenever, I am trying to show my face in front of the camera, the program is terminating with exit code 1 and showing the following error:
Traceback (most recent call last):
File "/home/prateek/recognizer.py", line 15, in <module>
id, conf = rec.predict(gray[y:y + h, x:x + w])
TypeError: 'int' object is not iterable
Process finished with exit code 1
Means, I am getting the error on line 15 which is as follows:
id, conf = rec.predict(gray[y:y + h, x:x + w])
I don't know how to resolve this problem. I am using Python3 and OpenCV3.3.
Finally, I have got the solution. The problem with was version of opencv. This code is for opencv2.4 and I was trying to run it on opencv3.
Well, The final code for opencv3 is as following:
import cv2
faceDetect = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
cam = cv2.VideoCapture(0)
rec = cv2.face.createLBPHFaceRecognizer()
rec.load('recognizer/trainningData.yml')
id = 0
font = cv2.FONT_HERSHEY_SIMPLEX
while True:
ret, img = cam.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = faceDetect.detectMultiScale(gray, 1.3, 5)
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 2)
id= rec.predict(gray[y:y + h, x:x + w])
cv2.putText(img, str(id), (x, y + h), font, 255, (255, 0, 0))
cv2.imshow("Face", img)
if cv2.waitKey(1) == ord('q'):
break
cam.release()
cv2.destroyAllWindows()
There is no need to mention the variable conf.
rec.predict(gray[y:y + h, x:x + w]) is returning the id of the person from the database.

Capture face area by camera

I am having problems getting face detection and cropping an image to the face working, below is my code.
import cv2
class Crop:
#constructor
def __init__(self, image):
self.data = image
def facechop(self):
# read xml for training data
facedata = "haarcascade_frontalface_default.xml"
cascade = cv2.CascadeClassifier(facedata)
# read image file
img = cv2.imread(self.data, 0)
minisize = (img.shape[1], img.shape[0])
miniframe = cv2.resize(img, minisize)
faces = cascade.detectMultiScale(miniframe)
for f in faces:
x, y, w, h = [ v for v in f ]
cv2.rectangle(img, (x, y), (x + w, y + h), (255, 255, 255))
sub_face = self.data[y:y + h, x:x + w]
# Show picture
cv2.imshow('img', sub_face)
return
input image file
picture = 'izz.jpg'
pic = Crop(gambar)
pic.facechop()
# keyboard input to destroy the window
while(True):
key = cv2.waitKey(0)
if key in [27, ord('Q'), ord('q')]:
break
when it's running, it doesn't do raw after for function till sub_face = self.data[y:y + h, x:x + w]. It directly goes to cv2.imshow('img', sub_face). So, sub_face is not known. Why does it not work well?
I'm using Aptana to debug it. Thank you.
Try changing your code to as follows:
import cv2
def crop(img):
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
sub_face = img
faces = face_cascade.detectMultiScale(img, 1.1, 5)
for (x,y,w,h) in faces:
sub_face = img[y:y+h, x:x+w]
return sub_face
imageToCrop = cv2.imread('izz.jpg',0)
croppedImage = crop(imageToCrop)
cv2.imshow('img',croppedImage)
cv2.waitKey(0)
cv2.destroyAllWindows()

Categories