mlfpm / deepof

DeepLabCut based data analysis package including pose estimation and representation learning mediated behavior recognition
MIT License
39 stars 6 forks source link

arena annotation error on google colab #27

Closed GergelyTuri closed 11 months ago

GergelyTuri commented 1 year ago

Hi, i am testing this package on an of video of mine in a Google Colab notebook. I am following the instructions described in Formatting your data: feature extraction from DLC output. It seems that the tutorial data works fine but my arena is rectangular so i would need to annotate it. I passed in arena="polygonal-manual" argument in the deepof.data.Project function, but when i try to run my_deepof_project.create(force=True) it throws the following error:

DisabledFunctionError                     Traceback (most recent call last)
[/tmp/ipykernel_3160/3247491054.py](https://localhost:8080/#) in <cell line: 1>()
----> 1 my_deepof_project = my_deepof_project.create(force=True)

5 frames
[/usr/local/lib/python3.10/dist-packages/google/colab/_import_hooks/_cv2.py](https://localhost:8080/#) in wrapped(*args, **kwargs)
     46   def wrapped(*args, **kwargs):
     47     if not os.environ.get(env_var, False):
---> 48       raise DisabledFunctionError(message, name or func.__name__)
     49     return func(*args, **kwargs)
     50 

DisabledFunctionError: cv2.imshow() is disabled in Colab, because it causes Jupyter sessions
to crash; see https://github.com/jupyter/notebook/issues/3935.
As a substitution, consider using
  from google.colab.patches import cv2_imshow

I tried to replace and modify the cv2.imshow calls to cv2_imshow in the source but this function works slightly differently than the original open CV one so i run into more issues that way. Any idea how to fix this?

lucasmiranda42 commented 1 year ago

Dear @GergelyTuri,

Thank you for your interest in DeepOF! I wasn't aware of this issue. I'll investigate and check whether we can easily patch it in a future version! In the meantime, I would unfortunately advise you to test this locally if possible. Running the first cells in the tutorials on an isolated environment in your local machine should pretty much replicate what we have on colab (although I know it's a bit more tedious...).

Best, and I'll let you know once we have news in this regard! Lucas

GergelyTuri commented 1 year ago

Hey @lucasmiranda42 , Thanks for the feedback. I looked into this a little bit. This is pretty rough around the edges, but something like this could work in a Colab notebook:

!pip install ipympl

!curl -o logo.png https://colab.research.google.com/img/colab_favicon_256px.png  

import ipywidgets as widgets
import matplotlib.pyplot as plt
from google.colab import output
output.enable_custom_widget_manager()
%matplotlib ipympl

click_coordinates = []

def onclick(event):
    ix, iy = event.xdata, event.ydata
    print(ix, iy)
    click_coordinates.append(((ix,iy)))

image_path = "./logo.png"
image = plt.imread(image_path)
figure = plt.figure(figsize=(6, 6))
plt.imshow(image)
plt.axis('off')  # Turn off axis for better visualization
plt.gca().get_xaxis().set_visible(False)
plt.gca().get_yaxis().set_visible(False)
cid = figure.canvas.mpl_connect('button_press_event', onclick)
lucasmiranda42 commented 1 year ago

Dear @GergelyTuri,

Thank you very much! I'll have a look and assess whether we can include this in the next release (which should come out around the second half of September). Feel free to submit a pull request to the development branch, so your contribution is properly credited!

Best, Lucas

lucasmiranda42 commented 1 year ago

Dear @GergelyTuri,

We put some effort in this regard, but it was unfortunately not as easy as we thought. The main issue seems to be that colab waits for the executed code to finish running before rendering the interactive output, which prevents us from capturing feedback and incorporating it into the project (which is the main goal of the GUI). So, for the time being, interactive arena detection will unfortunately remain a local-only feature.

That said, if you'd like to further explore and suggest or contribute solutions, these are really welcome! I do think it would be a nice thing to have.

Best, and thanks again for your interest, Lucas

GergelyTuri commented 11 months ago

Hi Lucas, Thanks for giving it a try. Best, Gergely