-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathar_paint_final.py
212 lines (173 loc) · 6.89 KB
/
ar_paint_final.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import argparse
import json
from functools import partial
import copy
from datetime import *
from imutils.video import VideoStream
import random
from functions import *
#Takes in arguments for the Json used for calibration and for activating shake prevention
def main():
video_flag = 0
parser = argparse.ArgumentParser(description='OPenCV example')
parser.add_argument('-j', '--json', required=True, type=str, help='Full path to json file')
parser.add_argument('-sp', '--use_shake_prevention', action='store_true',
help='Applies shake detection to the program')
args = vars(parser.parse_args())
# ----------------
# Inicializações
# ---------------
# criação da prints para user friendly
interface()
# leitura do ficheiro json
ranges = json.load(open(args['json']))
# print(ranges)
# min and max values in the json file
mins = np.array([ranges['B']['min'], ranges['G']['min'], ranges['R']['min']])
maxs = np.array([ranges['B']['max'], ranges['G']['max'], ranges['R']['max']])
# dicionario do path da imagem
d = {'Ball_painted.jpg': 'Ball.jpg',
'amongus_painted.jpg': 'amongus.jpg',
'snail_painted.jpg': 'snail.jpg'}
# setup da camera
vs = VideoStream(0).start()
frame = vs.read()
# size of the image from camera
(h, w) = frame.shape[:2]
# white canvas
img = np.zeros((h, w, 3), np.uint8)
img.fill(255)
window_name = 'canvas'
cv2.imshow(window_name, img)
# mask, gray e video só estou aqui para conseguir testar os mousecallbacks nessas janelas, são para ser removidos depois
# cv2.imshow('gray', img)
# cv2.imshow('mask', img)
# cv2.imshow('video', img)
# starts red
color = (0, 0, 255)
# THICCCCCC
thickness = 1
# Shape
shape = Shape.LINE
# Juntei para evitar os erros nos testes acionados ao premir a tecla q
img_color = None
"""
this block is just testing purposes
cv2.setMouseCallback('gray', partial(line_drawing, img, color, thickness))
cv2.setMouseCallback('mask', partial(line_drawing, img, color, thickness))
cv2.setMouseCallback('video', partial(line_drawing, img, color, thickness))
"""
# ----------------
# Execucoes
# ---------------
while True:
frame = vs.read()
frame = cv2.flip(frame, 1) # the second arguments value of 1 indicates that we want to flip horizontally
# converts frames to HSV
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# creates the mask with the values
mask = cv2.inRange(hsv, mins, maxs)
mask_size, x, y = removeSmallComponents(mask, 500)
# paint the biggest object in the original frame
frame_copy = copy.copy(frame)
video = frame.copy()
frame_copy[(mask_size == 255)] = (0, 255, 0)
# drawing the marker for the centroid, it is a cross
if x is not None:
cv2.line(frame_copy, (int(x) - 10, int(y) + 10), (int(x) + 10, int(y) - 10), (0, 0, 255), 5)
cv2.line(frame_copy, (int(x) + 10, int(y) + 10), (int(x) - 10, int(y) - 10), (0, 0, 255), 5)
# drawing in the canvas
if video_flag:
mask_drawing_video(window_name, video, img, color, thickness, x, y, shape, args['use_shake_prevention'])
cv2.setMouseCallback('canvas',
partial(line_drawing_video, w_name=window_name, img=video, mask=img, shape=shape,
color=color, thickness=thickness))
else:
mask_drawing(window_name, img, color, thickness, x, y, shape, args['use_shake_prevention'])
cv2.setMouseCallback('canvas',
partial(line_drawing, w_name=window_name, img=img, shape=shape, color=color,
thickness=thickness))
# show video, canvas, mask
# cv2.imshow('video', frame)
# cv2.imshow('video_changed', frame_copy)
# videos = cv2.vconcat([frame, frame_copy])
# cv2.imshow(window_name, img)
videos = np.concatenate((frame, frame_copy), axis=0)
cv2.imshow('videos', videos)
masks = np.concatenate((mask, mask_size), axis=0)
cv2.imshow('masks', masks)
# it needs to draw in the img but only show the video
if video_flag:
video = frame.copy()
(h_i, w_i) = img.shape[:2]
if h_i == h:
video[(img != 255)] = img[(img != 255)]
cv2.imshow(window_name, video)
key = cv2.waitKey(1)
if args['use_shake_prevention'] is True:
shake_prevention(x, y, past_x, past_y, color, img)
# it isnt needed
# if key != -1:
# choices for changing color
if key == ord('r'):
color = (0, 0, 255)
if key == ord('b'):
color = (255, 0, 0)
if key == ord('g'):
color = (0, 255, 0)
# clear the board
if key == ord('c'):
#img = cv2.resize(img, [w, h])
img.fill(255)
cv2.imshow(window_name, img)
# change the thickness
if key == ord('+'):
thickness += 1
if key == ord('-'):
thickness -= 1
if thickness == 0:
print('thickness cant be zero')
thickness = 1
# select shape for drawing on canvas
if key == ord('s'): # square
print("rectangle")
shape = Shape.RECTANGLE
if key == ord('f'): # circle
print("circle")
shape = Shape.CIRCLE
if key == ord('e'): # ellipse
shape = Shape.ELLIPSE
if key == ord('l'): #
print("Line")
shape = Shape.LINE
# capture image from videostream and set it on canvas to be drawn
if key == ord('p'):
img = frame
cv2.imshow(window_name, img)
# get a video in the canvas
if key == ord('m'):
video_flag = not video_flag
if key == ord('t'):
path_bw = random.choice(list(d.values()))
img = cv2.imread(path_bw)
cv2.imshow(window_name, img)
path_color = list(d.keys())[list(d.values()).index(path_bw)]
img_color = cv2.imread(path_color)
# saves the image in the directory of the code
if key == ord('w'):
# didnt put weekday
date_img = datetime.now().strftime("%H:%M:%S_%Y")
cv2.imwrite('image_' + date_img + '.png', img)
if key == ord('v'):
if img_color is not None:
accuracy(img, img_color)
# see the user panel again
if key == ord('h'):
print(Style.BRIGHT + 'Hey! It is me again' + Style.RESET_ALL)
interface()
# quit the program
if key == ord('q'):
break
cv2.destroyAllWindows()
if __name__ == '__main__':
main()