-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFinal_Code_5524
124 lines (90 loc) · 4.23 KB
/
Final_Code_5524
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
import skimage
from skimage import io, transform, color, morphology, measure
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib as mt
import math
import warnings
warnings.simplefilter("ignore")
def NCC(patch, template):
row, column = template.shape[0], template.shape[1]
NCC_r = np.sum(((patch[:,:,0] - np.mean(patch[:,:,0])) * (template[:,:,0]- temp_mean[:,:,0]))/(np.std(patch[:,:,0], ddof=1) * temp_std[0]))
NCC_g = np.sum(((patch[:,:,1] - np.mean(patch[:,:,1])) * (template[:,:,1]- temp_mean[:,:,1]))/(np.std(patch[:,:,1], ddof=1) * temp_std[1]))
NCC_b = np.sum(((patch[:,:,2] - np.mean(patch[:,:,2])) * (template[:,:,2]- temp_mean[:,:,2]))/(np.std(patch[:,:,2], ddof=1) * temp_std[2]))
NCC = NCC_r + NCC_g + NCC_b
NCC = NCC / ((s_row * s_col)- 1)
return NCC
def detect_hand(image):
img = io.imread(image)
if img.ndim == 3 and img.shape[2] == 4:
img = color.rgba2rgb(img)
hsv_img = color.rgb2hsv(img)
lower_skin = np.array([0.05, 0.2, 0.2])
upper_skin = np.array([0.25, 0.7, 0.8])
skin_mask = ((hsv_img[:,:,0] > lower_skin[0]) & (hsv_img[:,:,0] < upper_skin[0]) &
(hsv_img[:,:,1] > lower_skin[1]) & (hsv_img[:,:,1] < upper_skin[1]) &
(hsv_img[:,:,2] > lower_skin[2]) & (hsv_img[:,:,2] < upper_skin[2]))
cleaned_mask = morphology.remove_small_objects(skin_mask, min_size=100)
cleaned_mask = morphology.closing(cleaned_mask, morphology.disk(7))
labeled_mask = measure.label(cleaned_mask)
regions = measure.regionprops(labeled_mask)
# integrtaing regions
min_row, min_col, max_row, max_col = (np.inf, np.inf, -np.inf, -np.inf)
for region in regions:
if region.area <= 8500:
min_row = min(min_row, region.bbox[0])
min_col = min(min_col, region.bbox[1])
max_row = max(max_row, region.bbox[2])
max_col = max(max_col, region.bbox[3])
fig, ax = plt.subplots(figsize=(10, 6))
ax.imshow(img)
if max_row > min_row and max_col > min_col:
hand_detected = True
rect = plt.Rectangle((min_col, min_row), max_col - min_col, max_row - min_row, edgecolor='red', linewidth=2, fill=False)
ax.add_patch(rect)
plt.axis('off')
plt.savefig("righthandeye_detected.jpg") #replace with appropraite filename to save
plt.show()
else:
hand_detected = False
print("No hand detected.")
plt.close()
return hand_detected
def plot_match(index, position):
match = search[int(sorted_patch_ncc[index, 1]):int(sorted_patch_ncc[index, 1]) + temp_row, int(sorted_patch_ncc[index, 0]):int(sorted_patch_ncc[index, 0]) + temp_col]
ax = fig.add_subplot(2, 3, position)
ax.imshow(match)
plt.title(f"k = {index + 1} \n Location = ({int(sorted_patch_ncc[index, 1] + temp_row/2)}, {int(sorted_patch_ncc[index, 0] + temp_col/2)})")
plt.axis("off")
s = io.imread('right.jpg')
t = io.imread('template_eye.png')
scale_factor = 0.3
# Downscale the image using the rescale function
search = transform.rescale(s, scale_factor, anti_aliasing=True, multichannel=True)
template= transform.rescale(t, scale_factor, anti_aliasing=True, multichannel=True)
temp_row, temp_col = template.shape[0], template.shape[1]
s_row, s_col = search.shape[0], search.shape[1]
temp_mean = np.mean(template, axis=(0, 1), keepdims=True)
temp_std = np.std(template, axis=(0, 1), ddof=1)
patch_ncc = []
for i in range(s_row - temp_row):
for j in range(s_col - temp_col):
patch = search[i:i+temp_row, j:j+temp_col]
ncc_ = NCC(patch, template)
patch_ncc.append([j, i, ncc_])
patch_ncc.sort(reverse=True, key=lambda x: x[2])
sorted_patch_ncc = np.array(patch_ncc)
#ncc_plot = pd.Series(sorted_patch_ncc[:, 2])
#ncc_plot.plot.line(xlabel='k', ylabel='NCC')
filename = "righteye_detected.jpg" #replace with appropraite filename to save
fig, ax = plt.subplots(figsize=(10, 6))
ax.imshow(search)
rect = plt.Rectangle((sorted_patch_ncc[1,0], sorted_patch_ncc[1,1]), temp_col, temp_row, edgecolor='red', linewidth=2, fill=False)
ax.add_patch(rect)
plt.axis('off')
plt.savefig(filename)
plt.show()
has_hand = detect_hand(filename)
if(has_hand):
print("Hi, How can I help you?")