# ====================================================================
# DISCLAIMER:
# This code is provided as-is for educational and experimental
# purposes only. The author makes no representations or warranties of
# any kind concerning the safety, suitability, or accuracy of this
# code. Use at your own risk. The author assumes no liability for any
# damages, system failures, security breaches, or network issues
# resulting from the use or implementation of this script.
# ====================================================================
import cv2
import time
from picamera2 import Picamera2
import mediapipe as mp
import numpy as np
import secret
W=1280
H=720
tStart = time.time()
fps = 0
piCam = Picamera2(1)
piCam.preview_configuration.main.size = (W, H)
piCam.preview_configuration.main.format = "RGB888"
piCam.preview_configuration.controls.FrameRate = 60
piCam.preview_configuration.align()
piCam.configure("preview")
piCam.start()
cam = cv2.VideoCapture(secret.RTSP1,cv2.CAP_FFMPEG)
cam.set(cv2.CAP_PROP_BUFFERSIZE,0)
cam.set(cv2.CAP_PROP_POS_FRAMES,0)
textLowerLeft = (int(W*.01),int(H*.05))
fontFace = cv2.FONT_HERSHEY_SIMPLEX
fontThickness = int(W/425)
fontScale = H*.0015
fontColor = (0,0,255)
faceMesh = mp.solutions.face_mesh.FaceMesh(
max_num_faces=1,
refine_landmarks=True,
min_detection_confidence=0.5,
min_tracking_confidence=0.5)
cv2.namedWindow('Composite',cv2.WINDOW_GUI_NORMAL)
cv2.resizeWindow('Composite',W,H)
cv2.moveWindow('Composite',0,65)
cv2.namedWindow('meshLayer',cv2.WINDOW_GUI_NORMAL)
cv2.resizeWindow('meshLayer',320,180)
cv2.moveWindow('meshLayer',0,H+65)
cv2.namedWindow('grayLayer',cv2.WINDOW_GUI_NORMAL)
cv2.resizeWindow('grayLayer',320,180)
cv2.moveWindow('grayLayer',320,H+65)
cv2.namedWindow('maskLayer',cv2.WINDOW_GUI_NORMAL)
cv2.resizeWindow('maskLayer',320,180)
cv2.moveWindow('maskLayer',320*2,H+65)
cv2.namedWindow('invLayer',cv2.WINDOW_GUI_NORMAL)
cv2.resizeWindow('invLayer',320,180)
cv2.moveWindow('invLayer',320*3,H+65)
cv2.namedWindow('riverBG',cv2.WINDOW_GUI_NORMAL)
cv2.resizeWindow('riverBG',320,180)
cv2.moveWindow('riverBG',W,65)
cv2.namedWindow('meshFG',cv2.WINDOW_GUI_NORMAL)
cv2.resizeWindow('meshFG',320,180)
cv2.moveWindow('meshFG',W,180+65+20)
while True:
deltaT = time.time() - tStart
tStart=time.time()
fps = fps*.95 + (1/deltaT)*.05
frame = piCam.capture_array()
frame = cv2.flip(frame, -1)
_, river = cam.read()
river = cv2.resize(river,(W,H))
meshLayer = frame.copy()
meshLayer[0:H-1,0:W-1] = [255,255,255]
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
results = faceMesh.process(rgb)
if results.multi_face_landmarks:
for faceLandmarks in results.multi_face_landmarks:
# Optional second pass for glow effect
mp.solutions.drawing_utils.draw_landmarks(
image=meshLayer,
landmark_list=faceLandmarks,
connections=mp.solutions.face_mesh.FACEMESH_TESSELATION,
landmark_drawing_spec=None,
connection_drawing_spec=mp.solutions.drawing_utils.DrawingSpec(
color=(0, 150, 200),
thickness=2
)
)
# === Best looking translucent mesh ===
mp.solutions.drawing_utils.draw_landmarks(
image=meshLayer,
landmark_list=faceLandmarks,
connections=mp.solutions.face_mesh.FACEMESH_CONTOURS,
landmark_drawing_spec=None, # Hide dots
connection_drawing_spec=mp.solutions.drawing_utils.DrawingSpec(
color=(0, 0, 180), # Bright cyan/teal
thickness=7
)
)
# Optional: Extra detail on irises (makes eyes look better)
mp.solutions.drawing_utils.draw_landmarks(
image=meshLayer,
landmark_list=faceLandmarks,
connections=mp.solutions.face_mesh.FACEMESH_IRISES,
landmark_drawing_spec=None,
connection_drawing_spec=mp.solutions.drawing_utils.DrawingSpec(
color=(255, 0, 0),
thickness=7
)
)
gray = cv2.cvtColor(meshLayer,cv2.COLOR_BGR2GRAY)
_ , mask =cv2.threshold(gray, 250, 255, cv2.THRESH_BINARY)
invLayer = cv2.bitwise_not(mask)
riverBG =cv2.bitwise_and(river, river, mask = mask)
meshFG = cv2.bitwise_and(meshLayer,meshLayer, mask=invLayer)
composite = cv2.add(riverBG,meshFG)
myText = "FPS: "+str(round(fps,1))
cv2.putText(composite,myText,textLowerLeft,fontFace,fontScale,fontColor,fontThickness)
cv2.imshow("Composite", composite)
cv2.imshow("meshLayer",meshLayer)
cv2.imshow("grayLayer",gray)
cv2.imshow("maskLayer",mask)
cv2.imshow("invLayer",invLayer)
cv2.imshow("riverBG",riverBG)
cv2.imshow("meshFG",meshFG)
if cv2.waitKey(1) == ord('q'):
break
cv2.destroyAllWindows()
piCam.stop()