holzschu / Carnets

Carnets is a stand-alone Jupyter notebook server and client. Edit your notebooks on the go, even where there is no network.
https://holzschu.github.io/Carnets_Jupyter/
BSD 3-Clause "New" or "Revised" License
546 stars 31 forks source link

Accessing camera + sensors #307

Open Ohmagar opened 11 months ago

Ohmagar commented 11 months ago

Hi there,

is there a way to access the camera of an IPad? Or the sensors? I know some other python IDEs offer a module to access the gyroscope, the magnetometer and accelerometer (omz-software „The motion module allows you to access your iOS device’s motion sensor data (accelerometer, gyroscope, magnetometer).“).

Did/Do I just happen to miss it in Carnets or did you integrate ways to do that? Thank you so much for a reply in advance.

All the best, Ohm

holzschu commented 11 months ago

The camera is accessible through regular packages like OpenCV. I’ve seen people access the sensors using a bridge between Python and objective-C.

Ohmagar commented 10 months ago

Thank you for the quick response!

Unfortunately I ran into another problem:

error Traceback (most recent call last) Cell In [2], line 36 33 print("No available video devices found.") 35 if name == "main": ---> 36 main()

Cell In [2], line 31, in main() 28 print("Available video devices:", available_devices) 30 if available_devices: ---> 31 capture_and_display_frames(available_devices) 32 else: 33 print("No available video devices found.")

Cell In [2], line 15, in capture_and_display_frames(devices) 12 cap.release() 14 for idx, frame in enumerate(captured_frames): ---> 15 cv2.imshow(f"Captured Frame {idx}", frame) 17 cv2.waitKey(0) 18 cv2.destroyAllWindows()

error: OpenCV(4.5.2) /Users/holzschu/src/Xcode_iPad/Carnets/cpython/packages/opencv-python/opencv/modules/highgui/src/window.cpp:679: error: (-2:Unspecified error) The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Cocoa support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function 'cvShowImage'

do you have an idea what the problem might be? A quick reinstall or is it something that’s rooted deeper? best regards

holzschu commented 10 months ago

Debugging is always easier if you provide the code along with the error message.

Working with the error message, I think that OpenCV is trying to open a new window to show the captured frames. That makes sense if you are running a Python script in a terminal, but less sense in a Jupyter notebook, where the image would be better as embedded inside the notebook. Inside iOS, the notion of "open a new window" does not exist, so this won't work.

This short script, for example, captures a frame from the front camera, and displays it inside the notebook:

import cv2 as cv
from matplotlib import pyplot as plt
cap = cv.VideoCapture(1)
ret, frame = cap.read()
plt.imshow(frame)
plt.show()

(VideoCapture(0) would give you the back camera).