-
Notifications
You must be signed in to change notification settings - Fork 2
/
ballDetect.py
110 lines (79 loc) · 2.91 KB
/
ballDetect.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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import cv2
import numpy as np
from math import *
import time
cap = cv2.VideoCapture('video.3gp')
fourcc = cv2.VideoWriter_fourcc('M', 'J', 'P', 'G')
writer = cv2.VideoWriter('output.avi', fourcc, 30.0, (1280, 720))
# ball tracking class
class CBall:
ballPosition = {'x': 0, 'y': 0, 'r': 0}
distance = 0.0
countFit = 0
distanceThreshold = 15
repeatThreshold = 3
def setPos(self, x, y, r):
newDistance = sqrt(pow(x, 2) + pow(y, 2))
if abs(self.distance - newDistance) < self.distanceThreshold:
self.countFit += 1
print 'addFit'
else:
self.countFit = 0
print 'resetFit'
# update position
self.distance = newDistance
self.ballPosition['x'], self.ballPosition['y'], self.ballPosition['r'] = x, y, r
# is over limit
if self.countFit > self.repeatThreshold:
return True
else:
return False
ball = CBall()
tracker = None
while (True):
ret, frame = cap.read()
if ret:
startTime = time.time()
scale = 0.5
inputImg = cv2.resize(frame, (0, 0), fx=scale, fy=scale)
HSVImg = cv2.cvtColor(inputImg, cv2.COLOR_RGB2HSV)
thresholdImg = cv2.inRange(HSVImg[:, :, 0], 104, 112)
blurImg = cv2.blur(thresholdImg, (7, 7))
circles = cv2.HoughCircles(blurImg, cv2.HOUGH_GRADIENT, 2, 20, param1=400, param2=90)
blurImg = cv2.cvtColor(blurImg, cv2.COLOR_GRAY2BGR)
# draw circles
if circles is not None:
for (x, y, r) in circles[0, :]:
if ball.setPos(x, y, r):
# update tracker
del tracker
tracker = cv2.TrackerKCF_create()
ok = tracker.init(inputImg, (int(x - r), int(y - r), int(r * 2), int(r * 2)))
cv2.rectangle(HSVImg, (int(x - r), int(y - r)), (int(x + r), int(y + r)),
(200, 200, 200), 5, 1)
cv2.circle(blurImg, (x, y), r, (200, 200, 200), 5)
# if not ballFit:
if tracker:
ok, roi = tracker.update(inputImg)
tracker.clear()
if ok:
p1 = (int(roi[0]), int(roi[1]))
p2 = (int(roi[0]) + int(roi[2]), int(roi[1]) + int(roi[3]))
cv2.rectangle(inputImg, p1, p2, (0, 200, 200), 5, 1)
# hsvv = HSVImg[:, :, 0]
h, s, v = cv2.split(HSVImg)
hsvvn = cv2.cvtColor(h, cv2.COLOR_GRAY2BGR)
imccn1 = np.concatenate((inputImg, HSVImg), axis=1)
imccn2 = np.concatenate((hsvvn, blurImg), axis=1)
imccn3 = np.concatenate((imccn1, imccn2), axis=0)
cv2.imshow('imgOut', imccn3)
writer.write(imccn3)
cv2.waitKey(1)
endTime = time.time()
print 'time:', endTime - startTime
else:
break
cap.release()
writer.release()