-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
525 lines (410 loc) · 19 KB
/
Copy pathmain.py
File metadata and controls
525 lines (410 loc) · 19 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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
import cv2
import mediapipe as mp
import tkinter as tk
from tkinter import ttk, StringVar, messagebox, Canvas, Frame, Scrollbar
from PIL import Image, ImageTk
import sqlite3
import json
import os
import numpy as np
from time import time
import atexit
import logging
import threading
import zlib
import ttkbootstrap as tb
from ttkbootstrap.constants import *
import tkinter as tk
from tkinter import ttk, messagebox
logging.basicConfig(level=logging.INFO, format="%(asctime)s [%(levelname)s]: %(message)s")
logger = logging.getLogger()
mp_pose = mp.solutions.pose
mp_drawing = mp.solutions.drawing_utils
recording = False
recorded_data = []
last_frame_time = 0
frame_interval = 0.03 # 10ms interval (~100 FPS)
# Database setup
DB_FILE = "dance.db"
def initialize_database():
"""Initialize the SQLite database."""
with sqlite3.connect(DB_FILE) as conn:
cursor = conn.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS dances (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT UNIQUE NOT NULL
)
""")
cursor.execute("""
CREATE TABLE IF NOT EXISTS motions (
id INTEGER PRIMARY KEY AUTOINCREMENT,
dance_id INTEGER NOT NULL,
landmarks TEXT NOT NULL,
score REAL NOT NULL,
FOREIGN KEY (dance_id) REFERENCES dances (id)
)
""")
conn.commit()
logger.info("Database initialized.")
def add_dance(name):
"""Add a dance type to the database."""
with sqlite3.connect(DB_FILE) as conn:
cursor = conn.cursor()
cursor.execute("INSERT OR IGNORE INTO dances (name) VALUES (?)", (name,))
conn.commit()
logger.debug(f"Dance '{name}' added to database.")
def get_all_dances():
"""Get all dance types from the database."""
with sqlite3.connect(DB_FILE) as conn:
cursor = conn.cursor()
cursor.execute("SELECT id, name FROM dances")
return cursor.fetchall()
def add_motion(dance_id, landmarks, score):
"""Add motion data to the database with zlib compression."""
compressed_landmarks = zlib.compress(json.dumps(landmarks).encode('utf-8'))
with sqlite3.connect(DB_FILE) as conn:
cursor = conn.cursor()
cursor.execute("""
INSERT INTO motions (dance_id, landmarks, score)
VALUES (?, ?, ?)
""", (dance_id, compressed_landmarks, score))
conn.commit()
logger.debug(f"Motion data saved for dance ID {dance_id} with score {score}.")
# def get_dance_motions(dance_id):
# """Get all motions for a specific dance."""
# with sqlite3.connect(DB_FILE) as conn:
# cursor = conn.cursor()
# cursor.execute("SELECT landmarks, score FROM motions WHERE dance_id = ?", (dance_id,))
# motions = cursor.fetchall()
# return [(zlib.decompress(motion[0]).decode('utf-8'), motion[1]) for motion in motions]
def get_dance_motions(dance_id, min_score=50):
"""Get all motions for a specific dance with a minimum score filter."""
with sqlite3.connect(DB_FILE) as conn:
cursor = conn.cursor()
cursor.execute(
"SELECT landmarks, score FROM motions WHERE dance_id = ? AND score >= ?",
(dance_id, min_score),
)
motions = cursor.fetchall()
# Decode and ensure landmarks remain as JSON strings
return [(zlib.decompress(motion[0]).decode('utf-8'), motion[1]) for motion in motions]
# def calculate_score(reference, current):
# if len(reference) != len(current):
# return 0
# threshold = 0.05
# differences = [
# max(0, np.linalg.norm(np.array(r) - np.array(c)) - threshold)
# for r, c in zip(reference, current)
# ]
# average_difference = sum(differences) / len(differences)
# score = max(0, 100 - int(average_difference * 50))
# logger.debug(f"Thresholded Differences: {differences}, Average: {average_difference}, Score: {score}")
# return score
def calculate_score(reference, current, reference_score):
if len(reference) != len(current):
return 0
threshold = 0.05
differences = [
max(0, np.linalg.norm(np.array(r) - np.array(c)) - threshold)
for r, c in zip(reference, current)
]
average_difference = sum(differences) / len(differences)
score = max(0, reference_score - int(average_difference * 50))
logger.debug(f"Thresholded Differences: {differences}, Average: {average_difference}, Score: {score}")
return score
# def evaluate_motion(dance_id, recorded_data):
# """Evaluate the recorded motion against a dance type."""
# motions = get_dance_motions(dance_id)
# if not motions or not recorded_data:
# logger.warning("No data available for evaluation.")
# return "Tidak ada data untuk penilaian."
# best_score = 0
# for motion in motions:
# reference_landmarks = json.loads(motion[0])
# score = calculate_score(reference_landmarks, recorded_data)
# best_score = max(best_score, score)
# logger.info(f"Best score: {best_score}")
# return f"Skor terbaik: {best_score}"
def evaluate_motion(dance_id, recorded_data):
"""Evaluate the recorded motion against a dance type."""
motions = get_dance_motions(dance_id)
if not motions or not recorded_data:
logger.warning("No data available for evaluation.")
return "Tidak ada data untuk penilaian."
best_score = 0
for reference_landmarks, reference_score in motions:
score = calculate_score(reference_landmarks, recorded_data, reference_score)
best_score = max(best_score, score)
logger.info(f"Best score: {best_score}")
return f"Skor terbaik: {best_score}"
def filter_landmarks(landmarks):
"""Take all landmarks for complete body tracking."""
important_indices = [0, 11, 12, 13, 14, 23, 24]
return [(lm.x, lm.y, lm.z) for idx, lm in enumerate(landmarks) if idx in important_indices]
def update_frame(frame_label, pose, frame_callback=None):
"""Capture and process a frame."""
global cap
def run_capture():
global last_frame_time
while True:
current_time = time()
if current_time - last_frame_time < frame_interval:
continue
last_frame_time = current_time
ret, frame = cap.read()
if not ret:
logger.error("Failed to read frame from camera.")
break
frame = cv2.resize(frame, (320, 240))
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
results = pose.process(frame_rgb)
if results.pose_landmarks:
mp_drawing.draw_landmarks(
frame,
results.pose_landmarks,
mp_pose.POSE_CONNECTIONS,
mp_drawing.DrawingSpec(color=(0, 255, 0), thickness=2, circle_radius=2),
mp_drawing.DrawingSpec(color=(0, 0, 255), thickness=2, circle_radius=2),
)
if frame_callback:
frame_callback(results.pose_landmarks.landmark)
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
frame_pil = Image.fromarray(frame)
frame_tk = ImageTk.PhotoImage(frame_pil)
frame_label.imgtk = frame_tk
frame_label.configure(image=frame_tk)
thread = threading.Thread(target=run_capture, daemon=True)
thread.start()
class ScrollableFrame(Frame):
"""A scrollable frame using Canvas."""
def __init__(self, parent, *args, **kwargs):
Frame.__init__(self, parent, *args, **kwargs)
canvas = Canvas(self, borderwidth=0)
frame = Frame(canvas)
self.scrollbar = Scrollbar(self, orient="vertical", command=canvas.yview)
canvas.configure(yscrollcommand=self.scrollbar.set)
self.scrollbar.pack(side="right", fill="y")
canvas.pack(side="left", fill="both", expand=True)
canvas.create_window((4, 4), window=frame, anchor="nw", tags="frame")
frame.bind("<Configure>", lambda e: canvas.configure(scrollregion=canvas.bbox("all")))
self.canvas = canvas
self.frame = frame
class Page(Frame):
def __init__(self, parent, controller):
super().__init__(parent)
self.controller = controller
class AddDataPage(Page):
def __init__(self, parent, controller):
super().__init__(parent, controller)
self.dance_name_var = StringVar()
self.motion_score_var = StringVar()
self.recording_label = None
self.is_recording = False
self.recording_time = 0
self.timer_thread = None
self.timer_running = False
tb.Label(self, text="Tambah Data Gerakan Tari", font=("Helvetica", 16)).pack(pady=10)
video_label = tb.Label(self)
video_label.pack()
form_frame = ScrollableFrame(self)
form_frame.pack(fill="both", expand=True)
tb.Button(form_frame.frame, text="Mulai Rekam", command=self.start_recording).pack(pady=5)
tb.Button(form_frame.frame, text="Berhenti Rekam", command=self.stop_recording).pack(pady=5)
self.recording_label = tb.Label(form_frame.frame, text="", font=("Helvetica", 12), bootstyle="danger")
self.recording_label.pack(pady=5)
self.timer_label = tb.Label(form_frame.frame, text="Waktu: 00:00", font=("Helvetica", 12), bootstyle="info")
self.timer_label.pack(pady=5)
tb.Label(form_frame.frame, text="Nama Tari:").pack(pady=5)
tb.Entry(form_frame.frame, textvariable=self.dance_name_var).pack(pady=5)
tb.Label(form_frame.frame, text="Nilai Gerakan:").pack(pady=5)
tb.Entry(form_frame.frame, textvariable=self.motion_score_var).pack(pady=5)
tb.Button(form_frame.frame, text="Simpan Gerakan", command=self.save_motion).pack(pady=5)
tb.Button(form_frame.frame, text="Kembali", command=lambda: controller.show_frame("HomePage")).pack(pady=5)
self.pose = mp_pose.Pose()
update_frame(video_label, self.pose, self.capture_landmarks)
def start_recording(self):
global recording, recorded_data
if self.is_recording:
messagebox.showinfo("Info", "Perekaman sudah dimulai.")
return
recording = True
recorded_data = []
self.is_recording = True
self.recording_time = 0
self.recording_label.config(text="Sedang merekam...")
self.timer_label.config(text="Waktu: 00:00")
logger.info("Started recording motion.")
self.update_timer()
def update_timer(self):
"""Update the timer while recording using `after`."""
if self.is_recording:
minutes, seconds = divmod(self.recording_time, 60)
timer_text = f"Waktu: {minutes:02}:{seconds:02}"
self.timer_label.config(text=timer_text)
self.recording_time += 1
self.after(1000, self.update_timer)
def stop_recording(self):
global recording
if not self.is_recording:
messagebox.showinfo("Info", "Perekaman belum dimulai.")
return
recording = False
self.is_recording = False
self.recording_label.config(text="")
self.timer_label.config(text="Waktu: 00:00")
logger.info("Stopped recording motion.")
messagebox.showinfo("Rekam Selesai", "Perekaman gerakan selesai. Silakan masukkan nama tari dan nilai.")
def capture_landmarks(self, landmarks):
if self.is_recording:
if len(recorded_data) % (3 * 30) == 0: # 3 detik
filtered_landmarks = filter_landmarks(landmarks)
logger.debug(f"Captured landmarks: {filtered_landmarks}")
recorded_data.append(filtered_landmarks)
def save_motion(self):
if self.is_recording:
messagebox.showwarning("Peringatan", "Harap hentikan perekaman sebelum menyimpan data.")
return
dance_name = self.dance_name_var.get().strip()
motion_score = self.motion_score_var.get().strip()
if not recorded_data:
messagebox.showwarning("Peringatan", "Tidak ada gerakan yang direkam. Mulai rekam terlebih dahulu.")
return
if not dance_name:
messagebox.showwarning("Peringatan", "Nama tari tidak boleh kosong.")
return
if not motion_score.isdigit():
messagebox.showwarning("Peringatan", "Skor harus berupa angka.")
return
add_dance(dance_name)
dance_id = [dance[0] for dance in get_all_dances() if dance[1] == dance_name][0]
add_motion(dance_id, recorded_data, float(motion_score))
evaluate_page = self.controller.frames["EvaluatePage"]
evaluate_page.update_dropdown()
messagebox.showinfo("Sukses", f"Gerakan disimpan untuk tari '{dance_name}'.")
class EvaluatePage(Page):
def __init__(self, parent, controller):
super().__init__(parent, controller)
self.selected_dance_var = StringVar()
self.is_evaluating = False
self.best_score = 0
tb.Label(self, text="Penilaian Gerakan Tari", font=("Helvetica", 16)).pack(pady=10)
video_label = tb.Label(self)
video_label.pack()
dropdown_frame = ScrollableFrame(self)
dropdown_frame.pack(fill="both", expand=True)
tb.Label(dropdown_frame.frame, text="Pilih Tari:").pack(pady=5)
self.dropdown = ttk.Combobox(dropdown_frame.frame, textvariable=self.selected_dance_var, state="readonly")
self.dropdown.pack(pady=5)
self.update_dropdown()
tb.Button(dropdown_frame.frame, text="Mulai Evaluasi", command=self.start_evaluation).pack(pady=5)
tb.Button(dropdown_frame.frame, text="Berhenti Evaluasi", command=self.stop_evaluation).pack(pady=5)
self.evaluating_label = tb.Label(dropdown_frame.frame, text="", font=("Helvetica", 12), bootstyle="danger")
self.evaluating_label.pack(pady=5)
tb.Button(dropdown_frame.frame, text="Kembali", command=lambda: controller.show_frame("HomePage")).pack(pady=5)
self.pose = mp_pose.Pose()
update_frame(video_label, self.pose, self.evaluate_frame_callback)
def start_evaluation(self):
dance_name = self.selected_dance_var.get()
if not dance_name:
messagebox.showwarning("Peringatan", "Pilih tari terlebih dahulu.")
return
self.dance_id = [dance[0] for dance in get_all_dances() if dance[1] == dance_name][0]
self.is_evaluating = True
self.best_score = 0
recorded_data.clear()
self.evaluating_label.config(text="Sedang mengevaluasi...")
logger.info("Started evaluation.")
def stop_evaluation(self):
if not self.is_evaluating:
messagebox.showinfo("Info", "Evaluasi belum dimulai.")
return
self.is_evaluating = False
self.evaluating_label.config(text="")
logger.info("Stopped evaluation.")
messagebox.showinfo("Hasil Evaluasi", f"Evaluasi selesai. Skor terbaik: {self.best_score}")
# def evaluate_frame_callback(self, landmarks):
# if self.is_evaluating:
# filtered_landmarks = filter_landmarks(landmarks)
# recorded_data.append(filtered_landmarks)
# motions = get_dance_motions(self.dance_id)
# for motion in motions:
# reference_landmarks = json.loads(motion[0])
# score = calculate_score(reference_landmarks, recorded_data)
# self.best_score = max(self.best_score, score)
# logger.debug(f"Current best score: {self.best_score}")
def evaluate_frame_callback(self, landmarks):
if self.is_evaluating:
# Filter landmarks
filtered_landmarks = filter_landmarks(landmarks)
# Limit the size of recorded_data to avoid excessive memory usage
max_recorded_frames = 30 # Keep only the last 30 frames
recorded_data.append(filtered_landmarks)
if len(recorded_data) > max_recorded_frames:
recorded_data.pop(0)
# Fetch reference motions only once
if not hasattr(self, 'reference_motions'):
self.reference_motions = get_dance_motions(self.dance_id)
# Evaluate current frame against reference motions
for motion in self.reference_motions:
try:
reference_landmarks = json.loads(motion[0]) # Landmarks
reference_score = motion[1] # Score from database
score = calculate_score(reference_landmarks, recorded_data, reference_score)
self.best_score = max(self.best_score, score)
except (TypeError, json.JSONDecodeError) as e:
logger.error(f"Error processing motion data: {e}")
continue
logger.debug(f"Current best score: {self.best_score}")
def update_dropdown(self):
dances = get_all_dances()
self.dropdown["values"] = [dance[1] for dance in dances]
class HomePage(Page):
def __init__(self, parent, controller):
super().__init__(parent, controller)
tb.Label(self, text="Sistem Penilaian Gerakan Tari", font=("Helvetica", 20)).pack(pady=20)
tb.Button(self, text="Tambah Data Tari", command=lambda: controller.show_frame("AddDataPage")).pack(pady=10)
tb.Button(self, text="Penilaian Tari", command=lambda: controller.show_frame("EvaluatePage")).pack(pady=10)
tb.Button(self, text="Keluar", command=self.quit_application).pack(pady=10)
def quit_application(self):
release_resources()
self.controller.destroy()
class DanceApp(tb.Window):
def __init__(self):
super().__init__(themename="morph")
self.title("Dance Evaluation App")
self.geometry("800x600")
self.center_window(800, 600)
self.frames = {}
container = tb.Frame(self)
container.pack(fill="both", expand=True)
for PageClass in (HomePage, AddDataPage, EvaluatePage):
page_name = PageClass.__name__
frame = PageClass(parent=container, controller=self)
self.frames[page_name] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame("HomePage")
def center_window(self, width, height):
"""Memusatkan jendela aplikasi pada layar."""
screen_width = self.winfo_screenwidth()
screen_height = self.winfo_screenheight()
x_coordinate = (screen_width // 2) - (width // 2)
y_coordinate = (screen_height // 2) - (height // 2)
self.geometry(f"{width}x{height}+{x_coordinate}+{y_coordinate}")
def show_frame(self, page_name):
frame = self.frames[page_name]
frame.tkraise()
def release_resources():
if cap.isOpened():
cap.release()
cv2.destroyAllWindows()
logger.info("Resources released.")
if __name__ == "__main__":
print("Starting application.")
cap = cv2.VideoCapture(0)
if not cap.isOpened():
messagebox.showerror("Error", "Gagal membuka kamera. Pastikan kamera terhubung.")
exit()
app = DanceApp()
atexit.register(release_resources)
app.mainloop()