import cv2
import mediapipe as mp
import numpy as np
import ctypes
import os
import time
# ---------------------------------------
# CONFIGURATION
# ---------------------------------------
WALLPAPER_DIR = "wallpapers"
DETECTION_INTERVAL = 5 # seconds between wallpaper changes
MOOD_WALLPAPERS = {
"happy": "happy.jpg",
"sad": "sad.jpg",
"angry": "angry.jpg",
"neutral": "neutral.jpg"
}
# ---------------------------------------
# Windows Wallpaper Setter
# ---------------------------------------
def set_wallpaper(image_path):
ctypes.windll.user32.SystemParametersInfoW(
20, 0, image_path, 3
)
# ---------------------------------------
# Face & Landmark Setup
# ---------------------------------------
mp_face_mesh = mp.solutions.face_mesh
face_mesh = mp_face_mesh.FaceMesh(refine_landmarks=True)
mp_draw = mp.solutions.drawing_utils
# ---------------------------------------
# Simple Emotion Detection (Rule-Based)
# ---------------------------------------
def detect_emotion(landmarks):
# Mouth & eyebrow points
mouth_left = landmarks[61]
mouth_right = landmarks[291]
mouth_top = landmarks[13]
mouth_bottom = landmarks[14]
left_eyebrow = landmarks[70]
right_eyebrow = landmarks[300]
mouth_width = abs(mouth_right.x - mouth_left.x)
mouth_height = abs(mouth_bottom.y - mouth_top.y)
eyebrow_height = abs(left_eyebrow.y - right_eyebrow.y)
# Heuristic rules
if mouth_height > 0.035 and mouth_width > 0.04:
return "happy"
elif mouth_height < 0.015:
return "sad"
elif eyebrow_height < 0.01:
return "angry"
else:
return "neutral"
# ---------------------------------------
# MAIN LOOP
# ---------------------------------------
def main():
cap = cv2.VideoCapture(0)
last_change = time.time()
current_mood = None
print("🎠Mood-Based Wallpaper Switcher Started")
print("Press 'Q' to quit.\n")
while True:
ret, frame = cap.read()
if not ret:
break
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
results = face_mesh.process(rgb)
if results.multi_face_landmarks:
landmarks = results.multi_face_landmarks[0].landmark
mood = detect_emotion(landmarks)
cv2.putText(
frame,
f"Mood: {mood}",
(30, 50),
cv2.FONT_HERSHEY_SIMPLEX,
1,
(0, 255, 0),
2
)
# Change wallpaper only after interval
if mood != current_mood and time.time() - last_change > DETECTION_INTERVAL:
wallpaper_path = os.path.join(WALLPAPER_DIR, MOOD_WALLPAPERS[mood])
if os.path.exists(wallpaper_path):
set_wallpaper(os.path.abspath(wallpaper_path))
print(f"🖼️ Wallpaper changed → {mood}")
current_mood = mood
last_change = time.time()
cv2.imshow("AI Mood Detector", frame)
if cv2.waitKey(1) & 0xFF == ord("q"):
break
cap.release()
cv2.destroyAllWindows()
# ---------------------------------------
# RUN
# ---------------------------------------
if __name__ == "__main__":
main()