|
| 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() |
0 commit comments