Python Process share global Numpy Array - python

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()

Related

OpenCV When face not detected in live feed, exit

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()

why does the haarcascades does not work on opencv

I am trying to detect faces in opencv,but I'm running in some issues:
1-When I put the following syntax:gray = cv2.cvtColor(frames,cv2.COLOR_BGR2GRAY),it shows up in red and does not work.
2-The haarcascade also shows up in red:faces = face_cascade.detectMultiScale( gray,scaleFactor=1.1,minNeighbors=5,minSize=(30, 30),flags = cv2.CV_HAAR_SCALE_IMAGE).I tried to do like in some tutorials but it does not work.Would you have any idea?
Here is my code:
#importing packages
import cv2
import numpy as np
#variables
webcam = cv2.VideoCapture(0)
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
w_size = (700,500)
#turn the webcam on
while (True):
#reading camera and turing into frames
ret,frames = webcam.read()
frames = cv2.resize(frames,w_size)
#detection
gray = cv2.cvtColor(frames,cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale( gray,scaleFactor=1.1,minNeighbors=5,minSize=(30, 30),flags = cv2.CV_HAAR_SCALE_IMAGE)
for (x, y, w, h) in faces:
cv2.rectangle(frames, (x, y), (x+w, y+h), (0, 255, 0), 2)
cv2.imshow('face_recognition',frames)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
#running script
webcam.release()
cv2.destroyAllWindows()
i simply:
added cv2.data.haarcascades as prefix of the type CascadeClassifier
deleted cv2.CV_HAAR_SCALE_IMAGE (parameter not used anymore)
Code:
import cv2
import numpy as np
import cv2.data
#variables
webcam = cv2.VideoCapture(0)
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades+'haarcascade_frontalface_default.xml')
w_size = (700,500)
#turn the webcam on
while (True):
#reading camera and turing into frames
ret,frames = webcam.read()
frames = cv2.resize(frames,w_size)
#detection
gray = cv2.cvtColor(frames,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(frames, (x, y), (x+w, y+h), (0, 255, 0), 2)
cv2.imshow('face_recognition',frames)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
#running script
webcam.release()
cv2.destroyAllWindows()

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()

OpenCv Python: How to save name of the recognized face from face recognition program after the face is recognised?

Hi everyone I'm working on OpenCV(Python)on a face recognition program. I have two files, one which captures a new user's face and stores it by the name supplied by user. The second file recognizes the user using webcam. Now, my concern is that the user is getting recognised correctly but the name is only shown and not saved. How could I save the name of the recognised person so that it can be transfered or done some operations upon?
#__author__ = 'ADMIN'
import cv2, sys, numpy, os
size = 4
fn_haar = 'haarcascade_frontalface_default.xml'
fn_dir = 'att_faces'
fn_name = "aditya"
path = os.path.join(fn_dir, fn_name)
if not os.path.isdir(path):
os.mkdir(path)
(im_width, im_height) = (112, 92)
haar_cascade = cv2.CascadeClassifier(fn_haar)
webcam = cv2.VideoCapture(0)
# The program loops until it has 20 images of the face.
count = 0
while count < 20:
(rval, im) = webcam.read()
im = cv2.flip(im, 1, 0)
gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
mini = cv2.resize(gray, (gray.shape[1] / size, gray.shape[0] / size))
faces = haar_cascade.detectMultiScale(mini)
faces = sorted(faces, key=lambda x: x[3])
if faces:
face_i = faces[0]
(x, y, w, h) = [v * size for v in face_i]
face = gray[y:y + h, x:x + w]
face_resize = cv2.resize(face, (im_width, im_height))
pin=sorted([int(n[:n.find('.')]) for n in os.listdir(path)
if n[0]!='.' ]+[0])[-1] + 1
cv2.imwrite('%s/%s.png' % (path, pin), face_resize)
cv2.rectangle(im, (x, y), (x + w, y + h), (0, 255, 0), 3)
cv2.putText(im, fn_name, (x - 10, y - 10), cv2.FONT_HERSHEY_PLAIN,
1,(0, 255, 0))
count += 1
cv2.imshow('OpenCV', im)
key = cv2.waitKey(10)
if key == 27:
break
Code for face recognition from the dataset
__author__ = 'ADMIN'
import cv2, sys, numpy, os
size = 4
fn_haar = 'haarcascade_frontalface_default.xml'
fn_dir = 'att_faces'
# Part 1: Create fisherRecognizer
print('Training...')
# Create a list of images and a list of corresponding names
(images, lables, names, id) = ([], [], {}, 0)
for (subdirs, dirs, files) in os.walk(fn_dir):
for subdir in dirs:
names[id] = subdir
subjectpath = os.path.join(fn_dir, subdir)
for filename in os.listdir(subjectpath):
path = subjectpath + '/' + filename
lable = id
images.append(cv2.imread(path, 0))
lables.append(int(lable))
id += 1
(im_width, im_height) = (112, 92)
# Create a Numpy array from the two lists above
(images, lables) = [numpy.array(lis) for lis in [images, lables]]
# OpenCV trains a model from the images
# NOTE FOR OpenCV2: remove '.face'
model = cv2.createFisherFaceRecognizer()
model.train(images, lables)
# Part 2: Use fisherRecognizer on camera stream
haar_cascade = cv2.CascadeClassifier(fn_haar)
webcam = cv2.VideoCapture(0)
while True:
(rval, frame) = webcam.read()
frame=cv2.flip(frame,1,0)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
mini = cv2.resize(gray, (gray.shape[1] / size, gray.shape[0] / size))
faces = haar_cascade.detectMultiScale(mini)
for i in range(len(faces)):
face_i = faces[i]
(x, y, w, h) = [v * size for v in face_i]
face = gray[y:y + h, x:x + w]
face_resize = cv2.resize(face, (im_width, im_height))
# Try to recognize the face
prediction = model.predict(face_resize)
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 3)
# Write the name of recognized face
# [1]
cv2.putText(frame,
'%s - %.0f' % (names[prediction[0]],prediction[1]),
(x-10, y-10), cv2.FONT_HERSHEY_PLAIN,1,(0, 255, 0))
cv2.imshow('OpenCV', frame)
key = cv2.waitKey(10)
if key == 27:
break
This is my code. where i am not using any sql-server.
I am encoding images from the folder and it will show the recognized face with the name of the image saved. if the image is saved as .. abc.jpg. then it will detect the face during live streaming and show abc.jpg
here is my code :
from PIL import Image
import face_recognition
import cv2
import os
# Get a reference to webcam #0 (the default one)
video_capture = cv2.VideoCapture(0)
known_face_encodings=[]
known_face_names = []
user_appeared = []
root = "/home/erp-next/open cv/dataset/"
for filename in os.listdir(root):
if filename.endswith('.jpg' or '.png'):
try:
print(filename)
path = os.path.join(root, filename)
filter_image = face_recognition.load_image_file(path)
filter_face_encoding = face_recognition.face_encodings(filter_image)
known_face_encodings.append(filter_face_encoding[0])
known_face_names.append(filename)
except:
print("An exception occurred : " + filename )
#print(known_face_encodings)
print(known_face_names)
# Initialize some variables
face_locations = []
face_encodings = []
face_names = []
# process_this_frame = True
def face():
while True:
process_this_frame = True
# Grab a single frame of video
ret, frame = video_capture.read()
# Resize frame of video to 1/4 size for faster face recognition processing
small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
# Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
rgb_small_frame = small_frame[:, :, ::-1]
k = cv2.waitKey(1)
if k%256 == 27:
# ESC pressed
print("Escape hit, closing...")
break
# Only process every other frame of video to save time
if process_this_frame:
# Find all the faces and face encodings in the current frame of video
face_locations = face_recognition.face_locations(rgb_small_frame)
face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)
face_names = []
for face_encoding in face_encodings:
# See if the face is a match for the known face(s)
matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
name = "Unknown"
# If a match was found in known_face_encodings, just use the first one.
if True in matches:
first_match_index = matches.index(True)
name = known_face_names[first_match_index]
print(name)
face_names.append(name)
process_this_frame = not process_this_frame
# Display the results
for (top, right, bottom, left), name in zip(face_locations, face_names):
# Scale back up face locations since the frame we detected in was scaled to 1/4 size
top *= 4
right *= 4
bottom *= 4
left *= 4
# Draw a box around the face
cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
# Draw a label with a name below the face
cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
font = cv2.FONT_HERSHEY_DUPLEX
cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)
# Display the resulting image
cv2.imshow('Video', frame)
# Hit 'q' on the keyboard to quit!
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release handle to the webcam
video_capture.release()
cv2.destroyAllWindows()
face()
i am also using face_recognition library to encode and detect face.
Thanks.

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