steveyehtw commited on
Commit
c4aa613
·
1 Parent(s): 783f996

Add student project for 2026Spring專題班/胡惟媗 with bug fixes

Browse files
projects/2026Spring專題班/胡惟媗/天氣API顯示面板/main.py ADDED
@@ -0,0 +1,248 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pygame, sys, requests, math, random
2
+
3
+ pygame.init()
4
+
5
+ # ===== 全域常數與設定 =====
6
+ WIDTH, HEIGHT = 900, 650
7
+ NEON_BLUE = (0, 210, 255)
8
+ NEON_GREEN = (0, 255, 128)
9
+ NEON_ORANGE = (255, 150, 50)
10
+ NEON_PINK = (255, 80, 180)
11
+ TEXT_WHITE = (245, 245, 255)
12
+ TEXT_DIM = (160, 160, 180)
13
+
14
+ screen = pygame.display.set_mode((WIDTH, HEIGHT))
15
+ pygame.display.set_caption("數位助理儀表板 天氣版 (學生用模板)")
16
+ clock = pygame.time.Clock()
17
+
18
+ font_huge = pygame.font.SysFont("microsoftjhenghei", 76, bold=True)
19
+ font_big = pygame.font.SysFont("microsoftjhenghei", 38, bold=True)
20
+ font_mid = pygame.font.SysFont("microsoftjhenghei", 26, bold=True)
21
+ font_small = pygame.font.SysFont("microsoftjhenghei", 20)
22
+
23
+ # ===== TODO 0:客製化你的城市清單 =====
24
+ # 請隨意保留或更改你想查詢的城市名稱。
25
+ # 經緯度怎麼查?打開 Google Maps,對著你想查的地點按「滑鼠右鍵」,第一行出現的數字就是經緯度,點擊即可複製!
26
+ CITIES = {
27
+ "台北": {"lat": 25.04, "lon": 121.56},
28
+ "宜蘭": {"lat": 24.45, "lon": 121.45}, # <- 查好後,把 0.00 換成真實的緯度與經度
29
+ "台南": {"lat": 23.41, "lon": 120.25},
30
+ }
31
+
32
+ # ===== UI 繪圖輔助函式 (已經為你寫好啦!) =====
33
+ def draw_gradient_bg(surface, top_color, bottom_color):
34
+ rect = surface.get_rect()
35
+ color_surface = pygame.Surface((1, 2))
36
+ pygame.draw.line(color_surface, top_color, (0,0), (0,0))
37
+ pygame.draw.line(color_surface, bottom_color, (0,1), (0,1))
38
+ color_surface = pygame.transform.smoothscale(color_surface, (rect.width, rect.height))
39
+ surface.blit(color_surface, rect)
40
+
41
+ def draw_glass_card(surface, x, y, w, h, radius=18):
42
+ card = pygame.Surface((w, h), pygame.SRCALPHA)
43
+ pygame.draw.rect(card, (30, 35, 55, 220), (0, 0, w, h), border_radius=radius)
44
+ pygame.draw.rect(card, (255, 255, 255, 40), (0, 0, w, h), width=2, border_radius=radius)
45
+ surface.blit(card, (x, y))
46
+
47
+ def draw_glowing_circle(surface, color, center, radius):
48
+ for i in range(radius * 4, radius, -2):
49
+ alpha = int(120 * (radius / i))
50
+ glow = pygame.Surface((i*2, i*2), pygame.SRCALPHA)
51
+ pygame.draw.circle(glow, (*color, alpha), (i, i), i)
52
+ surface.blit(glow, (center[0]-i, center[1]-i))
53
+ pygame.draw.circle(surface, color, center, radius)
54
+
55
+ def draw_glowing_rect(surface, color, rect, radius):
56
+ for i in range(15, 0, -3):
57
+ alpha = int(80 * (1 - i/15))
58
+ glow = pygame.Surface((rect.width + i*2, rect.height + i*2), pygame.SRCALPHA)
59
+ pygame.draw.rect(glow, (*color, alpha), (0, 0, rect.width + i*2, rect.height + i*2), border_radius=radius+i)
60
+ surface.blit(glow, (rect.x - i, rect.y - i))
61
+
62
+ class NinjaButton:
63
+ def __init__(self, x, y, text, is_real=True):
64
+ self.w, self.h = 160, 45
65
+ self.rect = pygame.Rect(x, y, self.w, self.h)
66
+ self.text, self.is_real, self.visible, self.hovered = text, is_real, True, False
67
+
68
+ def draw(self, surface):
69
+ if not self.visible: return
70
+ color = NEON_PINK if self.hovered else (40, 50, 80)
71
+ if self.hovered: draw_glowing_rect(surface, NEON_PINK, self.rect, 12)
72
+ pygame.draw.rect(surface, color, self.rect, border_radius=12)
73
+ pygame.draw.rect(surface, TEXT_WHITE, self.rect, width=2, border_radius=12)
74
+ text_surf = font_small.render(self.text, True, TEXT_WHITE)
75
+ surface.blit(text_surf, text_surf.get_rect(center=self.rect.center))
76
+
77
+
78
+ # ===========================================
79
+ # ===== 🛠️ 以下是你的實作區域 (TODOs) 🛠️ =====
80
+ # ===========================================
81
+
82
+ def fetch_weather(city_name="台北"):
83
+ """抓取指定即時天氣"""
84
+ coords = CITIES.get(city_name, CITIES["台北"])
85
+ try:
86
+ # 【TODO 1-1】請使用 coords['lat'] 與 coords['lon'] 組合出正確的 Open-Meteo API 網址 (需加上 &timezone=Asia%2FTaipei)
87
+ url = f"https://api.open-meteo.com/v1/forecast?latitude={coords['lat']}&longitude={coords['lon']}&current_weather=true&temperature_unit=celsius&timezone=Asia/Taipei" # <- 替換為正確的字串
88
+
89
+ # 【TODO 1-2】使用 requests.get 發送請求,並設定 timeout=5
90
+ # r = ...
91
+ r = requests.get(url, timeout=5)
92
+ # 假設已經成功抓到了 (以下需要你把註解打開並完成填空)
93
+ if r.status_code == 200:
94
+ data = r.json()
95
+
96
+ # 【TODO 1-3】從 data 中安全地取出 'current_weather' 的字典
97
+ curr = data.get("current_weather",{})
98
+ print(f"curr = {curr}")
99
+
100
+ return {
101
+ "city": city_name,
102
+ # 【TODO 1-4】從 curr 字典中分別抓出 'temperature', 'windspeed', 'time' 的數值 (用 get 比較安全)
103
+ "temp": curr.get('temperature','--'), # <- 替換 ??
104
+ "wind": f"{curr.get('windspeed','--')} km/h", # <- 替換 ??
105
+ "time": curr.get("time","").replace("T", " "), # <- 替換 ??
106
+ "ok": True,
107
+ }
108
+ pass
109
+ except:
110
+ pass
111
+ return {"city": city_name, "temp": "N/A", "wind": "N/A", "time": "無法連線", "ok": False}
112
+
113
+
114
+ def fetch_ip():
115
+ """抓取目前的 IP 位址"""
116
+ try:
117
+ # 【TODO 2】使用 requests 抓取 "https://api.ipify.org?format=json" 的資料
118
+ # 並從回傳的 JSON 字典中取出 "ip" 這個欄位的字串回傳
119
+ url = "https://api.ipify.org?format=json"
120
+ r = requests.get(url, timeout=5)
121
+ if r.status_code == 200:
122
+ data = r.json()
123
+ return data.get("ip", "127.0.0.1 (Offline)")
124
+
125
+ except:
126
+ pass
127
+ return "127.0.0.1 (Offline)"
128
+
129
+
130
+ # 初始抓取(程式剛打開時的第一次呼叫)
131
+ weather = fetch_weather()
132
+ my_ip = fetch_ip()
133
+
134
+ # 跑馬燈與狀態初始化
135
+ marquee = font_mid.render("快點擊更新按鈕試試看影分身術吧!", True, NEON_ORANGE)
136
+ text_width = marquee.get_width()
137
+ scroll_x = WIDTH
138
+
139
+ main_btn = NinjaButton(340, 280, "點我更新")
140
+ ninja_buttons = [main_btn]
141
+ ninja_mode = False
142
+
143
+ # ===== 遊戲主迴圈 =====
144
+ while True:
145
+ mouse_pos = pygame.mouse.get_pos()
146
+ for btn in ninja_buttons:
147
+ btn.hovered = btn.rect.collidepoint(mouse_pos)
148
+
149
+ for event in pygame.event.get():
150
+
151
+ # 【TODO 3-1:關閉視窗】如果收到 pygame.QUIT 事件,請結束程式
152
+ if event.type == pygame.QUIT:
153
+ pygame.quit()
154
+ sys.exit()
155
+
156
+ # 【TODO 3-2:滑鼠點擊】如果收到滑鼠點擊事件 (pygame.MOUSEBUTTONDOWN) 且按鍵是左鍵 (event.button == 1)
157
+ if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
158
+ for btn in reversed(ninja_buttons):
159
+ if btn.visible and btn.hovered:
160
+ if not ninja_mode:
161
+ ninja_mode = True
162
+ ninja_buttons.clear()
163
+ positions = [(random.randint(50, WIDTH - 200), random.randint(120, HEIGHT - 150)) for _ in range(6)]
164
+ real_idx = random.randint(0, 5)
165
+ for i, pos in enumerate(positions):
166
+ ninja_buttons.append(NinjaButton(pos[0], pos[1], "點我更新", is_real=(i == real_idx)))
167
+ marquee = font_mid.render("警告:按鈕已發動影分身術!找出真正的按鈕才能更新天氣!", True, NEON_PINK)
168
+ text_width = marquee.get_width()
169
+ scroll_x = WIDTH
170
+ else:
171
+ if btn.is_real:
172
+ # 【TODO 4:打贏分身術的獎勵】玩家點到真正的按鈕了!請實作以下功能:
173
+ # 1. 用 random.choice() 與 list(CITIES.keys()) 抽出一個隨機城市,存入變數 next_city
174
+ # 2. 呼叫 fetch_weather(next_city) 重新取得天氣資料,並寫入 weather 全域變數中
175
+
176
+ next_city = random.choice(list(CITIES.keys()))
177
+ weather = fetch_weather(next_city)
178
+ ninja_mode = False
179
+ ninja_buttons = [main_btn]
180
+ main_btn.visible = True
181
+ marquee = font_mid.render(f"恭喜!成功識破影分身術,已為您切換至【{next_city}】的天氣!", True, NEON_GREEN)
182
+ text_width = marquee.get_width()
183
+ scroll_x = WIDTH
184
+ else:
185
+ btn.visible = False
186
+ break
187
+
188
+ scroll_x -= 3
189
+ if scroll_x < -text_width: scroll_x = WIDTH
190
+
191
+ draw_gradient_bg(screen, NEON_PINK,(30, 30, 60))
192
+ float_y = math.sin(pygame.time.get_ticks() / 350.0) * 6
193
+
194
+ # 標題
195
+ title = font_big.render("天氣預報", True, NEON_BLUE)
196
+ screen.blit(title, (40, 30))
197
+ pygame.draw.line(screen, (255, 255, 255, 80), (40, 80), (WIDTH - 40, 80), 2)
198
+
199
+ # 天氣面板
200
+ # 【TODO 5:繪製卡片背景】請呼叫 draw_glass_card(screen, x, y, w, h),將天氣面板畫在 x=40, y=110,寬長 480x240 的位置
201
+ draw_glass_card(screen,40, 110,480,240)
202
+ icon = font_mid.render(f"{weather.get('city', '台北')}即時天氣", True, NEON_ORANGE)
203
+ screen.blit(icon, (65, 135 + float_y))
204
+
205
+ # 【TODO 6:畫面字體渲染與蓋印】請用 font_huge 字體,將 weather['temp'] 渲染為白色 (TEXT_WHITE) 文字
206
+ # 並將產出的文字圖片繪製 (blit) 到畫面座標 (65, 185) 處
207
+ # temp_text = ...
208
+ # screen.blit(...)
209
+ temp_text = font_huge.render(f"{weather['temp']}°C", True, TEXT_WHITE)
210
+ screen.blit(temp_text, (65, 185))
211
+ wind = font_small.render(f"風速:{weather['wind']}", True, NEON_BLUE)
212
+ time_lbl = font_small.render(f"更新於:{weather['time']}", True, TEXT_DIM)
213
+ screen.blit(wind, (65, 275))
214
+ screen.blit(time_lbl, (65, 305))
215
+
216
+ # IP 面板
217
+ # 【TODO 5-2:繪製卡片背景】同樣地,把 IP 面板畫在 x=40, y=380,大小 480x120 的位置
218
+ draw_glass_card(screen, 40, 380, 480, 120)
219
+
220
+ ip_title = font_mid.render("你的網路位置 (IP)", True, NEON_PINK)
221
+ ip_text = font_big.render(my_ip, True, TEXT_WHITE)
222
+ screen.blit(ip_title, (65, 400))
223
+ screen.blit(ip_text, (65, 440))
224
+
225
+ # 狀態面板 (這張卡片先幫你寫好當參考)
226
+ draw_glass_card(screen, 540, 110, 320, 200)
227
+ sys_title = font_mid.render("系統狀態", True, TEXT_WHITE)
228
+ screen.blit(sys_title, (580, 135))
229
+
230
+ status_color = NEON_GREEN if weather["ok"] else (255, 50, 50)
231
+ draw_glowing_circle(screen, status_color, (600, 210), 4)
232
+ status_msg = "天氣 API 運作正常" if weather["ok"] else "天氣 API 離線中"
233
+ screen.blit(font_small.render(status_msg, True, TEXT_WHITE), (625, 198))
234
+
235
+ draw_glowing_circle(screen, NEON_GREEN, (600, 260), 4)
236
+ screen.blit(font_small.render("網際網路連線正常", True, TEXT_WHITE), (625, 248))
237
+
238
+ for btn in ninja_buttons:
239
+ btn.draw(screen)
240
+
241
+ # 跑馬燈
242
+ footer = pygame.Surface((WIDTH, 50), pygame.SRCALPHA)
243
+ pygame.draw.rect(footer, (0, 0, 0, 180), (0, 0, WIDTH, 50))
244
+ screen.blit(footer, (0, HEIGHT - 50))
245
+ screen.blit(marquee, (scroll_x, HEIGHT - 38))
246
+
247
+ pygame.display.flip()
248
+ clock.tick(60)
projects/2026Spring專題班/胡惟媗/手部辨識/hand_landmarker.task ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:fbc2a30080c3c557093b5ddfc334698132eb341044ccee322ccf8bcf3607cde1
3
+ size 7819105
projects/2026Spring專題班/胡惟媗/手部辨識/main.py ADDED
@@ -0,0 +1,154 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Week 10 - 魔法手勢:Pinch 偵測與手勢按鈕 (Template 填空版)
3
+ =============================================================
4
+ 本週目標:
5
+ 1. 用勾股定理計算大拇指 (4) 與食指 (8) 的距離
6
+ 2. 實作 Pinch (掐合) 偵測:距離 < Threshold 就是捏起來了
7
+ 3. 用手指碰觸 Pygame 畫面上的按鈕 + 掐合 = 魔法點擊
8
+ 4. 使用邊緣觸發 (Edge Detection) 防止重複觸發
9
+
10
+ 操作方式:
11
+ - 食指移動到按鈕上方 + 大拇指與食指捏合 = 點擊按鈕
12
+ - 顏色會隨點擊切換
13
+
14
+ 注意:
15
+ - 確保 hand_landmarker.task 檔案和這個腳本放在同一個資料夾。
16
+ """
17
+ import pygame
18
+ import cv2
19
+ import mediapipe as mp
20
+ from mediapipe.tasks import python
21
+ from mediapipe.tasks.python import vision
22
+ import math
23
+ import sys
24
+ import os
25
+
26
+ # ============================================
27
+ # 1. 初始化
28
+ # ============================================
29
+ pygame.init()
30
+ WIDTH, HEIGHT = 800, 600
31
+ screen = pygame.display.set_mode((WIDTH, HEIGHT))
32
+ pygame.display.set_caption("Week 10 - 魔法手勢按鈕 🖐️✨")
33
+ clock = pygame.time.Clock()
34
+
35
+ # 攝影機
36
+ cap = cv2.VideoCapture(0)
37
+ # 沒有攝影機就改成影片:cap = cv2.VideoCapture("backup_hand_video.mp4")
38
+
39
+ # MediaPipe (Tasks API)
40
+ base_options = python.BaseOptions(model_asset_path='hand_landmarker.task')
41
+ options = vision.HandLandmarkerOptions(base_options=base_options, num_hands=1)
42
+ detector = vision.HandLandmarker.create_from_options(options)
43
+
44
+ # ============================================
45
+ # 2. 遊戲變數
46
+ # ============================================
47
+ # 手指座標
48
+ finger_x, finger_y = 400, 300
49
+
50
+ # 【TODO 1】宣告 Pinch 相關變數
51
+ is_pinching = False
52
+ was_pinching = False # 邊緣觸發:紀錄上一幀的狀態
53
+ PINCH_THRESHOLD = 40 # 距離小於此值就算「捏起來」
54
+ detect_hand = False # 是否偵測到手了(預設沒有)
55
+ # 按鈕設定
56
+ button_rect = pygame.Rect(300, 250, 200, 100)
57
+ button_colors = [(100, 100, 220), (220, 100, 100), (100, 220, 100), (220, 180, 50)]
58
+ current_color_index = 0
59
+
60
+ # 點擊計數器
61
+ click_count = 0
62
+
63
+ # ============================================
64
+ # 3. 主迴圈
65
+ # ============================================
66
+ running = True
67
+ while running:
68
+ for event in pygame.event.get():
69
+ if event.type == pygame.QUIT:
70
+ running = False
71
+
72
+ # --- 讀取攝影機 ---
73
+ success, img = cap.read()
74
+ if success:
75
+ img = cv2.flip(img, 1)
76
+ img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
77
+
78
+ mp_image = mp.Image(image_format=mp.ImageFormat.SRGB, data=img_rgb)
79
+ results = detector.detect(mp_image)
80
+
81
+ if results.hand_landmarks:
82
+ for hand_lms in results.hand_landmarks:
83
+ detect_hand = True
84
+ # 取得食指尖 (8) 座標
85
+ lm8 = hand_lms[8]
86
+ finger_x = int(lm8.x * WIDTH)
87
+ finger_y = int(lm8.y * HEIGHT)
88
+ #
89
+ # # 【TODO 2】取得大拇指尖 (4) 座標
90
+ lm4 = hand_lms[4]
91
+ thumb_x = int(lm4.x * WIDTH)
92
+ thumb_y = int(lm4.y * HEIGHT)
93
+ #
94
+ # # 【TODO 3】用 math.hypot 計算兩指之間的距離
95
+ dist = math.hypot(finger_x - thumb_x, finger_y - thumb_y)
96
+ #
97
+ # # 【TODO 4】判斷是否掐合(距離 < PINCH_THRESHOLD)
98
+ is_pinching = dist < PINCH_THRESHOLD
99
+ else:
100
+ detect_hand = False
101
+ is_pinching = False
102
+
103
+
104
+ # 【TODO 5】邊緣觸發:只有「前一幀沒捏 + 這一幀捏了」才算一次點擊
105
+ if is_pinching and not was_pinching:
106
+ # 檢查手指是否在按鈕範圍內
107
+ finger_rect = pygame.Rect(finger_x - 5, finger_y - 5, 10, 10)
108
+ if finger_rect.colliderect(button_rect):
109
+ click_count += 1
110
+ current_color_index = (current_color_index + 1) % len(button_colors)
111
+ print(f"✨ 魔法點擊!第 {click_count} 次")
112
+
113
+ # 【TODO 6】更新 was_pinching
114
+ was_pinching = is_pinching
115
+
116
+ # --- 繪製畫面 ---
117
+ screen.fill((20, 20, 35))
118
+
119
+ # 畫按鈕
120
+ btn_color = button_colors[current_color_index]
121
+
122
+ if is_pinching:
123
+ button_rect = pygame.Rect(finger_x - 100, finger_y - 50, 200, 100)
124
+ pygame.draw.rect(screen, btn_color, button_rect, border_radius=16)
125
+
126
+ font = pygame.font.SysFont(None, 32)
127
+ btn_text = font.render(f"Clicks: {click_count}", True, (255, 255, 255))
128
+ screen.blit(btn_text, (button_rect.centerx - btn_text.get_width() // 2,
129
+ button_rect.centery - btn_text.get_height() // 2))
130
+
131
+ # 畫手指游標
132
+ # 【TODO 7】根據 is_pinching 狀態改變顏色(捏起=綠色,放開=紅色)
133
+ cursor_color = (255, 50, 50) # 預設紅色
134
+ if is_pinching:
135
+ cursor_color = (50, 255, 50) # 捏起來變綠色
136
+
137
+ pygame.draw.circle(screen, cursor_color, (finger_x, finger_y), 12)
138
+
139
+ # 狀態文字
140
+ small_font = pygame.font.SysFont(None, 24)
141
+ status = small_font.render(f"Finger: ({finger_x}, {finger_y})", True, (150, 150, 150))
142
+ screen.blit(status, (10, 10))
143
+
144
+ # 【TODO 8】顯示距離數值(dist)在畫面上
145
+ if detect_hand:
146
+ dist_text = small_font.render(f"Dist: {dist:.0f} px", True, (150, 150, 200))
147
+ screen.blit(dist_text, (10, 35))
148
+
149
+ pygame.display.update()
150
+ clock.tick(30)
151
+
152
+ cap.release()
153
+ pygame.quit()
154
+ sys.exit()
projects/2026Spring專題班/胡惟媗/臉部辨識/face_landmarker.task ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:64184e229b263107bc2b804c6625db1341ff2bb731874b0bcc2fe6544e0bc9ff
3
+ size 3758596
projects/2026Spring專題班/胡惟媗/臉部辨識/face_mesh_connections.py ADDED
@@ -0,0 +1,500 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright 2021 The MediaPipe Authors.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ """MediaPipe FaceMesh connections."""
15
+
16
+ FACEMESH_LIPS = frozenset([(61, 146), (146, 91), (91, 181), (181, 84), (84, 17),
17
+ (17, 314), (314, 405), (405, 321), (321, 375),
18
+ (375, 291), (61, 185), (185, 40), (40, 39), (39, 37),
19
+ (37, 0), (0, 267),
20
+ (267, 269), (269, 270), (270, 409), (409, 291),
21
+ (78, 95), (95, 88), (88, 178), (178, 87), (87, 14),
22
+ (14, 317), (317, 402), (402, 318), (318, 324),
23
+ (324, 308), (78, 191), (191, 80), (80, 81), (81, 82),
24
+ (82, 13), (13, 312), (312, 311), (311, 310),
25
+ (310, 415), (415, 308)])
26
+
27
+ FACEMESH_LEFT_EYE = frozenset([(263, 249), (249, 390), (390, 373), (373, 374),
28
+ (374, 380), (380, 381), (381, 382), (382, 362),
29
+ (263, 466), (466, 388), (388, 387), (387, 386),
30
+ (386, 385), (385, 384), (384, 398), (398, 362)])
31
+
32
+ FACEMESH_LEFT_IRIS = frozenset([(474, 475), (475, 476), (476, 477),
33
+ (477, 474)])
34
+
35
+ FACEMESH_LEFT_EYEBROW = frozenset([(276, 283), (283, 282), (282, 295),
36
+ (295, 285), (300, 293), (293, 334),
37
+ (334, 296), (296, 336)])
38
+
39
+ FACEMESH_RIGHT_EYE = frozenset([(33, 7), (7, 163), (163, 144), (144, 145),
40
+ (145, 153), (153, 154), (154, 155), (155, 133),
41
+ (33, 246), (246, 161), (161, 160), (160, 159),
42
+ (159, 158), (158, 157), (157, 173), (173, 133)])
43
+
44
+ FACEMESH_RIGHT_EYEBROW = frozenset([(46, 53), (53, 52), (52, 65), (65, 55),
45
+ (70, 63), (63, 105), (105, 66), (66, 107)])
46
+
47
+ FACEMESH_RIGHT_IRIS = frozenset([(469, 470), (470, 471), (471, 472),
48
+ (472, 469)])
49
+
50
+ FACEMESH_FACE_OVAL = frozenset([(10, 338), (338, 297), (297, 332), (332, 284),
51
+ (284, 251), (251, 389), (389, 356), (356, 454),
52
+ (454, 323), (323, 361), (361, 288), (288, 397),
53
+ (397, 365), (365, 379), (379, 378), (378, 400),
54
+ (400, 377), (377, 152), (152, 148), (148, 176),
55
+ (176, 149), (149, 150), (150, 136), (136, 172),
56
+ (172, 58), (58, 132), (132, 93), (93, 234),
57
+ (234, 127), (127, 162), (162, 21), (21, 54),
58
+ (54, 103), (103, 67), (67, 109), (109, 10)])
59
+
60
+ FACEMESH_NOSE = frozenset([(168, 6), (6, 197), (197, 195), (195, 5),
61
+ (5, 4), (4, 1), (1, 19), (19, 94), (94, 2), (98, 97),
62
+ (97, 2), (2, 326), (326, 327), (327, 294),
63
+ (294, 278), (278, 344), (344, 440), (440, 275),
64
+ (275, 4), (4, 45), (45, 220), (220, 115), (115, 48),
65
+ (48, 64), (64, 98)])
66
+
67
+ FACEMESH_CONTOURS = frozenset().union(*[
68
+ FACEMESH_LIPS, FACEMESH_LEFT_EYE, FACEMESH_LEFT_EYEBROW, FACEMESH_RIGHT_EYE,
69
+ FACEMESH_RIGHT_EYEBROW, FACEMESH_FACE_OVAL
70
+ ])
71
+
72
+ FACEMESH_IRISES = frozenset().union(*[FACEMESH_LEFT_IRIS, FACEMESH_RIGHT_IRIS])
73
+
74
+ FACEMESH_TESSELATION = frozenset([
75
+ (127, 34), (34, 139), (139, 127), (11, 0), (0, 37), (37, 11),
76
+ (232, 231), (231, 120), (120, 232), (72, 37), (37, 39), (39, 72),
77
+ (128, 121), (121, 47), (47, 128), (232, 121), (121, 128), (128, 232),
78
+ (104, 69), (69, 67), (67, 104), (175, 171), (171, 148), (148, 175),
79
+ (118, 50), (50, 101), (101, 118), (73, 39), (39, 40), (40, 73),
80
+ (9, 151), (151, 108), (108, 9), (48, 115), (115, 131), (131, 48),
81
+ (194, 204), (204, 211), (211, 194), (74, 40), (40, 185), (185, 74),
82
+ (80, 42), (42, 183), (183, 80), (40, 92), (92, 186), (186, 40),
83
+ (230, 229), (229, 118), (118, 230), (202, 212), (212, 214), (214, 202),
84
+ (83, 18), (18, 17), (17, 83), (76, 61), (61, 146), (146, 76),
85
+ (160, 29), (29, 30), (30, 160), (56, 157), (157, 173), (173, 56),
86
+ (106, 204), (204, 194), (194, 106), (135, 214), (214, 192), (192, 135),
87
+ (203, 165), (165, 98), (98, 203), (21, 71), (71, 68), (68, 21),
88
+ (51, 45), (45, 4), (4, 51), (144, 24), (24, 23), (23, 144),
89
+ (77, 146), (146, 91), (91, 77), (205, 50), (50, 187), (187, 205),
90
+ (201, 200), (200, 18), (18, 201), (91, 106), (106, 182), (182, 91),
91
+ (90, 91), (91, 181), (181, 90), (85, 84), (84, 17), (17, 85),
92
+ (206, 203), (203, 36), (36, 206), (148, 171), (171, 140), (140, 148),
93
+ (92, 40), (40, 39), (39, 92), (193, 189), (189, 244), (244, 193),
94
+ (159, 158), (158, 28), (28, 159), (247, 246), (246, 161), (161, 247),
95
+ (236, 3), (3, 196), (196, 236), (54, 68), (68, 104), (104, 54),
96
+ (193, 168), (168, 8), (8, 193), (117, 228), (228, 31), (31, 117),
97
+ (189, 193), (193, 55), (55, 189), (98, 97), (97, 99), (99, 98),
98
+ (126, 47), (47, 100), (100, 126), (166, 79), (79, 218), (218, 166),
99
+ (155, 154), (154, 26), (26, 155), (209, 49), (49, 131), (131, 209),
100
+ (135, 136), (136, 150), (150, 135), (47, 126), (126, 217), (217, 47),
101
+ (223, 52), (52, 53), (53, 223), (45, 51), (51, 134), (134, 45),
102
+ (211, 170), (170, 140), (140, 211), (67, 69), (69, 108), (108, 67),
103
+ (43, 106), (106, 91), (91, 43), (230, 119), (119, 120), (120, 230),
104
+ (226, 130), (130, 247), (247, 226), (63, 53), (53, 52), (52, 63),
105
+ (238, 20), (20, 242), (242, 238), (46, 70), (70, 156), (156, 46),
106
+ (78, 62), (62, 96), (96, 78), (46, 53), (53, 63), (63, 46),
107
+ (143, 34), (34, 227), (227, 143), (123, 117), (117, 111), (111, 123),
108
+ (44, 125), (125, 19), (19, 44), (236, 134), (134, 51), (51, 236),
109
+ (216, 206), (206, 205), (205, 216), (154, 153), (153, 22), (22, 154),
110
+ (39, 37), (37, 167), (167, 39), (200, 201), (201, 208), (208, 200),
111
+ (36, 142), (142, 100), (100, 36), (57, 212), (212, 202), (202, 57),
112
+ (20, 60), (60, 99), (99, 20), (28, 158), (158, 157), (157, 28),
113
+ (35, 226), (226, 113), (113, 35), (160, 159), (159, 27), (27, 160),
114
+ (204, 202), (202, 210), (210, 204), (113, 225), (225, 46), (46, 113),
115
+ (43, 202), (202, 204), (204, 43), (62, 76), (76, 77), (77, 62),
116
+ (137, 123), (123, 116), (116, 137), (41, 38), (38, 72), (72, 41),
117
+ (203, 129), (129, 142), (142, 203), (64, 98), (98, 240), (240, 64),
118
+ (49, 102), (102, 64), (64, 49), (41, 73), (73, 74), (74, 41),
119
+ (212, 216), (216, 207), (207, 212), (42, 74), (74, 184), (184, 42),
120
+ (169, 170), (170, 211), (211, 169), (170, 149), (149, 176), (176, 170),
121
+ (105, 66), (66, 69), (69, 105), (122, 6), (6, 168), (168, 122),
122
+ (123, 147), (147, 187), (187, 123), (96, 77), (77, 90), (90, 96),
123
+ (65, 55), (55, 107), (107, 65), (89, 90), (90, 180), (180, 89),
124
+ (101, 100), (100, 120), (120, 101), (63, 105), (105, 104), (104, 63),
125
+ (93, 137), (137, 227), (227, 93), (15, 86), (86, 85), (85, 15),
126
+ (129, 102), (102, 49), (49, 129), (14, 87), (87, 86), (86, 14),
127
+ (55, 8), (8, 9), (9, 55), (100, 47), (47, 121), (121, 100),
128
+ (145, 23), (23, 22), (22, 145), (88, 89), (89, 179), (179, 88),
129
+ (6, 122), (122, 196), (196, 6), (88, 95), (95, 96), (96, 88),
130
+ (138, 172), (172, 136), (136, 138), (215, 58), (58, 172), (172, 215),
131
+ (115, 48), (48, 219), (219, 115), (42, 80), (80, 81), (81, 42),
132
+ (195, 3), (3, 51), (51, 195), (43, 146), (146, 61), (61, 43),
133
+ (171, 175), (175, 199), (199, 171), (81, 82), (82, 38), (38, 81),
134
+ (53, 46), (46, 225), (225, 53), (144, 163), (163, 110), (110, 144),
135
+ (52, 65), (65, 66), (66, 52), (229, 228), (228, 117), (117, 229),
136
+ (34, 127), (127, 234), (234, 34), (107, 108), (108, 69), (69, 107),
137
+ (109, 108), (108, 151), (151, 109), (48, 64), (64, 235), (235, 48),
138
+ (62, 78), (78, 191), (191, 62), (129, 209), (209, 126), (126, 129),
139
+ (111, 35), (35, 143), (143, 111), (117, 123), (123, 50), (50, 117),
140
+ (222, 65), (65, 52), (52, 222), (19, 125), (125, 141), (141, 19),
141
+ (221, 55), (55, 65), (65, 221), (3, 195), (195, 197), (197, 3),
142
+ (25, 7), (7, 33), (33, 25), (220, 237), (237, 44), (44, 220),
143
+ (70, 71), (71, 139), (139, 70), (122, 193), (193, 245), (245, 122),
144
+ (247, 130), (130, 33), (33, 247), (71, 21), (21, 162), (162, 71),
145
+ (170, 169), (169, 150), (150, 170), (188, 174), (174, 196), (196, 188),
146
+ (216, 186), (186, 92), (92, 216), (2, 97), (97, 167), (167, 2),
147
+ (141, 125), (125, 241), (241, 141), (164, 167), (167, 37), (37, 164),
148
+ (72, 38), (38, 12), (12, 72), (38, 82), (82, 13), (13, 38),
149
+ (63, 68), (68, 71), (71, 63), (226, 35), (35, 111), (111, 226),
150
+ (101, 50), (50, 205), (205, 101), (206, 92), (92, 165), (165, 206),
151
+ (209, 198), (198, 217), (217, 209), (165, 167), (167, 97), (97, 165),
152
+ (220, 115), (115, 218), (218, 220), (133, 112), (112, 243), (243, 133),
153
+ (239, 238), (238, 241), (241, 239), (214, 135), (135, 169), (169, 214),
154
+ (190, 173), (173, 133), (133, 190), (171, 208), (208, 32), (32, 171),
155
+ (125, 44), (44, 237), (237, 125), (86, 87), (87, 178), (178, 86),
156
+ (85, 86), (86, 179), (179, 85), (84, 85), (85, 180), (180, 84),
157
+ (83, 84), (84, 181), (181, 83), (201, 83), (83, 182), (182, 201),
158
+ (137, 93), (93, 132), (132, 137), (76, 62), (62, 183), (183, 76),
159
+ (61, 76), (76, 184), (184, 61), (57, 61), (61, 185), (185, 57),
160
+ (212, 57), (57, 186), (186, 212), (214, 207), (207, 187), (187, 214),
161
+ (34, 143), (143, 156), (156, 34), (79, 239), (239, 237), (237, 79),
162
+ (123, 137), (137, 177), (177, 123), (44, 1), (1, 4), (4, 44),
163
+ (201, 194), (194, 32), (32, 201), (64, 102), (102, 129), (129, 64),
164
+ (213, 215), (215, 138), (138, 213), (59, 166), (166, 219), (219, 59),
165
+ (242, 99), (99, 97), (97, 242), (2, 94), (94, 141), (141, 2),
166
+ (75, 59), (59, 235), (235, 75), (24, 110), (110, 228), (228, 24),
167
+ (25, 130), (130, 226), (226, 25), (23, 24), (24, 229), (229, 23),
168
+ (22, 23), (23, 230), (230, 22), (26, 22), (22, 231), (231, 26),
169
+ (112, 26), (26, 232), (232, 112), (189, 190), (190, 243), (243, 189),
170
+ (221, 56), (56, 190), (190, 221), (28, 56), (56, 221), (221, 28),
171
+ (27, 28), (28, 222), (222, 27), (29, 27), (27, 223), (223, 29),
172
+ (30, 29), (29, 224), (224, 30), (247, 30), (30, 225), (225, 247),
173
+ (238, 79), (79, 20), (20, 238), (166, 59), (59, 75), (75, 166),
174
+ (60, 75), (75, 240), (240, 60), (147, 177), (177, 215), (215, 147),
175
+ (20, 79), (79, 166), (166, 20), (187, 147), (147, 213), (213, 187),
176
+ (112, 233), (233, 244), (244, 112), (233, 128), (128, 245), (245, 233),
177
+ (128, 114), (114, 188), (188, 128), (114, 217), (217, 174), (174, 114),
178
+ (131, 115), (115, 220), (220, 131), (217, 198), (198, 236), (236, 217),
179
+ (198, 131), (131, 134), (134, 198), (177, 132), (132, 58), (58, 177),
180
+ (143, 35), (35, 124), (124, 143), (110, 163), (163, 7), (7, 110),
181
+ (228, 110), (110, 25), (25, 228), (356, 389), (389, 368), (368, 356),
182
+ (11, 302), (302, 267), (267, 11), (452, 350), (350, 349), (349, 452),
183
+ (302, 303), (303, 269), (269, 302), (357, 343), (343, 277), (277, 357),
184
+ (452, 453), (453, 357), (357, 452), (333, 332), (332, 297), (297, 333),
185
+ (175, 152), (152, 377), (377, 175), (347, 348), (348, 330), (330, 347),
186
+ (303, 304), (304, 270), (270, 303), (9, 336), (336, 337), (337, 9),
187
+ (278, 279), (279, 360), (360, 278), (418, 262), (262, 431), (431, 418),
188
+ (304, 408), (408, 409), (409, 304), (310, 415), (415, 407), (407, 310),
189
+ (270, 409), (409, 410), (410, 270), (450, 348), (348, 347), (347, 450),
190
+ (422, 430), (430, 434), (434, 422), (313, 314), (314, 17), (17, 313),
191
+ (306, 307), (307, 375), (375, 306), (387, 388), (388, 260), (260, 387),
192
+ (286, 414), (414, 398), (398, 286), (335, 406), (406, 418), (418, 335),
193
+ (364, 367), (367, 416), (416, 364), (423, 358), (358, 327), (327, 423),
194
+ (251, 284), (284, 298), (298, 251), (281, 5), (5, 4), (4, 281),
195
+ (373, 374), (374, 253), (253, 373), (307, 320), (320, 321), (321, 307),
196
+ (425, 427), (427, 411), (411, 425), (421, 313), (313, 18), (18, 421),
197
+ (321, 405), (405, 406), (406, 321), (320, 404), (404, 405), (405, 320),
198
+ (315, 16), (16, 17), (17, 315), (426, 425), (425, 266), (266, 426),
199
+ (377, 400), (400, 369), (369, 377), (322, 391), (391, 269), (269, 322),
200
+ (417, 465), (465, 464), (464, 417), (386, 257), (257, 258), (258, 386),
201
+ (466, 260), (260, 388), (388, 466), (456, 399), (399, 419), (419, 456),
202
+ (284, 332), (332, 333), (333, 284), (417, 285), (285, 8), (8, 417),
203
+ (346, 340), (340, 261), (261, 346), (413, 441), (441, 285), (285, 413),
204
+ (327, 460), (460, 328), (328, 327), (355, 371), (371, 329), (329, 355),
205
+ (392, 439), (439, 438), (438, 392), (382, 341), (341, 256), (256, 382),
206
+ (429, 420), (420, 360), (360, 429), (364, 394), (394, 379), (379, 364),
207
+ (277, 343), (343, 437), (437, 277), (443, 444), (444, 283), (283, 443),
208
+ (275, 440), (440, 363), (363, 275), (431, 262), (262, 369), (369, 431),
209
+ (297, 338), (338, 337), (337, 297), (273, 375), (375, 321), (321, 273),
210
+ (450, 451), (451, 349), (349, 450), (446, 342), (342, 467), (467, 446),
211
+ (293, 334), (334, 282), (282, 293), (458, 461), (461, 462), (462, 458),
212
+ (276, 353), (353, 383), (383, 276), (308, 324), (324, 325), (325, 308),
213
+ (276, 300), (300, 293), (293, 276), (372, 345), (345, 447), (447, 372),
214
+ (352, 345), (345, 340), (340, 352), (274, 1), (1, 19), (19, 274),
215
+ (456, 248), (248, 281), (281, 456), (436, 427), (427, 425), (425, 436),
216
+ (381, 256), (256, 252), (252, 381), (269, 391), (391, 393), (393, 269),
217
+ (200, 199), (199, 428), (428, 200), (266, 330), (330, 329), (329, 266),
218
+ (287, 273), (273, 422), (422, 287), (250, 462), (462, 328), (328, 250),
219
+ (258, 286), (286, 384), (384, 258), (265, 353), (353, 342), (342, 265),
220
+ (387, 259), (259, 257), (257, 387), (424, 431), (431, 430), (430, 424),
221
+ (342, 353), (353, 276), (276, 342), (273, 335), (335, 424), (424, 273),
222
+ (292, 325), (325, 307), (307, 292), (366, 447), (447, 345), (345, 366),
223
+ (271, 303), (303, 302), (302, 271), (423, 266), (266, 371), (371, 423),
224
+ (294, 455), (455, 460), (460, 294), (279, 278), (278, 294), (294, 279),
225
+ (271, 272), (272, 304), (304, 271), (432, 434), (434, 427), (427, 432),
226
+ (272, 407), (407, 408), (408, 272), (394, 430), (430, 431), (431, 394),
227
+ (395, 369), (369, 400), (400, 395), (334, 333), (333, 299), (299, 334),
228
+ (351, 417), (417, 168), (168, 351), (352, 280), (280, 411), (411, 352),
229
+ (325, 319), (319, 320), (320, 325), (295, 296), (296, 336), (336, 295),
230
+ (319, 403), (403, 404), (404, 319), (330, 348), (348, 349), (349, 330),
231
+ (293, 298), (298, 333), (333, 293), (323, 454), (454, 447), (447, 323),
232
+ (15, 16), (16, 315), (315, 15), (358, 429), (429, 279), (279, 358),
233
+ (14, 15), (15, 316), (316, 14), (285, 336), (336, 9), (9, 285),
234
+ (329, 349), (349, 350), (350, 329), (374, 380), (380, 252), (252, 374),
235
+ (318, 402), (402, 403), (403, 318), (6, 197), (197, 419), (419, 6),
236
+ (318, 319), (319, 325), (325, 318), (367, 364), (364, 365), (365, 367),
237
+ (435, 367), (367, 397), (397, 435), (344, 438), (438, 439), (439, 344),
238
+ (272, 271), (271, 311), (311, 272), (195, 5), (5, 281), (281, 195),
239
+ (273, 287), (287, 291), (291, 273), (396, 428), (428, 199), (199, 396),
240
+ (311, 271), (271, 268), (268, 311), (283, 444), (444, 445), (445, 283),
241
+ (373, 254), (254, 339), (339, 373), (282, 334), (334, 296), (296, 282),
242
+ (449, 347), (347, 346), (346, 449), (264, 447), (447, 454), (454, 264),
243
+ (336, 296), (296, 299), (299, 336), (338, 10), (10, 151), (151, 338),
244
+ (278, 439), (439, 455), (455, 278), (292, 407), (407, 415), (415, 292),
245
+ (358, 371), (371, 355), (355, 358), (340, 345), (345, 372), (372, 340),
246
+ (346, 347), (347, 280), (280, 346), (442, 443), (443, 282), (282, 442),
247
+ (19, 94), (94, 370), (370, 19), (441, 442), (442, 295), (295, 441),
248
+ (248, 419), (419, 197), (197, 248), (263, 255), (255, 359), (359, 263),
249
+ (440, 275), (275, 274), (274, 440), (300, 383), (383, 368), (368, 300),
250
+ (351, 412), (412, 465), (465, 351), (263, 467), (467, 466), (466, 263),
251
+ (301, 368), (368, 389), (389, 301), (395, 378), (378, 379), (379, 395),
252
+ (412, 351), (351, 419), (419, 412), (436, 426), (426, 322), (322, 436),
253
+ (2, 164), (164, 393), (393, 2), (370, 462), (462, 461), (461, 370),
254
+ (164, 0), (0, 267), (267, 164), (302, 11), (11, 12), (12, 302),
255
+ (268, 12), (12, 13), (13, 268), (293, 300), (300, 301), (301, 293),
256
+ (446, 261), (261, 340), (340, 446), (330, 266), (266, 425), (425, 330),
257
+ (426, 423), (423, 391), (391, 426), (429, 355), (355, 437), (437, 429),
258
+ (391, 327), (327, 326), (326, 391), (440, 457), (457, 438), (438, 440),
259
+ (341, 382), (382, 362), (362, 341), (459, 457), (457, 461), (461, 459),
260
+ (434, 430), (430, 394), (394, 434), (414, 463), (463, 362), (362, 414),
261
+ (396, 369), (369, 262), (262, 396), (354, 461), (461, 457), (457, 354),
262
+ (316, 403), (403, 402), (402, 316), (315, 404), (404, 403), (403, 315),
263
+ (314, 405), (405, 404), (404, 314), (313, 406), (406, 405), (405, 313),
264
+ (421, 418), (418, 406), (406, 421), (366, 401), (401, 361), (361, 366),
265
+ (306, 408), (408, 407), (407, 306), (291, 409), (409, 408), (408, 291),
266
+ (287, 410), (410, 409), (409, 287), (432, 436), (436, 410), (410, 432),
267
+ (434, 416), (416, 411), (411, 434), (264, 368), (368, 383), (383, 264),
268
+ (309, 438), (438, 457), (457, 309), (352, 376), (376, 401), (401, 352),
269
+ (274, 275), (275, 4), (4, 274), (421, 428), (428, 262), (262, 421),
270
+ (294, 327), (327, 358), (358, 294), (433, 416), (416, 367), (367, 433),
271
+ (289, 455), (455, 439), (439, 289), (462, 370), (370, 326), (326, 462),
272
+ (2, 326), (326, 370), (370, 2), (305, 460), (460, 455), (455, 305),
273
+ (254, 449), (449, 448), (448, 254), (255, 261), (261, 446), (446, 255),
274
+ (253, 450), (450, 449), (449, 253), (252, 451), (451, 450), (450, 252),
275
+ (256, 452), (452, 451), (451, 256), (341, 453), (453, 452), (452, 341),
276
+ (413, 464), (464, 463), (463, 413), (441, 413), (413, 414), (414, 441),
277
+ (258, 442), (442, 441), (441, 258), (257, 443), (443, 442), (442, 257),
278
+ (259, 444), (444, 443), (443, 259), (260, 445), (445, 444), (444, 260),
279
+ (467, 342), (342, 445), (445, 467), (459, 458), (458, 250), (250, 459),
280
+ (289, 392), (392, 290), (290, 289), (290, 328), (328, 460), (460, 290),
281
+ (376, 433), (433, 435), (435, 376), (250, 290), (290, 392), (392, 250),
282
+ (411, 416), (416, 433), (433, 411), (341, 463), (463, 464), (464, 341),
283
+ (453, 464), (464, 465), (465, 453), (357, 465), (465, 412), (412, 357),
284
+ (343, 412), (412, 399), (399, 343), (360, 363), (363, 440), (440, 360),
285
+ (437, 399), (399, 456), (456, 437), (420, 456), (456, 363), (363, 420),
286
+ (401, 435), (435, 288), (288, 401), (372, 383), (383, 353), (353, 372),
287
+ (339, 255), (255, 249), (249, 339), (448, 261), (261, 255), (255, 448),
288
+ (133, 243), (243, 190), (190, 133), (133, 155), (155, 112), (112, 133),
289
+ (33, 246), (246, 247), (247, 33), (33, 130), (130, 25), (25, 33),
290
+ (398, 384), (384, 286), (286, 398), (362, 398), (398, 414), (414, 362),
291
+ (362, 463), (463, 341), (341, 362), (263, 359), (359, 467), (467, 263),
292
+ (263, 249), (249, 255), (255, 263), (466, 467), (467, 260), (260, 466),
293
+ (75, 60), (60, 166), (166, 75), (238, 239), (239, 79), (79, 238),
294
+ (162, 127), (127, 139), (139, 162), (72, 11), (11, 37), (37, 72),
295
+ (121, 232), (232, 120), (120, 121), (73, 72), (72, 39), (39, 73),
296
+ (114, 128), (128, 47), (47, 114), (233, 232), (232, 128), (128, 233),
297
+ (103, 104), (104, 67), (67, 103), (152, 175), (175, 148), (148, 152),
298
+ (119, 118), (118, 101), (101, 119), (74, 73), (73, 40), (40, 74),
299
+ (107, 9), (9, 108), (108, 107), (49, 48), (48, 131), (131, 49),
300
+ (32, 194), (194, 211), (211, 32), (184, 74), (74, 185), (185, 184),
301
+ (191, 80), (80, 183), (183, 191), (185, 40), (40, 186), (186, 185),
302
+ (119, 230), (230, 118), (118, 119), (210, 202), (202, 214), (214, 210),
303
+ (84, 83), (83, 17), (17, 84), (77, 76), (76, 146), (146, 77),
304
+ (161, 160), (160, 30), (30, 161), (190, 56), (56, 173), (173, 190),
305
+ (182, 106), (106, 194), (194, 182), (138, 135), (135, 192), (192, 138),
306
+ (129, 203), (203, 98), (98, 129), (54, 21), (21, 68), (68, 54),
307
+ (5, 51), (51, 4), (4, 5), (145, 144), (144, 23), (23, 145),
308
+ (90, 77), (77, 91), (91, 90), (207, 205), (205, 187), (187, 207),
309
+ (83, 201), (201, 18), (18, 83), (181, 91), (91, 182), (182, 181),
310
+ (180, 90), (90, 181), (181, 180), (16, 85), (85, 17), (17, 16),
311
+ (205, 206), (206, 36), (36, 205), (176, 148), (148, 140), (140, 176),
312
+ (165, 92), (92, 39), (39, 165), (245, 193), (193, 244), (244, 245),
313
+ (27, 159), (159, 28), (28, 27), (30, 247), (247, 161), (161, 30),
314
+ (174, 236), (236, 196), (196, 174), (103, 54), (54, 104), (104, 103),
315
+ (55, 193), (193, 8), (8, 55), (111, 117), (117, 31), (31, 111),
316
+ (221, 189), (189, 55), (55, 221), (240, 98), (98, 99), (99, 240),
317
+ (142, 126), (126, 100), (100, 142), (219, 166), (166, 218), (218, 219),
318
+ (112, 155), (155, 26), (26, 112), (198, 209), (209, 131), (131, 198),
319
+ (169, 135), (135, 150), (150, 169), (114, 47), (47, 217), (217, 114),
320
+ (224, 223), (223, 53), (53, 224), (220, 45), (45, 134), (134, 220),
321
+ (32, 211), (211, 140), (140, 32), (109, 67), (67, 108), (108, 109),
322
+ (146, 43), (43, 91), (91, 146), (231, 230), (230, 120), (120, 231),
323
+ (113, 226), (226, 247), (247, 113), (105, 63), (63, 52), (52, 105),
324
+ (241, 238), (238, 242), (242, 241), (124, 46), (46, 156), (156, 124),
325
+ (95, 78), (78, 96), (96, 95), (70, 46), (46, 63), (63, 70),
326
+ (116, 143), (143, 227), (227, 116), (116, 123), (123, 111), (111, 116),
327
+ (1, 44), (44, 19), (19, 1), (3, 236), (236, 51), (51, 3),
328
+ (207, 216), (216, 205), (205, 207), (26, 154), (154, 22), (22, 26),
329
+ (165, 39), (39, 167), (167, 165), (199, 200), (200, 208), (208, 199),
330
+ (101, 36), (36, 100), (100, 101), (43, 57), (57, 202), (202, 43),
331
+ (242, 20), (20, 99), (99, 242), (56, 28), (28, 157), (157, 56),
332
+ (124, 35), (35, 113), (113, 124), (29, 160), (160, 27), (27, 29),
333
+ (211, 204), (204, 210), (210, 211), (124, 113), (113, 46), (46, 124),
334
+ (106, 43), (43, 204), (204, 106), (96, 62), (62, 77), (77, 96),
335
+ (227, 137), (137, 116), (116, 227), (73, 41), (41, 72), (72, 73),
336
+ (36, 203), (203, 142), (142, 36), (235, 64), (64, 240), (240, 235),
337
+ (48, 49), (49, 64), (64, 48), (42, 41), (41, 74), (74, 42),
338
+ (214, 212), (212, 207), (207, 214), (183, 42), (42, 184), (184, 183),
339
+ (210, 169), (169, 211), (211, 210), (140, 170), (170, 176), (176, 140),
340
+ (104, 105), (105, 69), (69, 104), (193, 122), (122, 168), (168, 193),
341
+ (50, 123), (123, 187), (187, 50), (89, 96), (96, 90), (90, 89),
342
+ (66, 65), (65, 107), (107, 66), (179, 89), (89, 180), (180, 179),
343
+ (119, 101), (101, 120), (120, 119), (68, 63), (63, 104), (104, 68),
344
+ (234, 93), (93, 227), (227, 234), (16, 15), (15, 85), (85, 16),
345
+ (209, 129), (129, 49), (49, 209), (15, 14), (14, 86), (86, 15),
346
+ (107, 55), (55, 9), (9, 107), (120, 100), (100, 121), (121, 120),
347
+ (153, 145), (145, 22), (22, 153), (178, 88), (88, 179), (179, 178),
348
+ (197, 6), (6, 196), (196, 197), (89, 88), (88, 96), (96, 89),
349
+ (135, 138), (138, 136), (136, 135), (138, 215), (215, 172), (172, 138),
350
+ (218, 115), (115, 219), (219, 218), (41, 42), (42, 81), (81, 41),
351
+ (5, 195), (195, 51), (51, 5), (57, 43), (43, 61), (61, 57),
352
+ (208, 171), (171, 199), (199, 208), (41, 81), (81, 38), (38, 41),
353
+ (224, 53), (53, 225), (225, 224), (24, 144), (144, 110), (110, 24),
354
+ (105, 52), (52, 66), (66, 105), (118, 229), (229, 117), (117, 118),
355
+ (227, 34), (34, 234), (234, 227), (66, 107), (107, 69), (69, 66),
356
+ (10, 109), (109, 151), (151, 10), (219, 48), (48, 235), (235, 219),
357
+ (183, 62), (62, 191), (191, 183), (142, 129), (129, 126), (126, 142),
358
+ (116, 111), (111, 143), (143, 116), (118, 117), (117, 50), (50, 118),
359
+ (223, 222), (222, 52), (52, 223), (94, 19), (19, 141), (141, 94),
360
+ (222, 221), (221, 65), (65, 222), (196, 3), (3, 197), (197, 196),
361
+ (45, 220), (220, 44), (44, 45), (156, 70), (70, 139), (139, 156),
362
+ (188, 122), (122, 245), (245, 188), (139, 71), (71, 162), (162, 139),
363
+ (149, 170), (170, 150), (150, 149), (122, 188), (188, 196), (196, 122),
364
+ (206, 216), (216, 92), (92, 206), (164, 2), (2, 167), (167, 164),
365
+ (242, 141), (141, 241), (241, 242), (0, 164), (164, 37), (37, 0),
366
+ (11, 72), (72, 12), (12, 11), (12, 38), (38, 13), (13, 12),
367
+ (70, 63), (63, 71), (71, 70), (31, 226), (226, 111), (111, 31),
368
+ (36, 101), (101, 205), (205, 36), (203, 206), (206, 165), (165, 203),
369
+ (126, 209), (209, 217), (217, 126), (98, 165), (165, 97), (97, 98),
370
+ (237, 220), (220, 218), (218, 237), (237, 239), (239, 241), (241, 237),
371
+ (210, 214), (214, 169), (169, 210), (140, 171), (171, 32), (32, 140),
372
+ (241, 125), (125, 237), (237, 241), (179, 86), (86, 178), (178, 179),
373
+ (180, 85), (85, 179), (179, 180), (181, 84), (84, 180), (180, 181),
374
+ (182, 83), (83, 181), (181, 182), (194, 201), (201, 182), (182, 194),
375
+ (177, 137), (137, 132), (132, 177), (184, 76), (76, 183), (183, 184),
376
+ (185, 61), (61, 184), (184, 185), (186, 57), (57, 185), (185, 186),
377
+ (216, 212), (212, 186), (186, 216), (192, 214), (214, 187), (187, 192),
378
+ (139, 34), (34, 156), (156, 139), (218, 79), (79, 237), (237, 218),
379
+ (147, 123), (123, 177), (177, 147), (45, 44), (44, 4), (4, 45),
380
+ (208, 201), (201, 32), (32, 208), (98, 64), (64, 129), (129, 98),
381
+ (192, 213), (213, 138), (138, 192), (235, 59), (59, 219), (219, 235),
382
+ (141, 242), (242, 97), (97, 141), (97, 2), (2, 141), (141, 97),
383
+ (240, 75), (75, 235), (235, 240), (229, 24), (24, 228), (228, 229),
384
+ (31, 25), (25, 226), (226, 31), (230, 23), (23, 229), (229, 230),
385
+ (231, 22), (22, 230), (230, 231), (232, 26), (26, 231), (231, 232),
386
+ (233, 112), (112, 232), (232, 233), (244, 189), (189, 243), (243, 244),
387
+ (189, 221), (221, 190), (190, 189), (222, 28), (28, 221), (221, 222),
388
+ (223, 27), (27, 222), (222, 223), (224, 29), (29, 223), (223, 224),
389
+ (225, 30), (30, 224), (224, 225), (113, 247), (247, 225), (225, 113),
390
+ (99, 60), (60, 240), (240, 99), (213, 147), (147, 215), (215, 213),
391
+ (60, 20), (20, 166), (166, 60), (192, 187), (187, 213), (213, 192),
392
+ (243, 112), (112, 244), (244, 243), (244, 233), (233, 245), (245, 244),
393
+ (245, 128), (128, 188), (188, 245), (188, 114), (114, 174), (174, 188),
394
+ (134, 131), (131, 220), (220, 134), (174, 217), (217, 236), (236, 174),
395
+ (236, 198), (198, 134), (134, 236), (215, 177), (177, 58), (58, 215),
396
+ (156, 143), (143, 124), (124, 156), (25, 110), (110, 7), (7, 25),
397
+ (31, 228), (228, 25), (25, 31), (264, 356), (356, 368), (368, 264),
398
+ (0, 11), (11, 267), (267, 0), (451, 452), (452, 349), (349, 451),
399
+ (267, 302), (302, 269), (269, 267), (350, 357), (357, 277), (277, 350),
400
+ (350, 452), (452, 357), (357, 350), (299, 333), (333, 297), (297, 299),
401
+ (396, 175), (175, 377), (377, 396), (280, 347), (347, 330), (330, 280),
402
+ (269, 303), (303, 270), (270, 269), (151, 9), (9, 337), (337, 151),
403
+ (344, 278), (278, 360), (360, 344), (424, 418), (418, 431), (431, 424),
404
+ (270, 304), (304, 409), (409, 270), (272, 310), (310, 407), (407, 272),
405
+ (322, 270), (270, 410), (410, 322), (449, 450), (450, 347), (347, 449),
406
+ (432, 422), (422, 434), (434, 432), (18, 313), (313, 17), (17, 18),
407
+ (291, 306), (306, 375), (375, 291), (259, 387), (387, 260), (260, 259),
408
+ (424, 335), (335, 418), (418, 424), (434, 364), (364, 416), (416, 434),
409
+ (391, 423), (423, 327), (327, 391), (301, 251), (251, 298), (298, 301),
410
+ (275, 281), (281, 4), (4, 275), (254, 373), (373, 253), (253, 254),
411
+ (375, 307), (307, 321), (321, 375), (280, 425), (425, 411), (411, 280),
412
+ (200, 421), (421, 18), (18, 200), (335, 321), (321, 406), (406, 335),
413
+ (321, 320), (320, 405), (405, 321), (314, 315), (315, 17), (17, 314),
414
+ (423, 426), (426, 266), (266, 423), (396, 377), (377, 369), (369, 396),
415
+ (270, 322), (322, 269), (269, 270), (413, 417), (417, 464), (464, 413),
416
+ (385, 386), (386, 258), (258, 385), (248, 456), (456, 419), (419, 248),
417
+ (298, 284), (284, 333), (333, 298), (168, 417), (417, 8), (8, 168),
418
+ (448, 346), (346, 261), (261, 448), (417, 413), (413, 285), (285, 417),
419
+ (326, 327), (327, 328), (328, 326), (277, 355), (355, 329), (329, 277),
420
+ (309, 392), (392, 438), (438, 309), (381, 382), (382, 256), (256, 381),
421
+ (279, 429), (429, 360), (360, 279), (365, 364), (364, 379), (379, 365),
422
+ (355, 277), (277, 437), (437, 355), (282, 443), (443, 283), (283, 282),
423
+ (281, 275), (275, 363), (363, 281), (395, 431), (431, 369), (369, 395),
424
+ (299, 297), (297, 337), (337, 299), (335, 273), (273, 321), (321, 335),
425
+ (348, 450), (450, 349), (349, 348), (359, 446), (446, 467), (467, 359),
426
+ (283, 293), (293, 282), (282, 283), (250, 458), (458, 462), (462, 250),
427
+ (300, 276), (276, 383), (383, 300), (292, 308), (308, 325), (325, 292),
428
+ (283, 276), (276, 293), (293, 283), (264, 372), (372, 447), (447, 264),
429
+ (346, 352), (352, 340), (340, 346), (354, 274), (274, 19), (19, 354),
430
+ (363, 456), (456, 281), (281, 363), (426, 436), (436, 425), (425, 426),
431
+ (380, 381), (381, 252), (252, 380), (267, 269), (269, 393), (393, 267),
432
+ (421, 200), (200, 428), (428, 421), (371, 266), (266, 329), (329, 371),
433
+ (432, 287), (287, 422), (422, 432), (290, 250), (250, 328), (328, 290),
434
+ (385, 258), (258, 384), (384, 385), (446, 265), (265, 342), (342, 446),
435
+ (386, 387), (387, 257), (257, 386), (422, 424), (424, 430), (430, 422),
436
+ (445, 342), (342, 276), (276, 445), (422, 273), (273, 424), (424, 422),
437
+ (306, 292), (292, 307), (307, 306), (352, 366), (366, 345), (345, 352),
438
+ (268, 271), (271, 302), (302, 268), (358, 423), (423, 371), (371, 358),
439
+ (327, 294), (294, 460), (460, 327), (331, 279), (279, 294), (294, 331),
440
+ (303, 271), (271, 304), (304, 303), (436, 432), (432, 427), (427, 436),
441
+ (304, 272), (272, 408), (408, 304), (395, 394), (394, 431), (431, 395),
442
+ (378, 395), (395, 400), (400, 378), (296, 334), (334, 299), (299, 296),
443
+ (6, 351), (351, 168), (168, 6), (376, 352), (352, 411), (411, 376),
444
+ (307, 325), (325, 320), (320, 307), (285, 295), (295, 336), (336, 285),
445
+ (320, 319), (319, 404), (404, 320), (329, 330), (330, 349), (349, 329),
446
+ (334, 293), (293, 333), (333, 334), (366, 323), (323, 447), (447, 366),
447
+ (316, 15), (15, 315), (315, 316), (331, 358), (358, 279), (279, 331),
448
+ (317, 14), (14, 316), (316, 317), (8, 285), (285, 9), (9, 8),
449
+ (277, 329), (329, 350), (350, 277), (253, 374), (374, 252), (252, 253),
450
+ (319, 318), (318, 403), (403, 319), (351, 6), (6, 419), (419, 351),
451
+ (324, 318), (318, 325), (325, 324), (397, 367), (367, 365), (365, 397),
452
+ (288, 435), (435, 397), (397, 288), (278, 344), (344, 439), (439, 278),
453
+ (310, 272), (272, 311), (311, 310), (248, 195), (195, 281), (281, 248),
454
+ (375, 273), (273, 291), (291, 375), (175, 396), (396, 199), (199, 175),
455
+ (312, 311), (311, 268), (268, 312), (276, 283), (283, 445), (445, 276),
456
+ (390, 373), (373, 339), (339, 390), (295, 282), (282, 296), (296, 295),
457
+ (448, 449), (449, 346), (346, 448), (356, 264), (264, 454), (454, 356),
458
+ (337, 336), (336, 299), (299, 337), (337, 338), (338, 151), (151, 337),
459
+ (294, 278), (278, 455), (455, 294), (308, 292), (292, 415), (415, 308),
460
+ (429, 358), (358, 355), (355, 429), (265, 340), (340, 372), (372, 265),
461
+ (352, 346), (346, 280), (280, 352), (295, 442), (442, 282), (282, 295),
462
+ (354, 19), (19, 370), (370, 354), (285, 441), (441, 295), (295, 285),
463
+ (195, 248), (248, 197), (197, 195), (457, 440), (440, 274), (274, 457),
464
+ (301, 300), (300, 368), (368, 301), (417, 351), (351, 465), (465, 417),
465
+ (251, 301), (301, 389), (389, 251), (394, 395), (395, 379), (379, 394),
466
+ (399, 412), (412, 419), (419, 399), (410, 436), (436, 322), (322, 410),
467
+ (326, 2), (2, 393), (393, 326), (354, 370), (370, 461), (461, 354),
468
+ (393, 164), (164, 267), (267, 393), (268, 302), (302, 12), (12, 268),
469
+ (312, 268), (268, 13), (13, 312), (298, 293), (293, 301), (301, 298),
470
+ (265, 446), (446, 340), (340, 265), (280, 330), (330, 425), (425, 280),
471
+ (322, 426), (426, 391), (391, 322), (420, 429), (429, 437), (437, 420),
472
+ (393, 391), (391, 326), (326, 393), (344, 440), (440, 438), (438, 344),
473
+ (458, 459), (459, 461), (461, 458), (364, 434), (434, 394), (394, 364),
474
+ (428, 396), (396, 262), (262, 428), (274, 354), (354, 457), (457, 274),
475
+ (317, 316), (316, 402), (402, 317), (316, 315), (315, 403), (403, 316),
476
+ (315, 314), (314, 404), (404, 315), (314, 313), (313, 405), (405, 314),
477
+ (313, 421), (421, 406), (406, 313), (323, 366), (366, 361), (361, 323),
478
+ (292, 306), (306, 407), (407, 292), (306, 291), (291, 408), (408, 306),
479
+ (291, 287), (287, 409), (409, 291), (287, 432), (432, 410), (410, 287),
480
+ (427, 434), (434, 411), (411, 427), (372, 264), (264, 383), (383, 372),
481
+ (459, 309), (309, 457), (457, 459), (366, 352), (352, 401), (401, 366),
482
+ (1, 274), (274, 4), (4, 1), (418, 421), (421, 262), (262, 418),
483
+ (331, 294), (294, 358), (358, 331), (435, 433), (433, 367), (367, 435),
484
+ (392, 289), (289, 439), (439, 392), (328, 462), (462, 326), (326, 328),
485
+ (94, 2), (2, 370), (370, 94), (289, 305), (305, 455), (455, 289),
486
+ (339, 254), (254, 448), (448, 339), (359, 255), (255, 446), (446, 359),
487
+ (254, 253), (253, 449), (449, 254), (253, 252), (252, 450), (450, 253),
488
+ (252, 256), (256, 451), (451, 252), (256, 341), (341, 452), (452, 256),
489
+ (414, 413), (413, 463), (463, 414), (286, 441), (441, 414), (414, 286),
490
+ (286, 258), (258, 441), (441, 286), (258, 257), (257, 442), (442, 258),
491
+ (257, 259), (259, 443), (443, 257), (259, 260), (260, 444), (444, 259),
492
+ (260, 467), (467, 445), (445, 260), (309, 459), (459, 250), (250, 309),
493
+ (305, 289), (289, 290), (290, 305), (305, 290), (290, 460), (460, 305),
494
+ (401, 376), (376, 435), (435, 401), (309, 250), (250, 392), (392, 309),
495
+ (376, 411), (411, 433), (433, 376), (453, 341), (341, 464), (464, 453),
496
+ (357, 453), (453, 465), (465, 357), (343, 357), (357, 412), (412, 343),
497
+ (437, 343), (343, 399), (399, 437), (344, 360), (360, 440), (440, 344),
498
+ (420, 437), (437, 456), (456, 420), (360, 420), (420, 363), (363, 360),
499
+ (361, 401), (401, 288), (288, 361), (265, 372), (372, 353), (353, 265),
500
+ (390, 339), (339, 249), (249, 390), (339, 448), (448, 255), (255, 339)])
projects/2026Spring專題班/胡惟媗/臉部辨識/main.py ADDED
@@ -0,0 +1,273 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Week 11 - Face Mesh Expression Detection (Template Version)
3
+ ===========================================================
4
+ Curriculum Goals:
5
+ 1. Use MediaPipe Face Landmarker Tasks API to obtain 468 3D landmarks
6
+ 2. 【TODO 1】Compute face metrics: mouth opening ratio and Eye Aspect Ratio (EAR)
7
+ 3. 【TODO 2】Implement Hysteresis (double thresholds) to prevent edge value jitter
8
+ 4. 【TODO 3】Integrate Pygame to play sound effects on facial expressions, using a state flag to avoid overlay glitching
9
+ 5. Implement Frame Skip optimization for smoother Pygame framerate
10
+ """
11
+ import pygame
12
+ import cv2
13
+ import mediapipe as mp
14
+ from mediapipe.tasks import python
15
+ from mediapipe.tasks.python import vision
16
+ import math
17
+ import sys
18
+ import os
19
+ import time
20
+
21
+ # ============================================
22
+ # 1. Initialize Pygame & Mixer
23
+ # ============================================
24
+ pygame.init()
25
+ pygame.mixer.init()
26
+ WIDTH, HEIGHT = 800, 600
27
+ screen = pygame.display.set_mode((WIDTH, HEIGHT))
28
+ pygame.display.set_caption("Week 11 - Face Mesh Expression Detector (Template)")
29
+ clock = pygame.time.Clock()
30
+
31
+ font = pygame.font.SysFont(None, 36)
32
+ small_font = pygame.font.SysFont(None, 24)
33
+ title_font = pygame.font.SysFont(None, 48)
34
+
35
+ # Load sound effect
36
+ try:
37
+ trigger_sound = pygame.mixer.Sound("shoot.wav")
38
+ print("Success: Loaded sound file shoot.wav")
39
+ except FileNotFoundError:
40
+ trigger_sound = None
41
+ print("Warning: Could not find sound file shoot.wav")
42
+
43
+ # ============================================
44
+ # 2. Initialize MediaPipe Face Landmarker
45
+ # ============================================
46
+ model_path = 'face_landmarker.task'
47
+ if not os.path.exists(model_path):
48
+ print(f"Error: Model file not found at {model_path}")
49
+ pygame.quit()
50
+ sys.exit()
51
+
52
+ try:
53
+ base_options = python.BaseOptions(model_asset_path=model_path)
54
+ options = vision.FaceLandmarkerOptions(base_options=base_options, num_faces=1)
55
+ detector = vision.FaceLandmarker.create_from_options(options)
56
+ print("Success: Loaded MediaPipe detector model")
57
+ except Exception as e:
58
+ print(f"Error: Failed to load model: {e}")
59
+ pygame.quit()
60
+ sys.exit()
61
+
62
+ # ============================================
63
+ # 3. Face Landmark Indexes
64
+ # ============================================
65
+ LEFT_EYE_OUTER = 33
66
+ RIGHT_EYE_OUTER = 263
67
+ LIP_TOP = 13
68
+ LIP_BOTTOM = 14
69
+ LEFT_EYE_TOP = 159
70
+ LEFT_EYE_BOTTOM = 145
71
+ LEFT_EYE_LEFT = 33
72
+ LEFT_EYE_RIGHT = 133
73
+
74
+ # ============================================
75
+ # 4. Parameters and State Variables
76
+ # ============================================
77
+ MOUTH_OPEN_THRESHOLD = 0.10
78
+ MOUTH_CLOSE_THRESHOLD = 0.05
79
+
80
+ EAR_BLINK_THRESHOLD = 0.18
81
+ EAR_OPEN_THRESHOLD = 0.25
82
+
83
+ # 【TODO 3】Declare state flags and edge-trigger variables
84
+ is_mouth_open = False
85
+ was_mouth_open = False
86
+
87
+ is_blinking = False
88
+ was_blinking = False
89
+
90
+ mouth_open_count = 0
91
+ blink_count = 0
92
+
93
+ frame_counter = 0
94
+ FRAME_SKIP = 3
95
+ detector_results = None
96
+
97
+ # Open Webcam
98
+ cap = cv2.VideoCapture(0)
99
+ if not cap.isOpened():
100
+ print("Error: Could not open camera.")
101
+ pygame.quit()
102
+ sys.exit()
103
+
104
+ # ============================================
105
+ # 5. Main Game Loop
106
+ # ============================================
107
+ running = True
108
+ while running:
109
+ for event in pygame.event.get():
110
+ if event.type == pygame.QUIT:
111
+ running = False
112
+
113
+ success, img = cap.read()
114
+ if not success:
115
+ cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
116
+ continue
117
+
118
+ img = cv2.flip(img, 1)
119
+ img_h, img_w, _ = img.shape
120
+ frame_counter += 1
121
+
122
+ plot_points = {}
123
+
124
+ # Frame Skip Optimization
125
+ if frame_counter % FRAME_SKIP == 0 or detector_results is None:
126
+ try:
127
+ img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
128
+ mp_image = mp.Image(image_format=mp.ImageFormat.SRGB, data=img_rgb)
129
+ detector_results = detector.detect(mp_image)
130
+ except Exception as e:
131
+ print(f"Error: Inference exception: {e}")
132
+
133
+ # Feature Calculation and Metrics Parsing
134
+ if detector_results and detector_results.face_landmarks:
135
+ face_lms = detector_results.face_landmarks[0]
136
+
137
+ for idx in [LEFT_EYE_OUTER, RIGHT_EYE_OUTER, LIP_TOP, LIP_BOTTOM, LEFT_EYE_TOP, LEFT_EYE_BOTTOM, LEFT_EYE_RIGHT]:
138
+ pt = face_lms[idx]
139
+ plot_points[idx] = (int(pt.x * WIDTH), int(pt.y * HEIGHT))
140
+
141
+ p_lx, p_ly = plot_points[LEFT_EYE_OUTER]
142
+ p_rx, p_ry = plot_points[RIGHT_EYE_OUTER]
143
+
144
+ # 【TODO 1】Calculate the base distance using math.hypot
145
+ base_dist = math.hypot(p_rx - p_lx, p_ry - p_ly)
146
+ if base_dist > 0:
147
+ p_m_top_x, p_m_top_y = plot_points[LIP_TOP]
148
+ p_m_bot_x, p_m_bot_y = plot_points[LIP_BOTTOM]
149
+
150
+ # 【TODO 1】Calculate mouth_ratio by dividing mouth pixel distance by base_dist
151
+ mouth_dist = math.hypot(p_m_bot_x - p_m_top_x, p_m_bot_y - p_m_top_y)
152
+ mouth_ratio = mouth_dist / base_dist
153
+
154
+ p_e_top_x, p_e_top_y = plot_points[LEFT_EYE_TOP]
155
+ p_e_bot_x, p_e_bot_y = plot_points[LEFT_EYE_BOTTOM]
156
+ p_e_right_x, p_e_right_y = plot_points[LEFT_EYE_RIGHT]
157
+
158
+ # 【TODO 1】Calculate left eye height and width, and compute EAR (height / width)
159
+ ear_h = math.hypot(p_e_top_x - p_e_bot_x, p_e_top_y - p_e_bot_y)
160
+ ear_w = math.hypot(p_e_right_x - p_lx, p_e_right_y - p_ly)
161
+ ear = ear_h / ear_w if ear_w > 0 else 0
162
+
163
+ # 【TODO 2】Hysteresis Threshold Judgments
164
+ # A. Check if mouth_ratio exceeds MOUTH_OPEN_THRESHOLD (to open) or falls below MOUTH_CLOSE_THRESHOLD (to close)
165
+ if mouth_ratio > MOUTH_OPEN_THRESHOLD:
166
+ is_mouth_open = True
167
+ elif mouth_ratio < MOUTH_CLOSE_THRESHOLD:
168
+ is_mouth_open = False
169
+
170
+
171
+ # B. Check if ear falls below EAR_BLINK_THRESHOLD (to blink) or exceeds EAR_OPEN_THRESHOLD (to open)
172
+ if ear < EAR_BLINK_THRESHOLD:
173
+ is_blinking = True
174
+ elif ear > EAR_OPEN_THRESHOLD:
175
+ is_blinking = False
176
+
177
+ # 【TODO 3】Edge Triggering logic: play sound only when is_mouth_open is True and was_mouth_open is False
178
+ if is_mouth_open and not was_mouth_open:
179
+ mouth_open_count += 1
180
+ if trigger_sound:
181
+ trigger_sound.play()
182
+ print(f"Mouth opened! Count: {mouth_open_count} (ratio: {mouth_ratio:.2f})")
183
+
184
+ if is_blinking and not was_blinking:
185
+ blink_count += 1
186
+ print(f"Blink detected! Count: {blink_count} (EAR: {ear:.2f})")
187
+
188
+
189
+
190
+
191
+
192
+ was_mouth_open = is_mouth_open
193
+ was_blinking = is_blinking
194
+
195
+ else:
196
+ is_mouth_open = False
197
+ is_blinking = False
198
+ was_mouth_open = False
199
+ was_blinking = False
200
+ mouth_ratio = 0.0
201
+ ear = 0.30
202
+
203
+ # ============================================
204
+ # 6. Render UI Elements
205
+ # ============================================
206
+ if is_mouth_open:
207
+ screen.fill((16, 44, 28))
208
+ elif is_blinking:
209
+ screen.fill((15, 30, 54))
210
+ else:
211
+ screen.fill((20, 20, 30))
212
+
213
+ if plot_points:
214
+ pygame.draw.line(screen, (100, 100, 150), plot_points[LEFT_EYE_OUTER], plot_points[RIGHT_EYE_OUTER], 2)
215
+ pygame.draw.line(screen, (245, 158, 11), plot_points[LIP_TOP], plot_points[LIP_BOTTOM], 3)
216
+ pygame.draw.line(screen, (59, 130, 246), plot_points[LEFT_EYE_TOP], plot_points[LEFT_EYE_BOTTOM], 3)
217
+
218
+ for idx, pos in plot_points.items():
219
+ color = (255, 255, 255)
220
+ if idx in [LIP_TOP, LIP_BOTTOM]: color = (245, 158, 11)
221
+ elif idx in [LEFT_EYE_TOP, LEFT_EYE_BOTTOM, LEFT_EYE_OUTER, LEFT_EYE_RIGHT]: color = (59, 130, 246)
222
+ pygame.draw.circle(screen, color, pos, 6)
223
+
224
+ # HUD Card
225
+ pygame.draw.rect(screen, (30, 30, 45), (30, 30, 340, 220), border_radius=12)
226
+ pygame.draw.rect(screen, (50, 50, 70), (30, 30, 340, 220), width=2, border_radius=12)
227
+
228
+ screen.blit(font.render("Face Mesh Dashboard (Student)", True, (255, 255, 255)), (50, 45))
229
+
230
+ m_color = (34, 197, 94) if is_mouth_open else (245, 158, 11)
231
+ screen.blit(small_font.render(f"Mouth Ratio: {mouth_ratio:.2f}", True, m_color), (50, 90))
232
+ pygame.draw.rect(screen, (50, 50, 65), (50, 110, 200, 12), border_radius=4)
233
+ pygame.draw.rect(screen, m_color, (50, 110, int(min(mouth_ratio, 1.0) * 200), 12), border_radius=4)
234
+
235
+ e_color = (59, 130, 246) if not is_blinking else (239, 68, 68)
236
+ screen.blit(small_font.render(f"Eye EAR: {ear:.2f}", True, e_color), (50, 140))
237
+ pygame.draw.rect(screen, (50, 50, 65), (50, 160, 200, 12), border_radius=4)
238
+ pygame.draw.rect(screen, e_color, (50, 160, int(min(ear, 0.5) * 400), 12), border_radius=4)
239
+
240
+ # Stats Card
241
+ pygame.draw.rect(screen, (30, 30, 45), (WIDTH - 320, 30, 290, 110), border_radius=12)
242
+ pygame.draw.rect(screen, (50, 50, 70), (WIDTH - 320, 30, 290, 110), width=2, border_radius=12)
243
+ screen.blit(small_font.render(f"Mouth Open Count: {mouth_open_count}", True, (255, 255, 255)), (WIDTH - 300, 45))
244
+ screen.blit(small_font.render(f"Blink/Close Count: {blink_count}", True, (255, 255, 255)), (WIDTH - 300, 75))
245
+
246
+ # Center Status Text
247
+ if is_mouth_open:
248
+ status_lbl = title_font.render("MOUTH OPENED", True, (34, 197, 94))
249
+ screen.blit(status_lbl, (WIDTH // 2 - status_lbl.get_width() // 2, HEIGHT // 2 + 100))
250
+ elif is_blinking:
251
+ status_lbl = title_font.render("BLINKING", True, (59, 130, 246))
252
+ screen.blit(status_lbl, (WIDTH // 2 - status_lbl.get_width() // 2, HEIGHT // 2 + 100))
253
+ else:
254
+ if not plot_points:
255
+ status_lbl = title_font.render("NO FACE DETECTED", True, (239, 68, 68))
256
+ screen.blit(status_lbl, (WIDTH // 2 - status_lbl.get_width() // 2, HEIGHT // 2 - 20))
257
+ else:
258
+ status_lbl = title_font.render("NORMAL", True, (200, 200, 200))
259
+ screen.blit(status_lbl, (WIDTH // 2 - status_lbl.get_width() // 2, HEIGHT // 2 + 100))
260
+
261
+ # Footer
262
+ status_text = small_font.render(
263
+ f"Frame: {frame_counter} | Skip: {FRAME_SKIP} frames | FPS: {clock.get_fps():.0f}",
264
+ True, (100, 100, 120)
265
+ )
266
+ screen.blit(status_text, (20, HEIGHT - 35))
267
+
268
+ pygame.display.update()
269
+ clock.tick(30)
270
+
271
+ cap.release()
272
+ pygame.quit()
273
+ sys.exit()
projects/2026Spring專題班/胡惟媗/臉部辨識/shoot.wav ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:091481354d97c5469a2cd40f16d8fad63904eb4676992c34c1f208fc87a8173c
3
+ size 17684
projects/2026Spring專題班/胡惟媗/連線射擊遊戲/background.jpg ADDED

Git LFS Details

  • SHA256: 7b4550b2073dfd8267a1d2fc94620e82aada100ab95114d44a62eef8e40e3e7e
  • Pointer size: 131 Bytes
  • Size of remote file: 221 kB
projects/2026Spring專題班/胡惟媗/連線射擊遊戲/hit.wav ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:55440a99e00bb9de3a53bcaa709c83d412001f78dcf7103036ec02a5057c2fdd
3
+ size 13274
projects/2026Spring專題班/胡惟媗/連線射擊遊戲/main.py ADDED
@@ -0,0 +1,250 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pygame
2
+ import socket
3
+ import json
4
+ import sys
5
+
6
+ pygame.init()
7
+ pygame.mixer.init()
8
+ screen = pygame.display.set_mode((800, 600))
9
+ clock = pygame.time.Clock()
10
+
11
+ # --- 1. 網路設定(自動判斷 P1 / P2)---
12
+ sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
13
+ sock.setblocking(False)
14
+
15
+ # 嘗試綁定 port 5000,若被佔用則綁定 5001
16
+ # 先啟動的人 = P1 (左側),後啟動的人 = P2 (右側)
17
+ try:
18
+ sock.bind(("0.0.0.0", 5000))
19
+ MY_PORT = 5000
20
+ TARGET_PORT = 5001
21
+ player_role = "P1"
22
+ except OSError:
23
+ sock.bind(("0.0.0.0", 5001))
24
+ MY_PORT = 5001
25
+ TARGET_PORT = 5000
26
+ player_role = "P2"
27
+
28
+ # 請修改為隔壁同學的 IP 準備互連!
29
+ TARGET_IP = "127.0.0.1"
30
+
31
+ # --- 2. 遊戲參數與各種變數(根據角色自動設定位置與方向)---
32
+ if player_role == "P1":
33
+ my_pos = [100, 300] # P1 在左邊
34
+ enemy_pos = [600, 300] # 對手在右邊
35
+ my_bullet_dir = 10 # P1 子彈朝右飛
36
+ enemy_bullet_dir = -10 # 對手子彈朝左飛
37
+ else:
38
+ my_pos = [600, 300] # P2 在右邊
39
+ enemy_pos = [100, 300] # 對手在左邊
40
+ my_bullet_dir = -10 # P2 子彈朝左飛
41
+ enemy_bullet_dir = 10 # 對手子彈朝右飛
42
+
43
+ my_hp = 10
44
+ enemy_hp = 10
45
+ my_score = 0
46
+ enemy_score = 0
47
+ bullets = []
48
+ bullet_id_counter = 0 # 子彈編號計數器,用來同步雙方畫面
49
+ game_over = False
50
+ opponent_connected = False # 追蹤對手是否已連線
51
+
52
+ pygame.display.set_caption(f"連接對戰 [{player_role} | Port {MY_PORT}]")
53
+ print(f" {player_role}( port {MY_PORT},port {TARGET_PORT})")
54
+ print(f"位置:{'左側' if player_role == 'P1' else '右側'}")
55
+ print("等待連線中...")
56
+
57
+ # 字體與素材載入
58
+ font = pygame.font.SysFont(None, 36)
59
+ big_font = pygame.font.SysFont(None, 72)
60
+
61
+ try:
62
+ bg_img = pygame.image.load("background.jpg")
63
+ bg_img = pygame.transform.scale(bg_img, (800, 600))
64
+ except FileNotFoundError:
65
+ bg_img = None
66
+
67
+ try:
68
+ shoot_sound = pygame.mixer.Sound("shoot.wav")
69
+ except FileNotFoundError:
70
+ shoot_sound = None
71
+
72
+ try:
73
+ hit_sound = pygame.mixer.Sound("hit.wav")
74
+ except FileNotFoundError:
75
+ hit_sound = None
76
+
77
+
78
+ # --- 3. 遊戲主迴圈 ---
79
+ while True:
80
+ if bg_img:
81
+ screen.blit(bg_img, (0, 0))
82
+ else:
83
+ screen.fill((20, 20, 30))
84
+
85
+ # ======= [網路區間:接收最新消息] =======
86
+ while True:
87
+ try:
88
+ data, addr = sock.recvfrom(1024)
89
+ opponent_connected = True # 只要收到任何封包,就代表對手來了
90
+ pkg = json.loads(data.decode())
91
+ # 【TODO 1: 根據 pkg['type'] 判斷是 'pos', 'shoot', 'hit' 還是 'im_dead'!更新對手數值】
92
+ if pkg['type'] == 'pos':
93
+ enemy_pos[0] = pkg['x'] # 對手傳來最新座標,更新對手位置
94
+ enemy_pos[1] = pkg['y'] # 對手傳來最新座標,更新對手位置
95
+ elif pkg['type'] == 'shoot':
96
+ # 對手開槍了!在對手位置生成一顆子彈
97
+ bullets.append({
98
+
99
+ 'x': pkg['x'],
100
+ 'y': pkg['y'],
101
+ 'dir': enemy_bullet_dir,
102
+ 'owner': 'enemy',
103
+ 'id': pkg['id']
104
+ })
105
+ if shoot_sound:
106
+ shoot_sound.play()
107
+
108
+ elif pkg['type'] == 'hit':
109
+ enemy_hp = pkg.get('hp', 10)
110
+ hit_bullet_id = pkg.get('bullet_id')
111
+ if hit_bullet_id is not None:
112
+ bullets[:] = [b for b in bullets if not (b['owner'] == 'me' and b ['id'] == hit_bullet_id)]
113
+
114
+ pass
115
+ elif pkg['type'] == 'im_dead':
116
+ enemy_hp = pkg.get('hp', 0)
117
+ my_score += 1
118
+ hit_bullet_id = pkg.get('bullet_id')
119
+ if hit_bullet_id is not None:
120
+ bullets[:] = [b for b in bullets if not (b['owner'] == 'me' and b ['id'] == hit_bullet_id)]
121
+
122
+
123
+ pass
124
+ except (BlockingIOError, ConnectionResetError):
125
+ # 非阻塞模式下,Linux 拋 BlockingIOError,Windows 拋 ConnectionResetError
126
+ break
127
+ except json.JSONDecodeError:
128
+ # 防止有人亂傳不是 JSON 格式的垃圾訊息導致閃退
129
+ pass
130
+
131
+ # ======= [事件區間:玩家輸入] =======
132
+ for event in pygame.event.get():
133
+ if event.type == pygame.QUIT:
134
+ pygame.quit(); sys.exit()
135
+
136
+ if event.type == pygame.KEYDOWN:
137
+ # 只有活著且對手連線時才能開槍
138
+ if event.key == pygame.K_SPACE and not game_over and opponent_connected:
139
+ # 【TODO 2: 當玩家按下空白鍵:生成一顆屬於你的子彈,並送出 'shoot' 封包給對方】
140
+ # 提示:子彈方向請用 my_bullet_dir,發送後記得播放 shoot_sound.play()
141
+ bullets.append({
142
+ 'x': my_pos[0], # 子彈生成在玩家前方一點的位置
143
+ 'y': my_pos[1],
144
+ 'dir': my_bullet_dir,
145
+ 'owner': 'me',
146
+ 'id': bullet_id_counter
147
+ })
148
+ shoot_pkg = {
149
+ 'type': 'shoot','x': my_pos[0],'y': my_pos[1],'id': bullet_id_counter}
150
+ sock.sendto(json.dumps(shoot_pkg).encode(), (TARGET_IP, TARGET_PORT))
151
+ if shoot_sound:
152
+ shoot_sound.play()
153
+
154
+ # 死亡時可以按 R 鍵重生
155
+ if event.key == pygame.K_r and game_over:
156
+ # 【TODO 3: 重新開始設定,並傳送滿血復活的封包給對手】
157
+ game_over = False
158
+ my_hp = 10
159
+ my_pos = [100, 300] if player_role == "P1" else [600, 300]
160
+ respawn_pkg = {'type':'hit','hp': my_hp}
161
+ sock.sendto(json.dumps(respawn_pkg).encode(), (TARGET_IP, TARGET_PORT))
162
+
163
+
164
+ # 【TODO 4: 處理 WASD 或方向鍵移動 (活著才能移動)】
165
+ if not game_over:
166
+ keys = pygame.key.get_pressed()
167
+ if keys[pygame.K_w] or keys[pygame.K_UP]:
168
+ my_pos[1] -= 5
169
+ if keys[pygame.K_s] or keys[pygame.K_DOWN]:
170
+ my_pos[1] += 5
171
+ if keys[pygame.K_a] or keys[pygame.K_LEFT]:
172
+ my_pos[0] -= 5
173
+ if keys[pygame.K_d] or keys[pygame.K_RIGHT]:
174
+ my_pos[0] += 5
175
+
176
+
177
+
178
+ # ======= [運算與繪圖區間] =======
179
+ # 【TODO 5: 畫出自己與對手的人物實體 (Rect)】
180
+ my_rect = pygame.Rect(my_pos[0], my_pos[1], 40, 40)
181
+ enemy_rect = pygame.Rect(enemy_pos[0], enemy_pos[1], 40, 40)
182
+
183
+ if not game_over:
184
+ pygame.draw.rect(screen, (176, 209, 247), my_rect)
185
+
186
+ if opponent_connected and enemy_hp > 0:
187
+ pygame.draw.rect(screen, (165, 138, 186), enemy_rect)
188
+
189
+ # 【TODO 6: 畫出計分板與血條】
190
+ score_text = font.render(f"Score: {my_score} | {enemy_score}", True, (255, 255, 255))
191
+ screen.blit(score_text,(800//2 - score_text.get_width()//2, 15))
192
+
193
+ if not game_over:
194
+ my_hp_text = font.render(f"HP: {my_hp}", True, (255, 255, 255))
195
+ screen.blit(my_hp_text, (my_pos[0], my_pos[1] - 30))
196
+
197
+ if opponent_connected and enemy_hp > 0:
198
+ enemy_hp_text = font.render(f"HP: {enemy_hp}", True, (255, 255, 255))
199
+ screen.blit(enemy_hp_text, (enemy_pos[0], enemy_pos[1] - 30))
200
+ # 【TODO 7: 根據子彈陣列移動並畫出每一顆子彈】
201
+ for b in bullets[:]:
202
+ if not game_over:
203
+ b['x'] += b['dir']
204
+
205
+ color = (255, 50, 50) if b['owner'] == 'me' else (50, 255, 50)
206
+ bullet_rect = pygame.Rect(b['x'], b['y'], 10, 10)
207
+ pygame.draw.rect(screen, color, bullet_rect)
208
+
209
+ # 【TODO 8: 判斷自己的碰撞框是否被對方的子彈打中 (自己必須活著)】
210
+ # 若打中: 1) 自己的 my_hp 扣一滴並播放 hit_sound 2) 刪除該子彈 3) 送出去 'hit' 封包
211
+ # 若 my_hp 歸零,設定 game_over = True,改為發送 'im_dead' 封包
212
+ if b['owner'] == 'enemy' and not game_over and my_rect.colliderect(bullet_rect):
213
+ my_hp -= 1
214
+ if hit_sound:
215
+ hit_sound.play()
216
+ hit_bullet_id = b['id']
217
+ if b in bullets:
218
+ bullets.remove(b)
219
+ if my_hp <= 0:
220
+ game_over = True
221
+ enemy_score += 1
222
+ hit_pkg = {'type': 'im_dead', 'hp': 0, 'bullet_id': hit_bullet_id}
223
+ else:
224
+ hit_pkg = {'type': 'hit', 'hp': my_hp, 'bullet_id': hit_bullet_id}
225
+ sock.sendto(json.dumps(hit_pkg).encode(), (TARGET_IP, TARGET_PORT))
226
+ elif b['x'] < 0 or b['x'] > 800:
227
+ # 子彈飛出邊界就刪除
228
+ if b in bullets:
229
+ bullets.remove(b)
230
+
231
+ # 如果死亡,蓋上結算文字
232
+ if game_over:
233
+ dead_text = big_font.render("YOU DIED", True, (239, 68, 68))
234
+ screen.blit(dead_text, (800//2 - dead_text.get_width()//2, 600//2 - dead_text.get_height()//2 - 20))
235
+ hint_text = font.render("Press 'R' to Restart", True, (255, 255, 255))
236
+ screen.blit(hint_text, (800//2 - hint_text.get_width()//2, 600//2 + 40))
237
+
238
+ elif not opponent_connected:
239
+ # 對手還沒連線的提示畫面
240
+ wait_text = font.render("Waiting for opponent...", True, (255, 200, 0))
241
+ screen.blit(wait_text, (800//2 - wait_text.get_width()//2, 600//2 - wait_text.get_height()//2))
242
+
243
+ pygame.display.flip()
244
+ clock.tick(60)
245
+
246
+ # ======= [網路區間:發送最新座標] =======
247
+ # 【TODO 9: 若自己不僅存活而且移動了,每回合的最後將座標裝進 'pos' 封包發送��去】
248
+ if not game_over:
249
+ pos_pkg = {'type': 'pos', 'x': my_pos[0], 'y': my_pos[1]}
250
+ sock.sendto(json.dumps(pos_pkg).encode(), (TARGET_IP, TARGET_PORT))
projects/2026Spring專題班/胡惟媗/連線射擊遊戲/shoot.wav ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:091481354d97c5469a2cd40f16d8fad63904eb4676992c34c1f208fc87a8173c
3
+ size 17684