-
Notifications
You must be signed in to change notification settings - Fork 7
/
hand_detector_utils.py
128 lines (92 loc) · 4.05 KB
/
hand_detector_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
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
# -*- coding: utf-8 -*-
"""
File containing function for the hand detector. There are some method
@author: Alberto Zancanaro (Jesus)
"""
#%%
import numpy as np
import cv2
import math
import socket
import time
import torch
UDP_IP = "127.0.0.1"
UDP_PORT = 5065
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
def sendCommand(sock, UDP_IP, UDP_PORT, command, debug_var = True):
sock.sendto((command).encode(), (UDP_IP, UDP_PORT) )
if(debug_var): print("_"*10, command, " sent!", "_"*10)
def getBiggestBox(matrix_boxes_predicted):
"""
Return the biggest or the two biggest boxes.
"""
tmp_mat = np.zeros((2, 4))
vet_area = np.zeros(matrix_boxes_predicted.shape[0])
for i in range(len(vet_area)):
vet_area[i] = (matrix_boxes_predicted[i, 2] - matrix_boxes_predicted[i, 0]) * matrix_boxes_predicted[i, 3] - matrix_boxes_predicted[i, 1]
# Get the index of the two biggest box
ind = vet_area.argsort()[-2:][::-1]
tmp_mat[:, :] = matrix_boxes_predicted[ind, :]
if(checkIfInside(tmp_mat[0,:], tmp_mat[1,:])): # If one rect is inside another I return the biggest one
# print(tmp_mat)
# print("argmax: ", np.argmax(vet_area))
# print("vet_area: ", vet_area)
# print(matrix_boxes_predicted[np.argmax(vet_area), :])
tmp_mat = np.zeros((1, 4))
# tmp_mat[0, :] = matrix_boxes_predicted[np.argmax(vet_area), :]
tmp_mat[0, :] = matrix_boxes_predicted[ind[0], :]
return tmp_mat.astype(int)
else:
# print("tmp_mat: ", tmp_mat)
return tmp_mat.astype(int) # Otherwise I return both
def checkIfInside(rect1, rect2):
"""
Check if rect2 is inside rect1. The two rect are specified by a vector of 4 elements.
The first two are the upper left corner and the last two are the down right corner
"""
# Check if the upper left corner of rect2 is inside rect1
if(rect2[0] >= rect1[0] and rect2[0] <= rect1[2]):
if(rect2[1] >= rect1[1] and rect2[1] <= rect1[3]):
return True
# Check if the upper right corner of rect2 is inside rect1
if(rect2[2] >= rect1[0] and rect2[2] <= rect1[2]):
if(rect2[1] >= rect1[1] and rect2[1] <= rect1[3]):
return True
# Check if the down left corner of rect2 is inside rect1
if(rect2[0] >= rect1[0] and rect2[0] <= rect1[2]):
if(rect2[3] >= rect1[1] and rect2[3] <= rect1[3]):
return True
# Check if the down right corner of rect2 is inside rect1
if(rect2[2] >= rect1[0] and rect2[2] <= rect1[2]):
if(rect2[3] >= rect1[1] and rect2[3] <= rect1[3]):
return True
return False
def trackingHandWithRCNN(model, img, device):
img = torch.from_numpy(img).float().to(device)
img = img / 255
img = img.permute(2, 0, 1)
img = img.to(device)
predictions = model([img])
boxes_predict = predictions[0]['boxes'].cpu().detach().numpy().astype(int)
# print(boxes_predict)
if(boxes_predict.shape[0] >= 2):
return getBiggestBox(boxes_predict)
else: return boxes_predict
def centralPointInBox(box):
x = int(box[0] + (box[2] - box[0])/2)
y = int(box[1] + (box[3] - box[1])/2)
return (x, y)
def predictFingers(model, img, device, model_input_size = (140, 140)):
# Resize to the necessary input size
hand = cv2.resize(img, model_input_size)
# Convert the image in a Pytorch tensor, normalize, swap axis and move to GPU (if present)
hand_tensor = torch.from_numpy(hand).float().to(device)
hand_tensor = hand_tensor / 255
hand_tensor = hand_tensor.permute(2, 0, 1)
hand_tensor.to(device)
# Predict number of fingers and convert result in a numpy array
finger_predict = model(hand_tensor.unsqueeze(0))
finger_predict = np.argmax(finger_predict.cpu().detach().numpy())
# cv2.putText(frame, str(finger_predict), (50,50), cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 0, 255))
# print(finger_predict)
return finger_predict