From f6279b074be462f3659267dbf0914a525a3ba87e Mon Sep 17 00:00:00 2001
From: Anders Langlie <andeslan@stud.ntnu.no>
Date: Wed, 3 Mar 2021 09:52:49 +0100
Subject: [PATCH] Included sex_identification. Currently run from seperate
 file.

---
 .gitignore                                |  3 +-
 algorithm/train_src/isolated_sex_ident.py |  3 ++
 algorithm/train_src/sex_identification.py | 50 +++++++++++++++++++++++
 algorithm/train_src/test_straightening.py |  3 +-
 run.py                                    |  1 -
 5 files changed, 56 insertions(+), 4 deletions(-)
 create mode 100644 algorithm/train_src/isolated_sex_ident.py
 create mode 100644 algorithm/train_src/sex_identification.py

diff --git a/.gitignore b/.gitignore
index c12b113..e25c1ed 100644
--- a/.gitignore
+++ b/.gitignore
@@ -5,4 +5,5 @@ __pycache__
 algorithm/train_src/dlc_model
 img_analyze/*.png
 env
-temp_images
\ No newline at end of file
+temp_images
+algorithm/train_src/imageai_model
\ No newline at end of file
diff --git a/algorithm/train_src/isolated_sex_ident.py b/algorithm/train_src/isolated_sex_ident.py
new file mode 100644
index 0000000..19f4e2b
--- /dev/null
+++ b/algorithm/train_src/isolated_sex_ident.py
@@ -0,0 +1,3 @@
+import sex_identification
+
+print(sex_identification.identify("E:/PycharmProjects/salamander-api/img_analyze/female.png"))
\ No newline at end of file
diff --git a/algorithm/train_src/sex_identification.py b/algorithm/train_src/sex_identification.py
new file mode 100644
index 0000000..708a4ba
--- /dev/null
+++ b/algorithm/train_src/sex_identification.py
@@ -0,0 +1,50 @@
+from imageai.Detection.Custom import DetectionModelTrainer, CustomObjectDetection
+import os
+import tensorflow as tf
+
+
+def identify(image):
+    config = tf.compat.v1.ConfigProto()
+    config.gpu_options.allow_growth = True
+    session = tf.compat.v1.Session(config=config)
+
+    root_path = "../../../salamander-api"
+    os_directory_path = os.path.abspath(root_path)
+
+    image_detector = CustomObjectDetection()
+    image_detector.setModelTypeAsYOLOv3()
+    image_detector.setModelPath(os.path.join(os_directory_path,
+                                             "algorithm/train_src/imageai_model/models/detection_model-ex-012--loss-0018.483.h5"))
+    image_detector.setJsonPath(
+        os.path.join(os_directory_path, "algorithm/train_src/imageai_model/json/detection_config.json"))
+    image_detector.loadModel()
+
+    predictions = image_detector.detectObjectsFromImage(input_image=image,
+                                                        output_image_path=os.path.join(os_directory_path,
+                                                                                       "img_analyze/detected_salamander.png"),
+                                                        minimum_percentage_probability=40)
+    # winner = {"name": "male", "percentage_probability": 0}
+    # for prediction in predictions:
+    #     if prediction['percentage_probability'] > winner['percentage_probability']:
+    #         winner['percentage_probability'] = prediction['percentage_probability']
+    #         winner['name'] = prediction['name']
+
+    largest_p = max(predictions, key=lambda x: x['percentage_probability'])['name']
+    return largest_p
+
+
+def train():
+    config = tf.compat.v1.ConfigProto()
+    config.gpu_options.allow_growth = True
+    session = tf.compat.v1.Session(config=config)
+
+    directory_path = "../../../salamander-api/img_analyze"
+    os_directory_path = os.path.abspath(directory_path)
+
+    trainer = DetectionModelTrainer()
+    trainer.setModelTypeAsYOLOv3()
+    trainer.setDataDirectory(data_directory=os.path.join(os_directory_path, "algorithm/train_src/imageai_model"))
+    trainer.setTrainConfig(object_names_array=["male", "female"], batch_size=8, num_experiments=200)
+    # In the above,when training for detecting multiple objects,
+    # set object_names_array=["object1", "object2", "object3",..."objectz"]
+    trainer.trainModel()
diff --git a/algorithm/train_src/test_straightening.py b/algorithm/train_src/test_straightening.py
index ca88690..1a8f637 100644
--- a/algorithm/train_src/test_straightening.py
+++ b/algorithm/train_src/test_straightening.py
@@ -6,7 +6,7 @@ import matplotlib.pyplot as plt
 
 config_path= "dlc_model/config.yaml"
 directory_path= "../../../salamander-api/img_analyze"
-image_to_predict = "img40.png"
+image_to_predict = "female.png"
 
 config =  os.path.abspath(config_path)
 os_directory_path =  os.path.abspath(directory_path)
@@ -14,7 +14,6 @@ image_type = image_to_predict[-4:]
 img_directory = os_directory_path + '\\' + image_to_predict
 smallpath = os_directory_path + '/' + 'small' + image_to_predict
 
-
 # reading image:
 img = cv2.imread(img_directory)
 # You will have to convert the color if you use OpenCV.
diff --git a/run.py b/run.py
index e513e14..6e51c1a 100644
--- a/run.py
+++ b/run.py
@@ -1,5 +1,4 @@
 from api import app
 
-
 if __name__ == "__main__":
     app.run(debug=True, host='0.0.0.0')
-- 
GitLab