-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
136 lines (120 loc) · 4.61 KB
/
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
from exceptions import ImageNotFoundError
from img_proc import *
from constants import CANNY_THRESH_1, CANNY_THRESH_2, BLUR, MASK_DILATE_ITER, MASK_ERODE_ITER
from gesture_recognition import *
from cv2 import imread, imshow, waitKey, destroyAllWindows
from sys import argv
from io_utils import parseArguments
"""
FOR TEST ONLY
"""
imagePath = "images/paper_1.jpg"
imagePath = "images/paper_2.jpg"
imagePath = "images/paper_3.jpg"
imagePath = "images/paper_4.jpg" # --angleOffset 30
imagePath = "images/paper_5.jpg"
imagePath = "images/paper_6.jpg" # --angleOffset -15
imagePath = "images/paper_7.jpg" # --takeUpperHalf 1
imagePath = "images/paper_8.jpg"
imagePath = "images/paper_9.jpg"
imagePath = "images/paper_10.jpg"
imagePath = "images/paper_11.jpg" # --rotationAngle 90 --takeUpperHalf 1
imagePath = "images/paper_12.jpg" # --angleOffset 45
imagePath = "images/paper_13.jpg" # --rotationAngle -90
# # ### ROCK ###
imagePath = "images/rock_1.jpg"
imagePath = "images/rock_2.jpg"
imagePath = "images/rock_3.jpg"
imagePath = "images/rock_4.jpg" # --rotationAngle 90
imagePath = "images/rock_5.jpg"
imagePath = "images/rock_6.jpg" # --rotationAngle 90
imagePath = "images/rock_7.jpg"
imagePath = "images/rock_8.jpg"
imagePath = "images/rock_9.jpg"
imagePath = "images/rock_10.jpg"
imagePath = "images/rock_11.jpg" # prominent thumb --takeUpperHalf 1 # comment in serbian - palac strci malo
imagePath = "images/rock_12.jpg" # finger retraction --takeUpperHalf 1 # comment in serbian - prst uvucen malo
imagePath = "images/rock_13.jpg"
#
# ### SCISSORS ###
imagePath = "images/scissors_1.jpg"
imagePath = "images/scissors_2.jpg" # --rotationAngle -90 --angleOffset 15
imagePath = "images/scissors_3.jpg" # not perfect
imagePath = "images/scissors_4.jpg"
imagePath = "images/scissors_5.jpg"
imagePath = "images/scissors_6.jpg"
imagePath = "images/scissors_7.jpg"
imagePath = "images/scissors_8.jpg" # --angleOffset 50 --takeUpperHalf 1
imagePath = "images/scissors_9.jpg"
imagePath = "images/scissors_10.jpg"
imagePath = "images/scissors_11.jpg"
imagePath = "images/scissors_12.jpg"
imagePath = "images/scissors_13.jpg" # --rotationAngle 90
if __name__ == "__main__":
# ---- read image -----
arguments = argv[1:]
parseArguments(arguments)
image = imread(imagePath)
if image is None:
raise ImageNotFoundError("Image file cannot be found. Check your image path.")
if ImageParam.rotationAngle != 0:
image = rotateImage(image, ImageParam.rotationAngle)
# only for -90/90 deg rotations
firstRow = findEdgeNonBlackPixel(image)
if firstRow is not None:
image = image[firstRow:, :]
lastRow = findEdgeNonBlackPixel(image, "end")
if lastRow is not None:
image = image[:lastRow + 1, :]
# imshow("Original image", image)
# waitKey(0)
# ---- crop image and convert to grayscale -----
if ImageParam.takeUpperHalf:
image = getImageUpperPart(image, 1.5) # parametrize this
# imshow("Cropped image", image)
# waitKey(0)
gray = convertColorSpace(image, "grayscale")
# imshow("Grayscale image", gray)
# waitKey(0)
# ----- edge detection and blurring-----
edges = applyCannyEdge(gray, CANNY_THRESH_1, CANNY_THRESH_2)
# imshow("Edges", edges)
# waitKey(0)
edges = applyGaussianBlur(edges, (BLUR, BLUR))
# imshow("Edges after bluring", edges)
# waitKey(0)
# ----- morphological processing -----
edges = applyDilation(edges, None, iterations=1)
# imshow("After dilation", edges)
# waitKey(0)
edges = applyErosion(edges, None, iterations=1)
# imshow("After erosion", edges)
# waitKey(0)
# ----- Find contours -------
# #contours = cv2.findContours(edges, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
contours = findImageContours(edges)
maxContour = findMaxContour(contours)
contourImage = cloneImage(image)
drawImageContours(contourImage, contours, (255, 255, 255), 2)
# imshow("Contours", contourImage)
# waitKey(0)
# ----- Masking -------
mask = emptyImage(edges.shape)
for c in contours:
fillConvex(mask, c, 255)
# imshow("Mask", mask)
# waitKey(0)
# do some processing on mask
mask = applyDilation(mask, None, iterations=MASK_DILATE_ITER)
mask = applyErosion(mask, None, iterations=MASK_ERODE_ITER)
mask = applyGaussianBlur(mask, (BLUR, BLUR))
# imshow("Processed mask", mask)
# waitKey(0)
binMask = convertToSingleChannel(mask)
# imshow("Binary mask", binMask)
# waitKey(0)
imageCopy = cloneImage(image)
predictGesture(maxContour, imageCopy)
# waitUntilEnter()
waitKey(0)
destroyAllWindows()