forked from OpenCVChina/DeepRobotDog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdashboard_recognition.py
87 lines (73 loc) · 2.7 KB
/
dashboard_recognition.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
import time
import struct
import cv2 as cv
import numpy as np
import threading
from utils.DashboardRecognition import DashboardRecognition
from utils.Controller import Controller
Develop_Mode = True # True means use computer camera. False means use dog camera
client_address = ("192.168.1.103", 43897)
server_address = ("192.168.1.120", 43893)
global frame
global number
global is_update_number
if __name__ == '__main__':
dashboard_detector = DashboardRecognition()
controller = Controller(server_address)
if Develop_Mode:
cap = cv.VideoCapture(0)
else:
cap = cv.VideoCapture("/dev/video0", cv.CAP_V4L2)
# cap.set(cv.CAP_PROP_FRAME_WIDTH, 1920)
# cap.set(cv.CAP_PROP_FRAME_HEIGHT, 1080)
stop_heartbeat = False
# start to exchange heartbeat pack
def heart_exchange(con):
pack = struct.pack('<3i', 0x21040001, 0, 0)
while True:
if stop_heartbeat:
return
con.send(pack)
time.sleep(0.25) # 4Hz
heart_exchange_thread = threading.Thread(target=heart_exchange, args=(controller,))
heart_exchange_thread.start()
# stand up
print("Wait 10 seconds and stand up......")
pack = struct.pack('<3i', 0x21010202, 0, 0)
controller.send(pack)
time.sleep(5)
controller.send(pack)
time.sleep(5)
controller.send(pack)
print("Dog should stand up, otherwise press 'ctrl + c' and re-run the demo")
# try to use CUDA
if cv.cuda.getCudaEnabledDeviceCount() != 0:
backend = cv.dnn.DNN_BACKEND_CUDA
target = cv.dnn.DNN_TARGET_CUDA
else:
backend = cv.dnn.DNN_BACKEND_DEFAULT
target = cv.dnn.DNN_TARGET_CPU
print('CUDA is not set, will fall back to CPU.')
status_buffer = [None] * 3
while True:
ret, frame = cap.read()
if not ret:
continue
bbox = dashboard_detector.detect(frame)
frame = dashboard_detector.visualize(frame)
status = dashboard_detector.get_status(frame)
status_buffer.insert(0, status)
status_buffer.pop()
if status is not None and all(s == status_buffer[0] for s in status_buffer):
print("当前仪表盘压力值为 {}".format(status))
cv.imshow("Danger Sign Recognition", frame)
k = cv.waitKey(1)
if k == 113 or k == 81: # q or Q to quit
print("Demo is quiting......")
if not Develop_Mode:
controller.drive_dog("squat")
cap.release()
cv.destroyWindow("Danger Sign Recognition")
stop_heartbeat = True
is_update_number = False
break