-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
95 lines (86 loc) · 3.62 KB
/
utils.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
import csv
import numpy as np
import yaml
import cv2
import pandas as pd
import mediapipe as mp
from torch.utils.data import Dataset
import torch
def is_handsign_character(char:str):
return ord('a') <= ord(char) <=ord("z") or char == " "
def label_dict_from_config_file(relative_path):
with open(relative_path,"r") as f:
label_tag = yaml.full_load(f)["gestures"]
return label_tag
def test_all_resolution():
url = "https://en.wikipedia.org/wiki/List_of_common_resolutions"
table = pd.read_html(url)[0]
table.columns = table.columns.droplevel()
cap = cv2.VideoCapture(0)
resolutions = {}
for index, row in table[["W", "H"]].iterrows():
cap.set(cv2.CAP_PROP_FRAME_WIDTH, row["W"])
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, row["H"])
width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
resolutions[str(width)+"x"+str(height)] = "OK"
print(resolutions)
class HandDatasetWriter():
def __init__(self,filepath) -> None:
self.csv_file = open(filepath,"a")
self.file_writer = csv.writer(self.csv_file,delimiter=',',quotechar='|',quoting=csv.QUOTE_MINIMAL)
def add(self,hand,label):
self.file_writer.writerow([label,*np.array(hand).flatten().tolist()])
def close(self):
self.csv_file.close()
class HandLandmarksDetector():
def __init__(self) -> None:
self.mp_drawing = mp.solutions.drawing_utils
self.mp_drawing_styles = mp.solutions.drawing_styles
self.mp_hands = mp.solutions.hands
self.detector = self.mp_hands.Hands(False,max_num_hands=1,min_detection_confidence=0.5)
def detectHand(self,frame):
hands = []
frame = cv2.flip(frame, 1)
annotated_image = frame.copy()
results = self.detector.process(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
if results.multi_hand_landmarks is not None:
for hand_landmarks in results.multi_hand_landmarks:
hand = []
self.mp_drawing.draw_landmarks(
annotated_image,
hand_landmarks,
self.mp_hands.HAND_CONNECTIONS,
self.mp_drawing_styles.get_default_hand_landmarks_style(),
self.mp_drawing_styles.get_default_hand_connections_style())
for landmark in hand_landmarks.landmark:
x,y,z = landmark.x,landmark.y,landmark.z
hand.extend([x,y,z])
hands.append(hand)
return hands,annotated_image
class CustomImageDataset(Dataset):
def __init__(self, data_file):
self.data = pd.read_csv(data_file)
# self.labels = torch.nn.functional.one_hot(torch.from_numpy(self.data.iloc[:,0].apply( lambda char:ord(char)-97).to_numpy()))
self.labels = torch.from_numpy(self.data.iloc[:,0].to_numpy())
def __len__(self):
return len(self.data)
def __getitem__(self, idx):
one_hot_label = self.labels[idx]
torch_data = torch.from_numpy(self.data.iloc[idx,1:].to_numpy(dtype=np.float32))
return torch_data, one_hot_label
class EarlyStopper:
def __init__(self, patience=1, min_delta=0):
self.patience = patience
self.min_delta = min_delta
self.counter = 0
self.watched_metrics = np.inf
def early_stop(self, current_value):
if current_value < self.watched_metrics:
self.watched_metrics = current_value
self.counter = 0
elif current_value > (self.watched_metrics + self.min_delta):
self.counter += 1
if self.counter >= self.patience:
return True
return False