-
Notifications
You must be signed in to change notification settings - Fork 2
/
4-object-detection-with-color.py
85 lines (57 loc) · 2.18 KB
/
4-object-detection-with-color.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 cv2
import numpy as np
from collections import deque
# for keeping center points of object
buffer_size = 16
pts = deque(maxlen=buffer_size)
# blue HSV
blueLower = (84, 98, 0)
blueUpper = (179, 255, 255)
#capture
cap = cv2.VideoCapture(0)
cap.set(3,960)
cap.set(4,480)
while True:
success, imgOriginal = cap.read()
if success:
#blur
blurred = cv2.GaussianBlur(imgOriginal, (11,11), 0)
# HSV
hsv = cv2.cvtColor(blurred, cv2.COLOR_BGR2HSV)
cv2.imshow("HSV IMAGE", hsv)
# mask for blue
mask = cv2.inRange(hsv, blueLower, blueUpper)
# deleting noises which are in area of mask
mask = cv2.erode(mask, None, iterations=2)
mask = cv2.dilate(mask, None, iterations=2)
cv2.imshow("Mask + Erosion + Dilation", mask)
# contours
contours,_ = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
center = None
if len(contours) > 0:
# get max contour
c = max(contours, key=cv2.contourArea)
# return rectangle
rect = cv2.minAreaRect(c)
((x,y), (width, height), rotation) = rect
s = f"x {np.round(x)}, y: {np.round(y)}, width: {np.round(width)}, height: {np.round(height)}, rotation: {np.round(rotation)}"
print(s)
# box
box = cv2.boxPoints(rect)
box = np.int64(box)
# moment
M = cv2.moments(c)
center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))
# draw contour
cv2.drawContours(imgOriginal, [box], 0, (0, 255, 255), 2)
# point in center
cv2.circle(imgOriginal, center, 5, (255, 0, 255), -1)
# print inform
cv2.putText(imgOriginal, s, (25, 50), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (255, 255, 255), 2)
# deque
pts.appendleft(center)
for i in range(1, len(pts)):
if pts[i - 1] is None or pts[i] is None: continue
cv2.line(imgOriginal, pts[i - 1], pts[i], (0, 255, 0), 3)
cv2.imshow("DETECTED IMAGE", imgOriginal)
if cv2.waitKey(1) & 0xFF == ord("q"): break