Using USB API (MTP) with libghoto2 and Python bindings on MacOS, Raspberry Pi, Linux, ROS

First, thanks to @Hugues for providing this fix. Amazing that you could figure it out! I can’t even find the documentation for --set-config=/main/actions/opcode=

Second, for @NaokiSato102, the solution works for me. I believe that maybe you are not starting the video or your THETA is mounted as a file system?

Here is my complete test

unmount camera

If the camera is mounted, the commands may not work.

image

set to video mode

Using the API reference, we can see that video mode is hex 0x8002 or 32770 in base 10.

$ gphoto2 --set-config=5013=32770

start video

$ gphoto2 --set-config movie=1

stop video

$ gphoto2 --set-config=/main/actions/opcode=0x1018,0xFFFFFFFF

start video and stop after specified time

$ gphoto2 --set-config movie=1 --wait-event=2s --set-config movie=0 
Behavior with Audio Beep

There is a beep after each command and no errors on the console.

image

checking media

If you mount the camera again, you can see the media.

image

In my test, I have intentionally disabled internal stitching.

I suspect that your camera may be auto-mounting. Please check it and report back.

I have added this test to the USB API documentation on the Linux streaming documentation I am building. I’m providing attribution to Hugues by linking to his profile on this site along with his ID on this site.

(Missed stringing the replies together.)

I have firmly unmounted Theta and started recording and then tried it.

I would appreciate it if you all could tell me the results of your tests with information on your experimental environment.

Is it interference from other applications etc.? I do not know the reason.
I’m going to try it in the raspberry-pi environment as well.

The fact that the stop command works with --set-config suggests that it can work with the corresponding code in Python-gPhoto2. I can’t think of any code at the moment, but I think I can convert @mhenrie’s code to suit.

Also, was “gphoto2 --set-config movie=1 --wait-event=2s --set-config movie=0” able to exit after the shooting started successfully in @craig’s environment? This should have been inoperative in my perception, i.e. it was not possible to stop shooting.

RICOH THETA Z1 with firmware 1.50.1

craig@cube:~$ lsb_release -a
LSB Version:	core-11.1.0ubuntu2-noarch:security-11.1.0ubuntu2-noarch
Distributor ID:	Ubuntu
Description:	Ubuntu 20.04.1 LTS
Release:	20.04
Codename:	focal
craig@cube:~$ 

craig@cube:~$ cat /proc/cpuinfo 
processor	: 0
vendor_id	: GenuineIntel
cpu family	: 6
model		: 60
model name	: Intel(R) Pentium(R) CPU G3258 @ 3.20GHz

craig@cube:~$ gphoto2 --version
gphoto2 2.5.23

This version of gphoto2 is using the following software versions and options:
gphoto2         2.5.23         gcc, popt(m), exif, cdk, aa, jpeg, readline
libgphoto2      2.5.25         standard camlibs (SKIPPING lumix), gcc, ltdl, EXIF
libgphoto2_port 0.12.0         iolibs: disk ptpip serial usb1 usbdiskdirect usbscsi, gcc, ltdl, EXIF, USB, serial without locking

It worked in my test. The correct video was saved to file. Note that the camera was in video mode at the start of the sequence.

Test 2 Using RICOH THETA V

Equipment

Host system is the same as described above.

Camera is

$ gphoto2 --summary
Camera summary:                                                                
Manufacturer: Ricoh Company, Ltd.
Model: RICOH THETA V
  Version: 3.40.1
  Serial Number: 00105377
Vendor Extension ID: 0x6 (1.10)

Test 2.1 - manually starting and stopping video

craig@cube:~$ gphoto2 --set-config movie=1
craig@cube:~$ gphoto2 --set-config=/main/actions/opcode=0x1018,0xFFFFFFFF
craig@cube:~$    

Result: successful


Test 2.2 - automatically stopping video

craig@cube:~$ gphoto2 --set-config movie=1 --wait-event=2s --set-config movie=0 

Result: successful

It’s interesting that there are different versions of gphoto2. It may be derived from that.

I CAN NOT get gphoto2 2.5.23 by apt install.
What should I do?

When I installed gPhoto2 using gPhoto2-updater after removing an existing apt-derived gPhoto2 with reference to this article, I was able to execute the commands which @craig presented and the configuration of gPhoto2 became the same.
It seems to be better to use gPhoto2-updater than to install it with apt.
But I wonder why gPhoto2-updater has not been updated. I wonder why we have to separate gPhoto2-updater and apt.

add 20/10/04

As usual, “gphoto2 --set-config-index shutterspeed=10” did not work properly, but the error message changed.

Previously.

*** Error (-110: 'I/O in progress') ***

Present.

*** Error ***              
Shutterspeed not found in configuration tree.
*** Error (-1: 'Unspecified error') ***

[quote=“NaokiSato102, post:39, topic:4521, full:true”]
I CAN NOT get gphoto2 2.5.23 by apt ins[/quote]

Did you modify the source code per the tip from hughes and compile it?

Thanks craig. I was already able to do it the way I did one previous post, using gPhoto2-updater. Now I’m in the same environment as you. I haven’t added any of hughes’ improvements yet, however. We’ll get to that in a while.
It’s important to analyze the gPhoto2 Python bindings, but I have a mid-term presentation exercise on 10/7 with not much progress on the multiple Theta control apps. I’ll need a progress report at that time, so I have to hurriedly create a program that reflects the results of the analysis.

Could you try out scripts/pytheta.py in the Enhance-checking-Theta branch of this repository with multiple Thetas connected?

The comments are in Japanese. The comments are in Japanese, but they are for our own use only for now. Sorry.
I’m going to add English comments to this site when I have time.

By the way, where did you get this information?

I don’t remember, it was 2 years ago. I think I found the gphoto2 pattern for opcode in a forum for another camera brand. With examples, I knew how to pass parameters using opcode operation .
With theta USB API documentation provided by Ricoh, it was easy to get most of operations working.

1 Like

I tried to start and end shooting only in gPhoto with Python binding.
It started successfully, but failed to end.
It doesn’t even show an error message, just “0x1018,0xFFFFFFFFFFFFFFFF” on the console.
I don’t know what’s going on as I don’t fully understand its use.
Can you help me out?

Below is the code we tried.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
import time
import gphoto2 as gp

# milliseconds
TIMEOUT = 10

def wait_for_event(camera, timeout=TIMEOUT, event_type=gp.GP_EVENT_TIMEOUT):
	"""
	Wait for event_type to to be triggered.
	:param camera:
	:param timeout:
	:param event_type:
	:return: event_data
	"""
	while True:
		_event_type, event_data = camera.wait_for_event(timeout)
		if _event_type == gp.GP_EVENT_TIMEOUT:
			return
		if _event_type == event_type:
			return event_data

def main():
	camera = gp.Camera()
	try:
		camera_config = camera.get_config()
	except gp.GPhoto2Error:
		raise RuntimeError("Unable to connect to Camera")
	actions_config = camera_config.get_child_by_name('actions')
	movie = actions_config.get_child_by_name("movie")
	movie.set_value(1)
	camera.set_config(camera_config)
	wait_for_event(camera)
	time.sleep(5)

	opcode = actions_config.get_child_by_name("opcode")
	opcode.set_value("0x1018,0xFFFFFFFF")
	print(opcode.get_value() )

if __name__ == "__main__":
	main()

@craig! I’ve completed the video ingestion function in the multi_view_image repository I was developing!
Now you can connect multiple Theta’s, take a picture, get the remaining recording time, finish recording, and take in a video with one click!

2 Likes

This is fantastic. What version of Python and gphoto are you using?

python main.py 
[debug] check_if_theta 処理開始
[debug] [0]:[usb:003,009] [USB PTP Class Camera]
can't get debug descriptor: Resource temporarily unavailable
[debug] [b'  iProduct                2 RICOH THETA Z1\n']
[debug]シータです
[debug] check_if_thetaは正常終了
[/run/user/1000/gvfs/gphoto2:host=%5Busb%3A003%2C009%5D]はアンマウント済みです
Exception in thread usb:003,009:
Traceback (most recent call last):
  File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.8/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "/home/craig/Development/ricoh/usb/multi_view_image/scripts/pytheta.py", line 198, in inner_start_capture
    camera.set_config(camera_config)
gphoto2.GPhoto2Error: [-1] Unspecified error

...
*** Error ***              
Failed to set new configuration value 0x1018,0xFFFFFFFF for configuration entry /main/actions/opcode.
*** Error (-1: 'Unspecified error') ***       

Exception in thread usb:003,009:
Traceback (most recent call last):
  File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.8/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "/home/craig/Development/ricoh/usb/multi_view_image/scripts/pytheta.py", line 226, in inner_finish_capture
    sp.check_output(cmd, shell=True)
  File "/usr/lib/python3.8/subprocess.py", line 411, in check_output
    return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
  File "/usr/lib/python3.8/subprocess.py", line 512, in run
    raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command 'gphoto2 --set-config=/main/actions/opcode=0x1018,0xFFFFFFFF --port=usb:003,009' returned non-zero exit status 1.

Did you find out that you need to unmount gvfs?

def unmount_theta(theta_list):
	"""	
	マウントされているThetaをアンマウントする。  
	そもそもマウントしないようにすれば良いかもしれないが、  
	他の機材の使用に師匠が出うる設定が必要なのでこの実装となった。
	Parameters
	----------
	theta_list : list
		接続されているThetaのリスト
	"""
	for addr in theta_list:
		path = "/run/user/1000/gvfs/gphoto2:host=%5B"+url.quote(addr)+"%5D"
		if os.path.exists(path):
			print("[{:s}]をアンマウントします".format(path) )
			sp.check_output(["gvfs-mount", "-u", path])
			# cmd = "gvfs-mount -u /run/user/1000/gvfs/mtp:host=%5B"+url.quote(addr)+"%5D"
			# sp.check_output(cmd,shell=True) #こういう書き方もある。
		else:
			print("[{:s}]はアンマウント済みです".format(path))

the THETA Z1 is not mounted on my system.

$ gvfs-mount -l
This tool has been deprecated, use 'gio mount' instead.
See 'gio help mount' for more info.

Drive(0): Samsung SSD 860 QVO 1TB
  Type: GProxyDrive (GProxyVolumeMonitorUDisks2)
Drive(1): ST2000LM007-1R8174
  Type: GProxyDrive (GProxyVolumeMonitorUDisks2)
  Volume(0): 2.0 TB Volume
    Type: GProxyVolume (GProxyVolumeMonitorUDisks2)

The versions of each of the related applications in this system are as follows

gphoto2         2.5.23
libgphoto2      2.5.25
libgphoto2_port 0.12.0
Python          2.7.12
Python-gphoto2  2.2.2

This system will be used in ROS, so it is assumed to run on Python 2.7.

If you are having problems with the unmount process, it may be because the mount format may be slightly different depending on whether you have gvfs-backend or not. (Specifically, the name is different when mounted.)

The unmounted part is designed to make this system work without killing gvfs-backend, so if you’re not supposed to mount it in the first place, you don’t need that part. Comment out and give it a try.

1 Like

Found out why I can’t run a stop and The meaning of a function!
The reason is because I didn’t write “camera.set_config(camera_config)” after “opcode.set_value(“0x1018,0xFFFFFFFFFFFF”)”.

If you remember to write this function, you can perform the exit.

That means that camera.set_config() is meant to adapt the modified configuration to the device!

I’ll post a short piece of code below that you can easily try out.

from __future__ import print_function

import time
import gphoto2 as gp

def main():

	camera = gp.Camera()

	try:
		camera_config = camera.get_config()
	except gp.GPhoto2Error:
		raise RuntimeError("Unable to connect to Camera")

	actions_config = camera_config.get_child_by_name('actions')

	movie = actions_config.get_child_by_name("movie")
	movie.set_value(1)
	camera.set_config(camera_config) # It was discovered that this function was adapting the changed settings.

	time.sleep(5)


	opcode = actions_config.get_child_by_name("opcode")
	opcode.set_value("0x1018,0xFFFFFFFF")
	camera.set_config(camera_config) # This is where I adapted the settings. This is an additional point.
	print(opcode.get_value() )
	

if __name__ == "__main__":
	main()

I hope the authors of Python-gPhoto have something like this in their sample code. I guess I should just throw a pull request for it?

1 Like

Thanks for all your work and this information. I will try it out. I do not use ROS, though I have the components installed. Can you let me know a little more information about it?

  1. Is 16.04 the most common version? The Kinetic Kame version was released in May of 2016. Are there packages related to Kinetic Kame that are not on Melodic Morenia and Noetic Ninjemys. I’m wondering which version to use for testing. Support for Kinetic Kame is running out in April 2021. Maybe I should try Melodic Morenia on Ubuntu 18.04?
  2. What are you using ROS for? I’m wondering what packages I should test to get more familiar with ROS myself.

If you only want to run pytheta.py, then the ROS is irrelevant, except for the Python version.

I’m not sure which is more common in general terms. In the meantime, kinetic is close to running out of support, so new learners should avoid it unless there are very special circumstances.
And you should choose between Melodic and Noetic, if you like, for the following reasons
Melodic has more documentation, but is a bit older.
Noetic may not have as much documentation, but it is one of the most recent.

I’ll give some details about ROS versioning just in case.
My program is developed to be ROS Kinetic compatible, in order to use it in combination with the “orne_gamma” branch of the autonomous control package called “orne_navigation”.

There is also a melodic-ready branch, but the “orne_gamma” was born before the melodic-ready branch was born, so I haven’t adapted the difference yet.
And I’m not used to ROS, so I’ve been putting off applying the diffs until later.
I will continue to use kinetic for that reason.

1 Like
  • thank you for your contribution and information
  • You’re probably busy with your research, so you do not need to reply
  • I’m continuing on to try and get this to work as I feel it may be of use to other people. However, you don’t have to spend time on getting this to work on other systems unless it is of interest to you.

I’m using Python 2.7.18 on Ubuntu 20.04. This is just an FYI for you.

$ python --version
Python 2.7.18

$ python main.py 
[debug] check_if_theta 処理開始
[debug] [0]:[usb:003,013] [USB PTP Class Camera]
can't get debug descriptor: Resource temporarily unavailable
[debug] ['  iProduct                2 RICOH THETA Z1\n']
[debug]シータです
[debug] check_if_thetaは正常終了
Exception in thread usb:003,013:
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
    self.run()
  File "/usr/lib/python2.7/threading.py", line 754, in run
    self.__target(*self.__args, **self.__kwargs)
  File "/home/craig/Development/ricoh/multi_view_image/scripts/pytheta.py", line 236, in inner_start_capture
    camera.set_config(camera_config) # En:Change to appropriate value. JP:値を適応
GPhoto2Error: [-1] Unspecified error