OpenCV When face not detected in live feed, exit - python

So I am trying to make a code where when face is detected from webcam it shows a green square around face. That part is done. What I want to make next is that when face is no longer detected by program that it break the loop and exit program. I tried ways through "if" or "else" or find something online but I was not going anywhere. Is there some way to do it? Here is my code:
import cv2
import os
import time
cascPath = os.path.dirname(
cv2.__file__) + "/data/haarcascade_frontalface_alt2.xml"
faceCascade = cv2.CascadeClassifier(cascPath)
video_capture = cv2.VideoCapture(0)
while True:
ret, frame = video_capture.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(60, 60),
flags=cv2.CASCADE_SCALE_IMAGE)
for (x,y,w,h) in faces:
cv2.rectangle(frame, (x, y), (x + w, y + h),(0,255,0), 2)
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
video_capture.release()
cv2.destroyAllWindows()

How about adding this? Count the number of times no face is detected; break if passes a threshold:
iter_with_no_faces=0 #put this outside the main loop
### put the follwing after updating `faces`
if len(faces) ==0:
iter_with_no_faces+=1
## add break condition as this:
if iter_with_no_faces >100:
break
you can iter_with_no_faces in the faces loop: iter_with_no_faces=0
In sum, this might work with slight modification:
import cv2
import os
import time
cascPath = os.path.dirname(
cv2.__file__) + "/data/haarcascade_frontalface_alt2.xml"
faceCascade = cv2.CascadeClassifier(cascPath)
video_capture = cv2.VideoCapture(0)
iter_with_no_faces=0 #put this outside the main loop
while True:
ret, frame = video_capture.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(60, 60),
flags=cv2.CASCADE_SCALE_IMAGE)
for (x,y,w,h) in faces:
cv2.rectangle(frame, (x, y), (x + w, y + h),(0,255,0), 2)
if len(faces) ==0:
iter_with_no_faces+=1
else:
iter_with_no_faces=0 # I assume you want to continue program when a face detected for a duration. you can omit else statement
if iter_with_no_faces >100: #set this threshold to larger or smaller number
break
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
video_capture.release()
cv2.destroyAllWindows()

import cv2
def main():
cascPath = './haarcascade_frontalface_alt2.xml' # path to the xml file - change to your path
faceCascade = cv2.CascadeClassifier(cascPath)
video_capture = cv2.VideoCapture(0)
for i in range(10 ** 10):
ret, frame = video_capture.read()
if ret:
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(60, 60),
flags=cv2.CASCADE_SCALE_IMAGE)
if len(faces) > 0:
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.imshow('Video', frame)
k = cv2.waitKey(1)
if k == ord('q'):
break
else:
print('No face detected on iter {}'.format(i))
# add here break or whatever you want to do if no face detected
video_capture.release()
cv2.destroyAllWindows()
return
if __name__ == '__main__':
main()

Related

Python Process share global Numpy Array

Hey, I want to share a global NumpyArray(cameraImg) between my main
programm and the process which runs parallel. But it doesn't work. It says
"TypeError: only size-1 arrays can be converted to Python scalars"
The main Programm displa_camera() runs in a While Loop, and gets the Image of the Webcam. The img is saved in global cameraImg and used in recognize_faces() to identifiy the face depcit in the Image. It saves the name, which is added to the Image, above the Persons head. Hope you can help me, thx
import time, os, cv2 as cv2, face_recognition
from multiprocessing import Process, Array, Value
global name
name=""
window_name = 'facerecognition'
def display_camera():
cap = cv2.VideoCapture(0)
cap.set(3, 640) # set Width
cap.set(4, 480) # set Height
cascade = cv2.CascadeClassifier('cascade.xml')
global cameraImg, name
while True:
ret, img = cap.read()
cameraImg=Array('d',img)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = cascade.detectMultiScale(
gray,
scaleFactor=1.2,
minNeighbors=5,
minSize=(20, 20)
)
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x + w, y + h), (36, 255, 12), 1)
cv2.putText(img, name, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (36, 255, 12), 2)
roi_gray = gray[y:y + h, x:x + w]
roi_color = img[y:y + h, x:x + w]
cv2.imshow('video', img)
k = cv2.waitKey(30) & 0xff
if k == 27: # press 'ESC' to quit
break
cap.release()
cv2.destroyAllWindows()
def recognize_Faces():
global cameraImg, name
folder_dir = "../images/"
image_List=[]
for images in os.listdir(folder_dir):
# check if the image ends with png or jpg or jpeg
if (images.endswith(".png") or images.endswith(".jpg") \
or images.endswith(".jpeg")):
# display
image_List.append(images)
while True:
try:
newFaces=False
for path in image_List :
rgb_img = cv2.cvtColor(cameraImg, cv2.COLOR_BGR2RGB)
img_encoding = face_recognition.face_encodings(rgb_img)
img2 = cv2.imread("../images/"+path)
rgb_img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2RGB)
img_encoding2 = face_recognition.face_encodings(rgb_img2)[0]
result = face_recognition.compare_faces(img_encoding, img_encoding2)
print("Result: ", result)
if(result[0]):
newFaces=True
name= os.path.basename("../images/"+path).split('.')[0]
except Exception as e:
print(e)
if(newFaces==False):
name=""
k = cv2.waitKey(30) & 0xff
if k == 27: # press 'ESC' to quit
break
time.sleep(3)
if __name__ == '__main__':
secondaryThread = Process(target=recognize_Faces)
secondaryThread.start()
display_camera()

I need some help it's about a face recognition python opencv module

I'm trying to do a face recognition that can detect faces and for some weird reason my code is giving me error
the error :
line 13, in <module>
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.error: OpenCV(4.5.2) C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-
m8us58q4\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in
function 'cv::cvtColor'
the code itself:
import numpy as np
import cv2
faceCascade = cv2.CascadeClassifier('Cascades/haarcascade_frontalface_default.xml')
cap = cv2.VideoCapture(0)
cap.set(3,640) # set Width
cap.set(4,480) # set Height
while True:
ret , img = cap.read()
img = cv2.flip(img,-1)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.2,
minNeighbors=5,
minSize = (20,20)
)
for (x,y,w,h) in faces:
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]
cv2.imshow('video',img)
k = cv2.waitKey(30) & 0xff
if k == 27: # pressing esc in order to quit
break
cap.release()
cap.destroyAllWindows()
I would be very happy if some of the people who may read this will send me their solution by the way I'm very sorry about my indentation I'm just kinda new to stack overflow
It seems like your VideoCapture() did not grab frames.
Try to check the script on a saved video first (VideoCapture("vid_name.mp4")).
Try disabling the cv2.flip() and see if it helps, if yes, perhaps try move this flipping to after converting the source frame to gray.
Also it is a good idea to check the value of ret in ret, frame = cap.read() by using
if not ret:
print('no frame')
break
Then if the program exits with this error message, it means there's something wrong with the camera connection.
Modifying code like this worked for me. Putting the frame part outside the loop and then continuing:
import numpy as np
import cv2
faceCascade = cv2.CascadeClassifier('Cascades/haarcascade_frontalface_default.xml')
cap = cv2.VideoCapture(0)
cap.set(3,640) # set Width
cap.set(4,480) # set Height
ret , img = cap.read()
img = cv2.flip(img,-1)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
while True:
if ret == False:
continue
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.2,
minNeighbors=5,
minSize = (20,20)
)
for (x,y,w,h) in faces:
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]
cv2.imshow('video',img)
k = cv2.waitKey(30) & 0xff
if k == 27: # pressing esc in order to quit
break
cap.release()
cap.destroyAllWindows()
With regard to the error message you posted: The issue is the the HaarCascade classifer did not properly load due to the syntax in the load command. I was able to get it to work by
making sure I hade the file: https://github.com/opencv/opencv/blob/master/data/haarcascades/haarcascade_frontalface_default.xml
placing it into a known directory and modifying the load command to match.
...
import numpy as np
import cv2
faceCascade = cv2.CascadeClassifier('c:\\Cascades\\haarcascade_frontalface_default.xml')
cap = cv2.VideoCapture(0)
cap.set(3,640) # set Width
cap.set(4,480) # set Height
ret , img = cap.read()
img = cv2.flip(img,-1)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
while True:
if ret == False:
continue
faces = faceCascade.detectMultiScale(gray,scaleFactor=1.2,minNeighbors=5,minSize = (20,20))
for (x,y,w,h) in faces:
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]
cv2.imshow('video',img)
k = cv2.waitKey(30) & 0xff
if k == 27: # pressing esc in order to quit
break
cap.release()
cap.destroyAllWindows()

<class 'cv2.CascadeClassifier'> returned a result with an error set

so, I'm trying to code face detection but its not working. this is my code:
import cv2
import sys
cascPath = sys.argv[0]
faceCascade = cv2.CascadeClassifier(cascPath)
video_capture = cv2.VideoCapture(0)
while True:
ret, frame = video_capture.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)
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
video_capture.release()
cv2.destroyAllWindows()
I'm very new to coding so I don't know if I'm missing something obvious
Change your code as follows:
faceCascade = cv2.CascadeClassifier(cv2.data.haarcascades + cascPath)
This worked for me.

Python Thread in OpenCV

Why this code do not run parallel ? Until this process is completed next functions do not start.
If i try run after this thread other functions ( webview.start() ) the window do not show until opencv not stoped
def videoLoop(video_capture, face_cascade, anterior):
while True:
if not video_capture.isOpened():
print('Unable to load camera.')
sleep(5)
pass
ret, frame = video_capture.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(30, 30)
)
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
if anterior != len(faces):
todo()
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
def faceDetect():
cascPath = "haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascPath)
log.basicConfig(filename='faces.log',level=log.INFO)
video_capture = cv2.VideoCapture(0)
anterior = 0
if not video_capture.isOpened():
print("err")
else:
thread1 = Thread(target=videoLoop(video_capture, faceCascade, anterior), daemon=True)
thread1.start()
thread1.join()

Webcam window alwasy crash while trying to detect a face

I'm trying to make a simple haar cascade program to detect a face.
faceCascade = cv2.CascadeClassifier('D:\\Python\\Python37\\Lib\\site-packages\\cv2\\data\\haarcascade_frontalcatface.xml')
body_cascade = cv2.CascadeClassifier('haarcascade_upperbody.xml')
video_capture = cv2.VideoCapture(0)
img_counter = 0
while True:
# Capture frame-by-frame
ret, frame = video_capture.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.5,
minNeighbors=5,
minSize=(30, 30),
flags=cv2.CASCADE_SCALE_IMAGE
)
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.imshow('FaceDetection', frame)
k = input()
# ESC Pressed
if k % 256 == 27:
break
video_capture.release()
cv2.destroyAllWindows()
But every time I launch it, my webcam window just froze and crash :(
My PC is powerful enough for sure, why could it happen?
I have made few changes to your code. For human face use haarcascade_frontalface_default.xml and for the cat face use haarcascade_frontalcatface.xml. Try the code below and it will work like a charm :)
import cv2
#faceCascade = cv2.CascadeClassifier('D:\\Python\\Python37\\Lib\\site-packages\\cv2\\data\\haarcascade_frontalcatface.xml')
faceCascade = cv2.CascadeClassifier('D:\\Python\\Python37\\Lib\\site-packages\\cv2\\data\\haarcascade_frontalface_default.xml')
body_cascade = cv2.CascadeClassifier('haarcascade_upperbody.xml')
video_capture = cv2.VideoCapture(0)
img_counter = 0
while True:
# Capture frame-by-frame
ret, frame = video_capture.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.5,
minNeighbors=5,
minSize=(30, 30),
flags=cv2.CASCADE_SCALE_IMAGE
)
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
cv2.imshow('FaceDetection', frame)
#k = input()
k = cv2.waitKey(1) & 0xFF
if k == 27:
break
# ESC Pressed
# if k % 256 == 27:
# break
video_capture.release()
cv2.destroyAllWindows()

Categories