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

finalizing tests #28

Closed
wants to merge 3 commits into from
Closed
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
2 changes: 1 addition & 1 deletion .github/workflows/ml.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ jobs:
cd machine-learning-client
- name: Run tests
run: |
pipenv run pytest
pipenv run pytest ./*.py
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ numpy = "*"
pillow = "*"
jinja2 = "*"
coverage = "*"
pytest-cov = "*"


[dev-packages]
Expand Down
14 changes: 13 additions & 1 deletion Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion machine-learning-client/Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ black = "*"
pytest = "*"
coverage = "*"
pillow = "*"

mongomock = "*"

[dev-packages]

Expand Down
16 changes: 15 additions & 1 deletion machine-learning-client/Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 9 additions & 7 deletions machine-learning-client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
Pymongo: connect to MongoDB
Deepface: to detect emotions in images
Numpy: numerical operations
...
"""
import base64
from PIL import Image
Expand All @@ -16,10 +15,11 @@
import pymongo
import numpy as np


def get_emotion(image):
"""
Method for detecting emotions in an image containing humans,
using the deepface library. Works with images containing multiple faces.
using the deepface library. Works with images containing multiple faces, returns sentiment for majority.
"""
try:
bin_data = base64.b64decode(image)
Expand All @@ -29,8 +29,11 @@ def get_emotion(image):
emotions = obj[0]['emotion']
return emotions
except Exception as e:
return f"ERROR: Couldn't detect a face for emotion. {e}"
return f"ERROR: {e}"

def run_connection(option):
"arranged for utility"
connect_db(option)

def connect_db(option):
"""
Expand All @@ -45,6 +48,8 @@ def connect_db(option):
pass
x = temp.find_one()
emotion_message = get_emotion(x["photo"])
if not emotion_message:
return "No emotions found"
if temp.find_one():
temp.update_one(
{
Expand All @@ -56,11 +61,8 @@ def connect_db(option):
}
},
)
#print("REACHED HERE!")
time.sleep(1)
client.close()


if __name__ == "__main__":
#im = cv2.imread('./test0.png')
connect_db(True)
run_connection(True)
383 changes: 0 additions & 383 deletions machine-learning-client/myenv/README.md

This file was deleted.

8 changes: 0 additions & 8 deletions machine-learning-client/myenv/bin/deepface

This file was deleted.

8 changes: 0 additions & 8 deletions machine-learning-client/myenv/bin/flask

This file was deleted.

8 changes: 0 additions & 8 deletions machine-learning-client/myenv/bin/gdown

This file was deleted.

8 changes: 0 additions & 8 deletions machine-learning-client/myenv/bin/gunicorn

This file was deleted.

3 changes: 0 additions & 3 deletions machine-learning-client/myenv/package_info.json

This file was deleted.

13 changes: 0 additions & 13 deletions machine-learning-client/myenv/requirements.txt

This file was deleted.

87 changes: 0 additions & 87 deletions machine-learning-client/requirements.txt

This file was deleted.

54 changes: 49 additions & 5 deletions machine-learning-client/test_client.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,56 @@
from unittest.mock import patch, MagicMock
import pytest, os
from client import connect_db, get_emotion
from unittest.mock import patch
import mongomock
import base64
import io, cv2, os, pytest
from client import connect_db, get_emotion, run_connection, DeepFace
from PIL import Image
from pymongo import MongoClient
import pytest
import time

os.environ["IMAGEIO_FFMPEG_EXE"] = "/usr/bin/ffmpeg"

def test_bad_connection():
"""Testing loop exit for connection method"""
# pylint: disable=unused-variable
#pylint: disable=unused-variable
with patch("pymongo.MongoClient"):
connect_db(False)

def test_connect_option_true():
"""When the flag for running is true"""
with patch("client.connect_db") as mock_connection:
run_connection(True)
mock_connection.assert_called_once_with(True)


def test_connect_option_false():
"""run_connection method:false"""
with patch("client.connect_db") as mock_connection:
run_connection(False)
mock_connection.assert_called_once_with(False)


def test_get_emotion_invalid_input():
with open("test1.png", "rb") as file:
image = file.read()
assert get_emotion("test")[:33] == "ERROR: cannot identify image file"
assert get_emotion(image)[:33] == "ERROR: cannot identify image file"

def test_get_emotion_success():
"""Testing connection"""
# pylint: disable=unused-variable
with patch("pymongo.MongoClient") as mock_client:
with patch("pymongo.collection.Collection") as mock_collection:
mock_collection.find_one.return_value = {
"_id": "",
"photo": base64.b64encode(cv2.imread("test1.png")).decode("utf-8"),
}
connect_db(False)

def test_get_emotion_with_image():
"""Testing get_emotion method"""
with open("./test0.png", "rb") as file:
image = file.read()
print(get_emotion(base64.b64encode(image).decode("utf-8")))



Loading