【发布时间】:2020-06-15 05:46:30
【问题描述】:
我正在使用 Keras、Flask 和 Tensorflow。 我目前有两个文件,一个用于烧瓶,另一个用于神经网络。
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)
predict.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)
我在运行时收到此错误:
* 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'
我确信 Tensorflow 可以正常工作,并且我能够独立运行该文件。我还尝试从一个小的非 Flask Python 文件中导入该文件并运行它并运行代码。 任何帮助将不胜感激,谢谢。
【问题讨论】:
-
你的目录结构是什么样的?你是在 repo 子文件夹中运行张量流吗?
-
├── camera.py ├── current-working.py ├── prediction.py ├── pycache │ ├── camera.cpython-37.pyc │ ├── prediction.cpython-37.pyc │ └── web.cpython-37.pyc ├── README.md ├── start.sh ├── 模板 │ └── index.html ├── temp. py └── web.py 我确实在里面运行 tensorflow,它确实可以工作,但是由于某种原因,当我将它与 Flask 结合使用时,它就不行了。
-
如果您滚动到底部,我实际上已经发表了评论。还忘了补充一点,当我在笔记本电脑上运行它时,它的工作原理是我得到了一个与多线程、Tensorflow 和 Flask 相关的不同错误,这根本没有意义
标签: python python-3.x tensorflow flask keras