-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
1274 lines (1035 loc) · 43.3 KB
/
utils.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import copy
import cv2
import numpy as np
import matplotlib.pyplot as plt
from IPython.display import clear_output
import mediapipe as mp
import threading
import time
from midiutil.MidiFile import MIDIFile
def threadMultiple(functions, results):
"""Parallelize execution of given functions
Parameters
---------
functions : list of python Callback
functions to parallelize, with the following format:
[
{"function": foo, "args": [x, y, ...]},
{"function": bar, "args": [x, y, ...]},
...
]
results : list
list containing the output(s) of the functions
"""
def threadFunction(callback_and_args, results, index):
callback = callback_and_args["function"]
callback_args = callback_and_args["args"]
results[index] = callback(*callback_args)
threads = [None]*len(functions)
for i in range(len(functions)):
threads[i] = threading.Thread(target=threadFunction, args=(functions[i],results,i))
for thread in threads:
thread.start()
for thread in threads:
thread.join()
def plotImg(img, cmap=None):
"""Displays the given image
Parameters
---------
img : numpy array
the image to display
cmap : str, optional
Colormap
"""
if cmap is None:
plt.imshow(img)
plt.show()
plt.close()
else:
plt.imshow(img, cmap='gray')
plt.show()
plt.close()
def extractFrames(video, start_time, end_time=None, samples_per_second=None, callback=None, callback_args=None):
"""Extracts some frames from a video
Parameters
---------
video : str
the path to the video
start_time : float
the starting time in seconds
end_time : float, optional
samples_per_second : float, optional
callback : python Callback
a function that processes a frame (numpy array) and outputs another frame
callback_args : list
arguments of the callback function
Outputs
---------
ris : list of frames
processed by callback function
"""
cap = cv2.VideoCapture(video)
fps = np.ceil(cap.get(cv2.CAP_PROP_FPS))
if end_time == None:
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
end_time = frame_count/fps
if samples_per_second is None or samples_per_second > fps:
samples_per_second = np.ceil(fps)
ris = []
frames = start_time*fps #starting frame
cap.set(cv2.CAP_PROP_POS_FRAMES, frames)
samples_per_second *= 60//fps
while frames < end_time*fps:
print("{}/{}".format(frames,end_time*fps))
print("{}/{}".format(frames/fps,end_time))
is_read, frame = cap.read()
if not is_read:
break
if callback is not None:
frame_processed = callback(frame, *callback_args)
ris.append(frame_processed)
else:
ris.append(frame)
clear_output(wait=True)
frames += 60/samples_per_second
cap.set(cv2.CAP_PROP_POS_FRAMES, frames)
return ris
def playVideo(video, start_time, end_time=None, frames_every=1, callback=None, callback_args=None, save_video=False, video_name="out.mp4", video_size=(1920,1080)):
"""Plays the video with opencv library
Parameters
---------
video : str
the path to the video
start_time : float
the starting time in seconds
end_time : float, optional
frames_every : int, optional
interval to take frames between
callback : python Callback
a function that processes a frame (numpy array) and outputs another frame
callback_args : list
arguments of the callback function
save_video : bool, optional
video_name : str, optional
name of saved video file
video_size : tuple, optional
(width, height) of saved video
"""
cap = cv2.VideoCapture(video)
fps = np.ceil(cap.get(cv2.CAP_PROP_FPS))
out = None
if save_video:
out = cv2.VideoWriter(video_name, cv2.VideoWriter_fourcc(*'MJPG'), fps, video_size)
if end_time == None:
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
end_time = frame_count/fps
frames = start_time*fps #starting frame
cap.set(cv2.CAP_PROP_POS_FRAMES, frames)
while frames < end_time*fps:
print("{}/{}".format(frames,end_time*fps))
print("{}/{}".format(frames/fps,end_time))
for i in range(frames_every):
is_read, frame = cap.read()
if not is_read:
break
if callback is not None:
frame_processed = callback(frame, *callback_args)
cv2.rectangle(frame_processed, (10, 2), (100,20), (255,255,255), -1)
cv2.putText(frame_processed, str(cap.get(cv2.CAP_PROP_POS_FRAMES)), (15, 15), cv2.FONT_HERSHEY_SIMPLEX, 0.5 , (0,0,0))
cv2.imshow(video, cv2.resize(frame_processed,(960, 540)))
cv2.resizeWindow(video, 960, 540)
if save_video:
ph, pw = frame_processed.shape[0:2]
sw, sh = video_size
print("{} =?= {}, {} =?= {}".format(ph,sh,pw,sw))
if ph != sh or pw != sw:
print("Different sizes")
frame_processed = cv2.resize(frame_processed, video_size)
out.write(frame_processed)
else:
cv2.rectangle(frame, (10, 2), (100,20), (255,255,255), -1)
cv2.putText(frame, str(cap.get(cv2.CAP_PROP_POS_FRAMES)), (15, 15), cv2.FONT_HERSHEY_SIMPLEX, 0.5 , (0,0,0))
cv2.imshow(video, cv2.resize(frame,(960, 540)))
cv2.resizeWindow(video, 960, 540)
if save_video:
ph, pw = frame.shape[0:2]
sw, sh = video_size
print("{} =?= {}, {} =?= {}".format(ph,sh,pw,sw))
if ph != sh or pw != sw:
print("Different sizes")
frame = cv2.resize(frame, video_size)
out.write(frame)
clear_output(wait=True)
frames += frames_every
k = cv2.waitKey(50) & 0xFF
if k == 27: #escape key
break
cv2.destroyAllWindows()
def segmentationMaskTuning(frame):
"""Enables manual selection of HSV masks
Parameters
---------
frame : numpy array
the input frame
Outputs
---------
lower_thresh : list
lower thresholds for values of H,S and V
upper_thresh : list
upper thresholds for values of H,S and V
mask : numpy array
the HSV mask
"""
roi = frame.copy()
hsv = cv2.cvtColor(roi, cv2.COLOR_BGR2HSV)
lower_thresh = np.array([128,128,128], dtype=np.uint8)
upper_thresh = np.array([255,255,255], dtype=np.uint8)
mask = cv2.inRange(hsv, lower_thresh, upper_thresh)
cv2.namedWindow('image', cv2.WINDOW_KEEPRATIO) # make a window with name 'image'
cv2.createTrackbar('h_low', 'image',128,255, lambda x:x)
cv2.createTrackbar('h_high','image',255,255, lambda x:x)
cv2.createTrackbar('s_low', 'image',128,255, lambda x:x)
cv2.createTrackbar('s_high','image',255,255, lambda x:x)
cv2.createTrackbar('v_low', 'image',128,255, lambda x:x)
cv2.createTrackbar('v_high','image',255,255, lambda x:x)
def setChannel(low_ratio_track_bar, high_ratio_track_bar):
low_ratio = cv2.getTrackbarPos(low_ratio_track_bar, 'image')
high_ratio = cv2.getTrackbarPos(high_ratio_track_bar, 'image')
return low_ratio, high_ratio
resize = roi.shape[:2] #(320, int(roi.shape[0]*320/roi.shape[1]))
while(1):
resized_gray = cv2.resize(cv2.cvtColor(roi,cv2.COLOR_RGB2GRAY), resize)
resized_mask = cv2.resize(mask, resize)
numpy_horizontal_concat = np.concatenate((resized_mask, resized_gray), axis=1) # to display image side by side
cv2.imshow('image', numpy_horizontal_concat)
cv2.resizeWindow('image', 1280, 320)
k = cv2.waitKey(50) & 0xFF
if k == 27: #escape key
break
h_low, h_high = setChannel('h_low', 'h_high')
s_low, s_high = setChannel('s_low', 's_high')
v_low, v_high = setChannel('v_low', 'v_high')
lower_thresh = np.array([h_low,s_low,v_low], dtype=np.uint8)
upper_thresh = np.array([h_high,s_high,v_high], dtype=np.uint8)
mask = cv2.inRange(hsv, lower_thresh, upper_thresh)
cv2.destroyAllWindows()
return lower_thresh, upper_thresh, mask
def segmentationInRange(frame, lower_thresh, upper_thresh, show=True):
"""Returns image masks from HSV thresholds
Parameters
---------
frame : numpy array
the input frame
lower_thresh : list
lower thresholds for values of H,S and V
upper_thresh : list
upper thresholds for values of H,S and V
show : bool , optional
if true, plots the mask
Outputs
---------
mask : numpy array
the HSV mask
"""
frame_hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
frame_binary = cv2.inRange(frame_hsv, lower_thresh, upper_thresh)
if show:
fig = plt.figure(figsize=(16, 9))
ax1 = fig.add_subplot(2,2,1)
ax1.imshow(frame_binary, cmap='gray')
ax2 = fig.add_subplot(2,2,2)
ax2.imshow(frame, cmap='gray')
plt.show()
plt.close()
return frame_binary
def getRectifyingMatrix(frame, lower_thresh, upper_thresh, processed=False, pts2=None):
"""Returns rectifying matrix H for keyboard
Parameters
---------
frame : numpy array
the input frame, can be both binary (processed = true) or with colors
lower_thresh : list
lower thresholds for values of H,S and V
upper_thresh : list
upper thresholds for values of H,S and V
processed : bool, optional
whether the input frame is already a mask
pts2 : numpy array , optional
list of coordinates of the corresponding quadrangle vertices in the destination image
Outputs
---------
H_shape : numpy array
the rectifying homography
pts1 : numpy array
coordinates of vertices corresponding to four corners of keyboard in the source image
pts2 : numpy array
coordinates of vertices corresponding to four corners of keyboard in the rectified image
size : tuple
(keyboard_width, keyboard_height)
"""
if not processed:
frame_segmented = segmentationInRange(frame, lower_thresh, upper_thresh, False)
else:
frame_segmented = frame.copy()
frame_canny = cv2.Canny(frame_segmented, 255/3, 255, apertureSize=3)
# empty canvas
frame_empty = np.zeros(shape=frame.shape[0:2], dtype=np.uint8)
# to find max area contour
frame_max_area = cv2.dilate(frame_canny, np.ones((3,3)), iterations=1)
#frame_max_area = cv2.erode(frame_max_area, np.ones((3,3)), iterations=1)
contours, _ = cv2.findContours(frame_max_area, cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
if len(contours) == 0:
return np.eye(3), None, None, (10,5)
#sort array based on area of contours
contours = sorted(contours, key=lambda x: cv2.contourArea(x))
#if we have 2 or more large contours it means we split the keyboard contour in more parts
#and we have to join them
new_max = []
max1 = contours[-1]
for point in max1:
new_max.append(point)
for i in range(len(contours)-1):
max2 = contours[i]
if cv2.contourArea(max1) < 2.5*cv2.contourArea(max2): #arbitrary threshold
for point in max2:
new_max.append(point)
max_contour = np.array(new_max, dtype=np.int32)
#approximate contour to a polygon
hull = cv2.convexHull(max_contour)
#draw the keyboard contour on a black frame
cv2.drawContours(frame_empty, [hull], -1, (255,255,255), thickness=cv2.FILLED)
# the empty canvas now has the max convex hull, approximate it to a 4-point polygon
# we find again the contour and then use the proper opencv functions
#frame_empty = cv2.dilate(frame_empty, np.ones((5,5)), iterations=3)
contours, _ = cv2.findContours(frame_empty, cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
max_contour = max(contours, key = lambda x: cv2.contourArea(x))
peri = cv2.arcLength(max_contour,True)
approx = cv2.approxPolyDP(max_contour, 0.02*peri,True)
thresh = peri
approx = cv2.approxPolyDP(max_contour, peri/thresh,True)
while len(approx) != 4:
thresh -= 0.1
approx = cv2.approxPolyDP(max_contour, peri/thresh, True)
if len(approx) == 4: #if we have 4 points
# find position of top-left corner
min_val = approx[0][0][0]+approx[0][0][1] #sum of 2 coordinates of each point
top_left_pos = 0
for i in range(len(approx)):
corner = approx[i][0]
if corner[0]+corner[1] < min_val: #top-left has both minimum
top_left_pos = i
min_val = corner[0]+corner[1]
# approx always returns the points counter-clock wise
top_left = approx[top_left_pos][0]
bottom_left = approx[(top_left_pos+1) % 4][0]
bottom_right = approx[(top_left_pos+2) % 4][0]
top_right = approx[(top_left_pos+3) % 4][0]
keyboard_width = np.max([
top_right[0]-top_left[0],
bottom_right[0]-bottom_left[0]
])
keyboard_height = np.max([
bottom_right[1]-top_right[1],
bottom_left[1]-top_left[1]
])
pts1 = np.float32(approx)
if pts2 is None:
pts2 = np.roll(np.float32([
[top_left[0], top_left[1]],
[top_left[0], top_left[1]+keyboard_height],
[top_left[0]+keyboard_width, top_left[1]+keyboard_height],
[top_left[0]+keyboard_width, top_left[1]]]), top_left_pos, axis=0)
#map the approx points to a rectangle to find H
H_shape = cv2.getPerspectiveTransform(pts1,pts2)
cv2.drawContours(frame_canny, [approx], -1, (255,255,255), 3)
plotImg(frame_canny)
return H_shape, pts1, pts2, (keyboard_width, keyboard_height)
return np.eye(3), None, None, (10,5)
def getRectifyingMatrixManual(frame):
"""Returns rectifying matrix H, keyboard corner points taken manually
Parameters
---------
frame : numpy array
the input frame, can be both binary (processed = true) or with colors
Outputs
---------
H_shape : numpy array
the rectifying homography
pts1 : numpy array
coordinates of vertices corresponding to four corners of keyboard in the source image
pts2 : numpy array
coordinates of vertices corresponding to four corners of keyboard in the rectified image
size : tuple
(keyboard_width, keyboard_height)
"""
approx = []
h, w, _ = frame.shape
scaling_h = h/720
scaling_w = w/1280
def on_click_event(event, x, y, flags, params):
if event == cv2.EVENT_LBUTTONDOWN:
approx.append((x*scaling_w//1, y*scaling_h//1))
# register the callback
cv2.namedWindow("Get 4 points")
cv2.setMouseCallback("Get 4 points", on_click_event)
# loop until 'c' key is pressed, or four points have been collected
while True:
cv2.imshow("Get 4 points", cv2.resize(frame, (1280,720)))
k = cv2.waitKey(50) & 0xFF
if k == 27 or len(approx) == 4:
break
approx = np.float32(approx)
cv2.destroyAllWindows()
# find position of top-left corner
min_val = approx[0][0]+approx[0][1] #sum of 2 coordinates of each point
top_left_pos = 0
for i in range(len(approx)):
corner = approx[i]
if corner[0]+corner[1] < min_val: #top-left has both minimum
top_left_pos = i
min_val = corner[0]+corner[1]
# approx always returns the points counter-clock wise
top_left = approx[top_left_pos]
bottom_left = approx[(top_left_pos+1) % 4]
bottom_right = approx[(top_left_pos+2) % 4]
top_right = approx[(top_left_pos+3) % 4]
keyboard_width = np.max([
top_right[0]-top_left[0],
bottom_right[0]-bottom_left[0]
])
keyboard_height = np.max([
bottom_right[1]-top_right[1],
bottom_left[1]-top_left[1]
])
pts1 = np.float32(approx)
pts2 = np.roll(np.float32([
[top_left[0], top_left[1]],
[top_left[0], top_left[1]+keyboard_height],
[top_left[0]+keyboard_width, top_left[1]+keyboard_height],
[top_left[0]+keyboard_width, top_left[1]]]), top_left_pos, axis=0)
#map the approx points to a rectangle to find H
H_shape = cv2.getPerspectiveTransform(pts1,pts2)
frame_tmp = frame.copy()
cv2.drawContours(frame_tmp, [approx.astype(int)], -1, (255,255,255), thickness=5)
plotImg(frame_tmp)
return H_shape, pts1, pts2, (keyboard_width, keyboard_height)
def rectifyWithMatrix(frame, lower_thresh, upper_thresh, H_shape=None, rotation=np.eye(3), pts2=None):
"""Returns rectified frame
Parameters
---------
frame : numpy array
the input frame with colors
lower_thresh : list
lower thresholds for values of H,S and V
upper_thresh : list
upper thresholds for values of H,S and V
H_shape : numpy array
the rectifying homography
rotation : numpy array
the rotation matrix
pts2 : numpy array , optional
list of coordinates of the corresponding quadrangle vertices in the destination image
Outputs
---------
rectified frame : numpy array
"""
if H_shape is None and pts2 is not None:
H_shape, _, _, _ = getRectifyingMatrix(frame, lower_thresh, upper_thresh, pts2=pts2) #if we need to recompute H
H_shape = np.dot(rotation, H_shape)
return cv2.warpPerspective(frame, H_shape, (1920+192,1080+108)) #apply H
def projectPoint(p, H):
p_ = np.dot(H,[p[0], p[1], 1])
p_ /= p_[2]
return p_.astype(int)[0:2]
def getKeyboardContour(frame, lower_thresh, upper_thresh, pts2, H_shape, keyboard_shape, frame_segmented=None):
"""Finds contours of the regions of each key
Parameters
---------
frame : numpy array
the input frame with colors
lower_thresh : list
lower thresholds for values of H,S and V
upper_thresh : list
upper thresholds for values of H,S and V
pts2 : numpy array , optional
list of coordinates of the corresponding quadrangle vertices in the destination image
H_shape : numpy array
the rectifying homography
keyboard_shape : tuple
(keyboard_width , keyboard_height)
frame_segmented : numpy array, optional
the binary image containing the keyboard mask
Outputs
---------
keys : list
list containing the contours of each key
"""
if pts2 is None:
return None
w, h = keyboard_shape
# hide everything outside keyboard
mask = np.zeros((1080+108,1920+192), dtype=np.uint8)
cv2.drawContours(mask, [pts2.astype(int)], -1, (255, 255, 255), -1) # fill rectangle, fill the mask
# frame_contour = cv2.bitwise_and(frame_contour, frame_contour, mask = mask)
# segment
frame_contour = cv2.warpPerspective(frame, H_shape, (1920+192,1080+108))
if frame_segmented is None:
frame_segmented = segmentationInRange(frame_contour, lower_thresh, upper_thresh, False)
else:
frame_segmented = cv2.warpPerspective(frame_segmented, H_shape, (1920+192,1080+108))
frame_contour = cv2.bitwise_and(frame_segmented, mask)
# line crossing black keys
min_val = pts2[0][0]+pts2[0][1]
top_left_pos = 0
for i in range(len(pts2)):
corner = pts2[i]
if corner[0]+corner[1] < min_val:
top_left_pos = i
min_val = corner[0]+corner[1]
# counter-clock wise
top_left = tuple(map(int,pts2[top_left_pos]))
bottom_left = tuple(map(int,pts2[(top_left_pos+1) % 4]))
bottom_right = tuple(map(int,pts2[(top_left_pos+2) % 4]))
top_right = tuple(map(int,pts2[(top_left_pos+3) % 4]))
keys = {"white": [], "black": []}
w_multiplier = 2.5
if w < h: # keyboard in vertical
x = top_right[0]-w//3
x = int(x)
y1 = top_right[1]
y2 = bottom_right[1]
# find largest black key
widths = []
y = y1
y = int(y)
while y < y2:
a_extreme = y
pixel = frame_contour[y][x]
while pixel == 0 and y < y2:
y += 1
pixel = frame_contour[y][x]
b_extreme = y
if np.abs(b_extreme-a_extreme) != 0:
widths.append(np.abs(b_extreme-a_extreme))
y += 1
max_w = np.mean(widths) #max(widths)
# find the black keys extremes and their middle points
white_widths = []
b_extremes = []
a_extremes = []
y = y1
while y < y2:
a_extreme = y
pixel = frame_contour[y][x]
while pixel == 0 and y < y2:
y += 1
pixel = frame_contour[y][x]
b_extreme = y
cur_w = np.abs(b_extreme-a_extreme)
if cur_w*w_multiplier >= max_w:
a_extremes.append(a_extreme)
b_extremes.append(b_extreme)
mid = (b_extreme-a_extreme)//2+a_extreme
white_widths.append(mid)
y += 1
# find when one of the extremes meets white
black_widths_diff = []
white_widths = []
for i in range(len(a_extremes)):
a_extreme = a_extremes[i]
b_extreme = b_extremes[i]
black_widths_diff.append(b_extreme-a_extreme)
min_black_width = min(black_widths_diff)
for i in range(len(a_extremes)):
a_extreme = a_extremes[i]
b_extremes[i] = a_extremes[i]+min_black_width
b_extreme = b_extremes[i]
mid = (b_extreme-a_extreme)//2+a_extreme
white_widths.append(mid)
'''
'''
x_nodes = [] # x coordinates for first points touching white
y_nodes = [] # y coordinates for first points touching white
for mid in white_widths:
for i in range(top_right[0]-2,top_left[0],-1):
if frame_contour[mid][i] != 0:
x_nodes.append(i)
y_nodes.append(mid)
break
# Black keys rectangles
mid_line_x = int(np.mean(x_nodes))
for i in range(len(a_extremes)):
a_extreme = a_extremes[i]
b_extreme = b_extremes[i]
start_point = (top_right[0], a_extreme)
end_point = (mid_line_x, b_extreme)
keys["black"].append({ "id": i+1, "pt1": start_point, "pt2": end_point})
# White keys rectangles
keys["white"] = white_widths
# find minimum width of w a white key
white_widths_diff = [j-i for i, j in zip(white_widths[:-1], white_widths[1:])]
min_w = min(white_widths_diff)
# when a detected white width is unually wide, it means there are two keys
for i in range(1,len(white_widths)):
width = white_widths[i]-white_widths[i-1]
if width > min_w*1.4:
i = white_widths[i-1]+width//2
keys["white"].append(i)
keys["white"].sort()
# append the first white key in a tmp array
start_point = (top_left[0], top_left[1])
end_point = (top_right[0], keys["white"][1])
to_append = {"id": 1, "pt1": start_point, "pt2": end_point}
tmp_arr = [to_append]
# map widths to rectangles identifying white keys
for i in range(len(keys["white"])-1):
start_point = (top_left[0], keys["white"][i])
end_point = (top_right[0], keys["white"][i+1])
to_append = {"id": i+2, "pt1": start_point, "pt2": end_point}
tmp_arr.append(to_append)
keys["white"] = tmp_arr
# fill final undetected white keys
i = len(keys["white"])+1
while keys["white"][-1]["pt2"][1] < bottom_left[1]:
start_point = (top_left[0], keys["white"][-1]["pt2"][1])
end_point = (top_right[0], 2*keys["white"][-1]["pt2"][1]-keys["white"][-2]["pt2"][1])
to_append = {"id": i, "pt1": start_point, "pt2": end_point}
keys["white"].append(to_append)
i += 1
keys["white"][-1]["pt2"] = (keys["white"][-1]["pt2"][0], bottom_left[1])
else: # keyboard in horizontal
y = top_right[1]+h//3
y = int(y)
x1 = top_left[0]
x2 = top_right[0]
# find largest black key
widths = []
x = x1
x = int(x)
while x < x2:
a_extreme = x
pixel = frame_contour[y][x]
while pixel == 0 and x < x2:
x += 1
pixel = frame_contour[y][x]
b_extreme = x
if np.abs(b_extreme-a_extreme) != 0:
widths.append(np.abs(b_extreme-a_extreme))
x += 1
max_w = np.mean(widths) #max(widths)
# find the black keys extremes and their middle points
b_extremes = []
a_extremes = []
x = x1
while x < x2:
a_extreme = x
pixel = frame_contour[y][x]
while pixel == 0 and x < x2:
x += 1
pixel = frame_contour[y][x]
b_extreme = x
cur_w = np.abs(b_extreme-a_extreme)
if cur_w*w_multiplier >= max_w: #remove too small widths
a_extremes.append(a_extreme)
b_extremes.append(b_extreme)
mid = (b_extreme-a_extreme)//2+a_extreme
x += 1
# find when one of the extremes meets white
black_widths_diff = []
white_widths = []
for i in range(len(a_extremes)):
a_extreme = a_extremes[i]
b_extreme = b_extremes[i]
black_widths_diff.append(b_extreme-a_extreme)
min_black_width = min(black_widths_diff)
for i in range(len(a_extremes)):
a_extreme = a_extremes[i]
b_extremes[i] = a_extremes[i]+min_black_width
b_extreme = b_extremes[i]
mid = (b_extreme-a_extreme)//2+a_extreme
white_widths.append(mid)
'''
'''
x_nodes = [] # x coordinates for first points touching white
y_nodes = [] # y coordinates for first points touching white
for mid in white_widths:
for i in range(top_right[1],bottom_right[1]):
if frame_contour[i][mid] != 0:
x_nodes.append(mid)
y_nodes.append(i)
break
# Black keys rectangles
mid_line_y = int(np.mean(y_nodes))
for i in range(len(a_extremes)):
a_extreme = a_extremes[i]
b_extreme = b_extremes[i]
start_point = (a_extreme, top_right[1]) #top-left point of every black key
end_point = (b_extreme, mid_line_y) #bottom-right point
keys["black"].append({ "id": i+1, "pt1": start_point, "pt2": end_point})
# White keys rectangles
keys["white"] = white_widths
# find minimum width of w a white key
white_widths_diff = [j-i for i, j in zip(white_widths[:-1], white_widths[1:])]
min_w = min(white_widths_diff)
# when a detected white width is unually wide, it means there are two keys
for i in range(1,len(white_widths)):
width = white_widths[i]-white_widths[i-1]
if width > min_w*1.4:
i = white_widths[i-1]+width//2
keys["white"].append(i)
keys["white"].sort()
# append the first white key in a tmp array
start_point = (top_left[0], top_left[1])
end_point = (keys["white"][1], bottom_right[1])
to_append = {"id": 1, "pt1": start_point, "pt2": end_point}
tmp_arr = [to_append]
# map widths to rectangles identifying white keys
for i in range(len(keys["white"])-1):
start_point = (keys["white"][i], top_left[1])
end_point = (keys["white"][i+1], bottom_right[1])
to_append = {"id": i+2, "pt1": start_point, "pt2": end_point}
tmp_arr.append(to_append)
keys["white"] = tmp_arr
# fill final undetected white keys
i = len(keys["white"])+1
while keys["white"][-1]["pt2"][0] < bottom_right[0]:
start_point = (keys["white"][-1]["pt2"][0], top_left[1])
end_point = (2*keys["white"][-1]["pt2"][0]-keys["white"][-2]["pt2"][0], bottom_right[1])
to_append = {"id": i, "pt1": start_point, "pt2": end_point}
keys["white"].append(to_append)
i += 1
keys["white"][-1]["pt2"] = (bottom_right[0], keys["white"][-1]["pt2"][1])
H_shape_inv = np.linalg.inv(H_shape)
for key in keys["white"]:
x1, y1 = key["pt1"]
x2, y2 = key["pt2"]
key["contour"] = np.array([[
projectPoint([x1, y1], H_shape_inv),
projectPoint([x1, y2], H_shape_inv),
projectPoint([x2, y2], H_shape_inv),
projectPoint([x2, y1], H_shape_inv)
]])
for key in keys["black"]:
x1, y1 = key["pt1"]
x2, y2 = key["pt2"]
key["contour"] = np.array([[
projectPoint([x1, y1], H_shape_inv),
projectPoint([x1, y2], H_shape_inv),
projectPoint([x2, y2], H_shape_inv),
projectPoint([x2, y1], H_shape_inv)
]])
return keys
def candidateKeys(frame_segmented, keys,
white=True, white_hough_thresh=60, black_hough_thresh=40):
"""Selects the candidate keys with hough lines
Parameters
---------
frame_segmented : numpy array
the binary image containing the keyboard mask
keys : list
list containing the contours of each key
white : bool , optional
true if we are selecting white keys, false if black keys
white_hough_thresh : int , optional
Accumulator threshold for hough lines algorithm for white keys
black_hough_thresh : int , optional
Accumulator threshold for hough lines algorithm for black keys
Outputs
---------
candidate_keys : list
list of contours of the candidate keys
frame_lines : numpy array
frame with drawn hough lines
"""
if not white:
frame_lines = np.zeros(frame_segmented.shape[0:2])
contours, _ = cv2.findContours(frame_segmented, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
mean_area = np.mean([cv2.contourArea(x) for x in contours])
for cnt in contours:
if cv2.contourArea(cnt) > mean_area:
cv2.drawContours(frame_lines,[cnt],0,(255,255,255),thickness=cv2.FILLED)
frame_segmented = frame_lines
y = cv2.Sobel(frame_segmented, cv2.CV_64F, 1,0, ksize=3, scale=1)
frame_sobel = cv2.convertScaleAbs(y)
# cv2.imwrite("zdacanc\\soble_white_{}.png".format(white), frame_sobel)
key = keys["black"][0]
max_contour_h = max(key["contour"][0][:,1])
key = keys["black"][-1]
max_contour_h = (max_contour_h+max(key["contour"][0][:,1]))//2
split_height = max_contour_h
frame_sobel = np.concatenate((frame_sobel[0:split_height,0:1920],
np.zeros((1080-split_height,1920), dtype=np.uint8)),
axis=0)
frame_lines = frame_segmented.copy() #frame_canny.copy()
canditate_pressed = []
if white:
lines = cv2.HoughLines(frame_sobel, 1, np.pi/180, white_hough_thresh)
else:
lines = cv2.HoughLines(frame_sobel, 1, np.pi/180, black_hough_thresh)
if lines is None:
lines = []
theta_within = np.pi/2
theta_tol = 1
# The below for loop runs till r and theta values
# are in the range of the 2d array
for r_theta in lines:
arr = np.array(r_theta[0], dtype=np.float64)
r, theta = arr
if theta > np.pi:
theta = theta-np.pi
r_theta[0][1] = theta
# Stores the value of cos(theta) in a
a = np.cos(theta)
# Stores the value of sin(theta) in b
b = np.sin(theta)
# x0 stores the value rcos(theta)
x0 = a*r
# y0 stores the value rsin(theta)
y0 = b*r
# x1 stores the rounded off value of (rcos(theta)-1000sin(theta))
x1 = int(x0 + 1920*(-b))
# y1 stores the rounded off value of (rsin(theta)+1000cos(theta))
y1 = int(y0 + 1920*(a))
# x2 stores the rounded off value of (rcos(theta)+1000sin(theta))
x2 = int(x0 - 1920*(-b))
# y2 stores the rounded off value of (rsin(theta)-1000cos(theta))
y2 = int(y0 - 1920*(a))
if np.abs(theta+np.pi/2-theta_within) > np.pi/180*theta_tol:
continue
else:
canditate_pressed.append((x1+x2)//2)
# cv2.line draws a line in img from the point(x1,y1) to (x2,y2).
cv2.line(frame_lines, (x1, y1), (x2, y2), (255, 255, 255), 2)
candidate_keys = []
if white: