Skip to content

Instantly share code, notes, and snippets.

@SULAIMAN-5-AHMED
Last active November 5, 2022 17:24
Show Gist options
  • Save SULAIMAN-5-AHMED/2557258e6f70ec65acce2d37d55a5725 to your computer and use it in GitHub Desktop.
Save SULAIMAN-5-AHMED/2557258e6f70ec65acce2d37d55a5725 to your computer and use it in GitHub Desktop.
import cv2
import mediapipe as mp
import time
cap = cv2.VideoCapture(0)
pTime = 0
mpFacedtect = mp.solutions.face_detection
mpDraw = mp.solutions.drawing_utils
facetect = mpFacedtect.FaceDetection()
while True:
success, img = cap.read()
imdRgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
results = facetect.process(imdRgb)
if results.detections:
for id, detection in enumerate(results.detections):
mpDraw.draw_detection(img, detection)
boundBoxClass = detection.location_data.relative_bounding_box
imgHeight, imgWidth, imgChannels = img.shape
boundBox = int(boundBoxClass.xmin * imgWidth), int(boundBoxClass.ymin * imgHeight), \
int(boundBoxClass.width * imgWidth), int(boundBoxClass.height * imgHeight)
cv2.rectangle(img, boundBox, (255, 0, 255), 3)
cv2.putText(img, f"{int(detection.score[0] * 100)}%", (boundBox[0], boundBox[1] - 20),
cv2.FONT_HERSHEY_COMPLEX, 1, (0, 255, 0), 2)
cTime = time.time()
fps = 1 / (cTime - pTime)
pTime = cTime
cv2.putText(img, f'FPS: {int(fps)}', (20, 70), cv2.FONT_HERSHEY_PLAIN, 3, (0, 0, 0), 3)
cv2.imshow('VID', img)
cv2.waitKey(21)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment