-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpcv5.py
More file actions
executable file
·84 lines (75 loc) · 2.33 KB
/
pcv5.py
File metadata and controls
executable file
·84 lines (75 loc) · 2.33 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
import cv2
import time
import socket
import numpy as np
import torch
model = torch.hub.load(
"ultralytics/yolov5", "custom",
path="v5n.pt"
)
WIDTH, HEIGHT = 256, 144
ACTIONS = {
"hola": b"wave",
}
def check_substring(arr, word):
counter, n = 0, len(arr) - 1
for char in arr:
if char == word[min(counter, n)]:
counter += 1
return counter >= len(word)
def check(arr):
if len(arr) < 2:
return None
for word in ACTIONS.keys():
if check_substring(arr, word):
return word
return None
def add_text(img, detections, selected):
counter = 0
for idx, char in enumerate(detections):
color = (0, 0, 255)
if selected is not None and char == selected[counter]:
counter += 1
color = (0, 255, 0)
pos, sz = ((idx + 1) * (WIDTH - 10) // 13, HEIGHT - 20), WIDTH // 240
cv2.putText(img, char.upper(), pos, 0, sz, color, sz+1)
def main(server):
conn, _ = server.accept()
selected = None
detections = []
last_detection = None
while True:
data = conn.recv(100000000)
im0 = cv2.imdecode(np.frombuffer(data, np.uint8), 1)
if im0 is None:
continue
result, choice = model(im0[:, :, ::-1]), None
print(result)
if result.pred[0].tolist():
choice = model.names[int(result.pred[0].tolist()[-1][-1])]
if choice is not None and choice != last_detection:
detections.append(choice)
last_detection = choice
elif choice is None:
last_detection = None
detections = detections[-12:]
selected = check(detections)
add_text(im0, detections, selected)
cv2.imshow("Robot's view", im0)
if cv2.waitKey(1) % 256 == 27: # pressing ESC
conn.sendall(b"exit")
break
if selected is not None:
conn.sendall(ACTIONS[selected])
detections.clear()
time.sleep(1)
cv2.destroyAllWindows()
conn.close()
server.close()
if __name__ == "__main__":
IP, PORT = "192.168.149.68", 30000
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((IP, PORT))
server.listen()
print("Server Ready!")
main(server)