-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
180 lines (144 loc) · 5.49 KB
/
test.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
from bisect import bisect
import sys
import time
from tkinter import Y
import cv2
import torch
import numpy as np
def order_points_old(pts):
pts = np.array(pts)
# initialize a list of coordinates that will be ordered
# such that the first entry in the list is the top-left,
# the second entry is the top-right, the third is the
# bottom-right, and the fourth is the bottom-left
rect = np.zeros((4, 2), dtype="float32")
# the top-left point will have the smallest sum, whereas
# the bottom-right point will have the largest sum
s = pts.sum(axis=1)
rect[0] = pts[np.argmin(s)]
rect[2] = pts[np.argmax(s)]
# now, compute the difference between the points, the
# top-right point will have the smallest difference,
# whereas the bottom-left will have the largest difference
diff = np.diff(pts, axis=1)
rect[1] = pts[np.argmin(diff)]
rect[3] = pts[np.argmax(diff)]
# return the ordered coordinates
return rect
def crop_frame(frame, from_coords):
from_coords = np.array(order_points_old(from_coords), np.float32)
to_coords = np.array([
[0, 0],
[frame.shape[1], 0],
[frame.shape[1], frame.shape[0]],
[0, frame.shape[0]],
], np.float32)
trans = cv2.getPerspectiveTransform(from_coords, to_coords)
return cv2.warpPerspective(frame, trans, (frame.shape[1], frame.shape[0]))
def get_2_with_largest_area(coords):
largest = 0
coord1 = None
coord2 = None
for pair1 in coords:
for pair2 in coords:
area = abs((pair2[1] - pair1[1]) * (pair2[0] - pair1[0]))
if area > largest:
largest = area
coord1 = pair1
coord2 = pair2
return coord1, coord2
def detect_corner_rectangles(frame):
# Thresholding to extract colors of the tape
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# Split frame into two parts to analyze
sections = np.hsplit(hsv, 2)
bottom, top = sections[0], sections[1]
y_low = np.array([20, 130, 130])
y_high = np.array([30, 255, 255])
y_mask = cv2.inRange(top, y_low, y_high)
b_low = np.array([100, 140, 20])
b_high = np.array([130, 255, 255])
b_mask = cv2.inRange(bottom, b_low, b_high)
mask = np.hstack((b_mask, y_mask))
# mask = cv2.erode(mask, None, iterations=2)
# mask = cv2.dilate(mask, None, iterations=2)
extracted = cv2.bitwise_and(frame, frame, mask=mask)
extracted = cv2.cvtColor(extracted, cv2.COLOR_BGR2GRAY)
conts, _ = cv2.findContours(extracted, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
coords_ = []
for c in conts:
area = cv2.contourArea(c)
perim = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.1 * perim, True)
if (30 < area < 250) and len(approx) == 4:
m = cv2.moments(c)
x = int(m["m10"] / m["m00"])
y = int(m["m01"] / m["m00"])
# cv2.circle(frame, (x, y), 5, (0, 255, 0), -1)
coords_.append([x, y])
return coords_
def ball_detection(frame, prev_frame):
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
ball_low = np.array([40, 50, 50])
ball_high = np.array([70, 255, 255])
ball_mask = cv2.inRange(hsv, ball_low, ball_high)
ball_mask = cv2.morphologyEx(ball_mask, cv2.MORPH_OPEN, None)
extracted = cv2.bitwise_and(frame, frame, mask=ball_mask)
extracted = cv2.cvtColor(extracted, cv2.COLOR_BGR2GRAY)
cv2.imshow('extacted', extracted)
cv2.waitKey(1)
conts = cv2.findContours(extracted, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
new = []
for c in conts[0]:
c = cv2.convexHull(c)
area = cv2.contourArea(c)
if area > 2000 or area < 600:
pass
else:
new.append(c)
for c in new:
cv2.drawContours(frame, [c], 0, (255, 0, 0), 4)
# c = max(new, key=cv2.contourArea)
# ((x,y), r) = cv2.minEnclosingCircle(c)
# frame = cv2.circle(frame, (int(x),int(y)), int(r), (255,0,0), 3)
# for c in conts:
# area = cv2.contourArea(c)
# perim = cv2.arcLength(c, True)
# approx = cv2.approxPolyDP(c, 0.05*perim, True)
# k = cv2.isContourConvex(c)
# if area <1800 :
# cv2.drawContours(frame, [c], 0, (255, 0, 0), 4)
# cv2.imshow('test', frame)
# cv2.waitKey(0)
# Model
# model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)
# # Images
# imgs = ['/home/owhan/Downloads/homeless.jpg'] # batch of images
if __name__ == "__main__":
cap = cv2.VideoCapture('./datasets/green.mp4')
coords = np.array([[0, 0], [0, 1920], [1080, 0], [1080, 1920]])
runs = 0
new_time = 0
prev_time = time.time()
ok, prev_frame = cap.read()
while True:
ok, frame = cap.read()
if runs % 20 == 0:
new_coords = detect_corner_rectangles(frame)
if len(new_coords) == 4:
coords = new_coords
for coord in coords:
cv2.circle(frame, (coord[0], coord[1]), 8, (0, 255, 0), -1)
runs += 1
warped = crop_frame(frame, coords)
warped = cv2.GaussianBlur(warped, (21, 21), 0)
ball_detection(warped, prev_frame)
prev_frame = frame
new_time = time.time()
fps = str(int(1.0 / (new_time - prev_time)))
prev_time = new_time
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(warped, "fps: " + fps, (50, 50), font, 2, (255, 0, 255), 3, cv2.LINE_AA)
cv2.imshow("detections", frame)
cv2.imshow("warped", warped)
cv2.waitKey(1)