-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathimg2sgf.py
1271 lines (1091 loc) · 47.3 KB
/
img2sgf.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
# Load/capture an image and convert to SGF
# Alexander Hanysz, March 2020
# https://github.com/hanysz/img2sgf
# Written for personal use, largely to learn about OpenCV.
# Distributed without warranty, use at your own risk!
# This file is in four parts:
# imports/setup
# image processing functions
# GUI functions
# create GUI and main loop
# To do:
# Future enhancements
# problem with L19 diagrams (and others): stones close together don't get detected as circles. May need to replace Hough circle detection with contour detection?
# Part 1: imports/setup
import tkinter as tk
from tkinter import messagebox as mb
from tkinter import filedialog
from tkinter import scrolledtext as scrolledtext
import cv2 as cv
import numpy as np
from sklearn.cluster import AgglomerativeClustering
from enum import Enum, IntEnum
from bisect import bisect_left
import matplotlib # need to import top level package to get version number for log
import sklearn # ditto
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
from PIL import Image, ImageTk, ImageEnhance #, ImageGrab -- Windows/Mac only!
try:
from PIL import ImageGrab # ImageGrab is not available for Linux!
using_PIL_ImageGrab = True
except ImportError:
import pyscreenshot as ImageGrab
using_PIL_ImageGrab = False
from datetime import datetime
import os, sys, math, string
BOARD_SIZE = 19
threshold_default = 80 # line detection votes threshold
black_stone_threshold_default = 128 # brightness on a scale of 0-255
black_stone_threshold = black_stone_threshold_default
edge_min_default = 50 # edge detection min threshold
edge_max_default = 200
sobel_default = 3 # edge detection: Sobel filter size, choose from 3, 5 or 7
gradient_default = 1 # edge detection: 1=L1 norm, 2=L2 norm
maxblur = 3 # make four blurred images (blur=1, 3, 5, 7) for circle detection
angle_tolerance = 1.0 # accept lines up to 1 degree away from horizontal or vertical
angle_delta = math.pi/180*angle_tolerance
min_grid_spacing = 10
big_space_ratio = 1.6 # try to fill in grid spaces that are >1.6 * min spacing
contrast_default = 70 # by default, raise the contrast a bit, it often seems to help!
brightness_default = 50 # don't change brightness
image_size = 400
border_size = 20
header_size = 230
main_width = 3*image_size + 4*border_size
main_height = image_size + header_size + 3*border_size
settings_width = 900
s_width = 400 # width of frames within settings window
settings_height = 500
settings_visible = False
log_width = 650
log_height = 800
log_visible = False
class Direction(Enum):
HORIZONTAL = 1
HORIZ = 1
H = 1
VERTICAL = 2
VERT = 2
V = 2
class BoardStates(IntEnum):
EMPTY, BLACK, WHITE, STONE = range(4)
# use STONE as temporary flag for colour not yet determined
class Alignment(IntEnum):
TOP, BOTTOM, LEFT, RIGHT = range(4)
BLACK, WHITE = 1, 2 # can's use enum for this because tkIntVar() will only accept int!
image_loaded = False
found_grid = False
valid_grid = False
board_ready = False
selection_local = np.array((0,0,0,0))
# selection rectangle x1, y1, x2, y2 relative to current region
selection_global = np.array((0,0,0,0)) # current region relative to original image
stone_brightnesses = []
# Part 2: image processing functions
def rectangle_centre(a):
return np.array(( (a[0]+a[2])/2, a[1]+a[3]/2 ))
def crop_and_rotate_image():
global region_PIL
rotation_centre = tuple(rectangle_centre(selection_global))
region_PIL = input_image_PIL.rotate(angle=-rotate_angle.get(), fillcolor="white",
center = rotation_centre).crop(selection_global)
def process_image():
global input_image_np, edge_detected_image_np, edge_detected_image_PIL, \
circles, circles_removed_image_np, circles_removed_image_PIL, \
grey_image_np, region_PIL
# photos (_PIL images) need to be global so that the garbage collector doesn't
# clean them up and blank out the canvases
# numpy images (_np) are used by other functions
global threshold_hist, threshold_line
# global so that other functions can move and redraw the line
global found_grid, valid_grid, board_ready
# keep other functions informed of processing status
if not image_loaded:
return
found_grid = False
valid_grid = False
board_ready = False
log("\nProcessing image")
crop_and_rotate_image()
if rotate_angle.get() != 0:
log("Rotated by " + str(rotate_angle.get()) + " degrees")
log("Contrast = " + str(contrast.get()))
scaled_contrast = 102/(101-contrast.get())-1
# convert range 0-100 into range 0.01-101, with 50->1.0
region_PIL = ImageEnhance.Contrast(region_PIL).enhance(scaled_contrast)
log("Brightness = " + str(brightness.get()))
scaled_brightness = 450/(200-brightness.get())-2
# convert range 0-100 into range 0.25-2.5, with 50->1.0
region_PIL = ImageEnhance.Brightness(region_PIL).enhance(scaled_brightness)
input_image_np = np.array(region_PIL)
log("Converting to greyscale")
grey_image_np = cv.cvtColor(input_image_np, cv.COLOR_BGR2GRAY)
#log("Running Canny edge detection algorithm with parameters:\n" +
# "- min threshold=" + str(edge_min.get()) + "\n" +
# "- max threshold=" + str(edge_max.get()) + "\n" +
# "- Sobel aperture size=" + str(sobel.get()) + "\n" +
# "- L" + str(gradient.get()) + " norm")
log("Running Canny edge detection algorithm")
# no point logging the parameters now I've turned off the UI for changing them
edge_detected_image_np = cv.Canny(input_image_np,
edge_min.get(), edge_max.get(),
apertureSize = sobel.get(),
L2gradient = (gradient.get()==2))
edge_detected_image_PIL = Image.fromarray(edge_detected_image_np)
log("Detecting circles")
circles_removed_image_np = edge_detected_image_np.copy()
# Make a few different blurred versions of the image, so we can find most of the circles
blurs = [grey_image_np, edge_detected_image_np]
for i in range(maxblur+1):
b = 2*i + 1
blurs.append(cv.medianBlur(grey_image_np, b))
blurs.append(cv.GaussianBlur(grey_image_np, (b,b), b))
first_circles = True
circles = []
for b in blurs:
c = cv.HoughCircles(b, cv.HOUGH_GRADIENT, 1, 10, np.array([]), 100, 30, 1, 30)
if len(c)>0:
if first_circles:
circles = c[0]
first_circles = False
else:
circles = np.vstack((circles, c[0]))
# For each circle, erase the bounding box and replace by a single pixel in the middle
# This makes it easier to detect grid lines when
# there are lots of stones on top of the line
for i in range(len(circles)):
xc, yc, r = circles[i,:]
r = r+2 # need +2 because circle edges can stick out a little past the bounding box
ul = (int(round(xc-r)), int(round(yc-r)))
lr = (int(round(xc+r)), int(round(yc+r)))
middle = (int(round(xc)), int(round(yc)))
cv.rectangle(circles_removed_image_np, ul, lr, (0,0,0), -1) # -1 = filled
cv.circle(circles_removed_image_np, middle, 1, (255,255,255), -1)
circles_removed_image_PIL = Image.fromarray(circles_removed_image_np)
find_grid()
draw_images()
draw_histogram(stone_brightnesses) # this should erase the histogram from any previous board
def draw_histogram(stone_brightnesses):
global threshold_hist, threshold_line
black_thresh_subfigure.clear()
if not board_ready:
black_thresh_hist.draw()
return
threshold_hist = black_thresh_subfigure.hist(stone_brightnesses, bins=20,
range=[0,255], color='pink')
max_val = max(threshold_hist[0])
if threshold_line is not None:
threshold_line[0].remove() # remove old line before redrawing
threshold_line = black_thresh_subfigure.plot(2*[black_stone_threshold], [0,max_val],
color='red')
black_thresh_subfigure.text(black_stone_threshold, max_val*0.95,
str(black_stone_threshold), fontsize=8)
black_thresh_subfigure.text(black_stone_threshold-70, max_val*0.8,
str(num_black_stones) + " black", fontsize=8)
black_thresh_subfigure.text(black_stone_threshold+10, max_val*0.8,
str(num_white_stones) + " white", fontsize=8)
black_thresh_hist.draw()
def find_lines(threshold, direction):
# Lines are assumed to be horizontal or vertical
# Return value is a vector of x- or y-intercepts
# Remember that horizontal lines intercept the y-axis,
# be careful not to get x and y the wrong way round!
if direction == Direction.H:
lines = cv.HoughLines(circles_removed_image_np, rho=1, theta=math.pi/180.0, \
threshold=threshold, min_theta = math.pi/2 - angle_delta, \
max_theta = math.pi/2 + angle_delta)
else:
vlines1 = cv.HoughLines(circles_removed_image_np, rho=1, theta=math.pi/180.0, \
threshold=threshold, min_theta = 0, max_theta = angle_delta)
vlines2 = cv.HoughLines(circles_removed_image_np, rho=1, theta=math.pi/180.0, \
threshold=threshold, min_theta = math.pi - angle_delta, \
max_theta = math.pi)
if vlines2 is not None:
vlines2[:,0,0] = -vlines2[:,0,0]
vlines2[:,0,1] = vlines2[:,0,1] - math.pi
if vlines1 is not None:
lines = np.vstack((vlines1, vlines2))
else:
lines = vlines2
else:
lines = vlines1
return [] if lines is None else lines[:,0,0].reshape(-1,1)
# reshape because clustering function prefers column vector not row
def find_all_lines():
hlines = find_lines(threshold.get(), Direction.HORIZONTAL)
hcount = len(hlines)
vlines = find_lines(threshold.get(), Direction.VERTICAL)
vcount = len(vlines)
log("Found " + str(hcount) + " distinct horizontal lines and " +
str(vcount) + " distinct vertical lines")
return (hlines, vlines)
def find_clusters_fixed_threshold(threshold, direction):
lines = find_lines(threshold, direction)
if lines is not None:
cluster_model = AgglomerativeClustering(n_clusters=None, linkage = 'single', \
distance_threshold=min_grid_spacing)
try:
answer = cluster_model.fit(lines)
# this may fail if there's not enough lines
return answer
except:
return None
else:
return None
def get_cluster_centres(model, points):
if model is None:
return []
n = model.n_clusters_
answer = np.zeros(n)
for i in range(n):
this_cluster = points[model.labels_ == i]
answer[i] = this_cluster.mean()
answer.sort()
return answer
def cluster_lines(hlines, vlines):
global found_grid
hclusters = find_clusters_fixed_threshold(threshold.get(), Direction.HORIZ)
hcentres = get_cluster_centres(hclusters, hlines)
vsize_initial = len(hcentres) if hcentres is not None else 0
vclusters = find_clusters_fixed_threshold(threshold.get(), Direction.VERT)
vcentres = get_cluster_centres(vclusters, vlines)
hsize_initial = len(vcentres) if vcentres is not None else 0
log("Got " + str(vsize_initial) + " horizontal and " \
+ str(hsize_initial) + " vertical grid lines")
colours = 10*['r','g','b','c','k','y','m']
threshold_subfigure.clear()
if len(hlines)>0:
ymin, ymax = min(hlines), max(hlines)
if hclusters is not None:
for i in range(len(hlines)):
threshold_subfigure.plot(ymin, hlines[i], color=colours[hclusters.labels_[i]], marker=".")
for x in vcentres:
threshold_subfigure.plot((x,x), (ymin, ymax), "green", linewidth=1)
if len(vlines)>0:
xmin, xmax = min(vlines), max(vlines)
if vclusters is not None:
for i in range(len(vlines)):
threshold_subfigure.plot(vlines[i], xmin, color=colours[vclusters.labels_[i]], marker=".")
for y in hcentres:
threshold_subfigure.plot((xmin, xmax), (y,y), color="green", linewidth=1)
threshold_plot.draw()
if len(hcentres)>0 and len(vcentres)>0:
found_grid = True
return (hcentres, vcentres)
def complete_grid(x):
# Input: x is a set of grid coordinates, possibly with gaps
# stored as a numpy row vector, sorted
# Output: x with gaps filled in, if that's plausible, otherwise None if grid is invalid
if x is None or len(x)==0:
log("No grid lines found at all!")
return None
if len(x)==1:
log("Only found one grid line")
return None
spaces = x[1:] - x[:-1]
# Some of the spaces should equal the grid spacing, while some will be bigger because of gaps
min_space = min(spaces)
if min_space < min_grid_spacing:
log("Grid lines are too close together: minimum spacing is " + str(min_space) + " pixels")
return None
bound = min_space * big_space_ratio
big_spaces = spaces[spaces > bound]
if len(big_spaces)==0: # no gaps!
log("Got a complete grid of " + str(len(x)) + " lines")
return x
small_spaces = spaces[spaces <= bound]
max_space = max(small_spaces)
average_space = (min_space + max_space)/2
left = x[0]
right = x[-1]
n_exact = (right-left)/average_space
# Calculate total grid size, and check for weird gaps along the way
n = len(small_spaces)
for s in big_spaces:
m = s/average_space
n += int(round(m))
if n > BOARD_SIZE + 2:
log("Distance between edges of grid is " + str(n) +
" times minimum space.")
log("Extra lines on diagram, or a grid line detected twice?")
return None
# Now we know we have a valid grid (except maybe too big). Let's fill in the gaps.
n += 1 # need to increment because one gap equals two grid lines, two gaps=three lines etc
log("Got " + str(len(x)) + " lines within a grid of size " + str(n))
if len(x) < n:
log("Filling in gaps.")
answer = np.zeros(n)
answer[0] = x[0]
i, j = 1, 1 # i points to answer grid, j points to x grid
for s in spaces:
if s <= max_space:
answer[i] = x[j]
i += 1
j += 1
else:
m = int(round(s/average_space))
for k in range(m):
answer[i] = x[j-1] + (k+1)*s/m # linearly interpolate the missing 'x's
i += 1
j += 1 # yes, that's right, we've incremented i 'm' times but j only once
return answer
else:
return x
def truncate_grid(x):
# x is a vector of grid coordinates as for complete_grid()
# if size of x exceed board size by 1 or 2,
# the extra lines are likely to be a bounding box, board edge or text
# so we should drop them
if x is None:
return None
if len(x) == BOARD_SIZE + 2:
# two extra lines are likely to be a bounding box or board edges in the image
# so let's drop them
log("Dropping two extra lines at the outsides of the grid")
return(x[1:-1])
if len(x) == BOARD_SIZE + 1:
# most likely scenario is horizontal lines with a diagram caption underneath,
# and the text is recognised as an extra line
log("Dropping one extra line at the end of the grid")
return(x[:-1])
return(x)
def validate_grid(hcentres, vcentres):
log("Assessing horizontal lines.")
hcentres = truncate_grid(hcentres)
hcentres_complete = complete_grid(hcentres)
hcentres_complete = truncate_grid(hcentres_complete)
if hcentres_complete is None:
return [False, circles, 0, 0] + 4*[None]
log("Assessing vertical lines.")
vcentres = truncate_grid(vcentres)
vcentres_complete = complete_grid(vcentres)
vcentres_complete = truncate_grid(vcentres_complete)
if vcentres_complete is None:
return [False, circles, 0, 0] + 4*[None]
# Later we'll need the grid size and average spacing
vsize, hsize = len(hcentres_complete), len(vcentres_complete)
# Note: number of *horizontal* lines is the *vertical* sides of the board!
hspace = (hcentres_complete[-1] - hcentres_complete[0]) / vsize
vspace = (vcentres_complete[-1] - vcentres_complete[0]) / hsize
# And now that we know the spacing, let's get rid of any circles that are the wrong size
# (sometimes you get little circles from bits of letters and numbers on the diagram)
min_circle_size = min(hspace,vspace) * 0.3 # diameter must be > 60% of grid spacing
max_circle_size = max(hspace, vspace) * 0.65 # and less than 130% of grid spacing
newcircles = [c for c in circles if min_circle_size < c[2] < max_circle_size]
return (True, newcircles, vsize, hsize, hcentres_complete, vcentres_complete,
hspace, vspace)
def closest_index(a, x):
# Input: a is a number, x a sorted list of numbers
# Output: the index of the element of x closest to a
# Break ties to the left (smaller index)
i = bisect_left(x, a)
# This is the index of the largest element of x that's smaller than a
if i==0:
return 0
if i==len(x):
return i-1
# else i is between 1 and len(x)-1 inclusive
return i-1 if a-x[i-1] <= x[i]-a else i
def closest_grid_index(p):
# Input: p is (x, y) coordinates of a pixel (usually a circle centre)
# Output: (i, j) coordinates of p on the board
return (closest_index(p[0], vcentres_complete), closest_index(p[1], hcentres_complete))
def average_intensity(i, j):
# Input: i, j are grid coordinates of a point on the board
# Output: average pixel intensity of a neighbourhood of p,
# to help distinguish between black and white stones
x = vcentres_complete[i]
xmin, xmax = int(round(x-hspace/2)), int(round(x+hspace/2))
y = hcentres_complete[j]
ymin, ymax = int(round(y-vspace/2)), int(round(y+vspace/2))
# Truncate coordinates to stay in bounds: sometimes circles can go outside the image
xmin = max(0, xmin)
ymin = max(0, ymin)
xmax = min(grey_image_np.shape[1], xmax)
ymax = min(grey_image_np.shape[0], ymax)
return np.mean(grey_image_np[ymin:ymax, xmin:xmax]) #nb flip x,y for np indexing
def align_board(b, a):
# b is a part board, a is an alignment (top, left, etc)
# return a full board with b in the appropriate side/quadrant
board = np.zeros((BOARD_SIZE, BOARD_SIZE))
xoffset = BOARD_SIZE - hsize if a[0] == Alignment.RIGHT else 0
yoffset = BOARD_SIZE - vsize if a[1] == Alignment.BOTTOM else 0
for i in range(hsize):
for j in range(vsize):
board[i+xoffset, j+yoffset] = b[i,j]
return(board)
def identify_board():
global detected_board, full_board, stone_brightnesses, \
num_black_stones, num_white_stones
log("Guessing stone colours based on a threshold of " + str(black_stone_threshold))
detected_board = np.zeros((hsize, vsize))
num_black_stones, num_white_stones = 0,0
for c in circles:
detected_board[closest_grid_index(c[0:2])] = BoardStates.STONE
num_stones = np.count_nonzero(detected_board)
stone_brightnesses = np.zeros(num_stones)
i=0
for j in range(hsize):
for k in range(vsize):
if detected_board[j,k] == BoardStates.STONE:
stone_brightnesses[i] = average_intensity(j, k)
i += 1
num_black_stones = sum(stone_brightnesses <= black_stone_threshold)
black_text = str(num_black_stones) + " black stone"
if num_black_stones != 1:
black_text += "s"
num_white_stones = num_stones - num_black_stones
white_text = str(num_white_stones) + " white stone"
if num_white_stones != 1:
white_text += "s"
log("Detected " + black_text + " and " + white_text + " on a "
+ str(hsize) + "x" + str(vsize) + " board.")
# Guess whose move it is based on stone count:
# this will sometimes be wrong because of handicaps, captures, part board positions
# but the user can change it with a single click
if num_black_stones <= num_white_stones:
log("Guessing black to play")
side_to_move.set(BLACK)
else:
log("Guessing white to play")
side_to_move.set(WHITE)
draw_histogram(stone_brightnesses)
for i in range(hsize):
for j in range(vsize):
if detected_board[i,j] == BoardStates.STONE:
x = average_intensity(i, j)
detected_board[i,j] = BoardStates.BLACK if x <= black_stone_threshold \
else BoardStates.WHITE
full_board = align_board(detected_board.copy(), board_alignment)
def find_grid():
global valid_grid, board_ready, circles, vsize, hsize, hspace, vspace, \
hcentres, vcentres, hcentres_complete, vcentres_complete
# All the above are needed as inputs to identify_board() --
# easier to make them global rather than pass them in and out
hlines, vlines = find_all_lines()
hcentres, vcentres = cluster_lines(hlines, vlines)
valid_grid, circles, vsize, hsize, hcentres_complete, vcentres_complete, \
hspace, vspace = validate_grid(hcentres, vcentres)
if valid_grid:
# Plot any grid lines that got added to fill in gaps
added_hcentres = np.setdiff1d(hcentres_complete, hcentres)
added_vcentres = np.setdiff1d(vcentres_complete, vcentres)
xmin, xmax = min(vlines), max(vlines)
ymin, ymax = min(hlines), max(hlines)
for y in added_hcentres:
threshold_subfigure.plot((xmin, xmax), (y,y), color="red", linewidth=1)
for x in added_vcentres:
threshold_subfigure.plot((x,x), (ymin, ymax), "red", linewidth=1)
threshold_plot.draw()
if hsize > BOARD_SIZE:
log("Too many vertical lines!")
elif vsize > BOARD_SIZE:
log("Too many horizontal lines!")
else:
identify_board()
board_ready = True
save_button.configure(state=tk.ACTIVE)
draw_board() # if board_ready is false, this will blank out the board
def get_scale(img, c):
# img = image (PIL format)
# c = canvas
# return the scale factor for img to fit in c
x_c, y_c = c.winfo_width(), c.winfo_height()
x_i, y_i = img.size
return(min(x_c/x_i, y_c/y_i))
def scale_image(img, c):
# img = image (PIL format)
# c = canvas
# Return img scaled to fit in c, in PIL photo format ready for drawing
scale = get_scale(img, c)
x_i, y_i = img.size
scaled_image = img.resize((round(x_i*scale), round(y_i*scale)))
return (ImageTk.PhotoImage(scaled_image), scale)
# Part 3: GUI functions
def log(msg):
log_text.insert(tk.END, msg + "\n")
log_text.see(tk.END) # scroll to end when the text gets long
def choose_threshold(img):
# img is an image in PIL format
# Guess the best threshold for Canny line detection
# Generally, smaller images work better with smaller thresholds
x = min(img.size)
t = int(x/12.8 + 16) # just guessing the parameters, this seems to work OK
t = min(max(t, 20), 200) # restrict to t between 20 and 200
return int(t)
def initialise_parameters():
# common to open_file() and screen_capture()
global region_PIL, image_loaded, found_grid, valid_grid, \
board_ready, board_edited, board_alignment, \
previous_rotation_angle, black_stone_threshold, selection_global
image_loaded = True
found_grid = False
valid_grid = False
board_ready = False
save_button.configure(state=tk.DISABLED)
board_alignment = [Alignment.LEFT, Alignment.TOP]
reset_button.configure(state=tk.DISABLED)
rotate_angle.set(0)
previous_rotation_angle = 0
contrast.set(contrast_default)
brightness.set(brightness_default)
black_stone_threshold = black_stone_threshold_default
region_PIL = input_image_PIL.copy()
selection_global = np.array([0,0] + list(region_PIL.size))
rotate_angle.set(0)
threshold.set(choose_threshold(region_PIL))
process_image()
draw_images()
def open_file(input_file = None):
global input_image_PIL
if input_file is None:
input_file = filedialog.askopenfilename()
if len(input_file) > 0:
log("\n" + datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
log("Opening file " + input_file)
try:
input_image_PIL = Image.open(input_file).convert('RGB')
except:
log("Error: not a valid image file")
mb.showinfo("Can't open file",
input_file + " isn't a valid image file")
return
log("Image size " + str(input_image_PIL.size[0]) + "x" +
str(input_image_PIL.size[1]))
initialise_parameters()
# The next three functions collectively implement click and drag
# for selecting a rectangle.
# They're bound to input_canvas mouse events
def init_selection_rect(event):
global selection_local
selection_local = np.array((event.x, event.y, event.x, event.y))
def update_selection_rect(event):
global sel_rect_id, selection_local
if not image_loaded:
return
selection_local[2:4] = (event.x, event.y)
input_canvas.coords(sel_rect_id, tuple(selection_local))
def select_region():
global selection_local, selection_global, sel_rect_id, region_PIL
if not image_loaded:
return
xs = (selection_local[0], selection_local[2])
sel_x1, sel_x2 = min(xs), max(xs)
ys = (selection_local[1], selection_local[3])
sel_y1, sel_y2 = min(ys), max(ys)
if sel_x2-sel_x1 < 10 or sel_y2-sel_y1 <10:
return # don't select tiny rectangles
x_c, y_c = input_canvas.winfo_width(), input_canvas.winfo_height()
x_i, y_i = region_PIL.size
hscale, vscale = x_i/x_c, y_i/y_c
scale = max(hscale, vscale)
# need to calculate both scales because there might be empty space
# either to the right of or below the image
# but not both
old_centre = rectangle_centre(selection_global)
selection_global = np.array((
selection_global[0]+scale*sel_x1, selection_global[1]+scale*sel_y1,
selection_global[0]+scale*sel_x2, selection_global[1]+scale*sel_y2))
new_centre = rectangle_centre(selection_global)
# Adjust rectangle to compensate for rotation
offset = new_centre - old_centre
theta = -rotate_angle.get() * math.pi/180 # convert from degrees to radians
rotation_matrix = np.array(((math.cos(theta), math.sin(theta)),
(math.sin(theta), math.cos(theta))))
xdelta, ydelta = np.dot(rotation_matrix, offset) - offset
selection_global += (-xdelta, ydelta, -xdelta, ydelta)
# Make sure we haven't pushed the selection rectangle out of bounds,
# and round to whole numbers
selection_global[0] = round(max(selection_global[0], 0))
selection_global[1] = round(max(selection_global[1], 0))
selection_global[2] = round(min(selection_global[2], input_image_PIL.size[0]))
selection_global[3] = round(min(selection_global[3], input_image_PIL.size[1]))
new_hsize = int(selection_global[2]-selection_global[0])
new_vsize = int(selection_global[3]-selection_global[1])
log("\nZoomed in. Region size " + str(new_hsize) + "x" + str(new_vsize))
# Set line detection threshold appropriate for this image size:
threshold.set(choose_threshold(region_PIL))
process_image() # this will crop and rotate, and update everything else
# Reset selection rectangle drawn on image
input_canvas.delete("all")
sel_rect_id = input_canvas.create_rectangle(0,0,0,0,
dash=(6,6), fill='', outline='green', width=3)
draw_images()
def zoom_out(event):
global region_PIL, previous_rotation_angle
if image_loaded:
region_PIL = input_image_PIL.copy()
log("Zoomed out to full size")
initialise_parameters()
# The next three functions are for changing the black_stone_threshold setting
# by click and drag
# They're bound to black_thresh_canvas mouse events
def scale_brightness(event):
# Utility function: event.x is pixel coordinates on the black_thresh_canvas
# Rescale to 0-255 range
coords = black_thresh_subfigure.transData.inverted().transform((event.x,event.y))
return(int(coords[0]))
def set_black_thresh(event):
global black_stone_threshold, threshold_line
if not board_ready:
return
x_actual = scale_brightness(event)
x_min, x_max = black_thresh_subfigure.get_xlim()
if 0 <= x_actual <= x_max:
black_stone_threshold = scale_brightness(event)
# Prevent axis from resizing if line is at extreme right:
black_thresh_subfigure.set_xlim((x_min, x_max))
draw_histogram(stone_brightnesses)
def apply_black_thresh(event):
if not board_ready:
return
identify_board()
draw_board()
def screen_capture():
global input_image_PIL
main_window.state("iconic")
input_image_PIL = ImageGrab.grab()
main_window.state("normal")
log("\n" + datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
log("Screen capture")
log("Image size " + str(input_image_PIL.size[0]) + "x" +
str(input_image_PIL.size[1]))
initialise_parameters()
def to_SGF(board):
# Return an SGF representation of the board state
board_letters = string.ascii_lowercase # 'a' to 'z'
output = "(;GM[1]FF[4]SZ[" + str(BOARD_SIZE) + "]\n"
if side_to_move.get() == 1:
output += "PL[B]\n"
else:
output += "PL[W]\n"
black_moves, white_moves = "", ""
if BoardStates.BLACK in board:
black_moves += "AB"
for i in range(BOARD_SIZE):
for j in range(BOARD_SIZE):
if board[i,j] == BoardStates.BLACK:
black_moves += "[" + board_letters[i] + board_letters[j] + "]"
if BoardStates.WHITE in board:
white_moves += "AW"
for i in range(BOARD_SIZE):
for j in range(BOARD_SIZE):
if board[i,j] == BoardStates.WHITE:
white_moves += "[" + board_letters[i] + board_letters[j] + "]"
if side_to_move.get() == 1:
output += black_moves + "\n" + white_moves + "\n" + ")\n"
else:
output += white_moves + "\n" + black_moves + "\n" + ")\n"
# According to the SGF standard, it shouldn't make a difference
# which order the AB[] and AW[] tags come in,
# but at the time of writing,
# Lizzie uses this to deduce which side is to move (ignoring the PL[] tag)!
return output
def save_SGF():
global output_file
if output_file is not None:
output_file = filedialog.asksaveasfilename(initialfile = output_file)
else:
output_file = filedialog.asksaveasfilename()
sgf = open(output_file, "w")
sgf.write(to_SGF(full_board))
sgf.close()
log("Saved to file " + output_file)
def toggle_settings(status = None):
# If status is not given, change visible to hidden or vice versa
# If status = true, show the window; if false, hide it
global settings_visible
if status is not None:
settings_visible = not status # and we'll flip it below
if settings_visible:
settings_window.withdraw()
settings_visible = False
settings_button.configure(text="show settings")
else:
settings_window.deiconify()
settings_visible = True
settings_button.configure(text="hide settings")
def toggle_log(status = None):
global log_visible
if status is not None:
log_visible = not status # and we'll flip it below
if log_visible:
log_window.withdraw()
log_visible = False
log_button.configure(text="show log")
else:
log_window.deiconify()
log_visible = True
log_button.configure(text="hide log")
def reset_board():
global full_board
full_board = align_board(detected_board, board_alignment)
reset_button.configure(state=tk.DISABLED)
draw_board()
def draw_images():
global input_photo, processed_photo, sel_rect_id
# photos need to be global so that the garbage collector doesn't
# clean them up and blank out the canvases
if not image_loaded:
return
input_photo, scale = scale_image(region_PIL, input_canvas)
input_canvas.create_image(0, 0, image = input_photo, anchor="nw")
sel_rect_id = input_canvas.create_rectangle(0, 0, 0, 0,
dash=(6,6), fill='', outline='green', width=3)
processed_canvas.delete("all") # get rid of previous circles and grid
if show_circles.get() == 1:
processed_photo, scale = scale_image(edge_detected_image_PIL, processed_canvas)
else:
processed_photo, scale = scale_image(circles_removed_image_PIL, processed_canvas)
processed_canvas.create_image(0, 0, image = processed_photo, anchor="nw")
# Annotate processed image with grid and circles if applicable
if show_circles.get() == 1:
for c in circles:
x,y,r = [z*scale for z in c]
processed_canvas.create_oval(x-r, y-r, x+r, y+r, outline="orange")
if found_grid:
xmin, xmax = min(vcentres)*scale, max(vcentres)*scale
ymin, ymax = min(hcentres)*scale, max(hcentres)*scale
if valid_grid: # show the red lines where gaps have been filled
for y in hcentres_complete:
processed_canvas.create_line(xmin, y*scale, xmax, y*scale, fill="red", width=2)
for x in vcentres_complete:
processed_canvas.create_line(x*scale, ymin, x*scale, ymax, fill="red", width=2)
# if not valid, still show green lines with the uneven spacing or bad gaps
for y in hcentres:
processed_canvas.create_line(xmin, y*scale, xmax, y*scale, fill="green", width=2)
for x in vcentres:
processed_canvas.create_line(x*scale, ymin, x*scale, ymax, fill="green", width=2)
def draw_board():
output_canvas.configure(bg="#d9d9d9")
output_canvas.delete("all")
if not board_ready:
if image_loaded:
output_canvas.create_text((0,0), text="Board not detected!", anchor="nw")
output_canvas.create_text((0,30), text="Things to try:", anchor="nw")
output_canvas.create_text((0,60), text="- Select a smaller region", anchor="nw")
output_canvas.create_text((0,90), text="- Rotate the image", anchor="nw")
output_canvas.create_text((0,120), text="- Show settings", anchor="nw")
output_canvas.create_text((0,150), text=" -> Increase contrast", anchor="nw")
output_canvas.create_text((0,180), text=" -> Increase threshold", anchor="nw")
return
output_canvas.configure(bg="#FFC050")
w, h = output_canvas.winfo_width(), output_canvas.winfo_height()
s = min(w,h) # size of board+margin
if s < 220: # too small to draw the board
output_canvas.create_text((0,0), text="Too small!", anchor="nw")
return
width = s-60 # width of the actual board
r = width/18/2.1 # radius of stones
coords = [i*width/18 + 30 for i in range(19)]
cmin, cmax = min(coords), max(coords)
for c in coords:
output_canvas.create_line(c, cmin, c, cmax)
output_canvas.create_line(cmin, c, cmax, c)
# Star points
for i in [coords[3], coords[9], coords[15]]:
for j in [coords[3], coords[9], coords[15]]:
output_canvas.create_oval(i-2, j-2, i+2, j+2, fill="black")
# Stones
for i in range(BOARD_SIZE):
for j in range(BOARD_SIZE):
x, y = coords[i], coords[j]
if full_board[i,j] == BoardStates.WHITE:
output_canvas.create_oval(x-r, y-r, x+r, y+r, fill="white")
elif full_board[i,j] == BoardStates.BLACK:
output_canvas.create_oval(x-r, y-r, x+r, y+r, fill="black")
# Positioning circles: these should only appear for part board positions
pos_centres = []
if hsize < BOARD_SIZE and vsize < BOARD_SIZE:
# corner position
pos_centres = [(15,15), (15,width+45), (width+45,15), (width+45,width+45)]
elif hsize < BOARD_SIZE:
# left or right size position
pos_centres = [(15, coords[9]), (width+45, coords[9])]
elif vsize < BOARD_SIZE:
# top or bottom position
pos_centres = [(coords[9], 15), (coords[9], width+45)]
for i, j in pos_centres:
output_canvas.create_oval(i-2, j-2, i+2, j+2, fill="pink")
output_canvas.create_oval(i-8, j-8, i+8, j+8)
def edit_board(event):
global board_alignment, full_board
if not board_ready:
return
x,y = event.x, event.y
w, h = output_canvas.winfo_width(), output_canvas.winfo_height()
cmin, cmax = 30, min(w,h)-30
grid_space = (cmax-cmin)/18
if cmin-grid_space/2 < x < cmax+grid_space/2 and \
cmin-grid_space/2 < y < cmax+grid_space/2:
i, j = round((x-cmin)/(cmax-cmin)*18), round((y-cmin)/(cmax-cmin)*18)
current_state = full_board[i,j]
if event.num == 1: # left-click
if current_state == BoardStates.EMPTY:
full_board[i,j] = BoardStates.WHITE
elif current_state == BoardStates.WHITE:
full_board[i,j] = BoardStates.BLACK
else:
full_board[i,j] = BoardStates.EMPTY
if event.num == 3: # right-click
if current_state == BoardStates.EMPTY:
full_board[i,j] = BoardStates.BLACK
elif current_state == BoardStates.BLACK:
full_board[i,j] = BoardStates.WHITE
else:
full_board[i,j] = BoardStates.EMPTY
reset_button.configure(state=tk.ACTIVE)
else:
# Clicked outside the board
# If we've got a corner/side position not a full board,
# check for clicks near the positioning dots
c1, c2 = min(w,h)/2-12, min(w,h)/2+12
old_alignment = board_alignment.copy()
if hsize < BOARD_SIZE and vsize < BOARD_SIZE:
if not (cmin<x<cmax or cmin<y<cmax): # ignore clicks away from the corners
board_alignment[0] = Alignment.LEFT if x<cmin else Alignment.RIGHT
board_alignment[1] = Alignment.TOP if y<cmin else Alignment.BOTTOM
elif vsize < BOARD_SIZE and c1<x<c2: # only respond to click at top or bottom
board_alignment[1] = Alignment.TOP if y<cmin else Alignment.BOTTOM
elif hsize < BOARD_SIZE and c1<y<c2: # only respond to click at left or right
board_alignment[0] = Alignment.LEFT if x<cmin else Alignment.RIGHT
if board_alignment != old_alignment:
full_board = align_board(detected_board, board_alignment)
reset_button.configure(state=tk.DISABLED)
# Sorry, moving the board will wipe out any other added/removed stones, can't undo