forked from MemExplorer/poker-vision
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcv2utils.py
146 lines (124 loc) · 4.73 KB
/
cv2utils.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
import cv2
import numpy as np
from threading import Thread
class ThreadedCamera(object):
def __init__(self, source = 0, w = 1920, h = 1080):
self.capture = cv2.VideoCapture(source)
self.capture.set(cv2.CAP_PROP_FRAME_WIDTH, w)
self.capture.set(cv2.CAP_PROP_FRAME_HEIGHT, h)
self.capture.set(cv2.CAP_PROP_FPS, 60)
self.capture.set(cv2.CAP_PROP_BUFFERSIZE, 10)
self.thread = Thread(target = self.update, args = ())
self.thread.daemon = True
self.thread.start()
self.status = False
self.frame = None
def update(self):
while True:
if self.capture.isOpened():
(self.status, self.frame) = self.capture.read()
def grab_frame(self):
if self.status:
return self.frame
return None
# show image in a popup window
def show_image(image, title = "image"):
cv2.namedWindow(title, cv2.WINDOW_AUTOSIZE)
cv2.imshow(title, image)
cv2.waitKey(0)
# highlight object in an image
def draw_image(image, points):
color = (36, 255, 12)
pt1 = (int(points[0][0][0]), int(points[0][0][1]))
pt2 = (int(points[1][0][0]), int(points[1][0][1]))
pt3 = (int(points[2][0][0]), int(points[2][0][1]))
pt4 = (int(points[3][0][0]), int(points[3][0][1]))
cv2.line(image, pt1, pt2, color, 3)
cv2.line(image, pt2, pt3, color, 3)
cv2.line(image, pt3, pt4, color, 3)
cv2.line(image, pt4, pt1, color, 3)
# technique to check distance between two objects in an image regardless of image size
def calculate_normalized_distance(point1, point2, image_width, image_height):
# Calculate the Euclidean distance between two points
distance = np.linalg.norm(np.array(point1) - np.array(point2))
# Normalize the distance based on image dimensions
normalized_distance = distance / max(image_width, image_height)
return normalized_distance
# function for check if two objects in an image are close to one another or not
def find_if_close(image, cnt1, cnt2):
threshold = 0.2
image_height, image_width, _ = image.shape
object_centers = []
for contour in [cnt1, cnt2]:
M = cv2.moments(contour)
if M["m00"] != 0:
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])
object_centers.append((cX, cY))
normalized_distance = calculate_normalized_distance(object_centers[0], object_centers[1], image_width, image_height)
return normalized_distance < threshold
# sets name of the detected card
def set_image_text(image, name, pts):
average = np.sum(pts, axis=0)/len(pts)
text_x = int(average[0][0])
text_y = int(average[0][1])
# Define the text and font parameters
text = name
font = cv2.LINE_AA
font_scale = 0.5
font_color = (255, 0, 0) # Black color in BGR
font_thickness = 2
text_size_vec = cv2.getTextSize(text, font, font_scale, font_thickness)[0]
to_sub_len = int(text_size_vec[0] * 0.5)
yadd = int((text_size_vec[1] * 2.8))
# Put the text on the image
cv2.putText(image, text, (text_x - to_sub_len, text_y + yadd), font, font_scale, font_color, font_thickness)
# sets name of the detected group
def set_group_text(image, name, x, y):
text_x = int(x)
text_y = int(y)
# Define the text and font parameters
text = name
font = cv2.FONT_HERSHEY_SIMPLEX
font_scale = 0.8
font_color = (255,0,0) # Black color in BGR
font_thickness = 2
# Put the text on the image
cv2.putText(image, text, (text_x, text_y - 10), font, font_scale, font_color, font_thickness)
# highlights the group of cards
def highlight_grouped_cards(image, card_group, text):
x,y,w,h = union(card_group[0], card_group[-1])
wdiff = int(w * 0.05)
hdiff = int(h * 0.05)
x -= int(wdiff)
y -= int(hdiff)
w += wdiff * 2
h += hdiff * 2
cv2.rectangle(image, (x, y), (x + w, y + h),(0,0,255), 3)
set_group_text(image, text, x, y)
# highlights all the cards in a list
def highlight_card_list(image, card_list):
for b in card_list:
draw_image(image, b.points)
set_image_text(image, str(b.card), b.points)
# clusters near cards
def group_near_cards(image, card_list):
ret = []
curr_list = []
for i in range(len(card_list)):
curr_list.append(card_list[i])
if i + 1 < len(card_list) and find_if_close(image, card_list[i], card_list[i + 1]):
continue
curr_list.sort(key= lambda x: cv2.boundingRect(x)[0])
ret.append(curr_list)
curr_list = []
return ret
# union two contours
def union(a1,b1):
a = cv2.boundingRect(a1)
b = cv2.boundingRect(b1)
x = min(a[0], b[0])
y = min(a[1], b[1])
w = max(a[0]+a[2], b[0]+b[2]) - x
h = max(a[1]+a[3], b[1]+b[3]) - y
return (x, y, w, h)