-
Notifications
You must be signed in to change notification settings - Fork 0
/
skindetector.py
49 lines (35 loc) · 1.13 KB
/
skindetector.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
#Python Script To detect skin color from camera
import numpy as np
import cv2
def resize(image, width = None, height = None, inter = cv2.INTER_AREA):
dim = None
(h, w) = image.shape[:2]
if width is None and height is None:
return image
if width is None:
r = height / float(h)
dim = (int(w * r), height)
else:
r = width / float(w)
dim = (width, int(h * r))
resized = cv2.resize(image, dim, interpolation = inter)
return resized
#Colour Range for Skin
lower = np.array([0,48,80], dtype='uint8')
upper = np.array([20,255,255], dtype='uint8')
camera = cv2.VideoCapture(0)
while True:
(grabbed, frame) = camera.read()
frame=resize(frame,width=400)
converted = cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)
mask = cv2.inRange(converted,lower,upper)
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (11, 11))
mask = cv2.erode(mask, kernel, iterations = 2)
mask = cv2.dilate(mask, kernel, iterations = 2)
mask = cv2.GaussianBlur(mask, (3,3) , 0)
skin = cv2.bitwise_and(frame,frame,mask=mask)
cv2.imshow("images",np.hstack([frame,skin]))
if cv2.waitKey(1) & 0xFF ==ord("q"):
break
camera.release()
cv2.destroyAllWindows()