Started overall project refactoring
1. Git will now be ignoring files whose names start with `exp_`. If you want to do quick experimenting but are not ready to add such files to repository, name them starting with `exp_`. For example `exp_cool_motion.py`. 2 Use the file `nao_defaults.json` to store the global project settings (such as IP address of the robot, or our preferred resolution). In your scripts make sure to load the default values from that file if needed (i.e. do NOT hard-code any configuration in your scripts). `nao_defaults.json` is a file with the stable config. If you need to experiment with configs, create a file `exp_config.json`. 3. Colorpicker is now a saner tool (you can specify output width, and the default IP address of the nao is pulled from the JSON config file).
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,3 +1,5 @@
|
|||||||
*.sw*
|
*.sw*
|
||||||
*.pyc
|
*.pyc
|
||||||
*.jpg
|
*.jpg
|
||||||
|
~*~
|
||||||
|
exp_*
|
||||||
|
|||||||
@@ -1,7 +0,0 @@
|
|||||||
# Optimal HSV for ball detection
|
|
||||||
|
|
||||||
# LOW HSV
|
|
||||||
0 185 170
|
|
||||||
|
|
||||||
# HIGH HSV
|
|
||||||
6 255 255
|
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
from __future__ import print_function
|
from __future__ import print_function
|
||||||
|
from __future__ import division
|
||||||
|
|
||||||
import json
|
import json
|
||||||
import argparse
|
import argparse
|
||||||
@@ -50,14 +51,17 @@ class Colorpicker(object):
|
|||||||
cv2.setTrackbarPos(name, self.WINDOW_DETECTION_NAME,
|
cv2.setTrackbarPos(name, self.WINDOW_DETECTION_NAME,
|
||||||
self.settings[name])
|
self.settings[name])
|
||||||
|
|
||||||
def show_frame(self, frame):
|
def show_frame(self, frame, width=None):
|
||||||
frame_HSV = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
|
frame_HSV = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
|
||||||
frame_threshold = cv2.inRange(
|
frame_threshold = cv2.inRange(
|
||||||
frame_HSV,
|
frame_HSV,
|
||||||
tuple(map(self.settings.get, ('low_h', 'low_s', 'low_v'))),
|
tuple(map(self.settings.get, ('low_h', 'low_s', 'low_v'))),
|
||||||
tuple(map(self.settings.get, ('high_h', 'high_s', 'high_v')))
|
tuple(map(self.settings.get, ('high_h', 'high_s', 'high_v')))
|
||||||
)
|
)
|
||||||
frame_threshold = cv2.resize(frame_threshold, (640, 480))
|
if width:
|
||||||
|
sf = width / frame.shape[1]
|
||||||
|
frame = cv2.resize(frame, (0, 0), fx=sf, fy=sf)
|
||||||
|
frame_threshold = cv2.resize(frame_threshold, (0, 0), fx=sf, fy=sf)
|
||||||
cv2.imshow(self.WINDOW_CAPTURE_NAME, frame)
|
cv2.imshow(self.WINDOW_CAPTURE_NAME, frame)
|
||||||
cv2.imshow(self.WINDOW_DETECTION_NAME, frame_threshold)
|
cv2.imshow(self.WINDOW_DETECTION_NAME, frame_threshold)
|
||||||
return cv2.waitKey(1)
|
return cv2.waitKey(1)
|
||||||
@@ -75,9 +79,14 @@ class Colorpicker(object):
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
||||||
|
with open('nao_defaults.json') as f:
|
||||||
|
defaults = json.load(f)
|
||||||
|
|
||||||
parser = argparse.ArgumentParser(
|
parser = argparse.ArgumentParser(
|
||||||
epilog='When called without arguments specifying the video source, ' +
|
epilog='When called without arguments specifying the video source, ' +
|
||||||
'will try to use the webcam')
|
'will try to use the webcam'
|
||||||
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'-o', '--output-config',
|
'-o', '--output-config',
|
||||||
help='file, to which the settings will be saved (if given)'
|
help='file, to which the settings will be saved (if given)'
|
||||||
@@ -86,43 +95,52 @@ if __name__ == '__main__':
|
|||||||
'-i', '--input-config',
|
'-i', '--input-config',
|
||||||
help='file, from which to read the initial values'
|
help='file, from which to read the initial values'
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
imsource = parser.add_mutually_exclusive_group()
|
||||||
|
imsource.add_argument(
|
||||||
'--video-file',
|
'--video-file',
|
||||||
help='video file to use'
|
help='video file to use'
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
imsource.add_argument(
|
||||||
'--image-file',
|
'--image-file',
|
||||||
help='image to use'
|
help='image to use'
|
||||||
)
|
)
|
||||||
|
imsource.add_argument(
|
||||||
|
'--nao-ip',
|
||||||
|
help='ip address of the nao robot, from which to capture',
|
||||||
|
default=False,
|
||||||
|
const=defaults['ip'],
|
||||||
|
nargs='?'
|
||||||
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'--still',
|
'--still',
|
||||||
help='only take one image from video stream',
|
help='only take one image from video stream',
|
||||||
action='store_true'
|
action='store_true'
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
|
||||||
'--nao-ip',
|
|
||||||
help='ip address of the nao robot, from which to capture'
|
|
||||||
)
|
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'--nao-cam',
|
'--nao-cam',
|
||||||
choices=['upper', 'lower'],
|
choices=[0, 1],
|
||||||
help='choose a camera from nao'
|
help='0 for top camera, 1 for bottom camera',
|
||||||
|
default=defaults['cam']
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'--nao-res',
|
'--nao-res',
|
||||||
choices=[1, 2, 3],
|
choices=[1, 2, 3],
|
||||||
|
help='choose a nao resolution',
|
||||||
type=int,
|
type=int,
|
||||||
default=1
|
default=defaults['res']
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
'--width',
|
||||||
|
help='specify width of the image output',
|
||||||
|
type=int,
|
||||||
|
default=640
|
||||||
)
|
)
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
cp = Colorpicker()
|
cp = Colorpicker()
|
||||||
camera_ids = {
|
|
||||||
'upper': 0,
|
|
||||||
'lower': 1
|
|
||||||
}
|
|
||||||
if args.input_config:
|
if args.input_config:
|
||||||
cp.load(args.input_config)
|
cp.load(args.input_config)
|
||||||
|
|
||||||
if args.video_file:
|
if args.video_file:
|
||||||
rdr = VideoReader(args.video_file, loop=True)
|
rdr = VideoReader(args.video_file, loop=True)
|
||||||
elif args.image_file:
|
elif args.image_file:
|
||||||
@@ -130,22 +148,25 @@ if __name__ == '__main__':
|
|||||||
elif args.nao_ip:
|
elif args.nao_ip:
|
||||||
rdr = NaoImageReader(
|
rdr = NaoImageReader(
|
||||||
args.nao_ip,
|
args.nao_ip,
|
||||||
cam_id=camera_ids[args.nao_cam] if args.nao_cam else 0,
|
cam_id=args.nao_cam,
|
||||||
res=args.nao_res
|
res=args.nao_res
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
rdr = VideoReader(0)
|
rdr = VideoReader(0)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if args.still:
|
if args.still:
|
||||||
frame = rdr.get_frame()
|
frame = rdr.get_frame()
|
||||||
|
rdr.close()
|
||||||
while True:
|
while True:
|
||||||
if not args.still:
|
if not args.still:
|
||||||
frame = rdr.get_frame()
|
frame = rdr.get_frame()
|
||||||
key = cp.show_frame(frame)
|
key = cp.show_frame(frame, args.width)
|
||||||
if key == ord('q') or key == 27:
|
if key == ord('q') or key == 27:
|
||||||
break
|
break
|
||||||
finally:
|
finally:
|
||||||
cp.do_print()
|
cp.do_print()
|
||||||
if args.output_config:
|
if args.output_config:
|
||||||
cp.save(args.output_config)
|
cp.save(args.output_config)
|
||||||
rdr.close()
|
if not args.still:
|
||||||
|
rdr.close()
|
||||||
|
|||||||
6
scripts/nao_defaults.json
Normal file
6
scripts/nao_defaults.json
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"ip": "192.168.0.11",
|
||||||
|
"res": 1,
|
||||||
|
"port": 9559,
|
||||||
|
"cam": 0
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user