简体   繁体   中英

ModuleNotFoundError: No module named 'tensorflow.python.platform' Import error

I'm using Keras, Flask, and Tensorflow. I have two files currently, one for flask and the other for the neural network.

web.py

from flask import Flask, render_template, Response
from camera import VideoCamera
from prediction import NeuralNetwork

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

def gen(camera):
    model = NeuralNetwork()
    while True:
        frame = camera.get_frame()
        print(model.predict(frame))
        yield (b'--frame\r\n'
               b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')

@app.route('/video_feed')
def video_feed():
    return Response(gen(VideoCamera()),
                    mimetype='multipart/x-mixed-replace; boundary=frame')

if __name__ == '__main__':
    app.run(host='0.0.0.0', debug=True)

prediction.py

import numpy as np
from keras.preprocessing.image import img_to_array
from keras.applications import mobilenet
from keras.applications.imagenet_utils import decode_predictions
from PIL import Image

class NeuralNetwork(object):
    def __init__(self):
        self.model = mobilenet.MobileNet(weights='imagenet')

    def predict(self, frame):
        # get the image and convert it into a numpy array and into a format for our model
        img = Image.fromarray(frame, 'RGB')
        img = img.resize((224,224), Image.ANTIALIAS)
        np_image = img_to_array(img)
        image_batch = np.expand_dims(np_image, axis=0)
        processed_image = mobilenet.preprocess_input(image_batch.copy())

        # actual machine learning part
        predictions = self.model.predict(processed_image)
        return decode_predictions(predictions)

I get this error on runtime:

 * Serving Flask app "web.py"
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
Using TensorFlow backend.
Usage: flask run [OPTIONS]

Error: While importing "web", an ImportError was raised:

Traceback (most recent call last):
  File "/home/connor/.local/lib/python3.7/site-packages/flask/cli.py", line 240, in locate_app
    __import__(module_name)
  File "/home/connor/programming/python/Automated-Checkout/web.py", line 3, in <module>
    from prediction import NeuralNetwork
  File "/home/connor/programming/python/Automated-Checkout/prediction.py", line 2, in <module>
    from keras.preprocessing.image import img_to_array
  File "/home/connor/.local/lib/python3.7/site-packages/keras/__init__.py", line 3, in <module>
    from . import utils
  File "/home/connor/.local/lib/python3.7/site-packages/keras/utils/__init__.py", line 6, in <module>
    from . import conv_utils
  File "/home/connor/.local/lib/python3.7/site-packages/keras/utils/conv_utils.py", line 9, in <module>
    from .. import backend as K
  File "/home/connor/.local/lib/python3.7/site-packages/keras/backend/__init__.py", line 1, in <module>
    from .load_backend import epsilon
  File "/home/connor/.local/lib/python3.7/site-packages/keras/backend/load_backend.py", line 90, in <module>
    from .tensorflow_backend import *
  File "/home/connor/.local/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py", line 5, in <module>
    import tensorflow as tf
  File "/usr/local/lib/python3.7/dist-packages/tensorflow/__init__.py", line 28, in <module>
    from tensorflow.python import pywrap_tensorflow  # pylint: disable=unused-import
  File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/__init__.py", line 49, in <module>
    from tensorflow.python import pywrap_tensorflow
  File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/pywrap_tensorflow.py", line 25, in <module>
    from tensorflow.python.platform import self_check
ModuleNotFoundError: No module named 'tensorflow.python.platform'

I am certain that Tensorflow works and I am able to run the file standalone. I also tried importing the file from a small non-Flask Python file and running it and the code runs. Any help would be immensely appreciated, thank you.

From error it looks like Tensorflow not properly installed.

ModuleNotFoundError: No module named 'tensorflow.python.platform'

Follow the instructions mentioned below

#Install tensorflow using pip virtual env 
$pip install virtualenv
$virtualenv tf_2   # tf_2 is virtual env name
$source tf_2/bin/activate
tf_2 $ pip install tensorflow
tf_2 $ python
>>import tensorflow as tf
>>tf.__version__
2.6.0

For other reasons my script requires me to use older tf versions. I did experience this error with version 1.15.0, but after upgrading to version 1.15.2 it disappeared. So I assume there's a sweet spot of versions that include tensorflow.pypthon.platform .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM