-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo_yolo.py
153 lines (111 loc) · 4.38 KB
/
demo_yolo.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
import torch
import pyrealsense2 as rs
import numpy as np
import cv2
import time
import pandas as pd
import rospy
import baxter
import numpy as np
from baxter_core_msgs.msg import EndpointState
from pynput.keyboard import Key, Listener
from baxter_interface import Gripper
from baxter_interface import CHECK_VERSION
def convert(tuple):
#ust sol, ust sag, alt sol, alt sag
cam_coordinates = [(38,11), (33,631), (249,11), (248, 631)] #do not forget x and y are replaced
#robot_coordinates = [(0.91, -0.37), (0.67, 0.35), (0.95, -0.40), (0.67, 0.25)]
#robot-sag, robot-sol, orta-sag, orta-sol
robot_coordinates = [(0.66, -0.39), (0.66, 0.38), (0.95, -0.40), (0.95, 0.36)]
x_c2 = (cam_coordinates[0][0] + cam_coordinates[1][0]) / 2
x_c1 = (cam_coordinates[2][0] + cam_coordinates[3][0]) / 2
x_r2 = (robot_coordinates[0][0] + robot_coordinates[1][0]) / 2
x_r1 = (robot_coordinates[2][0] + robot_coordinates[3][0]) / 2
y_c2 = (cam_coordinates[0][1] + cam_coordinates[2][1]) / 2
y_c1 = (cam_coordinates[1][1] + cam_coordinates[3][1]) / 2
y_r2 = (robot_coordinates[0][1] + robot_coordinates[2][1]) / 2
y_r1 = (robot_coordinates[1][1] + robot_coordinates[3][1]) / 2
x_replaced = tuple[1]
y_replaced = tuple[0]
x_new = x_r1 - (((x_r1 - x_r2) * (x_c1 - x_replaced)) / (x_c1 - x_c2))
y_new = y_r1 - (((y_r1 - y_r2) * (y_c1 - y_replaced)) / (y_c1 - y_c2))
return (x_new, y_new)
def get_position():
#model = torch.hub.load('ultralytics/yolov5', 'custom', path='path/to/best.pt') # local model
model = torch.hub.load('/home/colors/yolov5', 'custom', path='/home/colors/yolov5/runs/train/exp6/weights/best.pt', source='local') # local repo
pipe = rs.pipeline()
cfg = rs.config()
cfg.enable_stream(rs.stream.color, 640,480, rs.format.bgr8, 30)
cfg.enable_stream(rs.stream.depth, 640,480, rs.format.z16, 30)
pipe.start(cfg)
frame = pipe.wait_for_frames()
time.sleep(5)
color_frame = frame.get_color_frame()
color_image = np.asanyarray(color_frame.get_data())
results = model(color_image)
print(results.pandas().xyxy[0])
time.sleep(10)
cv2.imshow('rgb', color_image)
time.sleep(10)
pipe.stop()
return results,color_image
def get_avg(result_line):
x0,y0,x1,y1,confi,cla = result_line
x_avg=(x0+x1)/2
y_avg=(y0+y1)/2
return(x_avg,y_avg)
def get_color(rgb):
red=[30, 80, 82]
blue=[34, 29, 0]
white=[35, 89, 86]
if ((abs(rgb[0]-red[0])<10) and (abs(rgb[1]-red[1])<10) and (abs(rgb[2]-red[2])<10)):
return 'r'
elif((abs(rgb[0]-blue[0])<10) and (abs(rgb[1]-blue[1])<10) and (abs(rgb[2]-blue[2])<10)):
return 'b'
else:
return 'w'
#object_coordinates = (438,198)
#new_robot_coordinates = convert(object_coordinates)
def get_object_at_position(objects, direction):
item = objects[1]
return item
results, color_img=get_position()
print(color_img)
print("results.xyxy[0][1].numpy()", results.xyxy[0][1].numpy())
print(type(results.xyxy))
object_list=[]
for i in range(len(results.xyxy[0])):
my_tuple0=get_avg(results.xyxy[0][i].numpy())
print(color_img[round(my_tuple0[0])][round(my_tuple0[1])])
print(get_color(color_img[round(my_tuple0[0])][round(my_tuple0[1])]))
object_list.append(my_tuple0)
baxter_coord=convert(my_tuple0)
print("baxter_coord {}: ".format(i), baxter_coord)
print("color_img[0][0]: ",color_img[0][0])
rospy.init_node("testing")
rospy.sleep(2.0)
robot = baxter.BaxterRobot(rate=100, arm="right")
rospy.sleep(2.0)
robot.set_robot_state(True)
grip_right = Gripper('right', CHECK_VERSION)
if grip_right.error():
grip_right.reset()
if (not grip_right.calibrated() and
grip_right.type() != 'custom'):
grip_right.calibrate()
msg = rospy.wait_for_message("/robot/limb/right/endpoint_state", EndpointState)
p = msg.pose.position
print(p)
q = msg.pose.orientation
robot.move_to_neutral()
robot.set_cartesian_position([0.745888,0.36672261,-0.065772],[1,0,0,0])
time.sleep(2)
grip_right.open()
robot.set_cartesian_position([baxter_coord[0],baxter_coord[1],p.z],[1,0,0,0])
time.sleep(2)
robot.set_cartesian_position([baxter_coord[0],baxter_coord[1],-0.14897744],[1,0,0,0])
grip_right.close()
robot.set_cartesian_position([baxter_coord[0],baxter_coord[1],p.z],[1,0,0,0])
msg = rospy.wait_for_message("/robot/limb/right/endpoint_state", EndpointState)
p = msg.pose.position
print(p)