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 3 #6

Open
wants to merge 5 commits into
base: master
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,6 @@ venv.bak/

# mypy
.mypy_cache/

# video
.mp4
Binary file added images/autumn.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/cars.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/city.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/cow.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/cow2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/google.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/people.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/plain.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 39 additions & 0 deletions samples/practice1_anastasia_funkner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import sys
import cv2
import logging as log
import argparse
import os

sys.path.append('..\\src')

from imagefilter import ImageFilter


def build_argparse():
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--input_path', help='input image path', type=str)

return parser


def main():
log.basicConfig(format="[ %(levelname)s ] %(message)s", level=log.INFO, stream=sys.stdout)
log.info("Hello image filtering")
args = build_argparse().parse_args()

image_path = args.input_path
log.info(args.input_path)

image = cv2.imread(image_path)
my_filter = ImageFilter(grey=True, shape=None, crop=True)

cv2.imshow("Image1", image)
cv2.imshow("Image2", my_filter.process_image(image))
cv2.waitKey(0)
cv2.destroyAllWindows()

return


if __name__ == '__main__':
sys.exit(main())
79 changes: 79 additions & 0 deletions samples/practice2_anastasia_funkner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import sys
import cv2
import argparse
import logging as log

sys.path.append('../src')
from ie_detector import InferenceEngineDetector


def build_argparse():
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--image_path', help='input image path', type=str)
parser.add_argument('-v', '--video_path', help='input video path', type=str)

return parser


def main():
log.basicConfig(format="[%(levelname)s] %(message)s", level=log.INFO,
stream=sys.stdout)

args = build_argparse().parse_args()

my_detector = InferenceEngineDetector(
r'C:\UNN_HPC_SCHOOL_2019_ML\samples\intel\pedestrian-and-vehicle-detector-adas-0001\FP32\pedestrian-and-vehicle-detector-adas-0001.bin',
r'C:\UNN_HPC_SCHOOL_2019_ML\samples\intel\pedestrian-and-vehicle-detector-adas-0001\FP32\pedestrian-and-vehicle-detector-adas-0001.xml',
'CPU',
r'C:\Program Files (x86)\IntelSWTools\openvino_2019.3.379\inference_engine\bin\intel64\Release\cpu_extension_avx2.dll',
10,
r'C:\UNN_HPC_SCHOOL_2019_ML\src\names_classes_2',

)

if args.image_path is not None:
image_path = args.image_path

image = cv2.imread(image_path)
cv2.imshow('Frame', my_detector.detect(image, False))

cv2.waitKey(0)
cv2.destroyAllWindows()

if args.video_path is not None:
log.info("Press 'Q' to exit!")
cap = cv2.VideoCapture(args.video_path)
# Create a VideoCapture object and read from input file
# If the input is the camera, pass 0 instead of the video file name
# Check if camera opened successfully
if not cap.isOpened():
print("Error opening video stream or file")

# Read until video is completed
while cap.isOpened():
# Capture frame-by-frame
ret, frame = cap.read()
if ret:

# Display the resulting frame
cv2.imshow('Frame', my_detector.detect(frame, False))

# Press Q on keyboard to exit
if cv2.waitKey(25) & 0xFF == ord('q'):
break

# Break the loop
else:
break

# When everything done, release the video capture object
cap.release()

# Closes all the frames
cv2.destroyAllWindows()
return


if __name__ == '__main__':
sys.exit(main())

101 changes: 101 additions & 0 deletions samples/practice3_anastasia_funkner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import sys
import cv2
import argparse
import logging as log
import time
import numpy as np

sys.path.append('../src')
from ie_detector import InferenceEngineDetector


def build_argparse():
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--image_path', help='input image path', type=str)
parser.add_argument('-v', '--video_path', help='input video path', type=str)

return parser


MODELS = {
'FP16': [r'C:\public\mobilenet-ssd\FP16\mobilenet-ssd.bin',
r'C:\public\mobilenet-ssd\FP16\mobilenet-ssd.xml',
'CPU',
r'C:\Program Files (x86)\IntelSWTools\openvino_2019.3.379\inference_engine\bin\intel64\Release\cpu_extension_avx2.dll',
20,
r'C:\UNN_HPC_SCHOOL_2019_ML\src\names_classes'],
'FP32': [r'C:\public\mobilenet-ssd\FP32\mobilenet-ssd.bin',
r'C:\public\mobilenet-ssd\FP32\mobilenet-ssd.xml',
'CPU',
r'C:\Program Files (x86)\IntelSWTools\openvino_2019.3.379\inference_engine\bin\intel64\Release\cpu_extension_avx2.dll',
20,
r'C:\UNN_HPC_SCHOOL_2019_ML\src\names_classes'],
'I8': [r'C:\public\mobilenet-ssd\FP32\mobilenet-ssd_i8.bin',
r'C:\public\mobilenet-ssd\FP32\mobilenet-ssd_i8.xml',
'CPU',
r'C:\Program Files (x86)\IntelSWTools\openvino_2019.3.379\inference_engine\bin\intel64\Release\cpu_extension_avx2.dll',
20,
r'C:\UNN_HPC_SCHOOL_2019_ML\src\names_classes']
}


def main():
log.basicConfig(format="[%(levelname)s] %(message)s", level=log.INFO,
stream=sys.stdout)

args = build_argparse().parse_args()

# if args.image_path is not None:
# image_path = args.image_path
#
# image = cv2.imread(image_path)
#
# for detector_param in MODELS:
# my_detector = InferenceEngineDetector(*detector_param)
# cv2.imshow('Frame', my_detector.detect(image))
#
# cv2.waitKey(0)
# cv2.destroyAllWindows()

if args.video_path is not None:

for name_model, detector_param in MODELS.items():
my_detector = InferenceEngineDetector(*detector_param)
times = []
t0_total = time.time()
cap = cv2.VideoCapture(args.video_path)

if not cap.isOpened():
print("Error opening video stream or file")

# Read until video is completed
while cap.isOpened():
# Capture frame-by-frame
ret, frame = cap.read()
if ret:
# print(len(times), end='...')
t0 = time.time()
my_detector.detect(frame)
t1 = time.time()
# times.append(t1 - t0)
times.append(1)

else:
log.info('ret False')
break

cap.release()

t1_total = time.time()
# latency = np.median(times)
fps = len(times) / (t1_total - t0_total)
print(name_model)
# print(times)
# print('latency', latency, sep=' : ')
print('fps', fps, sep=' : ')

return


if __name__ == '__main__':
sys.exit(main())
132 changes: 100 additions & 32 deletions src/ie_detector.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,110 @@
"""

Inference engine detector

"""
import cv2
import numpy as np

from matplotlib import cm, colors
from openvino.inference_engine import IENetwork, IECore


def renormalize(n, range2):
delta2 = range2[1] - range2[0]
return int(delta2 * n + range2[0])


def renormalize_coordinates(point, scale_len):
return (renormalize(point[0], (0, scale_len[1])),
renormalize(point[1], (0, scale_len[0])))


def write_conf(conf, point, img, shift=20):
new_point = point[0] + shift, point[1] + shift
cv2.putText(img, 'conf=' + str(round(conf * 100, 2)), new_point,
cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0),
2, cv2.LINE_AA)
return img


class InferenceEngineDetector:
def __init__(self, weightsPath = None, configPath = None,
device = 'CPU', extension = None):
#
# Add your code here
#

def __init__(self, weightsPath=None, configPath=None,
device='CPU', extension=None, class_num=10,
class_names_path=None,
color_map=cm.Set3):
"""
:param weightsPath: Путь до bin-файла модели.
:param configPath:Путь до xml-файла модели.
:param device: Тип устройства, на котором запускаемся (CPU или GPU).
:param extension: Для CPU необходим путь до библиотеки со слоями,
реализации которых нет в MKL-DNN
(C:\Program Files (x86)\IntelSWTools\openvino_2019.3.379\inference_engine\bin\intel64\Release\cpu_extension_avx2.dll).
"""

self.ie = IECore()
if device == 'CPU':
self.ie.add_extension(extension, device)

self.net = IENetwork(model=configPath, weights=weightsPath)

self.exec_net = self.ie.load_network(network=self.net, device_name=device)

self.class_names_dict = dict(enumerate([" "] * class_num))

if class_names_path is not None:
self.class_names_dict = dict(enumerate(open(class_names_path).readlines()))
class_num = len(self.class_names_dict)

self.color_dict = dict(enumerate(map(lambda nums: tuple(int(c * 255) for c in nums),
color_map(np.linspace(0, 1, class_num)))))

return

def draw_detection(self, detections, img):

#
# Add your code here
#

def write_class(self, class_num, point, img, shift=20):
new_point = point[0] + shift, point[1] + shift
cv2.putText(img, self.class_names_dict[class_num].strip(), new_point,
cv2.FONT_HERSHEY_SIMPLEX, 0.6, self.color_dict[class_num],
1, cv2.LINE_AA)
return img

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

#
# Add your code here
#

def draw_detection(self, detections, img, conf_low=0.5):
for det in detections:
if any(det[1:]):
image_id, label, conf, *init_coors = det
if conf < conf_low:
continue
# print(init_coors)
point_1 = renormalize_coordinates(init_coors[:2], img.shape)
point_2 = renormalize_coordinates(init_coors[2:], img.shape)
# print(point_1, point_2)
cv2.rectangle(img, point_1, point_2, self.color_dict[label], 1)
self.write_class(label, point_1, img)

return img

@staticmethod
def _prepare_image(image, h, w):
image = cv2.resize(image, (w, h))
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = image.transpose((2, 0, 1))
image = np.expand_dims(image, axis=0)

return image

def detect(self, image):

#
# Add your code here
#
detection = None

return detection

def detect(self, image, return_detection=True):
input_blob = next(iter(self.net.inputs))
out_blob = next(iter(self.net.outputs))
n, c, h, w = self.net.inputs[input_blob].shape

blob = self._prepare_image(image, h, w)


output = self.exec_net.infer(inputs={input_blob: blob})

output = output[out_blob]

# cv2.imshow("Detections", self.draw_detection(output[0][0], image))
# cv2.waitKey(0)
# cv2.destroyAllWindows()

# !!!!
if return_detection:
return output
else:
return self.draw_detection(output[0][0], image)
Loading