-
Notifications
You must be signed in to change notification settings - Fork 0
/
vision.py
147 lines (109 loc) · 5.06 KB
/
vision.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
#! /usr/bin/env python
import yaml
from copy import deepcopy
import cv2
import numpy as np
import rospy
from sensor_msgs.msg import Image
from std_msgs.msg import String
from std_msgs.msg import Int32MultiArray
from cv_bridge import CvBridge, CvBridgeError
import requests
#if it's helpful: ssh [email protected]
# password: robotics
# class ArucoMarker:
# def __init__(self, params):
# self.params = params
# marker_dict = eval(params['marker_dict'])
# self.arucoDict = cv2.aruco.Dictionary_get(marker_dict)
# self.arucoParams = cv2.aruco.DetectorParameters_create()
# self.bridge = CvBridge()
# def join(self, a, delim):
# if len(a) == 0:
# return
# string = str(a[0])
# for i in a[1:]:
# string += str(delim) + str(i)
# return string
# #CHANGETHIS: detect with CV should identify not Aruco markers, but the grid with its X's and O's
# def detect(self, cv2_img):
# H, W, _ = cv2_img.shape
# # Detect markers from image using CV2
# (corners, tracked_ids, _) = cv2.aruco.detectMarkers(cv2_img, self.arucoDict)
# # if no markers were found, return empty array
# if tracked_ids is None or len(corners) != 10:
# if tracked_ids is None:
# print("No tracked IDs found")
# server_result = []
# else:
# print "Only " + str(len(tracked_ids.tolist())) + " IDs found"
# server_result = [x[0] for x in tracked_ids.tolist()]
# # publish the data to the server
# try:
# requests.get("http://localhost:5000/set_markers", params={"markers": self.join(server_result, ',')})
# except requests.exceptions.ConnectionError:
# pass
# return []
# # publish the data to the server
# server_result = [x[0] for x in tracked_ids.tolist()]
# try:
# requests.get("http://localhost:5000/set_markers", params={"markers": self.join(server_result, ',')})
# except requests.exceptions.ConnectionError:
# pass
# # For each corner, first value is y measured from left to right and
# # second value is x measured top to bottom
# marker_tuples = []
# for c, arid in zip(corners, tracked_ids):
# # Check if the detected marker belongs to the list of known markers
# if arid in self.params['marker_ids']:
# corner_coord = c[0, 0] # corner_coord[0] is x, corner_coord[1] is y
# marker_tuples.append((arid[0], corner_coord[0], corner_coord[1]))
# # sorting function
# mean_y = sum([x[2] for x in marker_tuples]) / len(marker_tuples) # get the mean y value
# upper_row = [x for x in marker_tuples if x[2] > mean_y] # split items into upper and lower rows
# lower_row = [x for x in marker_tuples if x[2] < mean_y]
# # sort both by x
# upper_row.sort(key = lambda x: x[1])
# lower_row.sort(key = lambda x: x[1])
# if len(upper_row) != len(lower_row):
# print "Upper row has " + str(len(upper_row)) + " while lower row has " + str(len(lower_row))
# return []
# # combine the lists elementwise
# result = []
# for i in reversed(range(len(upper_row))):
# result.append(upper_row[i])
# result.append(lower_row[i])
# return [t[0] for t in result] # return only the IDs
# def detect_markers_from_rostopic(self):
# # Fetch image from rostopic
# msg = rospy.wait_for_message(self.params['image_topic'], Image)
# # Ros msg to cv image
# try:
# cv_image = self.bridge.imgmsg_to_cv2(msg, "bgr8")
# except CvBridgeError as e:
# print(e)
# return self.detect(cv_image)
def main():
# init the ROS node
rospy.init_node('vision_node')
# with open('./marker_config/ARMarker.yaml', 'r') as f:
# params = yaml.safe_load(f)
# ardetector = ArucoMarker(params)
# # CHANGETHIS: not markers publisher, game state publisher
# markers_publisher = rospy.Publisher('/markers', Int32MultiArray, queue_size=10)
# keep detecting markers and posting to the markers ROS topic
while not rospy.is_shutdown():
# Fetch image from rostopic
msg = rospy.wait_for_message('/head_camera/rgb/image_raw', Image)
# Ros msg to cv image
try:
cv_image = CvBridge().imgmsg_to_cv2(msg, "bgr8")
print("yay")
# Cool! So, we've gotten this far. We are now importing images; now what do we want to do with them?
# I'd like to see them, first of all. Then I can tell if I'm giving it anything useful. Can make a tic-tac-toe board right here, right now (laser-cut?)
# Then can start exploring options in OpenCV for processing them. Woot.
except CvBridgeError as e:
print(e)
rospy.spin()
if __name__ == '__main__':
main()