Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Classifier service #122

Open
wants to merge 18 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
scrapbook-classifier-service/DenseNet-BC-121-32.h5 filter=lfs diff=lfs merge=lfs -text
3 changes: 3 additions & 0 deletions scrapbook-classifier-service/DenseNet-BC-121-32.h5
Git LFS file not shown
Binary file not shown.
99 changes: 99 additions & 0 deletions scrapbook-classifier-service/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
from flask import Flask, request, jsonify
from utils import get_predictions
from flask_cors import CORS
import sys
from config import MONGO_URI
from flask_pymongo import PyMongo
from PIL import Image
import io
import base64
import json
from threading import Event
import signal
from PIL.ExifTags import TAGS
from flask_kafka import FlaskKafka

app = Flask(__name__)
CORS(app)
app.config["MONGO_URI"] = MONGO_URI
mongo = PyMongo(app)

INTERRUPT_EVENT = Event()

bus = FlaskKafka(INTERRUPT_EVENT,
bootstrap_servers=",".join(["localhost:9092"]),
group_id="consumer-grp-id-1"
)


def listen_kill_server():
signal.signal(signal.SIGTERM, bus.interrupted_process)
signal.signal(signal.SIGINT, bus.interrupted_process)
signal.signal(signal.SIGQUIT, bus.interrupted_process)
signal.signal(signal.SIGHUP, bus.interrupted_process)


@bus.handle('image')
def extract_classes(msg):
try:
print("debug1")
json_msg = json.loads(msg.value)
imageStr = base64.b64decode(str(json_msg['image']))
image = Image.open(io.BytesIO(imageStr))
filename = json_msg['imageName']
image_id = json_msg['imageId']
albumID = json_msg['albumId']
print("newdbug1")
detections = get_predictions(image)
print(detections)
class_tags = {"albumid":albumID, "imageid": image_id, "classLabels": detections}
mongo.db.classtags.insert_one(class_tags)
except Exception as e:
print(e)


@app.route('/image/fetch/all', methods=["GET"])
def autofill_data():
"""
Image service fetches the extracted meta data information from the image
"""

try:
albumid = request.args.get('albumid')
album_details = mongo.db.metadata.find({"albumid": albumid})
labels = album_details.distinct("classLabels")
metadata = {"classLabels":labels}
return jsonify(metadata), 200
except Exception as e:
return "Not Found", 404




@app.route('/image/class/fetch', methods=["GET"])
def retrieve_classlabels():
"""
This function fetches the extracted class labels information from the db

@params - image id
@return - the class tags
"""

try:
image_id = request.args.get('id')
tags = mongo.db.classtags.find_one_or_404({"id":image_id})
#to preserve JSON serializability
del tags['_id']
return jsonify(tags), 200
except Exception as e:
return "Not Found", 404



if __name__ == '__main__':
bus.run()
listen_kill_server()
app.run(port=10001, debug=True)



1 change: 1 addition & 0 deletions scrapbook-classifier-service/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
MONGO_URI = "mongodb://localhost:27017/scrapbookClassifierService"
3 changes: 3 additions & 0 deletions scrapbook-classifier-service/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Flask==1.1.2
Flask_Cors==3.0.10
imageai==2.1.6
16 changes: 16 additions & 0 deletions scrapbook-classifier-service/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from imageai.Classification import ImageClassification
import os

def get_predictions(image):
try:
execution_path = os.getcwd()
prediction = ImageClassification()
prediction.setModelTypeAsResNet50()
prediction.setModelPath(os.path.join(execution_path, "resnet50_imagenet_tf.2.0.h5"))
prediction.loadModel()
predictions, probabilities = prediction.classifyImage(image, result_count=3, input_type="array" )

return predictions
except Exception as e:
print(e)
return {}
138 changes: 138 additions & 0 deletions scrapbook-metadata-service/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
.idea/
# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/
14 changes: 14 additions & 0 deletions scrapbook-metadata-service/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
## Dependency

- Python3 +

### How to run


- `python3 -m venv env`

- `source env/bin/activate`

- `pip install -r requirements.txt`

- `python app.py`
113 changes: 113 additions & 0 deletions scrapbook-metadata-service/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
from flask import Flask, request, jsonify
from config import MONGO_URI
from flask_cors import CORS
from flask_pymongo import PyMongo
from PIL import Image
import io
import base64
import sys
import json
from threading import Event
import signal
from PIL.ExifTags import TAGS
from flask_kafka import FlaskKafka

app = Flask(__name__)
CORS(app)
app.config["MONGO_URI"] = MONGO_URI
mongo = PyMongo(app)

INTERRUPT_EVENT = Event()

bus = FlaskKafka(INTERRUPT_EVENT,
bootstrap_servers=",".join(["localhost:9092"]),
group_id="consumer-grp-id"
)


def listen_kill_server():
signal.signal(signal.SIGTERM, bus.interrupted_process)
signal.signal(signal.SIGINT, bus.interrupted_process)
signal.signal(signal.SIGQUIT, bus.interrupted_process)
signal.signal(signal.SIGHUP, bus.interrupted_process)


@bus.handle('image')
def extract_metadata(msg):
json_msg = json.loads(msg.value)
imageStr = base64.b64decode(str(json_msg['image']))
image = Image.open(io.BytesIO(imageStr))
filename = json_msg['imageName']
image_id = json_msg['imageId']
albumID = json_msg['albumId']
exif = {}
try:
if filename[-4:] != ".png":
data_chunk = image._getexif()
if data_chunk != None:
for tag, value in data_chunk.items():
if tag in TAGS and TAGS[tag] in ("Make", "FocalLength", "ApertureValue", "ISOSpeedRatings", "GPSInfo"):
if TAGS[tag] == "Make":
try:
exif["Camera"] = str(value) + " " + str(data_chunk[272])
except:
pass
else:
exif[TAGS[tag]] = str(value)
else:
try:
image.load() # Needed only for .png EXIF data
exifData = image.info
for key in exifData:
if '.' in key:
continue
if key in ("Make", "FocalLength", "ApertureValue", "ISOSpeedRatings", "GPSInfo"):
exif[str(key)] = str(exifData[key])
except Exception as ex:
print(ex)
meta_data = {"id": image_id, "albumid": albumID }
mongo.db.metadata.insert_one({**meta_data, **exif})

except Exception as e:
print(e)



@app.route('/metadata/fetch', methods=["GET"])
def retrieve_metadata():
"""
Image service fetches the extracted meta data information from the image
"""

try:
image_id = request.args.get('id')
tags = mongo.db.metadata.find_one_or_404({"id": image_id})
del tags['_id']
return jsonify(tags), 200
except Exception as e:
return "Not Found", 404


@app.route('/metadata/fetch/all', methods=["GET"])
def retrieve_autofill():
"""
Image service fetches the extracted meta data information from the image
"""

try:
albumid = request.args.get('albumid')
album_details = mongo.db.metadata.find({"albumid": albumid})
iso = album_details.distinct("ISOSpeedRatings")
aperture = album_details.distinct("ApertureValue")
focal = album_details.distinct("FocalLength")
camera = album_details.distinct("Camera")
gps = album_details.distinct("GPSInfo")
metadata = {"ISO":iso, "Aperture":aperture, "FocalLength":focal, "Camera": camera, "GPS": gps}
return jsonify(metadata), 200
except Exception as e:
return "Not Found", 404

if __name__ == '__main__':
bus.run()
listen_kill_server()
app.run(port = 12000, debug=True)
Loading