Skip to content

Commit

Permalink
upload back-end files and organize the front-end files
Browse files Browse the repository at this point in the history
  • Loading branch information
OmarKhalil10 committed Mar 13, 2023
1 parent a2c0228 commit 3d65506
Show file tree
Hide file tree
Showing 101 changed files with 229 additions and 29 deletions.
29 changes: 28 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,29 @@
# Image-Captioner-Using-Tensoflow
Image-Captioner-Using-Tensoflow
## To add a new page
* Create the html, css, js in the specified folder using the same folder structure.
* Create a new route in the [app.py](./app.py) file with the name you want using only dashes to seperate words.
```PYTHON
@app.route('NEW-ROUTE')
```
* Define your serving function using a unique name not used before in the whole application.
```PYTHON
def NEW_UNIQUE_NAME():
```
* Return your html file path using render_template.
```PYTHON
return render_template('FOLDER_PATH/FILE_PATH.html')
```
* Your newely created route should look like this.
```PYTHON
@app.route('NEW-ROUTE')
def NEW_UNIQUE_NAME():
return render_template('FOLDER_PATH/FILE_PATH.html')
```

## To run the development server
* Open git bash terminal
```bash
FLASK_APP=app.py
FLASK_ENV=development
flask run --reload
```
126 changes: 126 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import os
from flask import Flask, request, render_template, redirect, abort, jsonify, flash, url_for
from flask_cors import CORS
from sqlalchemy import or_
import numpy as np
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
import tensorflow as tf
from werkzeug.utils import secure_filename
from flask import send_from_directory
from keras.preprocessing import image
import matplotlib.pyplot as plt

ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif'])

def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS


def create_app(test_config=None):
# Create and configure the app
app = Flask(__name__)
app.config.from_pyfile('settings.py')
#setup_db(app)
CORS(app, resources={r"/api/*": {"origins": "*"}})

@app.after_request
def after_request(response):
response.headers.add('Access-Control-Allow-Headers', 'Content-Type, Authorization,true')
response.headers.add('Access-Control-Allow-Methods', 'GET, POST, PATCH, DELETE, OPTIONS')
return response

@app.route("/")
def landing_page():
return render_template("pages/index.html")

@app.route("/about")
def about_page():
return render_template("pages/about.html")

@app.route("/prediction", methods=["POST", "GET"])
def prediction_page():
# check if the post request has the file part
if request.method == 'POST':
if 'files' not in request.files:
flash('No file part')
file = request.files['files']
# if user does not select file, browser also
# submit an empty part without filename
if file.filename == '':
flash('No selected file')
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))

model = tf.keras.models.load_model('.\\model_weights.h5')

path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
img = image.load_img(path, target_size=(224, 224))
x=image.img_to_array(img)
x /= 255
x=np.expand_dims(x, axis=0)
images = np.vstack([x])

prediction = model.predict(images, batch_size=10)

return jsonify({
'prediction': prediction,
'success': True,
#'caption': caption
}), 200
return jsonify({
'success': False
}), 405

@app.route('/uploads/<filename>')
def uploaded_file(filename):
return send_from_directory(app.config['UPLOAD_FOLDER'],
filename)

@app.errorhandler(400)
def bad_request(error):
return jsonify({
'success': False,
'error': 400,
'message': 'bad request'
}), 400

@app.errorhandler(404)
def not_found(error):
return render_template('/pages/errors/error.html', data={
'success': False,
'error': 404,
'description': 'Sorry but the page you are looking for does not exist, have been removed, name changed or is temporarily unavailable.',
'message': 'Page Not Be Found'
}), 404

@app.errorhandler(405)
def method_not_allowed(error):
return jsonify({
'success': False,
'error': 405,
'message': 'method not allowed'
}), 405

@app.errorhandler(422)
def unprocessable(error):
return jsonify({
"success": False,
"error": 422,
"message": "unprocessable"
}), 422

@app.errorhandler(500)
def internal_server_error(error):
return jsonify({
'success': False,
'error': 500,
'message': 'internal server errors'
}), 500
return app

app = create_app()

if __name__ == '__main__':
app.run(host='127.0.0.1', port=4040, debug=True)
Binary file added model_weights.h5
Binary file not shown.
23 changes: 23 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
alembic==1.4.3
Babel==2.9.0
Flask==2.1.0
gunicorn==20.0.4
Flask-Cors==3.0.8
Flask-RESTful==0.3.7
requests==2.22.0
psycopg2
psycopg2-binary==2.8.2
Flask-Mail==0.9.1
python-dotenv
Flask-SQLAlchemy==2.1
SQLAlchemy==1.3.12
tensorflow==2.6.0
keras==2.6.0
Keras-Preprocessing==1.1.2
Jinja2==3.0
matplotlib==3.5.2
mysql-connector-python==8.0.26
mysqlclient==2.0.3
itsdangerous==2.0
six==1.15.0
Werkzeug==2.0
1 change: 1 addition & 0 deletions runtime.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python-3.7.8
9 changes: 9 additions & 0 deletions settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import os
from os.path import join, dirname
from dotenv import load_dotenv

dotenv_path = join(dirname(__file__), 'vars.env')
load_dotenv(dotenv_path)

UPLOAD_FOLDER = os.environ.get('UPLOAD_FOLDER')
DEBUG = True
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
12 changes: 12 additions & 0 deletions templates/pages/about.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=h1, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>About</h1>
</body>
</html>
20 changes: 10 additions & 10 deletions caption.html → templates/pages/caption.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="icon" href="images/dw.png" type="image/png">
<title>Define Me</title>
<link rel="stylesheet" href="css/bootstrap.css">
<link rel="stylesheet" href="css/style_1.css">
<link rel="stylesheet" href="css/font-awesome.min.css">
<link rel="stylesheet" href="css/owl.carousel.min.css">
<link rel="stylesheet" href="css/magnific-popup.css">
<link rel="stylesheet" href="css/nice-select.css">
<link rel="stylesheet" href="css/animate.css">
<link rel="stylesheet" href="css/flaticon.css">
<link rel="stylesheet" href="css/upload.css">
<link rel="stylesheet" href="css/style.css">
<link rel="stylesheet" href="static/css/bootstrap.css">
<link rel="stylesheet" href="static/css/style_1.css">
<link rel="stylesheet" href="static/css/font-awesome.min.css">
<link rel="stylesheet" href="static/css/owl.carousel.min.css">
<link rel="stylesheet" href="static/css/magnific-popup.css">
<link rel="stylesheet" href="static/css/nice-select.css">
<link rel="stylesheet" href="static/css/animate.css">
<link rel="stylesheet" href="static/css/flaticon.css">
<link rel="stylesheet" href="static/css/upload.css">
<link rel="stylesheet" href="static/css/style.css">
</head>

<body>
Expand Down
36 changes: 18 additions & 18 deletions index.html → templates/pages/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="icon" href="images/dw.png" type="image/png">
<link rel="icon" href="static/images/dw.png" type="image/png">
<title>Define Me</title>
<link rel="stylesheet" href="css/bootstrap.css">
<link rel="stylesheet" href="css/style_1.css">
<link rel="stylesheet" href="css/font-awesome.min.css">
<link rel="stylesheet" href="css/owl.carousel.min.css">
<link rel="stylesheet" href="css/magnific-popup.css">
<link rel="stylesheet" href="css/nice-select.css">
<link rel="stylesheet" href="css/animate.css">
<link rel="stylesheet" href="css/flaticon.css">
<link rel="stylesheet" href="css/style.css">
<link rel="stylesheet" href="static/css/bootstrap.css">
<link rel="stylesheet" href="static/css/style_1.css">
<link rel="stylesheet" href="static/css/font-awesome.min.css">
<link rel="stylesheet" href="static/css/owl.carousel.min.css">
<link rel="stylesheet" href="static/css/magnific-popup.css">
<link rel="stylesheet" href="static/css/nice-select.css">
<link rel="stylesheet" href="static/css/animate.css">
<link rel="stylesheet" href="static/css/flaticon.css">
<link rel="stylesheet" href="static/css/style.css">
</head>
<body>
<header class="header_area">
<div class="main_menu">
<nav class="navbar navbar-expand-lg navbar-light">
<div class="container">
<a class="navbar-brand logo_h" href="index.html"><img src="images/dw.png" alt=""></a>
<a class="navbar-brand logo_h" href="index.html"><img src="static/images/dw.png" alt=""></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="icon-bar"></span>
Expand All @@ -30,7 +30,7 @@
<div class="collapse navbar-collapse offset" id="navbarSupportedContent">
<ul class="nav navbar-nav menu_nav justify-content-center">
<li class="nav-item active"><a class="nav-link" href="index.html">Home</a></li>
<li class="nav-item"><a class="nav-link" href="#about">About</a></li>
<li class="nav-item"><a class="nav-link" href="about.html">About</a></li>
<li class="nav-item"><a class="nav-link" href="#Learn">How it Works</a>
</li>
</ul>
Expand All @@ -48,7 +48,7 @@
<div class="row">
<div class="col-lg-6">
<div class="home_left_img">
<img class="img-fluid" src="images/home-left.png" alt="">
<img class="img-fluid" src="static/images/home-left.png" alt="">
</div>
</div>
<div class="col-lg-6">
Expand Down Expand Up @@ -84,7 +84,7 @@ <h6>About Us </h6>
</div>
<div class="col-lg-6">
<div class="about_us_image_box justify-content-center">
<!--<img class="img-fluid w-100" src="images/about_img.png" alt="">-->
<!--<img class="img-fluid w-100" src="static/images/about_img.png" alt="">-->
<video controls loop width="500px" height="500px" autoplay muted controls="controls">
<source src="vid\Photo Captions Promo.mp4"
type="video/mp4">
Expand Down Expand Up @@ -123,7 +123,7 @@ <h6>About Us </h6>
<div class="row recent_update_text">
<div class="col-lg-6">
<div class="chart_img">
<img class="img-fluid" src="images/ha3.jpg" alt="">
<img class="img-fluid" src="static/images/ha3.jpg" alt="">
</div>
</div>
<div class="col-lg-6">
Expand All @@ -142,7 +142,7 @@ <h6>Our Story</h6>
<div class="row recent_update_text">
<div class="col-lg-6">
<div class="chart_img">
<img class="img-fluid" src="images/ha1.jpg" alt="">
<img class="img-fluid" src="static/images/ha1.jpg" alt="">
</div>
</div>
<div class="col-lg-6">
Expand All @@ -161,7 +161,7 @@ <h6>Our Vision</h6>
<div class="row recent_update_text">
<div class="col-lg-6">
<div class="chart_img">
<img class="img-fluid" src="images/ha4.jpg" alt="">
<img class="img-fluid" src="static/images/ha4.jpg" alt="">
</div>
</div>
<div class="col-lg-6">
Expand Down Expand Up @@ -195,7 +195,7 @@ <h6>Learn More</h6>
</div>
<div class="col-lg-6">
<div class="about_us_image_box justify-content-center">
<img class="img-fluid w-100" src="images/oh.jpeg" alt="">
<img class="img-fluid w-100" src="static/images/oh.jpeg" alt="">
</div>
</div>
</div>
Expand Down
2 changes: 2 additions & 0 deletions vars.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#.env
UPLOAD_FOLDER=.\\static\\uploads

0 comments on commit 3d65506

Please sign in to comment.