├── .gitignore ├── LICENSE ├── README.md ├── example.jpg ├── example.pkl ├── example_with_box.jpg ├── extras.sh ├── predictor.py └── requirements.txt /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | pip-wheel-metadata/ 24 | share/python-wheels/ 25 | *.egg-info/ 26 | .installed.cfg 27 | *.egg 28 | MANIFEST 29 | 30 | # PyInstaller 31 | # Usually these files are written by a python script from a template 32 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 33 | *.manifest 34 | *.spec 35 | 36 | # Installer logs 37 | pip-log.txt 38 | pip-delete-this-directory.txt 39 | 40 | # Unit test / coverage reports 41 | htmlcov/ 42 | .tox/ 43 | .nox/ 44 | .coverage 45 | .coverage.* 46 | .cache 47 | nosetests.xml 48 | coverage.xml 49 | *.cover 50 | *.py,cover 51 | .hypothesis/ 52 | .pytest_cache/ 53 | 54 | # Translations 55 | *.mo 56 | *.pot 57 | 58 | # Django stuff: 59 | *.log 60 | local_settings.py 61 | db.sqlite3 62 | db.sqlite3-journal 63 | 64 | # Flask stuff: 65 | instance/ 66 | .webassets-cache 67 | 68 | # Scrapy stuff: 69 | .scrapy 70 | 71 | # Sphinx documentation 72 | docs/_build/ 73 | 74 | # PyBuilder 75 | target/ 76 | 77 | # Jupyter Notebook 78 | .ipynb_checkpoints 79 | 80 | # IPython 81 | profile_default/ 82 | ipython_config.py 83 | 84 | # pyenv 85 | .python-version 86 | 87 | # pipenv 88 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 89 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 90 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 91 | # install all needed dependencies. 92 | #Pipfile.lock 93 | 94 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow 95 | __pypackages__/ 96 | 97 | # Celery stuff 98 | celerybeat-schedule 99 | celerybeat.pid 100 | 101 | # SageMath parsed files 102 | *.sage.py 103 | 104 | # Environments 105 | .env 106 | .venv 107 | env/ 108 | venv/ 109 | ENV/ 110 | env.bak/ 111 | venv.bak/ 112 | 113 | # Spyder project settings 114 | .spyderproject 115 | .spyproject 116 | 117 | # Rope project settings 118 | .ropeproject 119 | 120 | # mkdocs documentation 121 | /site 122 | 123 | # mypy 124 | .mypy_cache/ 125 | .dmypy.json 126 | dmypy.json 127 | 128 | # Pyre type checker 129 | .pyre/ 130 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 notAI.tech 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # LogoDet 2 | Neural Nets for logo detection 3 | 4 | # Using the free API 5 | - Signup [here](https://tech.notai.tech/signup/) for free API key. 6 | - Maximum 16 requests per hour allowed. 7 | - Each request can have maximum of 4 images to be processed. (i.e: maximum of 64 images can be processed per hour.) 8 | 9 | **Example usage of the free API** 10 | 11 | ```bash 12 | wget https://github.com/notAI-tech/fastDeploy/blob/master/cli/fastDeploy-file_client.py 13 | chmod +x fastDeploy-file_client.py 14 | 15 | # with webhook example 16 | ./fastDeploy-file_client.py --file ../../LogoDet/example.jpg --url "https://tech.notai.tech/logodet/async?api_key=YOUR_API_KEY" --webhook https://fastdeploy.requestcatcher.com 17 | 18 | # without webhook example 19 | ./fastDeploy-file_client.py --file ../../LogoDet/example.jpg --url "https://tech.notai.tech/logodet/async?api_key=YOUR_API_KEY" 20 | 21 | ``` 22 | 23 | # As a service (recommended) 24 | ```bash 25 | # Start service 26 | (sudo) docker run --name logodet -p 8080:8080 notaitech/fastdeploy-recipe:logodet 27 | 28 | # Running predictions 29 | wget https://github.com/notAI-tech/fastDeploy/blob/master/cli/fastDeploy-file_client.py 30 | chmod +x fastDeploy-file_client.py 31 | 32 | # Single input 33 | ./fastDeploy-file_client.py --file PATH_TO_YOUR_IMAGE 34 | 35 | # Client side batching 36 | ./fastDeploy-file_client.py --dir PATH_TO_FOLDER --ext jpg 37 | ``` 38 | 39 | # As a Python module 40 | ```bash 41 | git clone https://github.com/notAI-tech/LogoDet/ 42 | cd LogoDet 43 | pip install -r requirements.txt 44 | ``` 45 | 46 | ```python 47 | # Weights will be auto-downloaded 48 | from predictor import predictor 49 | 50 | image_paths = [image_1.jpg, image_2.png, ...] 51 | 52 | predictions = predictor(image_paths) 53 | 54 | ``` 55 | 56 | **Notes**: 57 | 58 | 1. Although we were able to generate/ gather data for more than 2000 unique company logos, the current release is limited to [these 292 logos](https://github.com/notAI-tech/LogoDet/releases/download/292_classes_v1/classes.txt) due to hardware constraints. 59 | 2. We recommend using LogoDet via fastDeploy, as it doesn't require the user to install dependencies separately. 60 | -------------------------------------------------------------------------------- /example.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/notAI-tech/LogoDet/57e85155c3bda1847ca9ef2db8ac3dda2a867497/example.jpg -------------------------------------------------------------------------------- /example.pkl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/notAI-tech/LogoDet/57e85155c3bda1847ca9ef2db8ac3dda2a867497/example.pkl -------------------------------------------------------------------------------- /example_with_box.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/notAI-tech/LogoDet/57e85155c3bda1847ca9ef2db8ac3dda2a867497/example_with_box.jpg -------------------------------------------------------------------------------- /extras.sh: -------------------------------------------------------------------------------- 1 | apt-get -y install libglib2.0-0 2 | apt-get install -y libsm6 libxext6 3 | apt-get install -y libxrender1 -------------------------------------------------------------------------------- /predictor.py: -------------------------------------------------------------------------------- 1 | import os 2 | import keras 3 | import pydload 4 | from keras_retinanet import models 5 | from keras_retinanet.utils.image import read_image_bgr, preprocess_image, resize_image 6 | from keras_retinanet.utils.visualization import draw_box, draw_caption 7 | from keras_retinanet.utils.colors import label_color 8 | 9 | import cv2 10 | import numpy as np 11 | 12 | WEIGHTS_URL = 'https://github.com/notAi-tech/LogoDet/releases/download/292_classes_v1/weights' 13 | CLASSES_URL = 'https://github.com/notAi-tech/LogoDet/releases/download/292_classes_v1/classes' 14 | 15 | home = os.path.expanduser("~") 16 | model_folder = os.path.join(home, '.LogoDet/') 17 | if not os.path.exists(model_folder): 18 | os.mkdir(model_folder) 19 | 20 | model_path = os.path.join(model_folder, 'weights') 21 | 22 | if not os.path.exists(model_path): 23 | print('Downloading the checkpoint to', model_path) 24 | pydload.dload(WEIGHTS_URL, save_to_path=model_path, max_time=None) 25 | 26 | classes_path = os.path.join(model_folder, 'classes') 27 | 28 | if not os.path.exists(classes_path): 29 | print('Downloading the class list to', classes_path) 30 | pydload.dload(CLASSES_URL, save_to_path=classes_path, max_time=None) 31 | 32 | detection_model = models.load_model(model_path, backbone_name='resnet50') 33 | classes = open(classes_path).readlines() 34 | classes = [i.strip() for i in classes if i.strip()] 35 | 36 | def detect_single(img_path, min_prob=0.4): 37 | image = read_image_bgr(img_path) 38 | image = preprocess_image(image) 39 | image, scale = resize_image(image) 40 | boxes, scores, labels = detection_model.predict_on_batch(np.expand_dims(image, axis=0)) 41 | boxes /= scale 42 | processed_boxes = [] 43 | for box, score, label in zip(boxes[0], scores[0], labels[0]): 44 | if score < min_prob: 45 | continue 46 | box = box.astype(int).tolist() 47 | label = classes[label] 48 | processed_boxes.append({'box': box, 'score': float(score), 'label': label}) 49 | 50 | return processed_boxes 51 | 52 | 53 | def detect_batch(): 54 | # TODO for videos 55 | pass 56 | 57 | def predictor(image_paths=[], batch_size=1): 58 | results = [] 59 | for image_path in image_paths: 60 | try: 61 | results.append(detect_single(image_path)) 62 | except Exception as ex: 63 | results.append([f'Failed with exception: {ex}']) 64 | 65 | return results 66 | 67 | if __name__ == '__main__': 68 | import json 69 | import pickle 70 | import base64 71 | 72 | example = ["example.jpg"] 73 | 74 | print(json.dumps(predictor(example))) 75 | 76 | example = { 77 | file_name: base64.b64encode(open(file_name, "rb").read()).decode("utf-8") 78 | for file_name in example 79 | } 80 | 81 | pickle.dump(example, open("example.pkl", "wb"), protocol=2) 82 | 83 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | https://github.com/notAI-tech/LogoDet/releases/download/292_classes_v1/keras-retinanet-20-06-2020.zip 2 | opencv-python-headless==4.2.0.34 3 | pydload 4 | keras==2.3.1 5 | tensorflow==2.2.0 --------------------------------------------------------------------------------