-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvideo_processing.py
More file actions
138 lines (107 loc) · 5.82 KB
/
Copy pathvideo_processing.py
File metadata and controls
138 lines (107 loc) · 5.82 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import cv2 as cv
import math
import numpy as np
from datetime import datetime as dt
from os import mkdir
folderName = f'{dt.now().day}.{dt.now().month}.{dt.now().year} {dt.now().hour}_{dt.now().minute}_{dt.now().second}'
mkdir(f'imgs/{folderName}')
q = 0
k = 0
cap = cv.VideoCapture(0)
screenCheck = False
while True:
try:
if screenCheck == False:
while True:
ret, frame = cap.read()
# dim = (int(frame.shape[1]*0.7), int(frame.shape[0]*0.7))
# frame = cv.resize(frame, dim)
cv.imshow('Video', frame)
if cv.waitKey(1) & 0xFF==ord('s'):
screenPath = f'imgs/{folderName}/screen{q}.png'
cv.imwrite(screenPath, frame)
screenCheck = True
q += 1
cv.destroyWindow('Video')
break
if cv.waitKey(1) & 0xFF == ord('e'):
screenCheck = None
break
elif screenCheck == True:
cv.namedWindow('Coordinates finder')
img = cv.imread(screenPath, cv.IMREAD_GRAYSCALE)
def nothing(x):
pass
cv.createTrackbar('MinDist', 'Coordinates finder', 1, 100, nothing)
cv.createTrackbar('Param1', 'Coordinates finder', 10, 100, nothing)
cv.createTrackbar('Param2', 'Coordinates finder', 10, 100, nothing)
cv.createTrackbar('MinRadius', 'Coordinates finder', 0, 100, nothing)
cv.setTrackbarPos('MinDist', 'Coordinates finder', 20)
cv.setTrackbarPos('Param1', 'Coordinates finder', 50)
cv.setTrackbarPos('Param2', 'Coordinates finder', 50)
cv.setTrackbarPos('MinRadius', 'Coordinates finder', 0)
minDist = param1 = param2 = minRadius = 0
while True:
try:
minDist = cv.getTrackbarPos('MinDist', 'Coordinates finder')
param1 = cv.getTrackbarPos('Param1', 'Coordinates finder')
param2 = cv.getTrackbarPos('Param2', 'Coordinates finder')
minRadius = cv.getTrackbarPos('MinRadius', 'Coordinates finder')
img = cv.medianBlur(img, 5)
cimg = cv.cvtColor(img, cv.COLOR_GRAY2BGR)
circles = cv.HoughCircles(img, cv.HOUGH_GRADIENT,
1.2, minDist=minDist,
param1=param1, param2=param2,
minRadius=minRadius, maxRadius=0)
circles = np.uint16(np.around(circles))
x0 = 0
y0 = 0
r0 = math.hypot(int(img.shape[1]), int(img.shape[0]))
for i in circles[0, :]:
# draw the outer circle
cv.circle(cimg, (i[0], i[1]), i[2], (0, 255, 0), 2)
# draw the center of the circle
cv.circle(cimg, (i[0], i[1]), 2, (0, 0, 255), 3)
if r0 > math.hypot(int(i[0]), (int(i[1])-int(img.shape[0]))):
x0 = i[0]
y0 = i[1]
r0 = math.hypot(int(i[0]), (int(i[1])-int(img.shape[0])))
n = 1
for i in circles[0, :]:
if i[0] != x0 and i[1] != y0:
cv.line(cimg, (x0, y0), (i[0], i[1]), (255, 0, 0), 2)
x1 = int(i[0]) - int(x0)
y1 = int(y0) - int(i[1])
dist = math.hypot(x1, y1)
if int(i[0])-50 < 0 and int(i[1])-15 > 0:
cv.putText(cimg, f'X{n}:{x1}, Y{n}: {y1}', (i[0], i[1]-10), cv.FONT_HERSHEY_SIMPLEX , 0.4, (255, 0, 0), 1, cv.LINE_AA)
elif int(i[0])-50 < 0 and int(i[1])-15 < 0:
cv.putText(cimg, f'X{n}:{x1}, Y{n}: {y1}', (i[0], i[1]+15), cv.FONT_HERSHEY_SIMPLEX , 0.4, (255, 0, 0), 1, cv.LINE_AA)
elif int(i[0])+100 > cimg.shape[1] and int(i[1])-15 < 0:
cv.putText(cimg, f'X{n}:{x1}, Y{n}: {y1}', (i[0]-100, i[1]+15), cv.FONT_HERSHEY_SIMPLEX , 0.4, (255, 0, 0), 1, cv.LINE_AA)
elif int(i[0])+100 > cimg.shape[1]:
cv.putText(cimg, f'X{n}:{x1}, Y{n}: {y1}', (i[0]-100, i[1]-10), cv.FONT_HERSHEY_SIMPLEX , 0.4, (255, 0, 0), 1, cv.LINE_AA)
else:
cv.putText(cimg, f'X{n}:{x1}, Y{n}: {y1}', (i[0]-50, i[1]-10), cv.FONT_HERSHEY_SIMPLEX , 0.4, (255, 0, 0), 1, cv.LINE_AA)
print(f'X{n}: {x1}, Y{n}: {y1}, R{n}: {dist}')
n += 1
cv.imshow('Coordinates finder', cimg)
except TypeError:
print('Упс! Ошибочка вышла, не перебарщивайте с параметрами!')
if cv.waitKey(1) & 0xFF==ord('s'):
cv.imwrite(f'imgs/{folderName}/circle_screen{k}.png', cimg)
k += 1
if cv.waitKey(1) & 0xFF == ord('c'):
screenPath = None
screenCheck = False
cv.destroyWindow('Coordinates finder')
break
if cv.waitKey(1) & 0xFF == ord('e'):
screenCheck = None
break
elif screenCheck == None:
break
except KeyboardInterrupt:
print('\nЗавершение работы программы\n')
break
cv.destroyAllWindows()