└── deteccao-de-gestos-com-automacao-DIO.py /deteccao-de-gestos-com-automacao-DIO.py: -------------------------------------------------------------------------------- 1 | import cv2 #importando o OPenCV 2 | import os 3 | import numpy as np 4 | import math 5 | cap = cv2.VideoCapture(0) 6 | 7 | while(1): 8 | 9 | try: #um erro ocorre se não encontrar nada na janela, pois não 10 | #pode encontrar o contorno da área máxima #portanto, esta instrução de erro try 11 | 12 | ret, frame = cap.read() 13 | frame=cv2.flip(frame,1) 14 | kernel = np.ones((3,3),np.uint8) 15 | 16 | #Define a região de interesse - máscara de análise do objeto 17 | roi=frame[100:300, 100:300] #tamanho da máscara 18 | 19 | 20 | cv2.rectangle(frame,(100,100),(300,300),(0,255,0),0) #leitura da máscara 21 | hsv = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV) #conversão do padrão RGB para HSV 22 | 23 | 24 | 25 | # definir gama de cor da pele em HSV 26 | lower_skin = np.array([0,20,70], dtype=np.uint8) 27 | upper_skin = np.array([20,255,255], dtype=np.uint8) 28 | 29 | ##extrair imagem do contorno da pele em relação ao fundo do objeto 30 | mask = cv2.inRange(hsv, lower_skin, upper_skin) 31 | 32 | 33 | #dilatação na mão para preencher manchas escuras dentro 34 | mask = cv2.dilate(mask,kernel,iterations = 4) 35 | 36 | #preencher mão com blur "borrar" na imagem 37 | mask = cv2.GaussianBlur(mask,(5,5),100) 38 | 39 | 40 | 41 | #Encontrando os contornos 42 | 43 | contours, hierarchy = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) 44 | 45 | #Encontre o contorno máximo da mão 46 | cnt = max(contours, key = lambda x: cv2.contourArea(x)) 47 | #função aplicada para detectar o contorno do objeto binarizado 48 | 49 | #aproximação do contorno ao objeto 50 | epsilon = 0.0005*cv2.arcLength(cnt,True) 51 | approx= cv2.approxPolyDP(cnt,epsilon,True) 52 | 53 | 54 | #fazer um objeto convexo ao redor da mão 55 | hull = cv2.convexHull(cnt) 56 | 57 | #define area of hull 58 | areahull = cv2.contourArea(hull) 59 | areacnt = cv2.contourArea(cnt) 60 | 61 | #define área do objeto e área da mão 62 | arearatio=((areahull-areacnt)/areacnt)*100 63 | 64 | #encontre os defeitos no objeto convexo em relação à mão 65 | hull = cv2.convexHull(approx, returnPoints=False) 66 | defects = cv2.convexityDefects(approx, hull) 67 | 68 | # l = sem defeitos 69 | l=0 70 | 71 | #definindo a região de interesse 72 | for i in range(defects.shape[0]): 73 | s,e,f,d = defects[i,0] 74 | start = tuple(approx[s][0]) 75 | end = tuple(approx[e][0]) 76 | far = tuple(approx[f][0]) 77 | pt= (100,180) 78 | 79 | 80 | # encontrar o comprimento de todos os lados do triângulo 81 | a = math.sqrt((end[0] - start[0])**2 + (end[1] - start[1])**2) 82 | b = math.sqrt((far[0] - start[0])**2 + (far[1] - start[1])**2) 83 | c = math.sqrt((end[0] - far[0])**2 + (end[1] - far[1])**2) 84 | s = (a+b+c)/2 85 | ar = math.sqrt(s*(s-a)*(s-b)*(s-c)) 86 | 87 | #calculo do perimetro do objeto 88 | 89 | #distância entre o ponto e o casco convexo 90 | d=(2*ar)/a 91 | 92 | # distância entre o ponto e o objeto convexo 93 | angle = math.acos((b**2 + c**2 - a**2)/(2*b*c)) * 57 94 | 95 | 96 | # ignore ângulos > 90 e ignore pontos muito próximos ao objeto convexo (geralmente vêm devido ao ruído) 97 | if angle <= 90 and d>30: 98 | l += 1 99 | cv2.circle(roi, far, 3, [255,0,0], -1) 100 | 101 | #desenhar linhas ao redor da mão 102 | cv2.line(roi,start, end, [0,255,0], 2) 103 | #o ROI define uma região de interesse 104 | 105 | 106 | l+=1 107 | 108 | #imprimir gestos correspondentes que estão em seus intervalos 109 | font = cv2.FONT_HERSHEY_SIMPLEX 110 | if l==1: 111 | 112 | if areacnt<2000: #significa que não existe objeto na região 113 | cv2.putText(frame,'Esperando dados',(0,50), font, 2, (0,0,255), 3, cv2.LINE_AA) 114 | else: 115 | executado = False 116 | if arearatio<12 and not executado: 117 | cv2.putText(frame,'0 = Navegador',(0,50), font, 2, (0,0,255), 3, cv2.LINE_AA) 118 | #os.system("start Chrome.exe --window-size=800,600") 119 | executado = True 120 | #break 121 | 122 | 123 | 124 | elif arearatio<17.5: 125 | cv2.putText(frame,'',(0,50), font, 2, (0,0,255), 3, cv2.LINE_AA) 126 | #os.system("start Arduino IDE.exe") 127 | 128 | 129 | else: 130 | cv2.putText(frame,'1 = Word',(0,50), font, 2, (0,0,255), 3, cv2.LINE_AA) 131 | #os.system("start WINWORD.EXE --window-size=600,400") 132 | #break 133 | 134 | elif l==2: 135 | cv2.putText(frame,'2 = Excel',(0,50), font, 2, (0,0,255), 3, cv2.LINE_AA) 136 | #os.system("start Excel.exe --window-size=600,400") 137 | #break 138 | 139 | 140 | elif l==3: 141 | 142 | if arearatio<27: 143 | cv2.putText(frame,'3 = Power Point',(0,50), font, 2, (0,0,255), 3, cv2.LINE_AA) 144 | #os.system("start POWERPNT.EXE --window-size=600,400") 145 | 146 | else: 147 | cv2.putText(frame,'ok',(0,50), font, 2, (0,0,255), 3, cv2.LINE_AA) 148 | 149 | 150 | elif l==4: 151 | cv2.putText(frame,'',(0,50), font, 2, (0,0,255), 3, cv2.LINE_AA) 152 | #os.system("start firefox.exe") 153 | 154 | elif l==5: 155 | cv2.putText(frame,'',(0,50), font, 2, (0,0,255), 3, cv2.LINE_AA) 156 | #os.system("start Spyder.launch.pyw") 157 | 158 | elif l==6: 159 | cv2.putText(frame,'reposition',(0,50), font, 2, (0,0,255), 3, cv2.LINE_AA) 160 | 161 | else : 162 | cv2.putText(frame,'reposition',(10,50), font, 2, (0,0,255), 3, cv2.LINE_AA) 163 | 164 | #show the windows 165 | cv2.imshow('mask',mask) 166 | cv2.imshow('frame',frame) 167 | except: 168 | pass 169 | 170 | 171 | k = cv2.waitKey(5) & 0xFF 172 | if k == 27: 173 | break 174 | 175 | cv2.destroyAllWindows() 176 | cap.release() 177 | --------------------------------------------------------------------------------