From 5ab222aee32f410adf848e2d3d3a5d8a8e328382 Mon Sep 17 00:00:00 2001
From: Tulis <79586741+Tulis12@users.noreply.github.com>
Date: Wed, 10 Sep 2025 19:51:05 +0200
Subject: [PATCH] working
---
.idea/vcs.xml | 2 -
.idea/workspace.xml | 112 ++++++++++----------
main.py | 244 +++++++++++++++++---------------------------
utils.py | 4 +
4 files changed, 154 insertions(+), 208 deletions(-)
diff --git a/.idea/vcs.xml b/.idea/vcs.xml
index b5f1a71..94a25f7 100644
--- a/.idea/vcs.xml
+++ b/.idea/vcs.xml
@@ -2,7 +2,5 @@
-
-
\ No newline at end of file
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 7b5c87e..556dd2c 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -4,29 +4,11 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
-
-
-
-
-
-
-
-
+
+
@@ -52,45 +34,46 @@
- {
+ "keyToString": {
+ "ModuleVcsDetector.initialDetectionPerformed": "true",
+ "Python.02_whole_body_from_image.executor": "Run",
+ "Python.3d.executor": "Run",
+ "Python.body3d.executor": "Run",
+ "Python.body3d_pose_lifter_demo.executor": "Run",
+ "Python.calculate.executor": "Run",
+ "Python.checkpoint.executor": "Run",
+ "Python.draw.executor": "Run",
+ "Python.filter.executor": "Run",
+ "Python.is_torch.executor": "Run",
+ "Python.local_visualizer_3d.executor": "Run",
+ "Python.main.executor": "Run",
+ "Python.openpose.executor": "Run",
+ "Python.receive_images.executor": "Run",
+ "Python.receiver.executor": "Run",
+ "Python.sender.executor": "Run",
+ "Python.test.executor": "Run",
+ "Python.ultralytics-test.executor": "Run",
+ "RunOnceActivity.ShowReadmeOnStart": "true",
+ "RunOnceActivity.TerminalTabsStorage.copyFrom.TerminalArrangementManager.252": "true",
+ "RunOnceActivity.git.unshallow": "true",
+ "git-widget-placeholder": "main",
+ "last_opened_file_path": "C:/Users/Kajetan/PycharmProjects/JustTwerk",
+ "node.js.detected.package.eslint": "true",
+ "node.js.detected.package.tslint": "true",
+ "node.js.selected.package.eslint": "(autodetect)",
+ "node.js.selected.package.tslint": "(autodetect)",
+ "nodejs_package_manager_path": "npm",
+ "settings.editor.selected.configurable": "configurable.group.editor",
+ "vue.rearranger.settings.migration": "true"
}
-}]]>
+}
-
+
@@ -166,7 +149,10 @@
-
+
+
+
+
@@ -176,7 +162,15 @@
1755963464017
-
+
+
+ 1756143470328
+
+
+
+ 1756143470328
+
+
@@ -184,9 +178,11 @@
-
+
+
+
diff --git a/main.py b/main.py
index dd4c1d1..c575fef 100644
--- a/main.py
+++ b/main.py
@@ -1,52 +1,23 @@
import pickle
import sys
-import regex as re
-import numpy as np
from ultralytics import YOLO
import cv2
import time
from calculate import normalize_pose, compare_poses_boolean
from draw import draw_new
+from video_methods import initialize_method
model = YOLO("yolo11x-pose.pt")
-if len(sys.argv) == 1:
- method = sys.argv[1]
+if len(sys.argv) == 2:
+ method_type = sys.argv[1]
else:
- print("Podaj argument 'cam', albo 'network'.")
+ print("Podaj argument 'cam', albo 'net'.")
exit(1)
-cap = cv2.VideoCapture(0)
-
-if not cap.isOpened():
- print("Nie można otworzyć kamerki")
- exit(1)
-
-last_time = time.time()
-
-
-startTime = time.time()
-stage = 0
-
-pose = normalize_pose(np.array([[ 353.17, 107.28],
- [ 363.3, 96.435],
- [ 347.1, 98.647],
- [ 390.12, 99.096],
- [ 346.09, 103.63],
- [ 425.77, 149.95],
- [ 327.92, 153.12],
- [ 495.16, 169.82],
- [ 260.86, 166.3],
- [ 565.53, 182.68],
- [ 192.34, 170.58],
- [ 393.83, 316.99],
- [ 337.05, 316.41],
- [ 388.36, 433.37],
- [ 333.88, 431.89],
- [ 383.58, 479.16],
- [ 330.89, 480]]))
+method = initialize_method(method_type)
do_pose_shot = False
@@ -56,149 +27,126 @@ def click_event(event, x, y, flags, param):
if event == cv2.EVENT_LBUTTONDOWN: # lewy przycisk myszy
do_pose_shot = not do_pose_shot
-currTimeIndex = 0
-currIndex = None
-currMove = None
-currStatus = "Zacznij tanczyc"
+def main():
+ last_time = time.time()
-mehCount = 0
-goodCount = 0
-failCount = 0
-failRate = 2
+ currTimeIndex = 0
+ currIndex = None
+ currMove = None
+ currStatus = "Zacznij tanczyc"
-def wczytaj_dane_z_txt(sciezka):
- wynik = []
- with open(sciezka, "r") as f:
- zawartosc = f.read()
+ mehCount = 0
+ goodCount = 0
+ failCount = 0
+ failRate = 2
- # Znajdź wszystkie krotki w formacie (float, array([...]))
- pattern = re.compile(r"\(([^,]+),\s*array\((\[.*?\]),\s*dtype=float32\)\)")
- matches = pattern.findall(zawartosc)
+ moves = []
- for m in matches:
- liczba = float(m[0])
- tablica = np.array(eval(m[1]), dtype=np.float32)
- wynik.append((liczba, tablica))
+ with open('moves.pkl', 'rb') as f: # 'rb' = read binary
+ moves = pickle.load(f)
- return wynik
+ startValue = moves[0][0]
+ totalCount = len(moves)
-moves = []
+ for i, move in enumerate(moves):
+ moves[i] = (move[0] - startValue, move[1], move[2])
-with open('moves.pkl', 'rb') as f: # 'rb' = read binary
- moves = pickle.load(f)
+ while True:
+ frame = method.receive_frame()
-startValue = moves[0][0]
-totalCount = len(moves)
+ frame = cv2.flip(frame, 1)
-for i, move in enumerate(moves):
- moves[i] = (move[0] - startValue, move[1], move[2])
+ results = model(frame, verbose=False)
-def find_closest(target):
- global moves
- return min(moves, key=lambda t: abs(t[0] - target))
+ current_time = time.time()
+ delta = current_time - last_time
+ last_time = current_time
-while True:
- ret, frame = cap.read()
- frame = cv2.flip(frame, 1)
+ fps = 1 / delta if delta > 0 else float('inf')
+ # print(f"\rDelta: {delta:.4f}s, FPS: {fps:.2f}", end="")
- results = model(frame, verbose=False)
+ for result in results:
+ kpts = result.keypoints.data[0] if len(result.keypoints.data) else None
- current_time = time.time()
- delta = current_time - last_time
- last_time = current_time
+ if kpts is None:
+ continue
- fps = 1 / delta if delta > 0 else float('inf')
- # print(f"\rDelta: {delta:.4f}s, FPS: {fps:.2f}", end="")
+ img = frame
- for result in results:
- kpts = result.keypoints.data[0] if len(result.keypoints.data) else None
+ normalized = normalize_pose(result.keypoints.xy.cpu().numpy()[0])
+ cv2.imshow('you', draw_new(result.keypoints.xy.cpu()[0]))
- if kpts is None:
- continue
+ if currTimeIndex != 0 and moves.index(find_closest(time.time() - currTimeIndex)) == len(moves) - 1:
+ mehCount = totalCount - failCount - goodCount
- img = frame
+ print(
+ f"PODSUMOWANIE: FAIL {failCount} MEH: {mehCount} PERFECT: {goodCount} PERCENTAGE: {(goodCount + (0.95 * mehCount)) / totalCount * 100}%")
+ exit(1)
- normalized = normalize_pose(result.keypoints.xy.cpu().numpy()[0])
- cv2.imshow('you', draw_new(result.keypoints.xy.cpu()[0]))
+ if currMove is None:
+ if compare_poses_boolean(moves[0][1], normalized):
+ currIndex = 1
+ currTimeIndex = time.time()
+ deltaTime = time.time()
+ currStatus = f"Zaczoles tanczyc {currIndex}"
+ currMove = moves[0]
- if currTimeIndex != 0 and moves.index(find_closest(time.time() - currTimeIndex)) == len(moves) - 1:
- mehCount = totalCount - failCount - goodCount
+ # thread = Thread(target=print_animation, args=(moves, False))
+ # thread.start()
+ else:
+ changed = False
- print(f"PODSUMOWANIE: FAIL {failCount} MEH: {mehCount} PERFECT: {goodCount} PERCENTAGE: {(goodCount + (0.95 * mehCount)) / totalCount * 100}%")
- exit(1)
+ closest = find_closest(time.time() - currTimeIndex)
+ cv2.imshow('Dots', draw_new(closest[2]))
- if currMove is None:
- if compare_poses_boolean(moves[0][1], normalized):
- currIndex = 1
- currTimeIndex = time.time()
- deltaTime = time.time()
- currStatus = f"Zaczoles tanczyc {currIndex}"
- currMove = moves[0]
+ if abs((time.time() - currTimeIndex) - moves[currIndex][0]) > failRate:
+ currStatus = f"FAIL!"
+ failCount += 1
- # thread = Thread(target=print_animation, args=(moves, False))
- # thread.start()
- else:
- changed = False
+ if compare_poses_boolean(closest[1], normalized):
+ # delays += (time.time() - deltaTime - moves[0][0]) * 1000
+ # delaysCount += 1
- closest = find_closest(time.time() - currTimeIndex)
- cv2.imshow('Dots', draw_new(closest[2]))
+ currStatus = f"SUPER! {currIndex} Zostalo {len(moves)} Delay {(time.time() - currTimeIndex - closest[0]) / 1000}ms"
+ deltaTime = time.time()
- if abs((time.time() - currTimeIndex) - moves[currIndex][0]) > failRate:
- currStatus = f"FAIL!"
- failCount += 1
+ currIndex = moves.index(closest) + 1
+ goodCount += 1
+ changed = True
- if compare_poses_boolean(closest[1], normalized):
- # delays += (time.time() - deltaTime - moves[0][0]) * 1000
- # delaysCount += 1
+ if not changed and compare_poses_boolean(moves[currIndex][1], normalized):
+ # delays += (time.time() - deltaTime - moves[0][0]) * 1000
+ # delaysCount += 1
- currStatus = f"SUPER! {currIndex} Zostalo {len(moves)} Delay {(time.time() - currTimeIndex - closest[0]) / 1000}ms"
- deltaTime = time.time()
+ currStatus = f"SUPER! {currIndex} Zostalo {len(moves)} Delay {(time.time() - currTimeIndex - closest[0]) / 1000}ms"
+ deltaTime = time.time()
- currIndex = moves.index(closest) + 1
- goodCount += 1
- changed = True
+ changed = True
- if not changed and compare_poses_boolean(moves[currIndex][1], normalized):
- # delays += (time.time() - deltaTime - moves[0][0]) * 1000
- # delaysCount += 1
+ currIndex += 1
+ goodCount += 1
- currStatus = f"SUPER! {currIndex} Zostalo {len(moves)} Delay {(time.time() - currTimeIndex - closest[0]) / 1000}ms"
- deltaTime = time.time()
+ # if do_pose_shot:
+ # moves.append((time.time() - startTime, normalize_pose(result.keypoints.xy.cpu().numpy()[0]), result.keypoints.xy.cpu()[0]))
+ # elif len(moves) != 0:
+ # with open('moves.pkl', 'wb') as f: # 'wb' = write binary
+ # pickle.dump(moves, f)
+ #
+ # exit(1)
- changed = True
+ cv2.putText(
+ img, # obraz
+ currStatus, # tekst
+ (50, 100), # pozycja (x, y) lewego dolnego rogu tekstu
+ cv2.FONT_HERSHEY_SIMPLEX, # czcionka
+ 1, # rozmiar (skalowanie)
+ (0, 0, 255), # kolor (BGR) - tutaj czerwony
+ 2, # grubość linii
+ cv2.LINE_AA # typ antyaliasingu
+ )
- currIndex += 1
- goodCount += 1
+ cv2.imshow('Klatka z kamerki', img)
+ cv2.setMouseCallback('Klatka z kamerki', click_event)
+ cv2.waitKey(1) # Czekaj na naciśnięcie klawisza
- # if do_pose_shot:
- # moves.append((time.time() - startTime, normalize_pose(result.keypoints.xy.cpu().numpy()[0]), result.keypoints.xy.cpu()[0]))
- # elif len(moves) != 0:
- # with open('moves.pkl', 'wb') as f: # 'wb' = write binary
- # pickle.dump(moves, f)
- #
- # exit(1)
-
- cv2.putText(
- img, # obraz
- currStatus, # tekst
- (50, 100), # pozycja (x, y) lewego dolnego rogu tekstu
- cv2.FONT_HERSHEY_SIMPLEX, # czcionka
- 1, # rozmiar (skalowanie)
- (0, 0, 255), # kolor (BGR) - tutaj czerwony
- 2, # grubość linii
- cv2.LINE_AA # typ antyaliasingu
- )
-
- cv2.imshow('Klatka z kamerki', img)
- cv2.setMouseCallback('Klatka z kamerki', click_event)
- cv2.waitKey(1) # Czekaj na naciśnięcie klawisza
-
-
-# Access the results
-for result in results:
- annotated_frame = result.plot() # zwraca obraz z naniesionymi keypoints
-
- # Wyświetlenie obrazu przy użyciu OpenCV
- cv2.imshow("Pose", annotated_frame)
- cv2.waitKey(0)
-cv2.destroyAllWindows()
\ No newline at end of file
+main()
\ No newline at end of file
diff --git a/utils.py b/utils.py
index 77e78e3..d8e7725 100644
--- a/utils.py
+++ b/utils.py
@@ -10,6 +10,10 @@ def recvall(sock, n):
data += packet
return data
+def find_closest(target):
+ global moves
+ return min(moves, key=lambda t: abs(t[0] - target))
+
def resize_with_padding(image, target_size=(640, 640)):
h, w = image.shape[:2]
target_w, target_h = target_size