ImportError: cannot import name ‘context’

this is my final task on college and i’m new to python… my coding is about emotion recognition with fer library. i had installed all of required library. this is my code

import os
import sys

import matplotlib
if os.name == 'posix' and "DISPLAY" not in os.environ:
    matplotlib.use("Agg")

import matplotlib.pyplot as plt

from fer.fer import FER
from fer import Video
import cv2

if _name_ == "_main_":
    try:
        videofile = sys.argv[1]
    except:
        videofile = "test.mp4"
    detector = FER(mtcnn=True)
    video = Video(videofile)

    # Output list of dictionaries
    raw_data = video.analyze(detector, display=False)

    # Convert to pandas for analysis
    df = video.to_pandas(raw_data)
    df = video.get_first_face(df)
    df = video.get_emotions(df)

    # Plot emotions
    df.plot()
    plt.show()

and i got this error :

Using TensorFlow backend.
Traceback (most recent call last):
  File "untitled.py", line 12, in <module>
    from fer.fer import FER
  File "/home/jn-a02/.local/lib/python3.6/site-packages/fer/fer.py", line 38, in <module>
    from keras.models import load_model
  File "/home/jn-a02/.local/lib/python3.6/site-packages/keras/_init_.py", line 3, in <module>
    from . import utils
  File "/home/jn-a02/.local/lib/python3.6/site-packages/keras/utils/_init_.py", line 6, in <module>
    from . import conv_utils
  File "/home/jn-a02/.local/lib/python3.6/site-packages/keras/utils/conv_utils.py", line 9, in <module>
    from .. import backend as K
  File "/home/jn-a02/.local/lib/python3.6/site-packages/keras/backend/_init_.py", line 1, in <module>
    from .load_backend import epsilon
  File "/home/jn-a02/.local/lib/python3.6/site-packages/keras/backend/load_backend.py", line 90, in <module>
    from .tensorflow_backend import *
  File "/home/jn-a02/.local/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 6, in <module>
    from tensorflow.python.eager import context
ImportError: cannot import name 'context'

I’m using Nvidia Jetson Nano A02, I’m pretty new to programming so I don’t know about this error. Can any one recommend what can I do to resolve it? Thanks

Hi,

This error is related to the TensorFlow version you installed.
Just check the version v2.4 and v1.15.x, both have tensorflow.python.eager.context:

Could you check which TensorFlow version you are used first?

$ python3
Python 3.6.9 (default, Jan 26 2021, 15:33:00)
[GCC 8.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
2021-06-28 11:48:01.185781: I tensorflow/stream_executor/platform/default/dso_loader.cc:49] Successfully opened dynamic library libcudart.so.10.2
>>> tf.__version__
'2.4.0'

Thanks.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.