-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
129 lines (103 loc) · 4.43 KB
/
main.py
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
from flask import Flask
from flask_cors import CORS
from threading import Thread
import cv2
import math
import time
# Flask bit for handling incoming requests / notifications...
app = Flask(__name__)
CORS(app, resources={r"/*": {"origins": "*"}})
new_participant_event = False
@app.route('/participant-joined', methods=['GET'])
def participant_joined():
global new_participant_event
new_participant_event = True
return "Participant join detected", 200
def run_flask():
app.run(port=5000, host='0.0.0.0', debug=False, use_reloader=False)
# animation params
wave_amplitude = 30 # vertical motion amplitude (pixels)
wave_frequency = 0.1 # wave speed
animation_duration = 3
def display_video():
# open the default webcam
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
if not cap.isOpened():
print("Error: Cannot access the webcam")
return
hand_image = cv2.imread('hand.png', cv2.IMREAD_UNCHANGED)
if hand_image is None:
print("Error: Cannot load the hand image")
return
hand_height, hand_width = hand_image.shape[:2]
frame_count = 0
show_animation = False
animation_start_time = None
while True:
# read frame from the webcam
ret, frame = cap.read()
if not ret:
print("Error: Unable to capture video")
break
# check if a new participant event was triggered
global new_participant_event
if new_participant_event:
show_animation = True
animation_start_time = time.time()
new_participant_event = False # reset the flag
# check for manual wave trigger (press 'w')
if cv2.waitKey(1) & 0xFF == ord('w'):
show_animation = True
animation_start_time = time.time()
# show waving...
if show_animation:
elapsed_time = time.time() - animation_start_time
if elapsed_time <= animation_duration:
wave_offset = int(wave_amplitude * math.sin(frame_count * wave_frequency))
# rotate the hand image
hand_height, hand_width = 250, 250
resized_hand = cv2.resize(hand_image, (hand_width, hand_height))
# calculate rotation angle (oscillating back and forth)
angle = 15 * math.sin(frame_count * wave_frequency) # 15 degrees max rotation
center = (hand_width // 2, hand_height // 2)
rotation_matrix = cv2.getRotationMatrix2D(center, angle, 1.0)
rotated_hand = cv2.warpAffine(resized_hand, rotation_matrix, (hand_width, hand_height), flags=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT, borderValue=(0, 0, 0, 0))
# set the hand overlay position
frame_height, frame_width, _ = frame.shape
x_offset = frame_width - hand_width - 300 + wave_offset
y_offset = frame_height - hand_height - 60 + wave_offset
# ensure the overlay doesn't exceed frame boundaries
y_end = min(y_offset + hand_height, frame_height)
x_end = min(x_offset + hand_width, frame_width)
y_offset = max(y_offset, 0)
x_offset = max(x_offset, 0)
rotated_hand = rotated_hand[:y_end - y_offset, :x_end - x_offset]
# handle transparency (alpha channel)
if rotated_hand.shape[2] == 4:
hand_rgb = rotated_hand[:, :, :3]
hand_alpha = rotated_hand[:, :, 3] / 255.0 # normalize alpha values to [0, 1]
roi = frame[y_offset:y_end, x_offset:x_end]
for c in range(3): # blend each color channel
roi[:, :, c] = (hand_alpha * hand_rgb[:, :, c] +
(1 - hand_alpha) * roi[:, :, c])
frame[y_offset:y_end, x_offset:x_end] = roi
else:
show_animation = False
cv2.imshow("Video Feed", frame)
frame_count += 1
# check for quit (press 'q')
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
# start Flask server and video display
if __name__ == "__main__":
flask_thread = Thread(target=run_flask)
flask_thread.daemon = True
flask_thread.start()
try:
display_video()
except KeyboardInterrupt:
print("Shutting down...")