-
Notifications
You must be signed in to change notification settings - Fork 0
/
view.py
executable file
·109 lines (83 loc) · 3.17 KB
/
view.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
#view.py
import os
os.environ['QT_QPA_PLATFORM'] = 'xcb'
import cv2
import numpy as np
import RPi.GPIO as GPIO
import sched
import time
capture = cv2.VideoCapture(0)
capture.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
last_distance = 0
def check_ground():
step_size = 6
fov_width = 173
edge_array = []
_, img = capture.read()
img = cv2.resize(img, (640, 480))
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img_gray = cv2.bilateralFilter(img_gray, 10, 25, 50)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (10,10))
dilate = cv2.morphologyEx(img_gray, cv2.MORPH_DILATE, kernel)
img_edge = cv2.Canny(dilate, 35, 150)
imagewidth = img_edge.shape[1] - 1
imageheight = img_edge.shape[0] - 1
for j in range(0, imagewidth, step_size):
for i in range(imageheight-5, 0, -1):
if img_edge.item(i, j) == 255:
edge_array.append((j, i))
break
else:
edge_array.append((j, 0))
edge_array.insert(0, (0, imageheight))
edge_array.append((imagewidth, imageheight))
top_left = (0, 0)
bottom_left = (0, imageheight)
top_right = (imagewidth, 0)
bottom_right = (imagewidth, imageheight)
pts1 = np.float32([top_left, bottom_left, top_right, bottom_right])
pts2 = np.float32([[fov_width, 0], [0, imageheight], [(imagewidth - fov_width), 0], [imagewidth, imageheight]])
transform_matrix = cv2.getPerspectiveTransform(pts1, pts2)
transformed_edge_array = []
for point in edge_array:
x, y = point
original_coordinates = np.array([x, y, 1])
transformed_coordinates = np.dot(transform_matrix, original_coordinates)
transformed_x = int(transformed_coordinates[0] / transformed_coordinates[2])
transformed_y = int(transformed_coordinates[1] / transformed_coordinates[2])
transformed_edge_array.append((transformed_x, transformed_y))
cv2.waitKey(1)
return transformed_edge_array
def ping():
global last_distance
GPIO.setmode(GPIO.BOARD)
TRIG_PIN = 13
ECHO_PIN = 15
GPIO.setup(TRIG_PIN, GPIO.OUT)
GPIO.setup(ECHO_PIN, GPIO.IN)
pulse_start_time = 0
pulse_end_time = 0
try:
GPIO.output(TRIG_PIN, GPIO.LOW)
time.sleep(2E-6)
GPIO.output(TRIG_PIN, GPIO.HIGH)
time.sleep(10E-6)
GPIO.output(TRIG_PIN, GPIO.LOW)
while GPIO.input(ECHO_PIN) == 0:
pulse_start_time = time.time()
while GPIO.input(ECHO_PIN) == 1:
pulse_end_time = time.time()
pulse_duration = pulse_end_time - pulse_start_time
distance = round(pulse_duration * 17150, 2)
if distance > 300 or distance <= 0:
distance = last_distance
else:
last_distance = distance
#print(distance)
time.sleep(0.1)
return distance
except Exception as e:
print(f"Error in ping function: {e}")
# pri zmene vzdalenosti se meni uhel, bude se muset prepocitavat uhel podle vzdalenosti z hc sr04
# pridat nejakou maximalni vzdalenost kamery od objektu aby nedochazelo k prilis nizkym hodnotam y v edge_array