Skip to content

Commit 894b951

Browse files
committed
Added av module
1 parent 75623f5 commit 894b951

File tree

8 files changed

+352
-1
lines changed

8 files changed

+352
-1
lines changed

apps/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# tobiiglasses-controller-examples
2+
3+
The Python examples require tobiiglasses-controller package
4+
5+
More information about the controller can be found here:
6+
https://github.com/ddetommaso/tobiiglasses-controller
7+
8+
# Examples
9+
10+
* connect.py - A Python script to connect with the Tobii Glasses Pro 2 eye-tracker.
11+
* calibrate_and_record.py - A Python script for managing calibrations and recordings.
12+
* streaming.py - A Python script for receiving live data from the eye-tracker
13+
* live_scene.py - A Python script for receiving live scene from the eye-tracker camera (Ipv4 and IPv6).
14+
* live_scene_and_gaze.py - A Python script for receiving live scene and synchronized gaze (Ipv4 and IPv6).

apps/calibrate_and_record.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# calibrate_and_record.py : A demo code for calibrating and recording
2+
#
3+
# Copyright (C) 2019 Davide De Tommaso
4+
#
5+
# This program is free software: you can redistribute it and/or modify
6+
# it under the terms of the GNU General Public License as published by
7+
# the Free Software Foundation, either version 3 of the License, or
8+
# (at your option) any later version
9+
#
10+
# This program is distributed in the hope that it will be useful,
11+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
# GNU General Public License for more details.
14+
#
15+
# You should have received a copy of the GNU General Public License
16+
# along with this program. If not, see <https://www.gnu.org/licenses/>
17+
18+
import time
19+
from tobiiglassesctrl.controller import TobiiGlassesController
20+
21+
if hasattr(__builtins__, 'raw_input'):
22+
input=raw_input
23+
24+
25+
def main():
26+
27+
tobiiglasses = TobiiGlassesController()
28+
print(tobiiglasses.get_battery_info())
29+
print(tobiiglasses.get_storage_info())
30+
31+
if tobiiglasses.is_recording():
32+
rec_id = tobiiglasses.get_current_recording_id()
33+
tobiiglasses.stop_recording(rec_id)
34+
35+
project_name = input("Please insert the project's name: ")
36+
project_id = tobiiglasses.create_project(project_name)
37+
38+
participant_name = input("Please insert the participant's name: ")
39+
participant_id = tobiiglasses.create_participant(project_id, participant_name)
40+
41+
calibration_id = tobiiglasses.create_calibration(project_id, participant_id)
42+
input("Put the calibration marker in front of the user, then press enter to calibrate")
43+
tobiiglasses.start_calibration(calibration_id)
44+
45+
res = tobiiglasses.wait_until_calibration_is_done(calibration_id)
46+
47+
if res is False:
48+
print("Calibration failed!")
49+
exit(1)
50+
51+
recording_id = tobiiglasses.create_recording(participant_id)
52+
print("Important! The recording will be stored in the SD folder projects/%s/recordings/%s" % (project_id, recording_id))
53+
input("Press enter to start recording")
54+
tobiiglasses.start_recording(recording_id)
55+
tobiiglasses.send_custom_event("start_recording", "Start of the recording ")
56+
input("Press enter to stop recording")
57+
tobiiglasses.send_custom_event("stop_recording", "Stop of the recording " + str(recording_id))
58+
tobiiglasses.stop_recording(recording_id)
59+
60+
61+
if res is False:
62+
print("Recording failed!")
63+
exit(1)
64+
65+
if __name__ == '__main__':
66+
main()

apps/connect.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# connect.py : A demo code for connecting with the Tobii Pro Glasses 2
2+
#
3+
# Copyright (C) 2019 Davide De Tommaso
4+
#
5+
# This program is free software: you can redistribute it and/or modify
6+
# it under the terms of the GNU General Public License as published by
7+
# the Free Software Foundation, either version 3 of the License, or
8+
# (at your option) any later version
9+
#
10+
# This program is distributed in the hope that it will be useful,
11+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
# GNU General Public License for more details.
14+
#
15+
# You should have received a copy of the GNU General Public License
16+
# along with this program. If not, see <https://www.gnu.org/licenses/>
17+
18+
from tobiiglassesctrl import TobiiGlassesController
19+
20+
def main():
21+
22+
"""
23+
How to connect with the tobii glasses
24+
25+
0. Automatic discovery of the device
26+
27+
TobiiGlassesController()
28+
29+
1. If you know the IPv6 addr of the tobii glasses
30+
31+
TobiiGlassesController("fe80::76fe:48ff:ff00:hell")
32+
33+
2. If you know the IPv6 addr of the tobii glasses and the net interface
34+
of your host system (in case of multiple interfaces)
35+
36+
TobiiGlassesController("fe80::76fe:48ff:ff00:ff00%eth0")
37+
38+
3. If you know the IPv4 addr of the tobii glasses (WLAN or LAN connections)
39+
40+
TobiiGlassesController("192.168.71.50")
41+
"""
42+
43+
TobiiGlassesController()
44+
45+
46+
if __name__ == '__main__':
47+
main()

apps/live_scene.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# live_scene.py : A demo for video streaming
2+
#
3+
# Copyright (C) 2019 Davide De Tommaso
4+
#
5+
# This program is free software: you can redistribute it and/or modify
6+
# it under the terms of the GNU General Public License as published by
7+
# the Free Software Foundation, either version 3 of the License, or
8+
# (at your option) any later version
9+
#
10+
# This program is distributed in the hope that it will be useful,
11+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
# GNU General Public License for more details.
14+
#
15+
# You should have received a copy of the GNU General Public License
16+
# along with this program. If not, see <https://www.gnu.org/licenses/>
17+
18+
import cv2
19+
import numpy as np
20+
21+
address = "192.168.71.50"
22+
#address = "fe80::76fe:48ff:ff00:ff00"
23+
cap = cv2.VideoCapture("rtsp://%s:8554/live/scene" % address)
24+
25+
# Check if camera opened successfully
26+
if (cap.isOpened()== False):
27+
print("Error opening video stream or file")
28+
29+
# Read until video is completed
30+
while(cap.isOpened()):
31+
# Capture frame-by-frame
32+
ret, frame = cap.read()
33+
if ret == True:
34+
35+
# Display the resulting frame
36+
cv2.imshow('Tobii Pro Glasses 2 - Live Scene',frame)
37+
38+
# Press Q on keyboard to exit
39+
if cv2.waitKey(1) & 0xFF == ord('q'):
40+
break
41+
42+
# Break the loop
43+
else:
44+
break
45+
46+
# When everything done, release the video capture object
47+
cap.release()
48+
49+
# Closes all the frames
50+
cv2.destroyAllWindows()

apps/live_scene_and_gaze.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# live_scene_and_gaze.py : A demo for video streaming and synchronized gaze
2+
#
3+
# Copyright (C) 2018 Davide De Tommaso
4+
#
5+
# This program is free software; you can redistribute it and/or modify
6+
# it under the terms of the GNU General Public License as published by
7+
# the Free Software Foundation; either version 2 of the License, or
8+
# (at your option) any later version.
9+
#
10+
# This program is distributed in the hope that it will be useful,
11+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
# GNU General Public License for more details.
14+
#
15+
# You should have received a copy of the GNU General Public License
16+
# along with this program. If not, see <http://www.gnu.org/licenses/>
17+
18+
import cv2
19+
import numpy as np
20+
21+
if hasattr(__builtins__, 'raw_input'):
22+
input=raw_input
23+
24+
from tobiiglassesctrl import TobiiGlassesController
25+
26+
ipv4_address = "192.168.71.50"
27+
28+
tobiiglasses = TobiiGlassesController(ipv4_address, video_scene=True)
29+
30+
project_id = tobiiglasses.create_project("Test live_scene_and_gaze.py")
31+
32+
participant_id = tobiiglasses.create_participant(project_id, "participant_test")
33+
34+
calibration_id = tobiiglasses.create_calibration(project_id, participant_id)
35+
36+
input("Put the calibration marker in front of the user, then press enter to calibrate")
37+
tobiiglasses.start_calibration(calibration_id)
38+
39+
res = tobiiglasses.wait_until_calibration_is_done(calibration_id)
40+
41+
42+
if res is False:
43+
print("Calibration failed!")
44+
exit(1)
45+
46+
47+
cap = cv2.VideoCapture("rtsp://%s:8554/live/scene" % ipv4_address)
48+
49+
# Check if camera opened successfully
50+
if (cap.isOpened()== False):
51+
print("Error opening video stream or file")
52+
53+
# Read until video is completed
54+
tobiiglasses.start_streaming()
55+
while(cap.isOpened()):
56+
# Capture frame-by-frame
57+
ret, frame = cap.read()
58+
if ret == True:
59+
height, width = frame.shape[:2]
60+
data_gp = tobiiglasses.get_data()['gp']
61+
if data_gp['ts'] > 0:
62+
cv2.circle(frame,(int(data_gp['gp'][0]*width),int(data_gp['gp'][1]*height)), 60, (0,0,255), 5)
63+
64+
# Display the resulting frame
65+
cv2.imshow('Tobii Pro Glasses 2 - Live Scene',frame)
66+
67+
# Press Q on keyboard to exit
68+
if cv2.waitKey(1) & 0xFF == ord('q'):
69+
break
70+
71+
# Break the loop
72+
else:
73+
break
74+
75+
# When everything done, release the video capture object
76+
cap.release()
77+
78+
# Closes all the frames
79+
cv2.destroyAllWindows()
80+
81+
tobiiglasses.stop_streaming()
82+
tobiiglasses.close()
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# live_scene_and_gaze.py : A demo for video streaming and synchronized gaze
2+
#
3+
# Copyright (C) 2021 Davide De Tommaso
4+
#
5+
# This program is free software; you can redistribute it and/or modify
6+
# it under the terms of the GNU General Public License as published by
7+
# the Free Software Foundation; either version 2 of the License, or
8+
# (at your option) any later version.
9+
#
10+
# This program is distributed in the hope that it will be useful,
11+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
# GNU General Public License for more details.
14+
#
15+
# You should have received a copy of the GNU General Public License
16+
# along with this program. If not, see <http://www.gnu.org/licenses/>
17+
18+
import av
19+
import cv2
20+
import numpy as np
21+
from tobiiglassesctrl import TobiiGlassesController
22+
23+
ipv4_address = "192.168.100.10"
24+
25+
tobiiglasses = TobiiGlassesController(ipv4_address)
26+
video = av.open("rtsp://%s:8554/live/scene" % ipv4_address, "r")
27+
28+
tobiiglasses.start_streaming()
29+
30+
try:
31+
for packet in video.demux():
32+
for frame in packet.decode():
33+
if isinstance(frame,av.video.frame.VideoFrame):
34+
#print(frame.pts)
35+
img = frame.to_ndarray(format='bgr24')
36+
height, width = img.shape[:2]
37+
data_gp = tobiiglasses.get_data()['gp']
38+
if data_gp['ts'] > 0:
39+
cv2.circle(img,(int(data_gp['gp'][0]*width),int(data_gp['gp'][1]*height)), 60, (0,0,255), 6)
40+
cv2.imshow('Tobii Pro Glasses 2 - Live Scene',img)
41+
if cv2.waitKey(1) & 0xFF == ord('q'):
42+
break
43+
except KeyboardInterrupt:
44+
pass
45+
cv2.destroyAllWindows()
46+
47+
tobiiglasses.stop_streaming()
48+
tobiiglasses.close()

apps/streaming.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# streaming.py : A demo for data streaming
2+
#
3+
# Copyright (C) 2019 Davide De Tommaso
4+
#
5+
# This program is free software: you can redistribute it and/or modify
6+
# it under the terms of the GNU General Public License as published by
7+
# the Free Software Foundation, either version 3 of the License, or
8+
# (at your option) any later version
9+
#
10+
# This program is distributed in the hope that it will be useful,
11+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
# GNU General Public License for more details.
14+
#
15+
# You should have received a copy of the GNU General Public License
16+
# along with this program. If not, see <https://www.gnu.org/licenses/>
17+
18+
import time
19+
import json
20+
from tobiiglassesctrl import TobiiGlassesController
21+
22+
23+
def main():
24+
25+
tobiiglasses = TobiiGlassesController("192.168.71.50")
26+
print(tobiiglasses.get_battery_status())
27+
28+
tobiiglasses.start_streaming()
29+
print("Please wait ...")
30+
time.sleep(3.0)
31+
32+
for i in range(1000):
33+
print("Head unit: %s" % tobiiglasses.get_data()['mems'])
34+
print("Left Eye: %s " % tobiiglasses.get_data()['left_eye'])
35+
print("Right Eye: %s " % tobiiglasses.get_data()['right_eye'])
36+
print("Gaze Position: %s " % tobiiglasses.get_data()['gp'])
37+
print("Gaze Position 3D: %s " % tobiiglasses.get_data()['gp3'])
38+
39+
tobiiglasses.stop_streaming()
40+
tobiiglasses.close()
41+
42+
43+
44+
if __name__ == '__main__':
45+
main()

examples

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)