forked from MUKESH-SK/Moving-Dartboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdart det.py
37 lines (25 loc) · 1.26 KB
/
dart det.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
# Importing all modules
import cv2
import numpy as np
# Specifying upper and lower ranges of color to detect in hsv format
lower = np.array([15, 150, 20])
upper = np.array([35, 255, 255]) # (These ranges will detect Yellow)
# lower = np.array([129, 50, 70])
# upper = np.array([158, 255, 255])
# Capturing webcam footage
webcam_video = cv2.VideoCapture(0)
while True:
success, video = webcam_video.read() # Reading webcam footage
img = cv2.cvtColor(video, cv2.COLOR_BGR2HSV) # Converting BGR image to HSV format
mask = cv2.inRange(img, lower, upper) # Masking the image to find our color
mask_contours, hierarchy = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # Finding contours in mask image
# Finding position of all contours
if len(mask_contours) != 0:
for mask_contour in mask_contours:
if cv2.contourArea(mask_contour) > 70:
x, y, w, h = cv2.boundingRect(mask_contour)
cv2.rectangle(video, (x, y), (x + w, y + h), (0, 0, 255), 3) #drawing rectangle
print(f"{((x + w) / 2, (y + h) / 2)}")
cv2.imshow("mask image", mask) # Displaying mask image
cv2.imshow("window", video) # Displaying webcam image
cv2.waitKey(1)