Skip to content

added face_detection.py to web_programming #12054

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions web_programming/face_detection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import cv2
import mediapipe as mp
import time

def resize_image(img, max_width=1000, max_height=800):

Check failure on line 5 in web_programming/face_detection.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (I001)

web_programming/face_detection.py:1:1: I001 Import block is un-sorted or un-formatted
"""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

Check failure on line 16 in web_programming/face_detection.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N806)

web_programming/face_detection.py:16:13: N806 Variable `bboxC` in function should be lowercase
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

Check failure on line 33 in web_programming/face_detection.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N806)

web_programming/face_detection.py:33:5: N806 Variable `mpFaceDetection` in function should be lowercase
mpDraw = mp.solutions.drawing_utils

Check failure on line 34 in web_programming/face_detection.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N806)

web_programming/face_detection.py:34:5: N806 Variable `mpDraw` in function should be lowercase

Check failure on line 34 in web_programming/face_detection.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (F841)

web_programming/face_detection.py:34:5: F841 Local variable `mpDraw` is assigned to but never used
faceDetection = mpFaceDetection.FaceDetection(0.75)

Check failure on line 35 in web_programming/face_detection.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N806)

web_programming/face_detection.py:35:5: N806 Variable `faceDetection` in function should be lowercase

pTime = 0 # Previous time for FPS calculation

Check failure on line 37 in web_programming/face_detection.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N806)

web_programming/face_detection.py:37:5: N806 Variable `pTime` in function should be lowercase

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)

Check failure on line 49 in web_programming/face_detection.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N806)

web_programming/face_detection.py:49:9: N806 Variable `imgRGB` in function should be lowercase
result = faceDetection.process(imgRGB)

# Draw face detections
draw_face_detections(img, result.detections)

# FPS calculation
cTime = time.time()

Check failure on line 56 in web_programming/face_detection.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N806)

web_programming/face_detection.py:56:9: N806 Variable `cTime` in function should be lowercase
fps = 1 / (cTime - pTime)
pTime = cTime

Check failure on line 58 in web_programming/face_detection.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N806)

web_programming/face_detection.py:58:9: N806 Variable `pTime` in function should be lowercase

# 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)
Loading