Video recording!
This commit is contained in:
@@ -16,10 +16,12 @@ class NaoImageReader(object):
|
||||
3: (960, 1280)
|
||||
}
|
||||
|
||||
def __init__(self, ip, port=9559, res=1, fps=30, cam_id=0):
|
||||
def __init__(self, ip, port=9559, res=1, fps=30, cam_id=0, video_file=None):
|
||||
ip = bytes(ip)
|
||||
self.res_id = res
|
||||
self.recording = []
|
||||
self.res = self.RESOLUTIONS[res]
|
||||
self.video_file = video_file
|
||||
self.cam_id=cam_id
|
||||
self.fps = fps
|
||||
self.vd = ALProxy('ALVideoDevice', ip, port)
|
||||
@@ -46,15 +48,25 @@ class NaoImageReader(object):
|
||||
raise RuntimeError(self.sub + " couldn't capture")
|
||||
else:
|
||||
height, width = self.res
|
||||
return np.frombuffer(result[6], dtype=np.uint8).reshape(
|
||||
frame = np.frombuffer(result[6], dtype=np.uint8).reshape(
|
||||
height, width, 3
|
||||
)
|
||||
if self.video_file is not None:
|
||||
self.recording.append(frame)
|
||||
return frame
|
||||
|
||||
def close(self):
|
||||
self.vd.unsubscribe(self.sub)
|
||||
if self.video_file is not None:
|
||||
vf = cv2.VideoWriter(self.video_file,
|
||||
cv2.cv.FOURCC('X', 'V', 'I', 'D'),
|
||||
5,
|
||||
(self.res[1], self.res[0]))
|
||||
for frame in self.recording:
|
||||
vf.write(frame)
|
||||
|
||||
def restart(self):
|
||||
self.close()
|
||||
self.vd.unsubscribe(self.sub)
|
||||
self.sub = self.vd.subscribeCamera(
|
||||
self.sub, self.cam_id, self.res_id, 13, self.fps
|
||||
)
|
||||
|
||||
@@ -15,24 +15,25 @@ from naoqi import ALProxy
|
||||
|
||||
class Striker(object):
|
||||
|
||||
VIDEO_FOLDER = '/home/nao/recordings/'
|
||||
|
||||
def __init__(self, nao_ip, nao_port, res, ball_hsv, goal_hsv, field_hsv,
|
||||
ball_min_radius):
|
||||
self.run_id = strftime('%Y%m%d%H%M%S')
|
||||
self.mover = NaoMover(nao_ip=nao_ip, nao_port=nao_port)
|
||||
self.mover.stand_up()
|
||||
self.upper_camera = NaoImageReader(nao_ip, port=nao_port, res=res,
|
||||
fps=30, cam_id=0)
|
||||
self.lower_camera = NaoImageReader(nao_ip, port=nao_port, res=res,
|
||||
fps=30, cam_id=1)
|
||||
self.upper_camera = NaoImageReader(
|
||||
nao_ip, port=nao_port, res=res, fps=30, cam_id=0,
|
||||
video_file='cam0_' + self.run_id + '.mpg'
|
||||
)
|
||||
self.lower_camera = NaoImageReader(
|
||||
nao_ip, port=nao_port, res=res, fps=30, cam_id=1,
|
||||
video_file='cam1_' + self.run_id + '.mpg'
|
||||
)
|
||||
self.ball_finder = BallFinder(tuple(ball_hsv[0]), tuple(ball_hsv[1]),
|
||||
ball_min_radius)
|
||||
self.field_finder = FieldFinder(tuple(field_hsv[0]),
|
||||
tuple(field_hsv[1]))
|
||||
self.goal_finder = GoalFinder(tuple(goal_hsv[0]), tuple(goal_hsv[1]))
|
||||
self.speaker = ALProxy('ALTextToSpeech', bytes(nao_ip), nao_port)
|
||||
self.recorder = ALProxy('ALVideoRecorder', bytes(nao_ip), nao_port)
|
||||
|
||||
self.is_over = False
|
||||
|
||||
@@ -52,17 +53,6 @@ class Striker(object):
|
||||
self.speaker.say(self.speach_queue.pop())
|
||||
sleep(0.1)
|
||||
|
||||
def start_record(self, cam_id):
|
||||
if self.recorder.isRecording():
|
||||
print('Already recording. Please stop first')
|
||||
return
|
||||
self.recorder.setCameraID(cam_id)
|
||||
self.recorder.startRecording(self.VIDEO_FOLDER,
|
||||
'cam' + str(cam_id) + self.run_id)
|
||||
|
||||
def stop_record(self):
|
||||
self.recorder.stopRecording()
|
||||
|
||||
def speak(self, text):
|
||||
self.speach_queue.appendleft(text)
|
||||
|
||||
@@ -317,8 +307,6 @@ class Striker(object):
|
||||
self.is_over = True
|
||||
if self.tts_thread.isAlive():
|
||||
self.tts_thread.join()
|
||||
if self.recorder.isRecording():
|
||||
self.stop_record()
|
||||
self.upper_camera.close()
|
||||
self.lower_camera.close()
|
||||
self.mover.stop_moving()
|
||||
@@ -418,7 +406,6 @@ if __name__ == '__main__':
|
||||
print('State:', state)
|
||||
|
||||
if state == 'init':
|
||||
striker.start_record(0)
|
||||
striker.mover.set_head_angles(0, 0)
|
||||
striker.ball_tracking(tol=0.05)
|
||||
# goal_center = striker.goal_search()
|
||||
@@ -430,8 +417,8 @@ if __name__ == '__main__':
|
||||
# start ball approach when ball is visible
|
||||
print('Soll angle')
|
||||
striker.ball_tracking(tol=0.05)
|
||||
break
|
||||
# state = 'ball_approach'
|
||||
# break
|
||||
state = 'align'
|
||||
|
||||
elif state == 'ball_approach':
|
||||
# bil = striker.get_ball_angles_from_camera(
|
||||
@@ -463,8 +450,6 @@ if __name__ == '__main__':
|
||||
state = 'tracking'
|
||||
|
||||
elif state == 'align':
|
||||
striker.stop_record()
|
||||
striker.start_record(1)
|
||||
striker.mover.set_head_angles(0, 0.25, 0.3)
|
||||
sleep(0.5)
|
||||
try:
|
||||
|
||||
@@ -1,43 +0,0 @@
|
||||
# This test demonstrates how to use the ALVideoRecorder module.
|
||||
# Note that you might not have this module depending on your distribution
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
from naoqi import ALProxy
|
||||
|
||||
# Replace this with your robot's IP address
|
||||
IP = "192.168.0.11"
|
||||
PORT = 9559
|
||||
|
||||
# Create a proxy to ALVideoRecorder
|
||||
try:
|
||||
videoRecorderProxy = ALProxy("ALVideoRecorder", IP, PORT)
|
||||
except Exception, e:
|
||||
print "Error when creating ALVideoRecorder proxy:"
|
||||
print str(e)
|
||||
exit(1)
|
||||
|
||||
videoRecorderProxy.setFrameRate(30.0)
|
||||
videoRecorderProxy.setResolution(2) # Set resolution to VGA (640 x 480)
|
||||
videoRecorderProxy.setCameraID(0)
|
||||
|
||||
|
||||
|
||||
|
||||
# storage folder of the recording and the file name
|
||||
videoRecorderProxy.startRecording("/home/nao/recordings/cameras", "camera1")
|
||||
|
||||
print "Video record started."
|
||||
|
||||
try:
|
||||
# set recording duration
|
||||
while True:
|
||||
pass
|
||||
|
||||
finally:
|
||||
videoInfo = videoRecorderProxy.stopRecording()
|
||||
|
||||
#print "Video was saved on the robot: ", videoInfo[1]
|
||||
#print "Total number of frames: ", videoInfo[0]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user