Skip to content
Snippets Groups Projects
Commit f6416bb8 authored by Timothé Boulet's avatar Timothé Boulet :alien:
Browse files

Add video capture

parent 67953124
No related branches found
No related tags found
No related merge requests found
File added
File added
#Objective of this file is to analyse a face
def detectEmotion(face):
return "Happy?"
\ No newline at end of file
#File to process images
import cv2
import faceAnalysis as fa
def imageProcess(image):
#Objectives : detect faces, identify emotion associated on it, modify the image by framing faces and writing their emotions associated
#Import faces and eyes detectors from cv2
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades+'haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades+'haarcascade_eye.xml')
#CV2 detection is made on gray pictures
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5) #This return a list of tuple locating faces on image
#For each face, detect eyes call imageProcess to process the face and modify the image
for face in faces:
x,y,w,h = face
#Create blue rectangle around face of thickness 2
cv2.rectangle(image, (x,y), (x+w,y+h), (255,0,0), 2 )
#Select face image
face_gray = gray[y:y+h, x:x+w]
face_color = image[y:y+h, x:x+w]
#Detect eyes on the face, create green rectangle
eyes = eye_cascade.detectMultiScale(face_gray)
for (ex,ey,ew,eh) in eyes:
cv2.rectangle(face_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),1)
#Write emotion on the image
emotion = fa.detectEmotion(face_color)
cv2.putText(image, emotion, (x,y), cv2.FONT_HERSHEY_SIMPLEX, 1, (255,0,0), 2)
\ No newline at end of file
#Use your camera for processing the video. Stop by pressing Q
import cv2
import imageProcess as ip
cap = cv2.VideoCapture(0) #0 means we capture the first camera, your webcam probably
while cap.isOpened():
ret, frame = cap.read() #Read next video frame, stop if frame not well read
if not ret: break
ip.imageProcess(frame) #Process frame
cv2.imshow("Image traitée", frame) #Show processed image in a window
if cv2.waitKey(1) & 0xFF == ord('q'): #If you press Q, stop the while and so the capture
break
cap.release()
cv2.destroyAllWindows()
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment