-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobject_track.py
119 lines (85 loc) · 3.26 KB
/
object_track.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
import cv2.cv2 as cv2
import numpy as np
import requests
# url = "http://192.168.1.34:8080//shot.jpg"
# Trackbar olusturma
def nothing(x):
pass
# Trackbar penceresi
cv2.namedWindow("Trackbar")
cv2.resizeWindow("Trackbar", 500, 500)
cv2.createTrackbar("Lower - H", "Trackbar", 0, 180, nothing)
cv2.createTrackbar("Lower - S", "Trackbar", 0, 255, nothing)
cv2.createTrackbar("Lower - V", "Trackbar", 0, 255, nothing)
cv2.createTrackbar("Upper - H", "Trackbar", 0, 180, nothing)
cv2.createTrackbar("Upper - S", "Trackbar", 0, 255, nothing)
cv2.createTrackbar("Upper - V", "Trackbar", 0, 255, nothing)
# trackbar pos ları default atama
cv2.setTrackbarPos("Upper - H", "Trackbar", 180)
cv2.setTrackbarPos("Upper - S", "Trackbar", 255)
cv2.setTrackbarPos("Upper - V", "Trackbar", 255)
cap = cv2.VideoCapture(0)
x_range = np.arange(320, 326, 1)
y_range = np.arange(235, 246, 1)
# print(y_range[:])
while True:
# img_resp = requests.get(url)
# img_arr = np.array(bytearray(img_resp.content),dtype=np.uint8)
# img = cv2.imdecode(img_arr,cv2.IMREAD_COLOR)
# img = cv2.resize(img,(640,480))
ret, frame = cap.read()
frame = cv2.flip(frame, 1)
hsv_img = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# Trackbar degerlerini alma
lower_h = cv2.getTrackbarPos("Lower - H", "Trackbar")
lower_s = cv2.getTrackbarPos("Lower - S", "Trackbar")
lower_v = cv2.getTrackbarPos("Lower - V", "Trackbar")
upper_h = cv2.getTrackbarPos("Upper - H", "Trackbar")
upper_s = cv2.getTrackbarPos("Upper - S", "Trackbar")
upper_v = cv2.getTrackbarPos("Upper - V", "Trackbar")
lower_color = np.array([lower_h, lower_s, lower_v])
upper_color = np.array([upper_h, upper_s, upper_v])
mask = cv2.inRange(hsv_img, lower_color, upper_color)
sensitivity = 15
lower_blue = np.array([120 - sensitivity, 100, 100])
upper_blue = np.array([120 + sensitivity, 255, 255])
kernel = np.ones((3, 3), np.uint8)
# mask = cv2.inRange(hsv_img, lower_blue, upper_blue)
# yumusatma
# erozyon ile
# mask = cv2.erode(mask, kernel)
# opening ile
mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)
cv2.imshow("mask", mask)
M = cv2.moments(mask)
if M["m10"] == 0:
M["m10"] = 1
if M["m00"] == 0:
M["m00"] = 1
if M["m01"] == 0:
M["m01"] = 1
# koordinat ekseni
cv2.line(frame, (320, 0), (320, 480), (255, 0, 0), 4)
cv2.line(frame, (0, 240), (640, 240), (255, 0, 0), 4)
# center green circle
cv2.circle(frame, (320, 240), 40, (0, 255, 0), 3)
X = int(M["m10"] / M["m00"])
Y = int(M["m01"] / M["m00"])
if M["m01"] and M["m00"] and M["m10"] != 1:
# object track circle
cv2.circle(frame, (X, Y), 40, (0, 0, 255), 5)
cv2.line(frame, (X, Y), (320, 240), (0, 0, 255), 3)
# origin = (320,240)
# print(X, "\n", Y)
if x_range.__contains__(X) & y_range.__contains__(Y):
cv2.putText(frame, "Hedefe Kilitlenildi!", (330, 420), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2,
lineType=cv2.LINE_AA)
# print("ortada")
cv2.imshow("frame", frame)
cv2.imshow("mask", mask)
if ret == False:
break
if cv2.waitKey(15) & 0xFF == ord("q"):
break
cap.release()
cv2.destroyAllWindows()