From af4f275df6366cfd638d8a1eef632d2bcaacba0f Mon Sep 17 00:00:00 2001 From: ANEEB P Date: Mon, 14 Oct 2024 08:30:16 +0530 Subject: [PATCH 1/3] added face_detection.py to web_programming --- web_programming/face_detection.py | 76 +++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 web_programming/face_detection.py diff --git a/web_programming/face_detection.py b/web_programming/face_detection.py new file mode 100644 index 000000000000..a8f6dd0bf97e --- /dev/null +++ b/web_programming/face_detection.py @@ -0,0 +1,76 @@ +import cv2 +import mediapipe as mp +import time + +def resize_image(img, max_width=1000, max_height=800): + """Resizes the image while maintaining aspect ratio.""" + height, width, _ = img.shape + scale = min(max_width / width, max_height / height) + return cv2.resize(img, None, fx=scale, fy=scale) + +def draw_face_detections(img, detections): + """Draws bounding boxes around detected faces.""" + if detections: + for detection in detections: + # Extract bounding box information + bboxC = detection.location_data.relative_bounding_box + ih, iw, _ = img.shape + bbox = ( + int(bboxC.xmin * iw), int(bboxC.ymin * ih), + int(bboxC.width * iw), int(bboxC.height * ih) + ) + # Draw the bounding box + cv2.rectangle(img, bbox, (17, 219, 13), 2) + +def main(video_path): + # Initialize video capture + cap = cv2.VideoCapture(video_path) + if not cap.isOpened(): + print("Error: Unable to open video file") + return + + # Mediapipe Face Detection setup + mpFaceDetection = mp.solutions.face_detection + mpDraw = mp.solutions.drawing_utils + faceDetection = mpFaceDetection.FaceDetection(0.75) + + pTime = 0 # Previous time for FPS calculation + + while True: + success, img = cap.read() + if not success: + print("Error: Can't read the video frame.") + break + + # Resize image for better performance + img = resize_image(img) + + # Convert image to RGB for face detection + imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) + result = faceDetection.process(imgRGB) + + # Draw face detections + draw_face_detections(img, result.detections) + + # FPS calculation + cTime = time.time() + fps = 1 / (cTime - pTime) + pTime = cTime + + # Display FPS on the video + cv2.putText(img, f"FPS: {int(fps)}", (20, 70), cv2.FONT_HERSHEY_PLAIN, 3, (0, 255, 0), 2) + + # Show the output image + cv2.imshow("Face Detection", img) + + # Break the loop on 'q' key press + if cv2.waitKey(10) & 0xFF == ord('q'): + break + + # Release video capture and destroy all windows + cap.release() + cv2.destroyAllWindows() + +if __name__ == "__main__": + video_path = 'path/to/your/video.mp4' # Update with the actual video path + main(video_path) From a4453c99d5ff5acb0dd861ee4eb0a374295f6497 Mon Sep 17 00:00:00 2001 From: ANEEB P Date: Mon, 14 Oct 2024 10:26:15 +0530 Subject: [PATCH 2/3] added face_detection changes --- web_programming/face_detection.py | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/web_programming/face_detection.py b/web_programming/face_detection.py index a8f6dd0bf97e..76bdbd76558f 100644 --- a/web_programming/face_detection.py +++ b/web_programming/face_detection.py @@ -13,14 +13,14 @@ def draw_face_detections(img, detections): if detections: for detection in detections: # Extract bounding box information - bboxC = detection.location_data.relative_bounding_box + bbox = detection.location_data.relative_bounding_box ih, iw, _ = img.shape - bbox = ( - int(bboxC.xmin * iw), int(bboxC.ymin * ih), - int(bboxC.width * iw), int(bboxC.height * ih) + box = ( + int(bbox.xmin * iw), int(bbox.ymin * ih), + int(bbox.width * iw), int(bbox.height * ih) ) # Draw the bounding box - cv2.rectangle(img, bbox, (17, 219, 13), 2) + cv2.rectangle(img, box, (17, 219, 13), 2) def main(video_path): # Initialize video capture @@ -30,11 +30,11 @@ def main(video_path): return # Mediapipe Face Detection setup - mpFaceDetection = mp.solutions.face_detection - mpDraw = mp.solutions.drawing_utils - faceDetection = mpFaceDetection.FaceDetection(0.75) + mp_face_detection = mp.solutions.face_detection + mp_draw = mp.solutions.drawing_utils + face_detection = mp_face_detection.FaceDetection(0.75) - pTime = 0 # Previous time for FPS calculation + prev_time = 0 # Previous time for FPS calculation while True: success, img = cap.read() @@ -46,16 +46,16 @@ def main(video_path): img = resize_image(img) # Convert image to RGB for face detection - imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) - result = faceDetection.process(imgRGB) + img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) + result = face_detection.process(img_rgb) # Draw face detections draw_face_detections(img, result.detections) # FPS calculation - cTime = time.time() - fps = 1 / (cTime - pTime) - pTime = cTime + current_time = time.time() + fps = 1 / (current_time - prev_time) + prev_time = current_time # Display FPS on the video cv2.putText(img, f"FPS: {int(fps)}", (20, 70), cv2.FONT_HERSHEY_PLAIN, 3, (0, 255, 0), 2) From 45292961b1313f0cae153ab76e3d1813b329e1d0 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 14 Oct 2024 04:58:05 +0000 Subject: [PATCH 3/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- web_programming/face_detection.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/web_programming/face_detection.py b/web_programming/face_detection.py index 76bdbd76558f..c3f704b0b61c 100644 --- a/web_programming/face_detection.py +++ b/web_programming/face_detection.py @@ -2,12 +2,14 @@ import mediapipe as mp import time + def resize_image(img, max_width=1000, max_height=800): """Resizes the image while maintaining aspect ratio.""" height, width, _ = img.shape scale = min(max_width / width, max_height / height) return cv2.resize(img, None, fx=scale, fy=scale) + def draw_face_detections(img, detections): """Draws bounding boxes around detected faces.""" if detections: @@ -16,12 +18,15 @@ def draw_face_detections(img, detections): bbox = detection.location_data.relative_bounding_box ih, iw, _ = img.shape box = ( - int(bbox.xmin * iw), int(bbox.ymin * ih), - int(bbox.width * iw), int(bbox.height * ih) + int(bbox.xmin * iw), + int(bbox.ymin * ih), + int(bbox.width * iw), + int(bbox.height * ih), ) # Draw the bounding box cv2.rectangle(img, box, (17, 219, 13), 2) + def main(video_path): # Initialize video capture cap = cv2.VideoCapture(video_path) @@ -58,19 +63,22 @@ def main(video_path): prev_time = current_time # Display FPS on the video - cv2.putText(img, f"FPS: {int(fps)}", (20, 70), cv2.FONT_HERSHEY_PLAIN, 3, (0, 255, 0), 2) + cv2.putText( + img, f"FPS: {int(fps)}", (20, 70), cv2.FONT_HERSHEY_PLAIN, 3, (0, 255, 0), 2 + ) # Show the output image cv2.imshow("Face Detection", img) # Break the loop on 'q' key press - if cv2.waitKey(10) & 0xFF == ord('q'): + if cv2.waitKey(10) & 0xFF == ord("q"): break # Release video capture and destroy all windows cap.release() cv2.destroyAllWindows() + if __name__ == "__main__": - video_path = 'path/to/your/video.mp4' # Update with the actual video path + video_path = "path/to/your/video.mp4" # Update with the actual video path main(video_path)