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

Practice num 2 #9

Open
wants to merge 3 commits into
base: main
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
22 changes: 17 additions & 5 deletions practice/1_OpenCV/cat_passport.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,38 @@
import argparse
import cv2
import sys


def make_cat_passport_image(input_image_path, haar_model_path):

# Read image

img = cv2.imread(input_image_path)
# Convert image to grayscale

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Normalize image intensity

gray = cv2.equalizeHist(gray)
# Resize image

# Detect cat faces using Haar Cascade
detector = cv2.CascadeClassifier(haar_model_path)
rects = detector.detectMultiScale(img, scaleFactor=1.1, minNeighbors=5,
minSize=(75, 75))

# Draw bounding box
for (i, (x, y, w, h)) in enumerate(rects):
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 2)
cv2.putText(img, f"Cat #{i + 1}", (x, y - 10),
cv2.FONT_HERSHEY_SIMPLEX, 0.55, (0, 0, 255), 2)

# Display result image

cv2.imshow("window_name", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
# Crop image

x, y, w, h = rects[0]
image = img[y:y + h, x:x + w]
# Save result image to file
cv2.imwrite('out.jpg', img)

return

Expand Down
Binary file added practice/1_OpenCV/out.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
38 changes: 26 additions & 12 deletions practice/2_Classification/doge_classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,44 @@ class InferenceEngineClassifier:
def __init__(self, model_path, device='CPU', classes_path=None):

# Add code for Inference Engine initialization
self.core = Core()

# Add code for model loading
self.model = self.core.read_model(model=model_path)

# Add code for classes names loading
self.exec_model = self.core.compile_model(model=self.model,
device_name=device)
if classes_path:
self.classes = [line.rstrip('\n') for line in open(classes_path, encoding='UTF-8')]

return

def get_top(self, prob, topN=1):
result = []

result = prob
result = np.squeeze(result)
result = np.argsort(result)[-topN:][::-1]
# Add code for getting top predictions

return result

def _prepare_image(self, image, h, w):

# Add code for image preprocessing

image = cv2.resize(image, (w, h))
image = image.transpose((2, 0, 1))
image = np.expand_dims(image, axis = 0)
return image

def classify(self, image):
probabilities = None

result = None
input_layer = self.exec_model.input(0)
output_layer = self.exec_model.output(0)
n, c, h, w = input_layer.shape
image = self._prepare_image(image, h, w)
# Add code for image classification using Inference Engine

return probabilities
result = self.exec_model([image])[output_layer]
return result


def build_argparser():
Expand All @@ -71,15 +83,17 @@ def main():
log.info("Start IE classification sample")

# Create InferenceEngineClassifier object

test = InferenceEngineClassifier(model_path=args.model, classes_path=args.classes)
# Read image

image = cv2.imread(args.input)
# Classify image

classfer = test.classify(image)
# Get top 5 predictions

predictions = test.get_top(classfer, 5)
classfer = np.squeeze(classfer)
# print result

predictions = [str(test.classes[predictions[i]]) + ', ' + str(classfer[predictions[i]]) for i in range(5)]
log.info("Predictions: " + str(predictions))
return


Expand Down
Binary file not shown.
Loading