-
Notifications
You must be signed in to change notification settings - Fork 0
/
detection.py
97 lines (63 loc) · 2.25 KB
/
detection.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Sep 6 10:40:02 2020
@author: ivan
"""
import cv2
from connection import shootpic
import matplotlib.pyplot as plt
import numpy as np
from functions import *
from classification import classification
from draw import draw_pattern, put_text
from connection import shootpic
from initSettings import cf
if cf.live:
img = shootpic()
else:
img = cv2.imread('full_image.jpeg')
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # convert to RGB
img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) # convert to HSV
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # convert to gray scale
# We apply a Canny edge detector
edges = cv2.Canny(img_gray, 200, 500)
# We find the contours
contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
masks = []
board = []
boxes = []
cv2.drawContours(img_gray, contours, -1, 255, thickness=5)
for idx in range(len(contours)):
#print(idx)
mask = np.zeros_like(img_gray) # Create mask where white is what we want, black otherwise
cv2.drawContours(mask, contours, idx, 255, -1) # Draw filled contour in mask
mask2 = mask.copy()
perc_nonzero = np.count_nonzero(mask) / (mask.shape[0]*mask.shape[1])
if perc_nonzero < cf.thresh_card_perc: continue
out = np.zeros_like(img) # Extract out the object and place into output image
out[mask == 255] = img[mask == 255]
rect = cv2.minAreaRect(contours[idx])
box = cv2.boxPoints(rect)
tup_props = classification(out)
num, shape, col, fil = tup_props
print('Number: {n} | Shape: {s} | Color: {c} | Filling: {f}'.format(c=COLORS[col], f=FILLS[fil], s=SHAPES[shape], n=str(num)))
if cf.show_cards:
# Show the output image
cv2.imshow('Output', cv2.resize(out, (750, 1000)))
cv2.waitKey(0)
cv2.destroyAllWindows()
board.append(Card(tup_props))
masks.append(mask)
boxes.append(box)
img_text = put_text(img_rgb, board, boxes)
plt.figure()
plt.imshow(img_text)
list_of_sets = find_sets(board)
if list_of_sets != []:
print('Sets found: {}'.format(list_of_sets))
else:
print('No sets found.')
img_out = draw_pattern(img_text, masks, list_of_sets)
plt.figure()
plt.imshow(img_out)