-
Notifications
You must be signed in to change notification settings - Fork 0
/
camera_test.py
109 lines (90 loc) · 3.72 KB
/
camera_test.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
import time
import sys
import numpy as np
import cv2
from sklearn.externals import joblib
dim = [10,20,30,40]
video_capture = cv2.VideoCapture(0)
time.sleep(2)
while True:
ret, frame = video_capture.read()
cv2.imshow("cam",frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
frame = cv2.resize(frame,(100,100))
# downsize it to reduce processing time
cv2.imshow("test_image",frame)
converted = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) # Convert from RGB to HSV
# tuned settings
lowerBoundary = np.array([0,40,30],dtype="uint8")
upperBoundary = np.array([43,255,254],dtype="uint8")
skinMask = cv2.inRange(converted, lowerBoundary, upperBoundary)
# apply a series of erosions and dilations to the mask using an elliptical kernel
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3,3))
skinMask = cv2.erode(skinMask, kernel, iterations = 2)
skinMask = cv2.dilate(skinMask, kernel, iterations = 2)
lowerBoundary = np.array([170,80,30],dtype="uint8")
upperBoundary = np.array([180,255,250],dtype="uint8")
skinMask2 = cv2.inRange(converted, lowerBoundary, upperBoundary)
skinMask = cv2.addWeighted(skinMask,0.5,skinMask2,0.5,0.0)
# blur the mask to help remove noise, then apply the
# mask to the frame
# skinMask = cv2.medianBlur(skinMask, 5)
skin = cv2.bitwise_and(frame, frame, mask = skinMask)
frame = cv2.addWeighted(frame,1.5,skin,-0.5,0)
skin = cv2.bitwise_and(frame, frame, mask = skinMask)
h,w = skin.shape[:2]
bw_image = cv2.cvtColor(skin, cv2.COLOR_HSV2BGR) # Convert image from HSV to BGR format
bw_image = cv2.cvtColor(skin, cv2.COLOR_BGR2GRAY) # Convert image from BGR to gray format
bw_image = cv2.GaussianBlur(bw_image,(5,5),0) # Highlight the main object
threshold = 1
for i in xrange(h):
for j in xrange(w):
if bw_image[i][j] > threshold:
bw_image[i][j] = 255 #Setting the skin tone to be White
else:
bw_image[i][j] = 0 #else setting it to zero.
sign_image = bw_image[:h-15,:] # Cropping 15 pixels from the bottom
contours, hierarchy = cv2.findContours(sign_image,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
# Finding the contour with the greatest area.
largestContourIndex = 0
if len(contours)<=0:
print "Skipping due to empty contour"
else:
largestContourArea = cv2.contourArea(contours[largestContourIndex])
i=1
while i<len(contours):
if cv2.contourArea(contours[i]) > cv2.contourArea(contours[largestContourIndex]):
largestContourIndex = i
i+=1
# Draw the largest contour in the image.
cv2.drawContours(sign_image,contours,largestContourIndex,(255,255,255),thickness = -1)
x,y,w,h = cv2.boundingRect(contours[largestContourIndex]) # Draw a rectangle around the contour perimeter
squareSide = max(w,h)-1
hHalf = (y+y+h)/2
wHalf = (x+x+w)/2
hMin, hMax = hHalf-squareSide/2, hHalf+squareSide/2
wMin, wMax = wHalf-squareSide/2, wHalf+squareSide/2
if (hMin>=0 and hMin<hMax and wMin>=0 and wMin<wMax):
sign_image = sign_image[hMin:hMax,wMin:wMax]
else:
print "No contour found!! Skipping this image"
sign_image = cv2.resize(sign_image,(100,100))
flattened_sign_image = sign_image.flatten() # Convert multi-dimensional array to a one-dimensional array
for d in dim:
clf = joblib.load('models/knn_10/knn_10_'+str(d)+'x'+str(d)+'.pkl')
temp_image = cv2.resize(sign_image,(d,d)).flatten()
predicted = clf.predict(temp_image)
pred = predicted[0]
print d
print pred
# pred = predicted[0].upper()
pred_path = 'test_img/'+str(pred)+'.jpg'
result = cv2.imread(pred_path)
result = cv2.resize(result,(100,100))
cv2.imshow("predicted by "+str(d)+"x"+str(d)+" k(=10) Nearest neighbours model: "+str(pred),result)
cv2.waitKey(1000)
#########################################################
camera.release()
cv2.destroyAllWindows()
print "The program completed successfully !!"