forked from olincollege/just-dance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
just_dance_main.py
96 lines (81 loc) · 2.93 KB
/
just_dance_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
"""
Set 'JustDanceGame' class and 'run_game' function to link all classes together
"""
import csv
from just_dance_model import JustDanceModel
from just_dance_view import JustDanceView
from just_dance_controller import JustDanceController
class JustDanceGame:
"""
A class to represent the application run using the JustDanceModel,
JustDanceView, JustDanceController classes
Attributes:
model: An object representing the application model
view: An object representing the application display
controller: An object representing the application
controller using the user input
Methods:
run: Run the JustDance Application
"""
def __init__(self, model_path, video_path, camera_index):
"""
Initialize a new instance of the JustDanceGame Class
Args:
model_path: An object representing the TensorFlow model path
video_path: An object representing the dance video file path
camera_index: An object representing the camera index for
the user input camera feed
"""
self.model = JustDanceModel(model_path=model_path)
self.view = JustDanceView(model=self.model)
self.controller = JustDanceController(
model=self.model, video_path=video_path, camera_index=camera_index
)
self.score = 0
def run(self, song):
"""
Run the JustDance application
Args:
song: A string representing the chosen for the user to
dance to
"""
self.controller.play_sound("songs_audio/" + song + ".mp3")
self.controller.process_frames()
self.controller.release_capture()
self.controller.close_windows()
def calculate_final_score(self):
"""
Calculate the player's final score for the current dance song.
"""
self.score = self.model.final_score(
self.controller.angles_video, self.controller.angles_camera, 20
)
def store_leaderboard(self, csv_file):
"""
Store the player's score in a leaderboard CSV file.
Args:
csv_file (str): The name of the CSV file to store the leaderboard.
Returns:
None
"""
# Write scores to the file
with open(csv_file, "a", newline="", encoding="utf-8") as file:
writer = csv.writer(file)
writer.writerow([self.score])
def run_game(song):
"""
Create an instance of the JustDanceGame class and
run the game based on the chosen song.
Calculates and stores the user score in a csv.
Args:
song: A string representing the chosen song for the user
dance to.
"""
game = JustDanceGame(
model_path="model/model.tflite",
video_path="songs/" + song + ".mp4",
camera_index=0,
)
game.run(song)
game.calculate_final_score()
game.store_leaderboard("leaderboard.csv")