-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path000test.py
executable file
·3051 lines (2580 loc) · 141 KB
/
000test.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# coding:utf-8
import cv2
import math
import numpy as np
import threading
import time
import datetime
import CMDcontrol
robot_IP = "192.168.3.52"
camera_out = "chest"
stream_pic = False
action_DEBUG = False
#################################################初始化#########################################################
if stream_pic:
stream_head = "http://"+ robot_IP +":8082/?action=stream?dummy=param.mjpg"
cap_head = cv2.VideoCapture(stream_head)
stream_chest = "http://"+ robot_IP +":8080/?action=stream?dummy=param.mjpg"
cap_chest = cv2.VideoCapture(stream_chest)
else:
cap_chest = cv2.VideoCapture(0)
cap_head = cv2.VideoCapture(2)
box_debug = False
debug = False
img_debug = False
state = 1
step = 0
state_sel = 'hole'
reset = 0
skip = 0
chest_ret = False # 读取图像标志位
ret = False # 读取图像标志位
ChestOrg_img = None # 原始图像更新
HeadOrg_img = None # 原始图像更新
ChestOrg_copy = None
HeadOrg_copy = None
r_width = 480
r_height = 640
chest_r_width = 480
chest_r_height = 640
head_r_width = 640
head_r_height = 480
################################################读取图像线程#################################################
def get_img():
global ChestOrg_img,HeadOrg_img,HeadOrg_img,chest_ret
global ret
global cap_chest
while True:
if cap_chest.isOpened():
chest_ret, ChestOrg_img = cap_chest.read()
ret, HeadOrg_img = cap_head.read()
if (chest_ret == False) or (ret == False):
print("ret faile ------------------")
if HeadOrg_img is None:
print("HeadOrg_img error")
if ChestOrg_img is None:
print("ChestOrg_img error")
else:
time.sleep(0.01)
ret=True
print("58L pic error ")
# 读取图像线程
th1 = threading.Thread(target=get_img)
th1.setDaemon(True)
th1.start()
################################################动作执行线程#################################################
def move_action():
global org_img
global step, level
global golf_angle_hole
global golf_angle_ball, golf_angle
global golf_dis, golf_dis_y
global golf_angle_flag, golf_dis_flag
global golf_angle_start, golf_dis_start
global golf_ok
global golf_hole, golf_ball
CMDcontrol.CMD_transfer()
# 动作执行线程
th2 = threading.Thread(target=move_action)
th2.setDaemon(True)
th2.start()
acted_name = ""
def action_append(act_name):
global acted_name
# print("please enter to continue...")
# cv2.waitKey(0)
if action_DEBUG == False:
if act_name == "forwardSlow0403" and (acted_name == "Forwalk02RL" or acted_name == "Forwalk02L"):
acted_name = "Forwalk02LR"
elif act_name == "forwardSlow0403" and (acted_name == "Forwalk02LR" or acted_name == "Forwalk02R"):
acted_name = "Forwalk02RL"
elif act_name != "forwardSlow0403" and (acted_name == "Forwalk02LR" or acted_name == "Forwalk02R"):
# CMDcontrol.action_list.append("Forwalk02RS")
# acted_name = act_name
print(act_name,"动作未执行 执行 Stand")
acted_name = "Forwalk02RS"
elif act_name != "forwardSlow0403" and (acted_name == "Forwalk02RL" or acted_name == "Forwalk02L"):
# CMDcontrol.action_list.append("Forwalk02LS")
# acted_name = act_name
print(act_name,"动作未执行 执行 Stand")
acted_name = "Forwalk02LS"
elif act_name == "forwardSlow0403":
acted_name = "Forwalk02R"
else:
acted_name = act_name
CMDcontrol.actionComplete = False
if len(CMDcontrol.action_list) > 0 :
print("队列超过一个动作")
CMDcontrol.action_list.append(acted_name)
else:
CMDcontrol.action_list.append(acted_name)
CMDcontrol.action_wait()
else:
print("-----------------------执行动作名:",act_name)
time.sleep(2)
color_range = {
'yellow_door': [(20, 140, 60), (40, 240, 150)],
'black_door': [(25, 25, 10), (110, 150, 30)],
'black_gap': [(0, 0, 0), (180, 255, 70)],
'yellow_hole': [(20, 120, 95), (30, 250, 190)],
'black_hole': [(5, 80, 20), (40, 255, 100)],
'chest_red_floor': [(0, 40, 60), (20,200, 190)],
'chest_red_floor1': [(0, 100, 60), (20,200, 210)],
'chest_red_floor2': [(110, 100, 60), (180,200, 210)],
'green_bridge': [(50, 75, 70), (80, 240, 210)],
}
color_dist = {'red': {'Lower': np.array([0, 160, 100]), 'Upper': np.array([180, 255, 250])},
'black_dir': {'Lower': np.array([0, 0, 10]), 'Upper': np.array([170, 170, 45])},
'black_line': {'Lower': np.array([0, 0, 20]), 'Upper': np.array([100, 160, 80])},
'blue': {'Lower': np.array([100, 80, 46]), 'Upper': np.array([124, 255, 255])},
'ball_red': {'Lower': np.array([160, 100, 70]), 'Upper': np.array([190, 215, 145])},
'blue_hole': {'Lower': np.array([100, 130, 80]), 'Upper': np.array([130, 255, 150])},
}
###############得到线形的总的轮廓###############
# 这个比值适应调整 handling
# 排除掉肩部黑色
def getLine_SumContour(contours, area=1):
global handling
contours_sum = None
for c in contours: # 初始化 contours_sum
area_temp = math.fabs(cv2.contourArea(c))
rect = cv2.minAreaRect(c)#最小外接矩形
box = np.int0(cv2.boxPoints(rect))#最小外接矩形的四个顶点
edge1=math.sqrt(math.pow(box[3, 1] - box[0, 1], 2) + math.pow(box[3, 0] - box[0, 0], 2))
edge2=math.sqrt(math.pow(box[3, 1] - box[2, 1], 2) + math.pow(box[3, 0] - box[2, 0], 2))
ratio=edge1/edge2 # 长与宽的比值大于3认为是条线
center_y = (box[0,1] + box[1,1] + box[2,1] + box[3,1]) / 4
if (area_temp > area) and (ratio>3 or ratio<0.33) and center_y > 240:
contours_sum = c
break
for c in contours:
area_temp = math.fabs(cv2.contourArea(c))
rect = cv2.minAreaRect(c)#最小外接矩形
box = np.int0(cv2.boxPoints(rect))#最小外接矩形的四个顶点
edge1=math.sqrt(math.pow(box[3, 1] - box[0, 1], 2) + math.pow(box[3, 0] - box[0, 0], 2))
edge2=math.sqrt(math.pow(box[3, 1] - box[2, 1], 2) + math.pow(box[3, 0] - box[2, 0], 2))
ratio=edge1/edge2
# print("ratio:",ratio,"area_temp:",area_temp)
if (area_temp > area) and (ratio>3 or ratio<0.33): # 满足面积条件 长宽比条件
rect = cv2.minAreaRect(c)#最小外接矩形
box = np.int0(cv2.boxPoints(rect))#最小外接矩形的四个顶点
center_x = (box[0,0] + box[1,0] + box[2,0] + box[3,0]) / 4
center_y = (box[0,1] + box[1,1] + box[2,1] + box[3,1]) / 4
if center_y > 240:# 满足中心点坐标条件
contours_sum = np.concatenate((contours_sum, c), axis=0) # 将所有轮廓点拼接到一起
if box_debug:
cv2.drawContours(handling, [box], -1, (0, 255, 0), 5)
cv2.imshow('handling', handling)
cv2.waitKey(10)
else:
if box_debug:
cv2.drawContours(handling, [box], -1, (0, 0, 255), 5)
cv2.imshow('handling', handling)
cv2.waitKey(10)
else: # 弃
rect = cv2.minAreaRect(c)#最小外接矩形
box = np.int0(cv2.boxPoints(rect))#最小外接矩形的四个顶点
if box_debug:
cv2.drawContours(handling, [box], -1, (0, 0, 255), 5)
cv2.imshow('handling', handling)
cv2.waitKey(10)
return contours_sum
# 得到最大轮廓和对应的最大面积
def getAreaMaxContour1(contours): # 返回轮廓 和 轮廓面积
contour_area_temp = 0
contour_area_max = 0
area_max_contour = None
for c in contours: # 历遍所有轮廓
contour_area_temp = math.fabs(cv2.contourArea(c)) # 计算轮廓面积
if contour_area_temp > contour_area_max:
contour_area_max = contour_area_temp
if contour_area_temp > 25: #只有在面积大于25时,最大面积的轮廓才是有效的,以过滤干扰
area_max_contour = c
return area_max_contour, contour_area_max # 返回最大的轮廓
########得到最大轮廓############
def getAreaMaxContour2(contours, area=1):
contour_area_max = 0
area_max_contour = None
for c in contours:
contour_area_temp = math.fabs(cv2.contourArea(c))
if contour_area_temp > contour_area_max:
contour_area_max = contour_area_temp
if contour_area_temp > area: # 面积大于1
area_max_contour = c
return area_max_contour
# 将所有面积大于1的轮廓点拼接到一起
def getSumContour(contours, area=1):
contours_sum = None
# print(len(contours))
for c in contours: # 初始化contours
area_temp = math.fabs(cv2.contourArea(c))
if (area_temp > area):
contours_sum = c
break
for c in contours:
area_temp = math.fabs(cv2.contourArea(c))
if (area_temp > area):
contours_sum = np.concatenate((contours_sum, c), axis=0) # 将所有面积大于1的轮廓点拼接到一起
return contours_sum
######### 得到所有轮廓的面积##########
def getAreaSumContour(contours):
contour_area_sum = 0
for c in contours: # 历遍所有轮廓
contour_area_sum += math.fabs(cv2.contourArea(c)) # 计算轮廓面积
return contour_area_sum # 返回最大的面积
# 通过两边的黑线,调整左右位置 和 角度
def head_angle_dis():
global HeadOrg_img,chest_copy, reset, skip
global handling
angle_ok_flag = False
angle = 90
dis = 0
bottom_centreX = 0
bottom_centreY = 0
see = False
dis_ok_count = 0
headTURN = 0
step = 1
print("/-/-/-/-/-/-/-/-/-head*angle*dis")
while True:
OrgFrame = HeadOrg_img.copy()
x_start = 260
blobs = OrgFrame[int(0):int(480), int(x_start):int(380)] # 只对中间部分识别处理 Y , X
# cv2.rectangle(blobs,(0,460),(120,480),(255,255,255),-1) # 涂白
handling = blobs.copy()
frame_mask = blobs.copy()
# 获取图像中心点坐标x, y
center = []
# 开始处理图像
hsv = cv2.cvtColor(frame_mask, cv2.COLOR_BGR2HSV)
hsv = cv2.GaussianBlur(hsv, (3, 3), 0)
Imask = cv2.inRange(hsv, color_dist['black_line']['Lower'], color_dist['black_line']['Upper'])
# Imask = cv2.erode(Imask, None, iterations=1)
Imask = cv2.dilate(Imask, np.ones((3, 3), np.uint8), iterations=2)
_, cnts, hierarchy = cv2.findContours(Imask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_TC89_L1) # 找出所有轮廓
# print("327L len:",len(cnts))
cnt_sum = getLine_SumContour(cnts, area=300)
# 初始化
L_R_angle = 0
blackLine_L = [0,0]
blackLine_R = [0,0]
if cnt_sum is not None:
see = True
rect = cv2.minAreaRect(cnt_sum)#最小外接矩形
box = np.int0(cv2.boxPoints(rect))#最小外接矩形的四个顶点
# cv2.drawContours(OrgFrame, [box], 0, (0, 255, 0), 2) # 将大矩形画在图上
if math.sqrt(math.pow(box[3, 1] - box[0, 1], 2) + math.pow(box[3, 0] - box[0, 0], 2)) > math.sqrt(math.pow(box[3, 1] - box[2, 1], 2) + math.pow(box[3, 0] - box[2, 0], 2)):
if box[3, 0] - box[0, 0]==0:
angle=90
else:
angle = - math.atan((box[3, 1] - box[0, 1]) / (box[3, 0] - box[0, 0]))*180.0/math.pi
if box[3,1]+box[0,1]>box[2,1]+box[1,1]:
Ycenter = int((box[2, 1] + box[1, 1]) / 2)
Xcenter = int((box[2, 0] + box[1, 0]) / 2)
if box[2, 1] > box[1, 1]:
blackLine_L = [box[2, 0] , box[2, 1]]
blackLine_R = [box[1, 0] , box[1, 1]]
else:
blackLine_L = [box[1, 0] , box[1, 1]]
blackLine_R = [box[2, 0] , box[2, 1]]
cv2.circle(OrgFrame, (Xcenter + x_start, Ycenter), 10, (255,255,0), -1)#画出中心点
else:
Ycenter = int((box[3, 1] + box[0, 1]) / 2)
Xcenter = int((box[3, 0] + box[0, 0]) / 2)
if box[3, 1] > box[0, 1]:
blackLine_L = [box[3, 0] , box[3, 1]]
blackLine_R = [box[0, 0] , box[0, 1]]
else:
blackLine_L = [box[0, 0] , box[0, 1]]
blackLine_R = [box[3, 0] , box[3, 1]]
cv2.circle(OrgFrame, (Xcenter + x_start, Ycenter), 10, (255,255,0), -1)#画出中心点
else:
if box[3, 0] - box[2, 0]==0:
angle=90
else:
angle = - math.atan((box[3, 1] - box[2, 1]) / (box[3, 0] - box[2, 0]))*180.0/math.pi # 负号是因为坐标原点的问题
if box[3,1]+box[2,1]>box[0,1]+box[1,1]:
Ycenter = int((box[1, 1] + box[0, 1]) / 2)
Xcenter = int((box[1, 0] + box[0, 0]) / 2)
if box[0, 1] > box[1, 1]:
blackLine_L = [box[0, 0] , box[0, 1]]
blackLine_R = [box[1, 0] , box[1, 1]]
else:
blackLine_L = [box[1, 0] , box[1, 1]]
blackLine_R = [box[0, 0] , box[0, 1]]
cv2.circle(OrgFrame, (Xcenter + x_start, Ycenter), 10, (255,255,0), -1)#画出中心点
else:
Ycenter = int((box[2, 1] + box[3, 1]) / 2)
Xcenter = int((box[2, 0] + box[3, 0]) / 2)
if box[3, 1] > box[2, 1]:
blackLine_L = [box[3, 0] , box[3, 1]]
blackLine_R = [box[2, 0] , box[2, 1]]
else:
blackLine_L = [box[2, 0] , box[2, 1]]
blackLine_R = [box[3, 0] , box[3, 1]]
cv2.circle(OrgFrame, (Xcenter + x_start, Ycenter), 10, (255,255,0), -1)#画出中心点
if blackLine_L[0] == blackLine_R[0]:
L_R_angle = 0
else:
L_R_angle = -math.atan( (blackLine_L[1]-blackLine_R[1]) / (blackLine_L[0]-blackLine_R[0]) ) *180.0/math.pi
if img_debug:
cv2.circle(OrgFrame, (blackLine_L[0] + x_start, blackLine_L[1]), 5, [0, 255, 255], 2)
cv2.circle(OrgFrame, (blackLine_R[0] + x_start, blackLine_R[1]), 5, [255, 0, 255], 2)
cv2.line(OrgFrame, (blackLine_R[0] + x_start,blackLine_R[1]), (blackLine_L[0] + x_start,blackLine_L[1]), (0, 255, 255), thickness=2)
cv2.putText(OrgFrame, "L_R_angle:" + str(L_R_angle),(10, OrgFrame.shape[0] - 30), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (0, 0, 255), 2)
cv2.putText(OrgFrame, "Xcenter:" + str(Xcenter + x_start),(10, OrgFrame.shape[0] - 50), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (0, 0, 255), 2)
cv2.putText(OrgFrame, "Ycenter:" + str(Ycenter),(200, OrgFrame.shape[0] - 50), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (0, 0, 255), 2)
# cv2.drawContours(frame_mask, cnt_sum, -1, (255, 0, 255), 3)
# cv2.imshow('frame_mask', frame_mask)
cv2.imshow('black', Imask)
cv2.imshow('OrgFrame', OrgFrame)
cv2.waitKey(10)
else:
see = False
# 决策执行动作
if step == 1:
print("157L 向右看 HeadTurn015")
action_append("HeadTurn015")
time.sleep(1) # timefftest
step = 2
elif step == 2:
if not see: # not see the edge
print("276L 右侧看不到黑线 左侧移 Left3move")
action_append("Left3move")
headTURN += 1
if headTURN > 3:
headTURN=0
print("276L 右侧看不到黑线 转为左看 waitKey")
step = 3
else: # 0
headTURN=0
if L_R_angle > 2:
if L_R_angle > 7:
print("416L 左da旋转 turn001L ",L_R_angle)
action_append("turn001L")
# elif L_R_angle > 5:
# print("419L 左da旋转 turn001L ",L_R_angle)
# action_append("turn001L")
else:
print("422L 左旋转 turn000L ",L_R_angle)
action_append("turn000L")
# time.sleep(1) # timefftest
elif L_R_angle < -2:
if L_R_angle < -7:
print("434L 右da旋转 turn001R ",L_R_angle)
action_append("turn001R")
# elif L_R_angle < -5:
# print("437L 右da旋转 turn001R ",L_R_angle)
# action_append("turn001R")
else:
print("461L 右旋转 turn000R ",L_R_angle)
action_append("turn000R")
# time.sleep(1) # timefftest
elif Ycenter >= 430:
if Ycenter > 450:
print("451L 左da侧移 Left3move >440 ",Ycenter)
action_append("Left3move")
else:
print("439L 左侧移 Left02move > 365 ",Ycenter)
action_append("Left02move")
elif Ycenter < 390:
if Ycenter < 370:
print("474L 右da侧移 Right3move <380 ",Ycenter)
action_append("Right3move")
else:
print("448L 右侧移 Right02move <400 ",Ycenter)
action_append("Right02move")
else:
dis_ok_count
print("444L 右看 X位置ok")
cv2.destroyAllWindows()
break
elif step == 3:
print("157L 向左看 HeadTurn180")
action_append("HeadTurn180")
time.sleep(1) # timefftest
step = 4
elif step == 4:
if not see: # not see the edge
print("294L 左侧 看不到黑线 转为右看")
headTURN += 1
if headTURN > 5:
headTURN=0
print("error 两侧都看不到 右侧移 Right3move")
action_append("Right3move")
else: # 0 +-1
headTURN=0
if L_R_angle > 3:
if L_R_angle > 8:
print("304L 左da旋转 turn001L ",L_R_angle)
action_append("turn001L")
else:
print("304L 左旋转 turn000L ",L_R_angle)
action_append("turn000L")
# time.sleep(1) # timefftest
elif L_R_angle < -3:
if L_R_angle < -8:
print("307L 右da旋转 turn001R ",L_R_angle)
action_append("turn001R")
else:
print("307L 右旋转 turn000R ",L_R_angle)
action_append("turn000R")
# time.sleep(1) # timefftest
elif Ycenter >= 430:
if Ycenter > 450:
print("498L 右da侧移 Right3move ",L_R_angle)
action_append("Right3move")
else:
print("501L 右侧移 Right02move ",L_R_angle)
action_append("Right02move")
elif Ycenter < 390:
if Ycenter < 370:
print("497L 左da侧移 Left3move ",L_R_angle)
action_append("Left02move")
else:
print("500L 左侧移 Left02move ",L_R_angle)
action_append("Left02move")
else:
dis_ok_count
print("495L 左看 X位置ok")
cv2.destroyAllWindows()
break
#################################################第二关:台阶##########################################
def floor():
global org_img, state, state_sel, step, reset, skip, debug
global camera_out
if (state == 2 or state == 6 or state == 8) and state_sel == 'floor': # 初始化
print("/-/-/-/-/-/-/-/-/-进入floor")
step = 0
r_w = chest_r_width
r_h = chest_r_height
top_angle = 0
T_B_angle = 0
topcenter_x = 0.5 * r_w
topcenter_y = 0
bottomcenter_x = 0.5 * r_w
bottomcenter_y = 0
state_sel = 'floor'
while state_sel == 'floor':
# 分析图像 # chest
if True: # 上下边沿
Corg_img = ChestOrg_img.copy()
Corg_img = np.rot90(Corg_img)
OrgFrame = Corg_img.copy()
# 初始化 bottom_right bottom_left
bottom_right = (480,0)
bottom_left = (0,0)
top_right = (480,0) # 右上角点坐标
top_left = (0,0) # 左上角点坐标
frame = cv2.resize(OrgFrame, (chest_r_width, chest_r_height), interpolation=cv2.INTER_LINEAR)
frame_copy = frame.copy()
# 获取图像中心点坐标x, y
center = []
# 开始处理图像
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
hsv = cv2.GaussianBlur(hsv, (3, 3), 0)
Imask = cv2.inRange(hsv, color_range['chest_red_floor1'][0], color_range['chest_red_floor1'][1]) # 对原图像和掩模(颜色的字典)进行位运算
# opened = cv2.morphologyEx(Imask, cv2.MORPH_OPEN, np.ones((3, 3), np.uint8)) # 开运算 去噪点
# Imask = cv2.morphologyEx(opened, cv2.MORPH_CLOSE, np.ones((3, 3), np.uint8)) # 闭运算 封闭连接
# Imask = cv2.erode(Imask, None, iterations=2)
Imask = cv2.dilate(Imask, np.ones((3, 3), np.uint8), iterations=2)
_, cnts, hierarchy = cv2.findContours(Imask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_TC89_L1) # 找出所有轮廓
cnt_sum, area_max = getAreaMaxContour1(cnts) # 找出最大轮廓
C_percent = round(area_max * 100 / (r_w * r_h), 2) # 最大轮廓百分比
cv2.drawContours(frame, cnt_sum, -1, (255, 0, 255), 3)
if cnt_sum is not None:
see = True
rect = cv2.minAreaRect(cnt_sum)#最小外接矩形
box = np.int0(cv2.boxPoints(rect))#最小外接矩形的四个顶点
bottom_right = cnt_sum[0][0] # 右下角点坐标
bottom_left = cnt_sum[0][0] # 左下角点坐标
top_right = cnt_sum[0][0] # 右上角点坐标
top_left = cnt_sum[0][0] # 左上角点坐标
for c in cnt_sum:
if c[0][0] + 1 * (r_h - c[0][1]) < bottom_left[0] + 1 * (r_h - bottom_left[1]):
bottom_left = c[0]
if c[0][0] + 1 * c[0][1] > bottom_right[0] + 1 * bottom_right[1]:
bottom_right = c[0]
if c[0][0] + 3 * c[0][1] < top_left[0] + 3 * top_left[1]:
top_left = c[0]
if (r_w - c[0][0]) + 3 * c[0][1] < (r_w - top_right[0]) + 3 * top_right[1]:
top_right = c[0]
# if debug:
# handling = ChestOrg_img.copy()
# cv2.circle(handling, (c[0][0], c[0][1]), 5, [0, 255, 0], 2)
# cv2.circle(handling, (bottom_left[0], bottom_left[1]), 5, [255, 255, 0], 2)
# cv2.circle(handling, (bottom_right[0], bottom_right[1]), 5, [255, 0, 255], 2)
# cv2.imshow('handling', handling) # 显示图像
# cv2.waitKey(2)
bottomcenter_x = (bottom_left[0] + bottom_right[0]) / 2 # 得到bottom中心坐标
bottomcenter_y = (bottom_left[1] + bottom_right[1]) / 2
topcenter_x = (top_right[0] + top_left[0]) / 2 # 得到top中心坐标
topcenter_y = (top_left[1] + top_right[1]) / 2
bottom_angle = -math.atan( (bottom_right[1]-bottom_left[1]) / (bottom_right[0]-bottom_left[0]) ) *180.0/math.pi
top_angle = -math.atan( (top_right[1]-top_left[1]) / (top_right[0]-top_left[0]) ) *180.0/math.pi
if math.fabs(topcenter_x - bottomcenter_x) <= 1: # 得到连线的角度
T_B_angle = 90
else:
T_B_angle = - math.atan((topcenter_y - bottomcenter_y) / (topcenter_x - bottomcenter_x)) * 180.0 / math.pi
if img_debug:
cv2.drawContours(frame_copy, [box], 0, (0, 255, 0), 2) # 将大矩形画在图上
cv2.line(frame_copy, (bottom_left[0],bottom_left[1]), (bottom_right[0],bottom_right[1]), (255, 255, 0), thickness=2)
cv2.line(frame_copy, (top_left[0],top_left[1]), (top_right[0],top_right[1]), (255, 255, 0), thickness=2)
cv2.line(frame_copy, (int(bottomcenter_x),int(bottomcenter_y)), (int(topcenter_x),int(topcenter_y)), (255, 255, 255), thickness=2) # T_B_line
cv2.putText(frame_copy, "bottom_angle:" + str(bottom_angle), (30, 450), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (0, 0, 0),2) # (0, 0, 255)BGR
cv2.putText(frame_copy, "top_angle:" + str(top_angle),(30, 150), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (0, 0, 0), 2)
cv2.putText(frame_copy, "T_B_angle:" + str(T_B_angle),(30, 400), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (0, 0, 255), 2)
cv2.putText(frame_copy, "bottomcenter_x:" + str(bottomcenter_x), (30, 480), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (0, 0, 0),2) # (0, 0, 255)BGR
cv2.putText(frame_copy, "y:" + str(int(bottomcenter_y)), (300, 480), cv2.FONT_HERSHEY_SIMPLEX, 0.65,(0, 0, 0), 2) # (0, 0, 255)BGR
cv2.putText(frame_copy, "topcenter_x:" + str(topcenter_x), (30, 180), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (0, 0, 0),2) # (0, 0, 255)BGR
cv2.putText(frame_copy, "topcenter_y:" + str(int(topcenter_y)), (230, 180), cv2.FONT_HERSHEY_SIMPLEX, 0.65,(0, 0, 0), 2) # (0, 0, 255)BGR
cv2.putText(frame_copy, 'C_percent:' + str(C_percent) + '%', (30, 100), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (0, 0, 0), 2)
cv2.putText(frame_copy, "step:" + str(step), (30, 70), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (0, 0, 0),2) # (0, 0, 255)BGR
cv2.circle(frame_copy, (int(topcenter_x), int(topcenter_y)), 5, [255, 0, 255], 2)
cv2.circle(frame_copy, (int(bottomcenter_x), int(bottomcenter_y)), 5, [255, 0, 255], 2)
cv2.circle(frame_copy, (top_right[0], top_right[1]), 5, [0, 255, 255], 2)
cv2.circle(frame_copy, (top_left[0], top_left[1]), 5, [0, 255, 255], 2)
cv2.circle(frame_copy, (bottom_right[0], bottom_right[1]), 5, [0, 255, 255], 2)
cv2.circle(frame_copy, (bottom_left[0], bottom_left[1]), 5, [0, 255, 255], 2)
cv2.imshow('Chest_Camera', frame_copy) # 显示图像
cv2.imshow('chest_red_mask', Imask)
cv2.waitKey(100)
else:
print("chest NONE")
# 决策执行动作
angle_ok_flag = False
if step == 0: # 前进依据chest 调整大致位置,方向 看底边线调整角度
if C_percent > 1 and bottomcenter_y < 380:
print("676L 继续前行 forwardSlow0403",bottomcenter_y)
action_append("forwardSlow0403")
elif 380 <= bottomcenter_y <430:
if bottom_angle > 3: # 需要左转
if bottom_angle > 6:
print("725L 大左转一下 turn001L ",bottom_angle)
action_append("turn001L")
else:
print("728L bottom_angle > 3 需要小左转 turn001L ",bottom_angle)
action_append("turn001L")
elif bottom_angle < -3: # 需要右转
if bottom_angle < -6:
print("732L 右da旋转 turn001R < -6 ",Head_L_R_angle)
action_append("turn001R")
else:
print("735L bottom_angle < -3 需要小右转 turn001R ",bottom_angle)
action_append("turn001R")
elif -3 <= bottom_angle <= 3: # 角度正确
print("448L 角度合适")
angle_ok_flag = True
if angle_ok_flag:
if bottomcenter_x < 200:
print("431L 向左侧移 Left02move ",bottomcenter_x)
action_append("Left02move")
elif bottomcenter_x > 260:
print("433L 向右侧移 Right02move ",bottomcenter_x)
action_append("Right02move")
else:
print("483L 变小步继续前行 Forwalk01",bottomcenter_y)
action_append("Forwalk01")
elif 430 <= bottomcenter_y <= 540:
if bottom_angle > 4: # 需要左转
if bottom_angle > 6:
print("746L 大左转一下 turn001L ",bottom_angle)
action_append("turn001L")
else:
print("749L bottom_angle > 4 需要小左转 turn001L ",bottom_angle)
action_append("turn001L")
elif bottom_angle < -4: # 需要右转
if bottom_angle < -6:
print("338L 右da旋转 turn001R < -6 ",bottom_angle)
action_append("turn001R")
else:
print("746L bottom_angle < -4 需要小右转 turn001R ",bottom_angle)
action_append("turn001R")
elif -3 <= bottom_angle <= 3: # 角度正确
print("448L 角度合适")
angle_ok_flag = True
if angle_ok_flag:
if bottomcenter_x < 200:
print("431L 向左侧移 Left1move ",bottomcenter_x)
action_append("Left1move")
elif bottomcenter_x > 260:
print("433L 向右侧移 Right1move ",bottomcenter_x)
action_append("Right1move")
else:
print("486L 到达上台阶边沿,变前挪动 Forwalk00 bottomcenter_y:",bottomcenter_y)
action_append("Forwalk00")
elif bottomcenter_y > 540:
print("然后开始第二步------")
step = 1
angle_ok_flag = False
else: # C_percent < 1 and bottomcenter_y < 380
print("error769L 前进")
elif step == 1: # 看中线调整角度
print("719L 上台阶 上台阶 UpBridge")
action_append("UpBridge")
step = 2
elif step == 2: # 已经上台阶 调整方向 快走三步 看上方顶点边线
if 0 < T_B_angle < 86: # 右转
print("730L 右转 turn001R T_B_angle:",T_B_angle)
action_append("turn001R")
# time.sleep(1) # timefftest
elif -86 < T_B_angle < 0: # 左转
print("359L 左转 turn001L T_B_angle:",T_B_angle)
action_append("turn001L")
# time.sleep(1) # timefftest
elif T_B_angle <= -86 or T_B_angle >= 86: # 角度正确
print("738L T_B_angle 角度合适 ")
if topcenter_x < 200:
print("740L <210 向左侧移 Left1move ",topcenter_x)
action_append("Left1move")
elif topcenter_x > 240:
print("743L >260 向右侧移 Right1move ",topcenter_x)
action_append("Right1move")
elif topcenter_y < 360:
print("516L 上台阶后,快走 fastForward06 topcenter_y:",topcenter_y)
action_append("fastForward06")
print("step 3333 ,",topcenter_y)
step = 3
elif step == 3: # 快走结束
if topcenter_y < 510 and C_percent > 6:
if top_angle > 1.5: # 需要左转
if top_angle <= 3:
print("468L 3 < < 1.5 需要小左转 turn000L ",top_angle)
action_append("turn000L")
else:
print("468L > 3 需要小左转 turn001L ",top_angle)
action_append("turn001L")
elif top_angle < -1.5: # 需要右转
if top_angle > -3:
print("470L -3 < < -1.5 需要小右转 turn000R ",top_angle)
action_append("turn000R")
else:
print("470L < -3 需要小右转 turn001R ",top_angle)
action_append("turn001R")
elif -1.5 <= top_angle <= 1.5: # 角度正确
print("474L top_angle 角度合适 ")
if topcenter_x < 190:
print("456L <210 向左侧移 Left1move ",topcenter_x)
action_append("Left1move")
elif topcenter_x > 260:
print("458L >260 向右侧移 Right1move ",topcenter_x)
action_append("Right1move")
else:
print("590L <topcenter_y<510 Forwalk01 ",topcenter_y)
action_append("Forwalk01")
else: # > 510
print("step 4444 ,",topcenter_y)
step = 4
elif step == 4: # 调整角度
if topcenter_y > 550 and C_percent < 6:
if C_percent >= 2 :
print("823L 下台阶前前进一点点 Forwalk00")
action_append("Forwalk00")
print("487L 下台阶 下台阶 DownBridge")
action_append("DownBridge")
step = 5
else:
print("566L 微微前挪 y:",topcenter_y," C_percent:",C_percent)
action_append("Forwalk00")
elif step ==5:
print("899L 完成bridge")
cv2.destroyAllWindows()
break
#################################################第三关:雷阵#############################################
head_flag = "MM"
Head_L_R_angle = 0
see_flag = False
Bbox_centerY = 0
head_step = 1
# 通过两边的黑线,仅仅调整角度
def head_only_angle(resize_width, resize_height):
global head_copy,chest_copy, reset, skip
global handling,Head_L_R_angle,see_flag,head_flag,Bbox_centerY,head_step,Bbox_center,blue_rail
angle_ok_flag = False
angle = 90
dis = 0
bottom_centreX = 0
bottom_centreY = 0
dis_ok_count = 0
headTURN = 0
while True:
OrgFrame = HeadOrg_img.copy()
x_start = 260
blobs = OrgFrame[int(0):int(480), int(x_start):int(380)] # 只对中间部分识别处理 Y , X
# cv2.rectangle(blobs,(0,460),(120,480),(255,255,255),-1) # 涂白
handling = blobs.copy()
frame_mask = blobs.copy()
# 获取图像中心点坐标x, y
center = []
# 开始处理图像
hsv = cv2.cvtColor(frame_mask, cv2.COLOR_BGR2HSV)
hsv = cv2.GaussianBlur(hsv, (3, 3), 0)
Imask = cv2.inRange(hsv, color_dist['black_line']['Lower'], color_dist['black_line']['Upper'])
# Imask = cv2.erode(Imask, None, iterations=2)
Imask = cv2.dilate(Imask, np.ones((3, 3), np.uint8), iterations=2)
_, cnts, hierarchy = cv2.findContours(Imask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_TC89_L1) # 找出所有轮廓
# print("len:",len(cnts))
cnt_sum = getLine_SumContour(cnts, area=300)
cv2.drawContours(frame_mask, cnt_sum, -1, (255, 0, 255), 3)
# cv2.imshow('black', Imask)
# 初始化
Head_L_R_angle = 0
blackLine_L = [0,0]
blackLine_R = [0,0]
if cnt_sum is not None:
see_flag = True
rect = cv2.minAreaRect(cnt_sum)#最小外接矩形
box = np.int0(cv2.boxPoints(rect))#最小外接矩形的四个顶点
# cv2.drawContours(OrgFrame, [box], 0, (0, 255, 0), 2) # 将大矩形画在图上
if math.sqrt(math.pow(box[3, 1] - box[0, 1], 2) + math.pow(box[3, 0] - box[0, 0], 2)) > math.sqrt(math.pow(box[3, 1] - box[2, 1], 2) + math.pow(box[3, 0] - box[2, 0], 2)):
if box[3, 0] - box[0, 0]==0:
angle=90
else:
angle = - math.atan((box[3, 1] - box[0, 1]) / (box[3, 0] - box[0, 0]))*180.0/math.pi
if box[3,1]+box[0,1]>box[2,1]+box[1,1]:
Ycenter = int((box[2, 1] + box[1, 1]) / 2)
Xcenter = int((box[2, 0] + box[1, 0]) / 2)
if box[2, 1] > box[1, 1]:
blackLine_L = [box[2, 0] , box[2, 1]]
blackLine_R = [box[1, 0] , box[1, 1]]
else:
blackLine_L = [box[1, 0] , box[1, 1]]
blackLine_R = [box[2, 0] , box[2, 1]]
cv2.circle(OrgFrame, (Xcenter + x_start, Ycenter), 10, (255,255,0), -1)#画出中心点
else:
Ycenter = int((box[3, 1] + box[0, 1]) / 2)
Xcenter = int((box[3, 0] + box[0, 0]) / 2)
if box[3, 1] > box[0, 1]:
blackLine_L = [box[3, 0] , box[3, 1]]
blackLine_R = [box[0, 0] , box[0, 1]]
else:
blackLine_L = [box[0, 0] , box[0, 1]]
blackLine_R = [box[3, 0] , box[3, 1]]
cv2.circle(OrgFrame, (Xcenter + x_start, Ycenter), 10, (255,255,0), -1)#画出中心点
else:
if box[3, 0] - box[2, 0]==0:
angle=90
else:
angle = - math.atan((box[3, 1] - box[2, 1]) / (box[3, 0] - box[2, 0]))*180.0/math.pi # 负号是因为坐标原点的问题
if box[3,1]+box[2,1]>box[0,1]+box[1,1]:
Ycenter = int((box[1, 1] + box[0, 1]) / 2)
Xcenter = int((box[1, 0] + box[0, 0]) / 2)
if box[0, 1] > box[1, 1]:
blackLine_L = [box[0, 0] , box[0, 1]]
blackLine_R = [box[1, 0] , box[1, 1]]
else:
blackLine_L = [box[1, 0] , box[1, 1]]
blackLine_R = [box[0, 0] , box[0, 1]]
cv2.circle(OrgFrame, (Xcenter + x_start, Ycenter), 10, (255,255,0), -1)#画出中心点
else:
Ycenter = int((box[2, 1] + box[3, 1]) / 2)
Xcenter = int((box[2, 0] + box[3, 0]) / 2)
if box[3, 1] > box[2, 1]:
blackLine_L = [box[3, 0] , box[3, 1]]
blackLine_R = [box[2, 0] , box[2, 1]]
else:
blackLine_L = [box[2, 0] , box[2, 1]]
blackLine_R = [box[3, 0] , box[3, 1]]
cv2.circle(OrgFrame, (Xcenter + x_start, Ycenter), 10, (255,255,0), -1)#画出中心点
if blackLine_L[0] == blackLine_R[0]:
Head_L_R_angle = 0
else:
Head_L_R_angle = -math.atan( (blackLine_L[1]-blackLine_R[1]) / (blackLine_L[0]-blackLine_R[0]) ) *180.0/math.pi
if img_debug:
cv2.putText(OrgFrame, "Head_L_R_angle:" + str(Head_L_R_angle),(10, OrgFrame.shape[0] - 30), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (0, 0, 255), 2)
cv2.putText(OrgFrame, "Xcenter:" + str(Xcenter + x_start),(10, OrgFrame.shape[0] - 50), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (0, 0, 255), 2)
cv2.putText(OrgFrame, "Ycenter:" + str(Ycenter),(200, OrgFrame.shape[0] - 50), cv2.FONT_HERSHEY_SIMPLEX, 0.65, (0, 0, 255), 2)
cv2.circle(OrgFrame, (blackLine_L[0] + x_start, blackLine_L[1]), 5, [0, 255, 255], 2)
cv2.circle(OrgFrame, (blackLine_R[0] + x_start, blackLine_R[1]), 5, [255, 0, 255], 2)
cv2.line(OrgFrame, (blackLine_R[0] + x_start,blackLine_R[1]), (blackLine_L[0] + x_start,blackLine_L[1]), (0, 255, 255), thickness=2)
# cv2.imshow('frame_mask', frame_mask)
# cv2.imshow('OrgFrame', OrgFrame)
# cv2.waitKey(100)
else:
see_flag = False
# 决策执行动作
if head_step == 1:
print("317L 向右看 HeadTurn015")
action_append("HeadTurn015")
head_flag = 'R'
head_step = 2
time.sleep(1) # timefftest
elif head_step == 2:
if not see_flag: # not see_flag the edge
print("276L 右侧看不到黑线 转为左看")
headTURN += 1
if headTURN > 5:
headTURN=0
head_step = 3
else:
headTURN=0
# Angle
if Head_L_R_angle > 3:
if Head_L_R_angle > 8:
print("329L 左da旋转 turn001L > 7 ",Head_L_R_angle)
action_append("turn001L")
else:
print("333L 左旋转 turn000L > 2 ",Head_L_R_angle)
action_append("turn000L")
elif Head_L_R_angle < -1:
if Head_L_R_angle < -6:
print("338L 右da旋转 turn001R < -7 ",Head_L_R_angle)
action_append("turn001R")
else:
print("342L 右旋转 turn000R < -2 ",Head_L_R_angle)
action_append("turn000R")
# 栏杆标志位 蓝横杆出现时时,允许侧移
elif Ycenter > 365 and blue_rail:
if Ycenter > 400: # 400
print("3942L 左侧移 Left3move > 400 ",Ycenter)
action_append("Left3move")
else:
print("3942L 左侧移 Left02move > 365 ",Ycenter)
action_append("Left02move")
break
elif Ycenter < 360 and blue_rail:
if Ycenter < 350:
print("390L 右侧移 Right3move <350 ",Ycenter)
action_append("Right3move")
else:
print("390L 右侧移 Right02move <360 ",Ycenter)
action_append("Right02move")
else:
print("1133L Ycenter:",Ycenter," blue_rail:",blue_rail)
dis_ok_count
print("1092L before blueBOX X位置ok")
break
elif head_step == 3:
print("359L 向左看 HeadTurn180")
action_append("HeadTurn180")
head_flag = 'L'
head_step = 4