Skip to content
This repository was archived by the owner on Nov 25, 2023. It is now read-only.

Classify image client script #1

Merged
merged 3 commits into from
May 15, 2023
Merged
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
90 changes: 81 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,51 @@ For problem description and requirements, see [Project Statement](project-statem

```
.
├── font/
├── notebooks/
│ ├── images/
│ ├── Step1.EDA.ipynb
│ ├── Step2.DataPrep.ipynb
│ └── Step3.Classifier-BaselineModel.ipynb
├── scraping/
│ └── scrape.py
├── classify.py
├── recommend.py
├── requirements.txt
├── .gitignore
├── project-statement.md
├── README.md
└── LICENSE
```

1. `notebooks/`: This folder contains all Jupyter Notebooks for this project and their exported plots in `notebooks/images/`.
2. `scrape/`: This folder contains a scraping script to get more images from the internet for our dataset. All downloaded images will also be in this folder.
3. `requirements.txt`: Text file for `pip` installation of necessary packages for development environment.
4. `.gitignore`: This file contains ignore VCS ignore rules.
5. `README.md`: A text file containing useful reference information about this project, including how to run the algorithm.
6. `LICENSE`: MIT
1. `font/`: This folder contains the fonts used in our client script's GUI mode.
2. `notebooks/`: This folder contains all Jupyter Notebooks for this project and their exported plots in `notebooks/images/`.
3. `scrape/`: This folder contains a scraping script to get more images from the internet for our dataset. All downloaded images will also be in this folder.
4. `classify.py`: Client script for classifying flower images using trained models.
5. `recommend.py`: Client script for recommending flower images using trained models.
6. `requirements.txt`: Text file for `pip` installation of necessary packages for development environment.
7. `.gitignore`: This file contains ignore VCS ignore rules.
8. `README.md`: A text file containing useful reference information about this project, including how to run the algorithm.
9. `LICENSE`: MIT


Additionally, these folders will be created during dataset fetching and model training:

1. `data/`: This folder contains out datasets.
2. `logs/`: This folder contains training logs exported from training our models.
2. `log/`: This folder contains training logs exported from training our models.
3. `models/`: This folder contains trained models exported after training.

---


## Getting Started 🚀

Clone this repository:

```bash
git clone https://github.com/miketvo/rmit2023a-cosc2753-assignment2.git
```


### Development Environment

Expand All @@ -54,6 +66,8 @@ pip install -r requirements.txt

Refer to [requirements.txt](requirements.txt) for package dependencies and their versions.

<span style="color:gold">**NOTE:**</span> It is recommended that you use a Python virtual environment to avoid conflict with your global packages, and to keep your global Python installation clean. This is because we require specific versions of Numpy, Tensorflow and Keras in our code to maintain backward compatibility and compatibility between trained models and client code.


### Download Dataset

Expand Down Expand Up @@ -91,10 +105,68 @@ Skip this step if you just want to use the pre-trained model packages available

If you are using one of our pre-trained model packages, download your desired version from [Packages](https://github.com/miketvo?tab=packages&repo_name=rmit2023a-cosc2753-assignment2) (.zip archives) and extract its contents into this project's root directory using your preferred zip program.

On your terminal, make sure that you have the environment activated for the client script to have access to all required packages:

- Python Virtualenv:

```bash
./venv/Scripts/activate
```

- Conda:

```bash
conda activate ./envs
```

#### Classifying Flower Images

To be written.
Use the `classify.py` client script. Its syntax is as follows:

```text
python ./classify.py [-h] -f FILE -m MODEL [-g] [-v {0,1,2}]

options:
-h, --help show this help message and exit
-f FILE, --file FILE the image to be classified
-c CLASSIFIER, --classifier CLASSIFIER the machine learning model used for classification
-g, --gui show classification result using GUI
-v {0,1,2}, --verbose-level {0,1,2} verbose level, default: 0
```

Example use:

```text
$ python ./classify.py -f path/to/your/your/image.png -m ./models/clf-baseline -v=1
Image image.png is classified as "Chrysanthemum" (model: "clf-baseline")
```

It also has a rudimentary GUI mode using your system's default GUI image viewer, which will display the image with a caption of what flower type it is classified as:

```bash
python ./classify.py --gui -f path/to/your/your/image.png -m ./models/clf-baseline
```


#### Recommending Flower Images

To be written.
Use the `recommend.py` client script. Its syntax is as follows:

```text
python ./recommend.py [-h] -f FILE -r RECOMMENDER -c CLASSIFIER [-n NUM]

options:
-h, --help show this help message and exit
-f FILE, --file FILE reference image
-r RECOMMENDER, --recommender RECOMMENDER the machine learning model used for recommendation
-c CLASSIFIER, --classifier CLASSIFIER the machine learning model used for classification
-n NUM, --num NUM number of recommendations, default: 10
```

Example:

```bash
python ./recommend.py --gui -f path/to/your/your/image.png -r ./models/recommender -c ./models/clf-baseline
```

When executed, the code above will display (using your system's default GUI image viewer) 10 similar flower images of the same type, based on your reference image.
56 changes: 56 additions & 0 deletions classify.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import os
import argparse
from PIL import Image, ImageDraw, ImageFont
import tensorflow as tf
from keras.engine.training import Model

from utils.glob import TARGET_IMG_SIZE
from utils.glob import CLASS_LABELS
import utils.data_manip as manip


def classify(image_path: str, model_path: str, verbose: bool = False) -> tuple:
im_original = Image.open(image_path)
im_processed = manip.remove_transparency(im_original)
im_processed = manip.resize_crop(im_processed, TARGET_IMG_SIZE, TARGET_IMG_SIZE)
im_processed = manip.normalize_pixels(im_processed)
im_processed = tf.expand_dims(im_processed, axis=0)

model: Model = tf.keras.models.load_model(model_path)
pred = model.predict(im_processed, verbose=1 if verbose else 0)

pred_class_idx = tf.argmax(pred, axis=1).numpy()[0]
pred_class_label = CLASS_LABELS[pred_class_idx]

return im_original, pred_class_label


if __name__ == '__main__':
ap = argparse.ArgumentParser()
ap.add_argument('-f', '--file', required=True, help='the image to be classified')
ap.add_argument('-c', '--classifier', required=True, help='the machine learning model used for classification')
ap.add_argument('-g', '--gui', action='store_true', help='show classification result using GUI')
ap.add_argument('-v', '--verbose-level', required=False, choices=['0', '1', '2'], default='0', help="verbose level, default: 0")
args = vars(ap.parse_args())
verbose_level = int(args['verbose_level'])

img = os.path.abspath(args['file'])
clf = os.path.abspath(args['classifier'])
image, predicted_label = classify(img, clf, False if verbose_level < 1 else True)

if args['gui']:
canvas = ImageDraw.Draw(image)
canvas.text(
(10, 10),
predicted_label,
fill=(255, 0, 0),
stroke_fill=(0, 0, 0),
stroke_width=2,
font=ImageFont.truetype(os.path.abspath('font/OpenSans-Regular.ttf'), size=24)
)
image.show()
else:
if verbose_level == 0:
print(predicted_label)
else:
print(f'Image {os.path.basename(img)} is classified as "{predicted_label}" (model: "{os.path.basename(clf)}")')
93 changes: 93 additions & 0 deletions font/OFL.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
Copyright 2020 The Open Sans Project Authors (https://github.com/googlefonts/opensans)

This Font Software is licensed under the SIL Open Font License, Version 1.1.
This license is copied below, and is also available with a FAQ at:
http://scripts.sil.org/OFL


-----------------------------------------------------------
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
-----------------------------------------------------------

PREAMBLE
The goals of the Open Font License (OFL) are to stimulate worldwide
development of collaborative font projects, to support the font creation
efforts of academic and linguistic communities, and to provide a free and
open framework in which fonts may be shared and improved in partnership
with others.

The OFL allows the licensed fonts to be used, studied, modified and
redistributed freely as long as they are not sold by themselves. The
fonts, including any derivative works, can be bundled, embedded,
redistributed and/or sold with any software provided that any reserved
names are not used by derivative works. The fonts and derivatives,
however, cannot be released under any other type of license. The
requirement for fonts to remain under this license does not apply
to any document created using the fonts or their derivatives.

DEFINITIONS
"Font Software" refers to the set of files released by the Copyright
Holder(s) under this license and clearly marked as such. This may
include source files, build scripts and documentation.

"Reserved Font Name" refers to any names specified as such after the
copyright statement(s).

"Original Version" refers to the collection of Font Software components as
distributed by the Copyright Holder(s).

"Modified Version" refers to any derivative made by adding to, deleting,
or substituting -- in part or in whole -- any of the components of the
Original Version, by changing formats or by porting the Font Software to a
new environment.

"Author" refers to any designer, engineer, programmer, technical
writer or other person who contributed to the Font Software.

PERMISSION & CONDITIONS
Permission is hereby granted, free of charge, to any person obtaining
a copy of the Font Software, to use, study, copy, merge, embed, modify,
redistribute, and sell modified and unmodified copies of the Font
Software, subject to the following conditions:

1) Neither the Font Software nor any of its individual components,
in Original or Modified Versions, may be sold by itself.

2) Original or Modified Versions of the Font Software may be bundled,
redistributed and/or sold with any software, provided that each copy
contains the above copyright notice and this license. These can be
included either as stand-alone text files, human-readable headers or
in the appropriate machine-readable metadata fields within text or
binary files as long as those fields can be easily viewed by the user.

3) No Modified Version of the Font Software may use the Reserved Font
Name(s) unless explicit written permission is granted by the corresponding
Copyright Holder. This restriction only applies to the primary font name as
presented to the users.

4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
Software shall not be used to promote, endorse or advertise any
Modified Version, except to acknowledge the contribution(s) of the
Copyright Holder(s) and the Author(s) or with their explicit written
permission.

5) The Font Software, modified or unmodified, in part or in whole,
must be distributed entirely under this license, and must not be
distributed under any other license. The requirement for fonts to
remain under this license does not apply to any document created
using the Font Software.

TERMINATION
This license becomes null and void if any of the above conditions are
not met.

DISCLAIMER
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
OTHER DEALINGS IN THE FONT SOFTWARE.
Binary file added font/OpenSans-Regular.ttf
Binary file not shown.
Empty file removed main.py
Empty file.
Loading