-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
76 lines (58 loc) · 2.77 KB
/
main.py
File metadata and controls
76 lines (58 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import cv2
import numpy as np
# بارگذاری کاسکاد چهره
cascade_path = "C:/Users/Atlas/Downloads/haarcascade_frontalface_default.xml"
face_cascade = cv2.CascadeClassifier(cascade_path)
# بارگذاری شکلکها (با مدیریت خطا و مسیر مطمئن)
emoji_images = {}
emoji_names = ['smile', 'angry', 'sad']
emoji_dir = "./" # مسیر دقیق پوشه شکلکها
for emoji_name in emoji_names:
emoji_path = f"{emoji_dir}/{emoji_name}.png"
emoji_image = cv2.imread(emoji_path, cv2.IMREAD_UNCHANGED) # بارگذاری با کانال آلفا
if emoji_image is None:
print(f"خطا: تصویر {emoji_path} پیدا نشد!")
exit()
emoji_images[emoji_name] = emoji_image
# متغیر برای انتخاب شکلک (میتوانید از ورودی کاربر بگیرید)
selected_emoji = "smile"
# قسمت اصلی پردازش تصویر از وبکم
cap = cv2.VideoCapture(0)
if not cap.isOpened():
print("خطا: نمیتوان وبکم را باز کرد.")
exit()
while True:
ret, frame = cap.read()
if not ret:
print("خطا: نمیتوان فریم را از وبکم خواند.")
break
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5)
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
# دریافت شکلک انتخابشده
emoji = emoji_images[selected_emoji]
# اندازه شکلک را مطابق با صورت تنظیم کنید
emoji_resized = cv2.resize(emoji, (w, h))
# بررسی وجود کانال آلفا
if emoji_resized.shape[2] == 4:
alpha_channel = emoji_resized[:, :, 3] / 255.0 # مقدار آلفا در محدوده [0,1]
background = frame[y:y + h, x:x + w]
foreground = emoji_resized[:, :, :3]
# ترکیب شکلک با تصویر اصلی
for c in range(3):
background[:, :, c] = background[:, :, c] * (1 - alpha_channel) + foreground[:, :, c] * alpha_channel
frame[y:y + h, x:x + w] = background
else:
print(f"هشدار: شکلک {selected_emoji} کانال آلفا ندارد!")
if cv2.waitKey(1) & 0xFF == ord('1'):
selected_emoji = "smile"
elif cv2.waitKey(1) & 0xFF == ord('2'):
selected_emoji = "angry"
elif cv2.waitKey(1) & 0xFF == ord('3'):
selected_emoji = "sad"
cv2.imshow('face recognition', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()