Google open-source MediaPipe was first introduced in June, 2019. It aims to make our life easy by providing some integrated computer vision and machine learning features. Media Pipe is a framework for building multimodal(e.g video,audio or any time series data),cross-platform (i.eAndroid,IOS,web,edge devices) applied ML pipelines. Mediapipe also facilitates the deployment of machine learning technology into demos and applications on a wide variety of different hardware platforms.
- Importing all the essential libraries
- Setting up webcam
- Initialize the face_detection class from the Mediapipe library
- Performing face detection
- Drawing the results with confidence level
import cv2
import mediapipe as mp
cap = cv2.VideoCapture()
cap.open(0, cv2.CAP_DSHOW)
It is quicker to get web cam live in Windows environment by adding cv2.CAP_DSHOW attribute.
cap = cv2.VideoCapture(0)
mp_facedetector = mp.solutions.face_detection
#mp_draw = mp.solutions.drawing_utils
If you want to draw facial key points, uncomment the line.
#mp_draw = mp.solutions.drawing_utils
with mp_facedetector.FaceDetection(min_detection_confidence=0.7) as face_detection:
while cap.isOpened():
success, image = cap.read()
results = face_detection.process(image)
if results.detections:
for id, detection in enumerate(results.detections):
#mp_draw.draw_detection(image, detection)
#print(id, detection)
bBox = detection.location_data.relative_bounding_box
h, w, c = image.shape
boundBox = int(bBox.xmin * w), int(bBox.ymin * h), int(bBox.width * w), int(bBox.height * h)
cv2.rectangle(image, boundBox, (0, 255, 0), 3)
cv2.putText(image, f'{int(detection.score[0]*100)}%', (boundBox[0], boundBox[1] - 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,255,0), 2)
cv2.imshow('Face Detection', image)
if cv2.waitKey(1) == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
If you want to draw facial key points, uncomment the line.
#mp_draw.draw_detection(image, detection)