-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_yolo_opticlfow.py
150 lines (127 loc) · 5.41 KB
/
test_yolo_opticlfow.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
import argparse
import time
import signal
import cv2
import os
import torch
from Arducamlib.Arducam import *
from Arducamlib.ImageConvert import *
from yolov5 import buildmodel_engine, buildmodel_onnx, inference
def judge_from_window(pos_list):
count = {}
for i in set(pos_list):
count[i] = pos_list.count(i)
max_direction = max(count, key=count.get)
return max_direction
def run(
config_path,
weight_path,
model_type='onnx',
data_path = None,
device=torch.device('cpu'),
half=False,
imgsz=320
):
# Set camera config
config_file = config_path
verbose = False
# preview_width = -1
no_preview = False
# Openvideo
video = cv2.VideoCapture('/home/yunhaoshui/FootKick/test.mp4')
conf_threshold = 0.7
ret = True
prev = None
prev_isempty = True
action = None
window_size = 10
from collections import deque
window = deque(maxlen=window_size)
# Initialize Yolov5
model = inference.model_init(weight_path, model_type, data_path, device, half=half, imgsz=320)
total_time = []
yolo_time = []
LK_time = []
# Begin detection
while ret:
ret, image = video.read()
frame_count=video.get(cv2.CAP_PROP_FRAME_COUNT)
# ret, data, cfg = camera.read()
# display_fps(0)
if ret:
# image = convert_image(data, cfg, camera.color_mode)
imgae = np.array(image[:,:,:3])
start_time0 = time.time()
xyxy,conf,cls,img0 = inference.inference_openmmlab(image, model)
end_time0 = time.time()
yolotime = end_time0 - start_time0
# print('yolo time:',round((yolotime)*1000,2),'ms')
# image = preprocess.DBSCAN_denoise(image, 1.4,5)
dire_vec1 = np.array([])
position = None
if len(cls) == 0:
print('nothing')
position = None
if len(cls) != 0:
if cls[0] == 1: # means not shoe
print('not shoe')
position = None
if cls[0] == 0: # means there exists a shoe
start_time = time.time()
cur = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
if not prev_isempty:
p0 = cv2.goodFeaturesToTrack(prev,40,0.06,10)
p1,st,err = cv2.calcOpticalFlowPyrLK(prev, cur, p0, None, winSize=(30,30), maxLevel=2)
len_valid = len(np.nonzero(st)[0])
if len_valid == 0:
continue
dire_vec1 = np.array([0,0])
for i in range(len(st)):
if st[i] == 1:
dire_vec1[0] += p0[i,:,0] - p1[i,:,0]
dire_vec1[1] += p0[i,:,1] - p1[i,:,1]
dire_vec1[0] /= len_valid
dire_vec1[1] /= len(np.nonzero(st)[1])
prev = cur
prev_isempty = False
dire = dire_vec1 if len(dire_vec1) !=0 else [0,0]
if abs(dire[0]) > abs(dire[1]):
if dire[0] > 0:
position = 'right'
else:
position = 'left'
else:
if dire[1] > 0:
position = 'down'
else:
position = 'up'
end_time = time.time()
LKtime = end_time-start_time
# print('current direc:',position,'LK process time:', round((LK_time)*1000,2),'ms')
total_time.append(round((yolotime+LKtime)*1000,2))
yolo_time.append(round((yolotime)*1000,2))
LK_time.append(round((LKtime)*1000,2))
print(
'total_time', round((yolotime+LKtime)*1000,2),'ms',
' yolotime:', round((yolotime)*1000,2),'ms',
' LK time:',round((LKtime)*1000,2),'ms'
)
window.append(position)
if len(window) == window_size:
current_window = list(window)
action = judge_from_window(current_window)
cv2.putText(img0, str(action), (20,20), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,255,0), 3)
cv2.imshow("Arducam", img0) #248,324,4
cv2.waitKey(20)
else:
print('average process time:', np.average(total_time),'ms',
'average YOLO time:', np.average(yolo_time),'ms',
'average LK time:', np.average(LK_time),'ms')
return
if __name__ == "__main__":
config_path = "/home/yunhaoshui/FootKick/resources/SDVS320_RGB_324x248.cfg"
# weight_path = "/home/yunhaoshui/FootKick/resources/best_cpu.onnx"
# data_path = '/home/yunhaoshui/FootKick/resources/footkick_ultralytics.yaml'
weight_path = "/home/yunhaoshui/FootKick/resources/end2end.onnx"
data_path = '/home/yunhaoshui/FootKick/resources/footkick_openmmlab.yaml'
run(config_path=config_path, weight_path=weight_path, data_path =data_path)