my print("Found faces", str(len(faces))) not executed - python

I'm trying to detect multiple faces using opencv in python. I'm doing this in raspbian OS (raspberry Pi 3). Although the code is working properly, i.e, it's detecting a face and drawing a rectangular boundary around the face. It successfully saves the image in my local folder as well. The problem is : the statement print("Found faces", str(len(faces))) isn't working and the console remains blank. What am I missing here or where am I going wrong?
import io
import picamera
import cv2
import numpy
stream = io.BytesIO()
with picamera.PiCamera() as camera:
camera.resolution = (320, 240)
camera.hflip = True
camera.capture(stream, format='jpeg')
buff = numpy.fromstring(stream.getvalue(), dtype=numpy.uint8)
image = cv2.imdecode(buff, 1)
face_cascade = cv2.CascadeClassifier('face1.xml')
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.1, 5)
print("Found faces", str(len(faces)))
for (x,y,w,h) in faces:
cv2.rectangle(image,(x,y),(x+w,y+h),(255,255,0),2)
cv2.imwrite('result.jpg',image)

Related

Save Not Recognize Faces In a Array

I am working on a "Face Recognition" project.
I have done the recognition part, my project is based on 3 code file
Face Detection (Taking samples Of Person approx 50)
Face Train (Will train the captured images)
Face Recognition (Will be able to recognize trained Images at real
time)
Now what I have to do is if an unknown person which is not in dataset detected more than 3 times in camera, the face recognition program will wait for a wile and the face detection will run which will take samples of the person and train so the next time that person is on the camera he/she will be recognizable. Here is the code For my Face Recognition
import cv2
from picamera.array import PiRGBArray
from picamera import PiCamera
import picamera
import numpy as np
import pickle
import RPi.GPIO as GPIO
from time import sleep
from subprocess import call
import time
import datetime
import boto3
from botocore.client import Config
import serial
# port = serial.Serial('/dev/ttyUSB0',9600)
now = datetime.datetime.now()
currentDate = str(now.month) + "_" + str(now.day) + "_" + str(now.year)+ "_" + str(now.hour)
cloudpath ='Videos/cctvfootage'+currentDate+'.mp4'
with open('labels', 'rb') as f:
dict= pickle.load(f)
f.close()
#setup the camera
camera =PiCamera()
camera.resolution = (600,500)
camera.framerate = 30
rawCapture = PiRGBArray(camera, size=(600, 500))
# Load prebuilt model for Frontal Face detection
faceCascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
# Create Local Binary Patterns Histograms for face recognization
recognizer = cv2.face.createLBPHFaceRecognizer()
# Load the trained mode
recognizer.load("trainer.yml")
font = cv2.FONT_HERSHEY_SIMPLEX
# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'X264')
out = cv2.VideoWriter(cloudpath, fourcc, 2.0, (camera.resolution))
lastRes=''
for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
frame = frame.array
cv2.rectangle(frame, (0, 0), (455, 30), (0,0,0), thickness=-1)
cv2.putText(frame, time.asctime(), (10, 25), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 255, 0), lineType=cv2.LINE_AA, thickness=2)
data = open(cloudpath, 'rb')
# Convert the captured frame into grayscale
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(gray, scaleFactor = 1.5, minNeighbors = 5)
# For each face in faces
for (x, y, w, h) in faces:
# Create rectangle around the face
roiGray = gray[y:y+h, x:x+w]
# Recognize the face belongs to which ID
id_, conf = recognizer.predict(roiGray)
for name, value in dict.items():
if value == id_:
break
print(name)
print(conf)
#agar confidence <=70 hoga toh door open hoga wrna nhi
# Put text describe who is in the picture
if conf <= 70:
cv2.rectangle(frame,(x-20,y-20), (x+w+20,y+h+20), (0,255,0), 4)
cv2.putText(frame,name,(x,y-40), font, 1, (255,255,255), 3)
else:
cv2.rectangle(frame,(x-20,y-20), (x+w+20,y+h+20), (0,255,0), 4)
cv2.putText(frame,"Unknown", (x,y-40), font, 1, (255,255,255),3)
cv2.imshow('frame', frame)
out.write(frame)
key = cv2.waitKey(1)
rawCapture.truncate(0)
#if cross button is pressed close the cam
if key == 27:
print("Video Saved Successfully..")
break
cv2.destroyAllWindows()
Did you try deepface? Its stream function accesses your web cam and applies real time face recognition, and facial attribute analysis (age, gender and emotion prediction) as well. You can switch web cam streaming content to video as well.
#!pip install deepface
from deepface import DeepFace
DeepFace.stream("my_db")
Here, my_db is a folder stores my facial database.

opencv how to use cascade on screen recorder

So I'm new to opencv and after practicing with some face detectors and understanding how to use the library, I created my own cascade and it's supposed to identify icons on my computer such as logos and others.
first to make sure my cascade worked I wrote one the detects the icons from the images I took,I took a screenshot and processed it through the cascade as an image and worked fine. the code for that is
import numpy as np
import cv2
img = cv2.imread('body.jpg')
face_csc = cv2.CascadeClassifier('new_cascade.xml')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_csc.detectMultiScale(gray, 1.1 , 4)
for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),3)
cv2.imshow('img',img)
cv2.waitKey(0)
after some time I wrote this for it to render my screen while detecting the icons the same way it did when I tried it on a screenshot:
import numpy as np
import cv2
from PIL import ImageGrab
fourcc = cv2.VideoWriter_fourcc(*'XVID')
face_csc = cv2.CascadeClassifier('new_cascade.xml')
out = cv2.VideoWriter("test_output.avi", fourcc, 5.0, (1366, 768))
while True:
img = ImageGrab.grab(bbox=(100, 10, 750, 750))
# convert image to numpy array
img_np = np.array(img)
# convert color space from BGR to RGB
frame = cv2.cvtColor(img_np, cv2.COLOR_BGR2RGB)
# show image on OpenCV frame
faces = face_csc.detectMultiScale(frame, 1.1 , 4)
cv2.imshow("stream", frame)
# write frame to video writer
out.write(frame)
for (x,y,w,h) in faces:
cv2.rectangle(frame,(x,y),(x+w,y+h), (255,0,0), 2)
roi_gray = frame[y:y+h, x:x+w]
roi_color = img_np[y:y+h,x:x+w]
if cv2.waitKey(1) == 27:
break
cv2.waitKey(0)
out.release()
but when running the code it doesn't show any errors but it also doesn't detect or identify any of the icons it just records my screen, I've tried debugging this for hours now to no avail, any ideas?
You should show and write the video after you draw rectangles, not before.
import numpy as np
import cv2
from PIL import ImageGrab
fourcc = cv2.VideoWriter_fourcc(*'XVID')
face_csc = cv2.CascadeClassifier('new_cascade.xml')
out = cv2.VideoWriter("test_output.avi", fourcc, 5.0, (1366, 768))
while True:
img = ImageGrab.grab(bbox=(100, 10, 750, 750))
# convert image to numpy array
img_np = np.array(img)
# convert color space from BGR to RGB
frame = cv2.cvtColor(img_np, cv2.COLOR_BGR2RGB)
# show image on OpenCV frame
faces = face_csc.detectMultiScale(frame, 1.1 , 4)
for (x,y,w,h) in faces:
cv2.rectangle(frame,(x,y),(x+w,y+h), (255,0,0), 2)
roi_gray = frame[y:y+h, x:x+w]
roi_color = img_np[y:y+h,x:x+w]
if cv2.waitKey(1) == 27:
break
cv2.imshow("stream", frame)
# write frame to video writer
out.write(frame)
cv2.waitKey(0)
out.release()

windows is not responding in face detection using python and open CV

The problem i faced is the that window opens but is not responding. I am using pycharm IDE for the development of the code. When I debugged the code i found that the statements in for loop are not executed and code recursively moves to while true condition only comes up to for loop and then again to the while true condition.
The code is as below :
import cv2
import numpy as np
face_detect = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
cam = cv2.VideoCapture(0)
recognizer=cv2.face.createLBPHFaceRecognizer()
recognizer.load("Training/trainingdataset.yml")
id=0
font=cv2.FONT_HERSHEY_SIMPLEX
while True:
ret,image = cam.read()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY);
faces=face_detect.detectMultiScale(gray,1.3,5)
for (x,y,w,h) in faces:
cv2.rectangle(image,(x,y),(x+w,y+h),(0,255,0),2)
id,conf=recognizer.predict(gray[x:x+w,y:y+h])
cv2.putText(image, str(id), (x,y+h) , font , 1 ,(0,255,0))
cv2.imshow("facedetector",image)

python face detection raspberry pi with picamera

I am a newbie with python and opencv i am trying to build a face detection project with raspberry pi. i am getting this error and here is my code
Traceback (most recent call last):
File "/home/pi/Desktop/picamera-code/FaceDetection1.0", line 19, in <module>
for frame in
camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
Code:
import numpy as np
import cv2
from picamera.array import PiRGBArray
from picamera import PiCamera
import time
camera = PiCamera()
camera.resolution = (640, 480)
camera.framerate = 32
rawCapture = PiRGBArray(camera, size=(640, 480))
time.sleep(0.1)
face_cascade = cv2.CascadeClassifier('/home/pi/Downloads/haarcascade_frontalface_default.xml')
for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
img=np.asarray(frame.array)
gray = cv2.cvtColor(img, 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 = img[y:y+h, x:x+w]
cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
The problem is in your camera.capture_continuos. First value, output, cannot be just an array as it records with an infinite iteration as the docs says. Instead of this you should put an output file. If you want an stream to capture this you can use the io.Bytes as well.
In this link it explains you examples on how tu use the frame and where should you redirect the output.
You can do something like what suggest on the API docs. Take the stream and truncate it to get the image that you are currently getting:
import io
import time
import picamera
with picamera.PiCamera() as camera:
stream = io.BytesIO()
for foo in camera.capture_continuous(stream, format='jpeg'):
# YOURS: for frame in camera.capture_continuous(stream, format="bgr", use_video_port=True):
# Truncate the stream to the current position (in case
# prior iterations output a longer image)
stream.truncate()
stream.seek(0)
if process(stream):
break
The correct answer is that you need to truncate the stream at the end of the loop. Add
rawCapture.truncate(0)
at the end of the first for loop.
if you change the part in line 11 640, 420 to 160, 120 it should work

Superimposing image over webcam feed using OpenCV 2.4.7.0 in Python 2.7

I am trying to superimpose an image over a camera feed in python. I can get an image to superimpose over another image, but when I apply the same thing to my camera feed it doesn't work. Here's my code so far:
#!/usr/bin/python
import cv2
import time
cv2.cv.NamedWindow("Hawk Eye", 1)
capture = cv2.cv.CaptureFromCAM(0)
cv2.cv.SetCaptureProperty(capture, cv2.cv.CV_CAP_PROP_FRAME_WIDTH, 800)
cv2.cv.SetCaptureProperty(capture, cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, 600)
x_offset=y_offset=50
arrows = cv2.imread("arrows.png")
while True:
webcam=cv2.cv.QueryFrame(capture)
#webcam[y_offset:y_offset+arrows.shape[0], x_offset:x_offset+arrows.shape[1]]=arrows
cv2.cv.ShowImage("Hawk Eye", webcam)
if cv2.cv.WaitKey(10) == 27:
break
cv2.cv.DestroyAllWindows()
If I uncomment:
img[y_offset:y_offset+arrows.shape[0], x_offset:x_offset+arrows.shape[1]]=arrows
the line that imposes the image, it shows just the camera feed, but when I add it in my loop it stops working. Thanks!
This works OK using the cv2 API:
import cv2
import time
cv2.namedWindow("Hawk Eye", 1)
capture = cv2.VideoCapture(0)
capture.set(cv2.cv.CV_CAP_PROP_FRAME_WIDTH, 800)
capture.set(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT, 600)
x_offset=y_offset=50
arrows = cv2.imread("hawk.png")
while True:
ret, webcam = capture.read()
if ret:
webcam[y_offset:y_offset+arrows.shape[0], x_offset:x_offset+arrows.shape[1]]=arrows
cv2.imshow("Hawk Eye", webcam)
if cv2.waitKey(10) == 27:
break
cv2.destroyAllWindows()

Categories