forked from MUKESH-SK/Moving-Dartboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasket_detection.py
85 lines (71 loc) · 2.63 KB
/
basket_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
import math
from datetime import time
import time
from imutils.video import VideoStream
from imutils.video import FPS
import cv2
import cvzone
from cvzone.ColorModule import ColorFinder
import numpy as np
# Initialize the Video
# cap = cv2.VideoCapture('Videos/vid (4).mp4')
cap = VideoStream(src=0).start()
time.sleep(2.0)
fps = FPS().start()
# Create the color Finder object
myColorFinder = ColorFinder(False)
hsvVals = {'hmin': 8, 'smin': 96, 'vmin': 115, 'hmax': 14, 'smax': 255, 'vmax': 255}
# Variables
posListX, posListY = [], []
xList = [item for item in range(0, 1300)]
prediction = False
while True:
# Grab the image
img = cap.read()
# img = cv2.imread("Ball.png")
img = img[0:900, :]
# Find the Color Ball
imgColor, mask = myColorFinder.update(img, hsvVals)
# Find location of the Ball
imgContours, contours = cvzone.findContours(img, mask, minArea=500)
if contours:
posListX.append(contours[0]['center'][0])
posListY.append(contours[0]['center'][1])
if posListX:
# Polynomial Regression y = Ax^2 + Bx + C
# Find the Coefficients
A, B, C = np.polyfit(posListX, posListY, 2)
for i, (posX, posY) in enumerate(zip(posListX, posListY)):
pos = (posX, posY)
cv2.circle(imgContours, pos, 10, (0, 255, 0), cv2.FILLED)
if i == 0:
cv2.line(imgContours, pos, pos, (0, 255, 0), 5)
else:
cv2.line(imgContours, pos, (posListX[i - 1], posListY[i - 1]), (0, 255, 0), 5)
for x in xList:
y = int(A * x ** 2 + B * x + C)
cv2.circle(imgContours, (x, y), 2, (255, 0, 255), cv2.FILLED)
if len(posListX) < 10:
# Prediction
# X values 330 to 430 Y 590
a = A
b = B
c = C - 590
# x = int((-b - math.sqrt(b ** 2 - (4 * a * c))) / (2 * a))
# prediction = 330 < x < 430
# if prediction:
# cvzone.putTextRect(imgContours, "Basket", (50, 150),
# scale=5, thickness=5, colorR=(0, 200, 0), offset=20)
# else:
# cvzone.putTextRect(imgContours, "No Basket", (50, 150),
# scale=5, thickness=5, colorR=(0, 0, 200), offset=20)
# Display
imgContours = cv2.resize(imgContours, (0, 0), None, 0.7, 0.7)
# cv2.imshow("Image", img)
cv2.imshow("ImageColor", imgContours)
# cv2.imshow("Frame", frame)
key = cv2.waitKey(1) & 0xFF
if key == ord("q"):
break
fps.update()
# cv2.waitKey(0)