我正在使用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)
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)
我在运行时遇到此错误:
* 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小文件导入文件并运行它,然后代码运行。 任何帮助将不胜感激,谢谢。