/
hand_extraction.py
79 lines (62 loc) · 2.71 KB
/
hand_extraction.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
import cv2
import numpy as np
import math
from utils import *
class HandExtraction:
def __init__(self):
self.trained_hand = False
self.hand_row_nw = None
self.hand_row_se = None
self.hand_col_nw = None
self.hand_col_se = None
self.hand_hist = None
def draw_hand_rect(self, frame):
rows,cols = frame.shape[:2]
dels = 20
#change distance between rects
self.hand_row_nw = np.array([6*rows/dels,6*rows/dels,6*rows/dels,
9*rows/dels,9*rows/dels,9*rows/dels,
12*rows/dels,12*rows/dels,12*rows/dels], dtype=np.int16)
self.hand_col_nw = np.array([9*cols/dels,10*cols/dels,11*cols/dels,
9*cols/dels,10*cols/dels,11*cols/dels,
9*cols/dels,10*cols/dels,11*cols/dels], dtype=np.int16)
self.hand_row_se = self.hand_row_nw + 50 #change height of rects
self.hand_col_se = self.hand_col_nw + 30 #change width of rects
size = self.hand_row_nw.size
for i in range(size):
cv2.rectangle(frame,(self.hand_col_nw[i],self.hand_row_nw[i]),(self.hand_col_se[i],self.hand_row_se[i]),(0,255,0),1)
black = np.zeros(frame.shape, dtype=frame.dtype)
frame_final = np.vstack([black, frame])
return frame_final
def train_hand(self, frame):
self.trained_hand = True
return self.set_hand_hist(frame)
#calculate color range for hand extraction based on colors from rects
def set_hand_hist(self, frame):
hsv = ycbcr(frame)
roi = np.zeros([90,10,3], dtype=hsv.dtype)
size = self.hand_row_nw.size
for i in range(size):
roi[i*10:i*10+10,0:10] = hsv[self.hand_row_nw[i]:self.hand_row_nw[i]+10, self.hand_col_nw[i]:self.hand_col_nw[i]+10]
temp_lo = []
temp_hi = []
for i in range(0, 3):
croped = roi[:, :, i].copy()
blured = gaussian_blur(croped, 9).flatten()
temp_lo.append(min(blured))
temp_hi.append(max(blured))
'''
temp_lo = []
temp_hi = []
for i in range(0, 2):
croped = roi[:, :, i].copy()
blured = gaussian_blur(croped, 9).flatten()
mul_min, mul_max = 1.0, 1.0
#if (i == 1):
#mul_min, mul_max = 0.95, 1.03
temp_lo.append(int (min(blured) * mul_min))
temp_hi.append(int (max(blured) * mul_max))
#temp_lo.append(0)
#temp_hi.append(255)
'''
return np.array(temp_lo), np.array(temp_hi)