Preview MJPEG Stream on a Ricoh Theta X with Python and OpenCV

I have a live MJPEG preview running over a USB-C ethernet adapter to the Ricoh Theta X.
Here is a screen shot of the preview running from Visual Studio Code:

Screen Shot of Live Preview for Ricoh Theta X

Written in Phyton using OpenCV to display the MJPEG stream.

#Begin Phyton Code
import requests
from requests.auth import HTTPDigestAuth
import cv2
import numpy as np

url = http://[Camera IP Address]/osc/commands/execute
username = "CameraSerialNumber"
password = "DigitsOnlyofCameraSerialNumber"

payload = {
    "name": "camera.getLivePreview"
}

headers = {
    "Content-Type": "application/json;charset=utf-8"
}

response = requests.post(url, auth=HTTPDigestAuth(username, password), json=payload, headers=headers, stream=True)

if response.status_code == 200:
    bytes_ = bytes()
    for chunk in response.iter_content(chunk_size=1024):
        if chunk:
            bytes_ += chunk
            a = bytes_.find(b'\xff\xd8')
            b = bytes_.find(b'\xff\xd9')
            if a != -1 and b != -1:
                jpg = bytes_[a:b+2]
                bytes_ = bytes_[b+2:]
                img = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8), cv2.IMREAD_COLOR)
                cv2.imshow("Preview", img)
                if cv2.waitKey(1) == 27:
                    break
else:
    print("Error: ", response.status_code)

cv2.destroyAllWindows()
#End Python Code
2 Likes

Wow, this is fantastic. Thanks for sharing it!

We’d like to give you a small thanks for your contribution to the community with a community thanks award. I’ll send you an email with more info.

I have this working very beautifully and very smoothly with a THETA V

1 Like

Looking good! I’m working on converting this to .NET as well. Having a bit of difficulty with the CV2 in .net. Happy to share the code I have so far if you want to take a crack at it.

The quality of the stream from OpenCV is better than most client implementations that I’ve seen. Your technique produces a nice result. I made a video of the video stream so that other people can easily see the quality of the camera → computer stream.

I’ve only used C# with Unity, so I’m not sure I’d be of any help. However, I could test it on my Windows 11 laptop. It’s possible other people may be able to provide assistance.

The python OpenCV technique is pretty cool. I’m going to try some transformations with OpenCV and build a simple GUI for the Python script to make it more fun for people in my office to play around with it.

1 Like

Right on. I’ll keep working on the .net version. If I get it working will share the code with you.

Bob Katter

1 Like

now with Canny edge detection.

canny_demo

mod

# showWindow 1: normal view
# showWindow 2: canny edge detection
showWindow = 2
...
...
  if (showWindow == 1):
      cv2.imshow("Preview", img)

  if (showWindow == 2):
      # Convert to graycsale
      img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
      # Blur the image for better edge detection
      img_blur = cv2.GaussianBlur(img_gray, (3,3), 0) 
      # Canny Edge Detection
      edges = cv2.Canny(image=img_blur, threshold1=100, threshold2=200) # Canny Edge Detection
      # Display Canny Edge Detection Image
      cv2.imshow('Canny Edge Detection', edges) 

That is cool. I’ll have to play around with that. I’m also looking into using CV2 to stitch our images together as I’ve been looking for a better method than we currently use., which does work well. Here is an example of what we use the 360 cameras for.

Thanks,
Bob

image001.png

1 Like

I like your introduce very much. However, I meet a connection error for my code.

Is the theta at 192.168.1.1.

From your computer that is connected to the THETA, can you open a browser or curl (command line) to http://192.168.1.1/osc/info ?

The error message indicates that the camera did not respond. Maybe your computer and camera connection is down?

Can you access /osc/info and /osc/state with urllib.request?

1 Like

I have it running on Windows 11 with WSL with a Z1.

Tested with Z1 firmware 3.01.1

$ curl http://192.168.1.1/osc/info
{"api":["/osc/info","/osc/state","/osc/checkForUpdates","/osc/commands/execute","/osc/commands/status"],"apiLevel":[2],"_bluetoothMacAddress":"58:38:79:03:0C:47","endpoints":{"httpPort":80,"httpUpdatesPort":80},"firmwareVersion":"3.01.1","gps":false,"gyro":true,"manufacturer":"RICOH","model":"RICOH THETA Z1","serialNumber":"1001010(ricoh_theta_mjpeg_opencv)

opencv_z1

I used this code from GitHub and it ran with no problems with the Z1.

Thank you so much, craig. I tried your method successfully, I am trying to transfer the data to unity, and show the real time video in VR.

1 Like

The live stream over the USB cable has a higher framerate and resolution. If your application allows, connect the camera to a computer with a USB cable and either connect the headset to the same computer or retransmit it to the headset.

HI @craig and @katterr thank you for the code and explanation of how it will work. I have a question, I want to save an image instead of a live video can I use cv2.imwrite() to get the image, and also will it be a 360-degree image or normal 2D image? I will try and have a look at it to get the video first.

I am not that familiar with OpenCV. However, after this line, you will have the img stored in the variable as a jpeg image. You may have to write as bytes. I don’t know the equivalent in OpenCV.

There are issues with this approach:

  • the frames will be coming in at around 30fps, so you’ll get a lot of frames. you’ll need to increment the filenames
  • each frame will be a lower resolution than if you took an image
  • each frame will not have spherical metadata. Some viewers may not be able to display image without the metadata

For a test, you can use this tool to inject the spatial media metadata back into the image frame.


This is an example in dart of saving the frame.

Multiple frames


Instead of extracting the frame, does your application allow you to take an image of the scene that you want?

If not, can you take an 8K 2fps video and then extract the frames from the video file?

If you advise on your use case, we may be able to provide more ideas.

For the live preview I use Datastead’s TVideoGrabber SDK (TVideoGrabber SDK – Video SDK) for capturing images and video from the Ricoh. Howerver for image and video capture I use the API to ensure I the highest quality output possible. The Live Preview is lower resolution than the native capture the API can capture.

Thank you for the inputs but I am facing issues connecting with my richo theta, http://192.168.1.1/osc/commands/execute this URL does not seem to be accessible also /osc/info as well.
so how can I able to connect the Richo cameras?
Update:
I can able to access the URL now, but only info have some information


and other execute has no information in it
image

How can I move forward with this?

For the S, you need to specify the API version. The S has an older implementation of the API

thank you for the help @craig , finally i can get the image and save it.here is the python code incase anyone needed

Note: I am having issues with connecting, I have to disconnect and reconnect again if any error happens or process the code one time. I am checking for how to reset the camera so I don’t need to disconnect and connect again. If anyone knows how to do that that would be helpful.

I’m don’t quite understand what the problem you are encountering is.

It might be useful to look at the algorithms in the official RICOH SDK

https://github.com/ricohapi/theta-client/blob/main/kotlin-multiplatform/src/commonMain/kotlin/com/ricoh360/thetaclient/ThetaApi.kt

It is in Kotlin, but it may give you ideas on how to handle the API calls under different circumstances.

I’m not sure what you mean by “reset” the camera.

There is an reset API, but it will reboot the camera.

https://github.com/ricohapi/theta-api-specs/blob/main/theta-web-api-v2.1/commands/camera.reset.md