Сделали класс
This commit is contained in:
+36
-32
@@ -1,55 +1,59 @@
|
|||||||
color=''
|
class ColorDetect():
|
||||||
|
def __init__(self):
|
||||||
|
self.color = None
|
||||||
|
|
||||||
def detect_color_callback(data, print_type='str'):
|
def getColor(self):
|
||||||
'''определение цвета'''
|
return self.color
|
||||||
|
|
||||||
global color
|
def detect_color_callback(self, data, print_type='str'):
|
||||||
frame = bridge.imgmsg_to_cv2(data, 'bgr8')
|
'''определение цвета'''
|
||||||
height, width = frame.shape[:2]
|
|
||||||
y_start = int(height * 0.33)
|
|
||||||
y_end = int(height * 0.67)
|
|
||||||
x_start = int(width * 0.31)
|
|
||||||
x_end = int(width * 0.69)
|
|
||||||
|
|
||||||
cropped_frame = frame[y_start:y_end, x_start:x_end]
|
frame = bridge.imgmsg_to_cv2(data, 'bgr8')
|
||||||
|
height, width = frame.shape[:2]
|
||||||
|
y_start = int(height * 0.33)
|
||||||
|
y_end = int(height * 0.67)
|
||||||
|
x_start = int(width * 0.31)
|
||||||
|
x_end = int(width * 0.69)
|
||||||
|
|
||||||
if cropped_frame.size == 0:
|
cropped_frame = frame[y_start:y_end, x_start:x_end]
|
||||||
return
|
|
||||||
|
if cropped_frame.size == 0:
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
if print_type == 'rgb':
|
if print_type == 'rgb':
|
||||||
|
|
||||||
mean_bgr = cv2.mean(cropped_frame)[:3]
|
mean_bgr = cv2.mean(cropped_frame)[:3]
|
||||||
mean_rgb = (int(mean_bgr[2]), int(mean_bgr[1]), int(mean_bgr[0]))
|
mean_rgb = (int(mean_bgr[2]), int(mean_bgr[1]), int(mean_bgr[0]))
|
||||||
color = str(mean_rgb)
|
self.color = str(mean_rgb)
|
||||||
|
|
||||||
|
|
||||||
elif print_type == 'str':
|
elif print_type == 'str':
|
||||||
|
|
||||||
# Получаем средние значения BGR
|
# Получаем средние значения BGR
|
||||||
mean_bgr = cv2.mean(cropped_frame)[:3]
|
mean_bgr = cv2.mean(cropped_frame)[:3]
|
||||||
b, g, r = int(mean_bgr[0]), int(mean_bgr[1]), int(mean_bgr[2])
|
b, g, r = int(mean_bgr[0]), int(mean_bgr[1]), int(mean_bgr[2])
|
||||||
|
|
||||||
|
|
||||||
if b > g and b > r and b > 100:
|
if b > g and b > r and b > 100:
|
||||||
color = 'blue'
|
self.color = 'blue'
|
||||||
elif g > b and g > r and g > 100:
|
elif g > b and g > r and g > 100:
|
||||||
color = 'green'
|
self.color = 'green'
|
||||||
elif r > b and r > g and r > 100:
|
elif r > b and r > g and r > 100:
|
||||||
color = 'red'
|
self.color = 'red'
|
||||||
else:
|
else:
|
||||||
color = 'unknown'
|
self.color = None
|
||||||
return(color)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#в MAIN для вызова функции должно быть:
|
#в MAIN для вызова функции должно быть:
|
||||||
|
# cd = ColorDetect()
|
||||||
#color_sub = rospy.Subscriber('main_camera/image_raw', Image,
|
#color_sub = rospy.Subscriber('main_camera/image_raw', Image,
|
||||||
#lambda img: detect_color_callback(img, print_type='rgb'),
|
#lambda img: cd.detect_color_callback(img, print_type='rgb'),
|
||||||
#queue_size=1)
|
#queue_size=1)
|
||||||
|
# print(cd.getColor())
|
||||||
#rospy.sleep(2)
|
#rospy.sleep(2)
|
||||||
#color_sub.unregister()
|
#color_sub.unregister()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user