This script is an extensive implementation of a camera and image manipulation toolset using OpenCV, Mediapipe, and PIL. Here’s a breakdown of its key components:
-
funcamClass:- Contains methods for camera operations, such as accessing, viewing, recording, and applying filters like grayscale or HLS on live video feeds.
-
photographClass:- Provides image capture and detection functionalities, including detecting faces and eyes in captured frames.
- Supports filters on static images and live preview with adjustable filters.
-
handDetectorClass:- Uses Mediapipe to detect hand landmarks, count fingers, and compute distances between specified points on the hand.
- Functions include finding and drawing hand landmarks on an image.
-
FacetrackerClass:- Tracks facial landmarks using Mediapipe's face mesh model, specifically for eyes and mouth tracking.
- Includes methods to detect and draw landmarks of interest like eyes and mouth, face rejection based on certain conditions, and mouse movement tracking based on facial movements.
-
photoshopClass:- Offers basic image editing functions such as adjusting RGB channels, cropping, and rotating.
- Uses PIL to handle image file operations and save the edited output.
-
Other Functionalities:
- The script also provides helper functions like
findsameandsamepin theFacetrackerclass for detecting and comparing similarities between images based on contour analysis.
- The script also provides helper functions like
To use this code:
- Instantiate the appropriate class (
funcam,photograph,handDetector,Facetracker, orphotoshop) based on the desired operation. - Call the class methods for live video, image processing, or hand/face tracking as needed.
- Exception Handling: Use try-except blocks to gracefully handle errors (e.g., camera access or image file reading).
- Modularization: Move repeated code blocks, such as filter application or error messages, to separate helper functions.
- Docstrings and Comments: Add docstrings for each class and method for better readability and understanding.
Some examples of the implementation of this module
# ----------------- Examples for funcam Class -----------------
# Example 1: Display live camera feed in grayscale
cam = funcam()
cam.graycam(stopkey="q") # Press 'q' to stop the live grayscale feed
# Example 2: Record a 5-second video with a yellow-light filter
cam.filter_record(filter="yellowlight", stopkey="q", outputname="yellowlight_output.avi")
# ----------------- Examples for photograph Class -----------------
# Example 1: Capture a photo, apply face detection, and display
photo = photograph()
frame = photo.camera()
detected_face = photo.seeface(frame)
photo.show(detected_face)
# Example 2: Apply grayscale filter and detect eyes in a photo
photo = photograph()
photo.show("sample_image.jpg", f="gray") # Show the image with grayscale filter
eyes_detected = photo.see_eye(cv.imread("sample_image.jpg"))
cv.imshow("Eye Detection", eyes_detected)
cv.waitKey(0) # Close the window by pressing any key
# ----------------- Examples for handDetector Class -----------------
# Example 1: Detect and draw hands on a live camera feed
hand_det = handDetector()
while True:
frame = cam.testacsess()
hand_det.findHands(frame, draw=True)
cv.imshow("Hand Detection", frame)
if cv.waitKey(1) & 0xFF == ord('q'):
break
# Example 2: Check which fingers are up in a live feed
hand_det = handDetector()
while True:
frame = cam.testacsess()
hand_det.findHands(frame, draw=True)
hand_positions, _ = hand_det.findPosition(frame, draw=True)
fingers = hand_det.fingersUp()
print("Fingers Up:", fingers) # List showing which fingers are up
cv.imshow("Finger Detection", frame)
if cv.waitKey(1) & 0xFF == ord('q'):
break
# ----------------- Examples for Facetracker Class -----------------
# Example 1: Track and display face mesh points
face_tracker = Facetracker()
while True:
frame = cam.testacsess()
face_frame = face_tracker.Facefinder(frame)
cv.imshow("Face Mesh", face_frame)
if cv.waitKey(1) & 0xFF == ord('q'):
break
# Example 2: Track eyes using specific landmark points
face_tracker = Facetracker()
while True:
frame = cam.testacsess()
eye_frame = face_tracker.eyeTracker(frame)
cv.imshow("Eye Tracker", eye_frame)
if cv.waitKey(1) & 0xFF == ord('q'):
break
# ----------------- Examples for photoshop Class -----------------
# Example 1: Crop an image and display
photo_editor = photoshop("sample_image.jpg")
photo_editor.cut(left=50, top=50, right=300, bottom=300, save=True, path="cropped_image.jpg")
# Example 2: Rotate an image by 45 degrees and save it
photo_editor = photoshop("sample_image.jpg")
photo_editor.routate(45, fit=True, save=True, path="rotated_image.jpg")