Skip to content

Instantly share code, notes, and snippets.

@hasan-haider
Created April 14, 2020 22:10
Show Gist options
  • Save hasan-haider/a5c274dd250bf3e60e66193130c8497a to your computer and use it in GitHub Desktop.
Save hasan-haider/a5c274dd250bf3e60e66193130c8497a to your computer and use it in GitHub Desktop.
Model prediction
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Pipenv (flask_h5)" project-jdk-type="Python SDK" />
<component name="PyCharmProfessionalAdvertiser">
<option name="shown" value="true" />
</component>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/flask_h5.iml" filepath="$PROJECT_DIR$/.idea/flask_h5.iml" />
</modules>
</component>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ChangeListManager">
<list default="true" id="f9989f19-03b0-4c60-91a8-52a61b3c6eea" name="Default Changelist" comment="" />
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="FileTemplateManagerImpl">
<option name="RECENT_TEMPLATES">
<list>
<option value="Python Script" />
<option value="HTML File" />
</list>
</option>
</component>
<component name="ProjectId" id="1aUYUCUPOXzI9ygTmgwTXQpAyJn" />
<component name="ProjectViewState">
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showExcludedFiles" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent">
<property name="DefaultHtmlFileTemplate" value="HTML File" />
<property name="RunOnceActivity.ShowReadmeOnStart" value="true" />
<property name="last_opened_file_path" value="$PROJECT_DIR$" />
<property name="settings.editor.selected.configurable" value="com.jetbrains.python.configuration.PyActiveSdkModuleConfigurable" />
</component>
<component name="RunManager" selected="Python.temp">
<configuration name="process" type="PythonConfigurationType" factoryName="Python" temporary="true" nameIsGenerated="true">
<module name="flask_h5" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" />
<envs>
<env name="PYTHONUNBUFFERED" value="1" />
</envs>
<option name="SDK_HOME" value="" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/app" />
<option name="IS_MODULE_SDK" value="true" />
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/app/process.py" />
<option name="PARAMETERS" value="" />
<option name="SHOW_COMMAND_LINE" value="false" />
<option name="EMULATE_TERMINAL" value="false" />
<option name="MODULE_MODE" value="false" />
<option name="REDIRECT_INPUT" value="false" />
<option name="INPUT_FILE" value="" />
<method v="2" />
</configuration>
<configuration name="temp" type="PythonConfigurationType" factoryName="Python" temporary="true" nameIsGenerated="true">
<module name="flask_h5" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" />
<envs>
<env name="PYTHONUNBUFFERED" value="1" />
</envs>
<option name="SDK_HOME" value="" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/app" />
<option name="IS_MODULE_SDK" value="true" />
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/app/temp.py" />
<option name="PARAMETERS" value="" />
<option name="SHOW_COMMAND_LINE" value="false" />
<option name="EMULATE_TERMINAL" value="false" />
<option name="MODULE_MODE" value="false" />
<option name="REDIRECT_INPUT" value="false" />
<option name="INPUT_FILE" value="" />
<method v="2" />
</configuration>
<recent_temporary>
<list>
<item itemvalue="Python.temp" />
<item itemvalue="Python.process" />
</list>
</recent_temporary>
</component>
<component name="SvnConfiguration">
<configuration />
</component>
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
<changelist id="f9989f19-03b0-4c60-91a8-52a61b3c6eea" name="Default Changelist" comment="" />
<created>1586791652557</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1586791652557</updated>
</task>
<servers />
</component>
<component name="WindowStateProjectService">
<state x="137" y="23" key="#com.intellij.execution.impl.EditConfigurationsDialog" timestamp="1586798830616">
<screen x="0" y="0" width="1366" height="728" />
</state>
<state x="137" y="23" key="#com.intellij.execution.impl.EditConfigurationsDialog/0.0.1366.728@0.0.1366.728" timestamp="1586798830616" />
<state x="442" y="175" key="#com.intellij.fileTypes.FileTypeChooser" timestamp="1586792091380">
<screen x="0" y="0" width="1366" height="728" />
</state>
<state x="442" y="175" key="#com.intellij.fileTypes.FileTypeChooser/0.0.1366.728@0.0.1366.728" timestamp="1586792091380" />
<state x="463" y="123" key="FileChooserDialogImpl" timestamp="1586869283323">
<screen x="0" y="0" width="1366" height="728" />
</state>
<state x="463" y="123" key="FileChooserDialogImpl/0.0.1366.728@0.0.1366.728" timestamp="1586869283323" />
<state x="521" y="266" width="347" height="195" key="Github.CreateGistDialog" timestamp="1586902175355">
<screen x="0" y="0" width="1366" height="728" />
</state>
<state x="521" y="266" width="347" height="195" key="Github.CreateGistDialog/0.0.1366.728@0.0.1366.728" timestamp="1586902175355" />
<state width="1345" height="179" key="GridCell.Tab.0.bottom" timestamp="1586898866258">
<screen x="0" y="0" width="1366" height="728" />
</state>
<state width="1345" height="179" key="GridCell.Tab.0.bottom/0.0.1366.728@0.0.1366.728" timestamp="1586898866258" />
<state width="1345" height="179" key="GridCell.Tab.0.center" timestamp="1586898866257">
<screen x="0" y="0" width="1366" height="728" />
</state>
<state width="1345" height="179" key="GridCell.Tab.0.center/0.0.1366.728@0.0.1366.728" timestamp="1586898866257" />
<state width="1345" height="179" key="GridCell.Tab.0.left" timestamp="1586898866257">
<screen x="0" y="0" width="1366" height="728" />
</state>
<state width="1345" height="179" key="GridCell.Tab.0.left/0.0.1366.728@0.0.1366.728" timestamp="1586898866257" />
<state width="1345" height="179" key="GridCell.Tab.0.right" timestamp="1586898866257">
<screen x="0" y="0" width="1366" height="728" />
</state>
<state width="1345" height="179" key="GridCell.Tab.0.right/0.0.1366.728@0.0.1366.728" timestamp="1586898866257" />
<state x="186" y="10" key="SettingsEditor" timestamp="1586877531539">
<screen x="0" y="0" width="1366" height="728" />
</state>
<state x="186" y="10" key="SettingsEditor/0.0.1366.728@0.0.1366.728" timestamp="1586877531539" />
<state x="391" y="193" key="com.intellij.ide.util.TipDialog" timestamp="1586887232722">
<screen x="0" y="0" width="1366" height="728" />
</state>
<state x="391" y="193" key="com.intellij.ide.util.TipDialog/0.0.1366.728@0.0.1366.728" timestamp="1586887232722" />
</component>
</project>
from app import app
if __name__=='__main__':
app.run(debug=True)
from flask import Flask
import os
app=Flask(__name__,static_folder='static')
app.config['SECRET_KEY']='blablabla'
APP_ROOT=os.path.dirname(os.path.abspath(__file__))
from app import routes
from imageai.Prediction import ImagePrediction
import os
from app import APP_ROOT
static_loc=os.path.join(APP_ROOT,'static/')
def predict_img(filename):
target=os.path.join(APP_ROOT,'temp/'+filename) #location of image present in temp directory
prediction = ImagePrediction()
prediction.setModelTypeAsResNet()
prediction.setModelPath(os.path.join(static_loc, "resnet50_weights_tf_dim_ordering_tf_kernels.h5"))
prediction.loadModel()
predictions, probabilities = prediction.predictImage(target, result_count=1)
d={} #dictionary that will save results
for eachPrediction, eachProbability in zip(predictions, probabilities):
d[eachPrediction]=eachProbability
#print(eachPrediction , " : " , eachProbability)
os.remove(target) #delete temporary file
return d
import os
from flask import render_template, request
from werkzeug.utils import redirect
from app import app,APP_ROOT
from app.process import predict_img
@app.route('/')
def home():
return render_template('index.html',title='Home')
@app.route('/about')
def about():
return render_template('about.html',title='About',name='Passed by variable')
@app.route("/predict")
def predict():
return render_template("predict.html",title="Predict")
@app.route("/upload",methods=["GET","POST"])
def upload():
target = os.path.join(APP_ROOT, 'temp/')
if request.method == 'POST':
file = request.files['img']
filename = file.filename
# filename = filename(filename)
file.save("".join([target, filename]))
print("upload Completed")
return redirect('/prediction/{}'.format(filename))
@app.route("/prediction/<filename>",methods=["GET","POST"])
def prediction(filename):
#import process.py
x=predict_img(filename)
return render_template('output.html',results=x)
import tensorflow as tf
from keras.models import load_model
from keras.preprocessing import image
import numpy as np
# dimensions of our images ----- are these then grayscale (black and white)?
img_width, img_height = 240, 240
# load the model we saved
with open('static/model.json', 'r') as f:
model_json = f.read()
model = tf.keras.models.model_from_json(model_json)
# load weights into new model
model.load_weights("static/model.h5")
# Get test image ready
test_image = image.load_img('static/Y1.jpg', target_size=(img_width, img_height))
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis=0)
test_image = test_image.reshape(1,img_width, img_height,3) # Ambiguity!
# Should this instead be: test_image.reshape(img_width, img_height, 3) ??
result = model.predict(test_image, batch_size=1)
print(result)
#best_model=load_model("static/model.h5")
'''with open('static/model.json', 'r') as f:
model_json = f.read()
model = tf.keras.models.model_from_json(model_json)
# load weights into new model
model.load_weights("static/model.h5")
print(model)
img = cv2.imread("static/1no.jpeg")
img = cv2.resize(img,(240,240),3)
img=img.reshape((-1, 240, 240, 3))
image = imread("static/Y1.jpg")
pred = model.predict(img)
print(pred)'''
{% extends 'layout.html' %}
{% block ourpage %}
<div class="card">
<div class="card-body">
<h4 class="card-title">About Us</h4>
<p class="card-text">We are Engineers</p>
<p> {{ name }} </p>
</div>
</div>>
{% endblock %}
{% extends 'layout.html' %}
{% block ourpage %}
<h1 class="display-1">Hello! Welcome to our page</h1>
{% endblock %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{{title}}</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="/">Flak APP</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="/">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{{ url_for('about') }}">About</a>
</li>
<li class="nav-item">
<a class="nav-link" href="{{ url_for('predict') }}">Predict</a>
</li>
</ul>
</div>
</nav>
{% block ourpage %}
{% endblock %}
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
</body>
</html>
{% extends 'layout.html' %}
{% block ourpage %}
<<h1 class="display-2">
{{results}}
</h1>
{% endblock %}
{% extends 'layout.html' %}
{% block ourpage %}
<div class="container">
<form method="post" action="/upload" enctype="multipart/form-data">
<div class="form-group">
<label for="img">Image</label>
<input type="file" class="form-control-file" name="img" accept="image/*" id="img" aria-describedby="fileHelpId">
<small id="fileHelpId" class="form-text text-muted">Select Imaget</small>
</div>
<input type="submit" value="Submit" class="btn btn-primary">
</form>
</div>
{% endblock %}
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true
[dev-packages]
[packages]
imageai = "*"
tensorflow = "==1.15.0"
keras = "*"
matplotlib = "*"
opencv-python = "*"
[requires]
python_version = "3.7"
click==7.1.1
Flask==1.1.2
itsdangerous==1.1.0
Jinja2==2.11.1
MarkupSafe==1.1.1
Werkzeug==1.0.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment