-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathposition-ver-3.py
178 lines (71 loc) · 2.7 KB
/
position-ver-3.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import torch
from yolov5.models.experimental import attempt_load
from yolov5.utils.general import non_max_suppression
from PIL import Image
from torchvision.transforms import functional as F
import serial
import time
# import the opencv library
import cv2
serial1 = serial.Serial('/dev/ttyACM0', 9600)
serial1.close() # Close the serial connection initially
time.sleep(2) # Wait for the serial connection to be established
serial1.open()
# Load the YOLOv8 model from a saved weights file
model = attempt_load('best.pt') #, map_location=torch.device('cpu')
# define a video capture object
vid = cv2.VideoCapture(0)
# Set the desired width and height for the captured frame
desired_width = 640
desired_height = 480
vid.set(cv2.CAP_PROP_FRAME_WIDTH, desired_width)
vid.set(cv2.CAP_PROP_FRAME_HEIGHT, desired_height)
# Get the current image from the webcam
ret, img = vid.read()
# Set the confidence threshold for predictions
conf_thresh = 0.25
# Set the maximum number of detections to keep after non-maximum suppression
max_det = 1000
# Set the IOU threshold for non-maximum suppression
iou_thresh = 0.45
# Load a single test image
# img = Image.open(r'test-image.jpg')
# Convert the image to RGB format
# img = img.convert('RGB')
# Convert the image to RGB format
img=cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
#cv2.imshow(img)
cv2.imwrite('testimagef2.jpg', img)
# Resize the image to the model input size
#img = img.resize(size=(800, 800))
# Convert the image to a PyTorch tensor
img_tensor = F.to_tensor(img)
# Add a batch dimension to the tensor
img_tensor = img_tensor.unsqueeze(0)
# Predict bounding boxes on the input image using the loaded model
with torch.no_grad():
output = model(img_tensor)
# Perform non-maximum suppression to remove overlapping detections
output = non_max_suppression(output, conf_thresh, iou_thresh, max_det=max_det)[0]
coords = []
for det in output:
x1, y1, x2, y2, conf, cls = det.cpu().numpy()
# print(cls)
if (cls == 1):
center_x = (x1 + x2) / 2
center_y = (y1 + y2) / 2
coords.append([center_x, center_y])
x = int(center_x)
y = int(center_y)
data = str(x) + ',' + str(y)
print(data)
data = data.encode('utf-8')
time.sleep(2)
serial1.write(data)
response = serial1.readline().decode().strip()
print(response)
if response != '0':
time.sleep(100)
pass
coords.append([x,y])
print(coords)