Разработка сетевого сервиса автоматического трекинга человека PTZ IP видеокамерой с использованием TensorFlow

Характеристика разработки алгоритма для связи потоков обработки кадра и управления камерой. Обеспечение стабильности трекинга на протяжении длительного времени. Основные методы компьютерного зрения. Особенность обнаружения объектов на изображении.

Рубрика Программирование, компьютеры и кибернетика
Вид дипломная работа
Язык русский
Дата добавления 28.10.2019
Размер файла 1,1 M

Отправить свою хорошую работу в базу знаний просто. Используйте форму, расположенную ниже

Студенты, аспиранты, молодые ученые, использующие базу знаний в своей учебе и работе, будут вам очень благодарны.

# 3.4. Get frame

def read(self):

return self.frame

def stop(self):

self.stopped = True

Листинг скрипта Tensor.py

import smtplib

import traceback

import sys

import numpy as np

import logging

import tarfile

import time

from time import sleep

import tensorflow as tf

import Utility_Functions as UF

from threading import Thread

from object_detection.utils import label_map_util

from object_detection.utils import visualization_utils as vis_util

class Tensor:

# 4.1. Initialization

def __init__(self, visible, length = 720, hight = 405, model_name = 'ssd_mobilenet_v2_coco_2018_03_29', name="Tensor"):

self.name = name

self.dellay = 0

self.flag = False

self.arr = []

self.new_image = np.zeros((hight, length, 3))

self.old_image = np.zeros((hight, length, 3))

self.stopped = False

self.visible = visible

self.count = 0

init_logger = logging.getLogger("Main.%s.init" % (self.name))

try:

pwd = UF.get_pwd("detection_models")

self.model_name = model_name

PATH_TO_FROZEN_GRAPH = pwd + '/' + self.model_name + '.pb'

PATH_TO_LABELS = pwd + '/mscoco_label_map.pbtxt'

self.detection_graph = tf.Graph()

with self.detection_graph.as_default():

od_graph_def = tf.GraphDef()

with tf.gfile.GFile(PATH_TO_FROZEN_GRAPH, 'rb') as fid:

serialized_graph = fid.read()

od_graph_def.ParseFromString(serialized_graph)

tf.import_graph_def(od_graph_def, name='')

self.boxes = self.detection_graph.get_tensor_by_name('detection_boxes:0')

self.scores = self.detection_graph.get_tensor_by_name('detection_scores:0')

self.classes = self.detection_graph.get_tensor_by_name('detection_classes:0')

self.num_detections = self.detection_graph.get_tensor_by_name('num_detections:0')

self.image_tensor = self.detection_graph.get_tensor_by_name('image_tensor:0')

self.category_index = label_map_util.create_category_index_from_labelmap(PATH_TO_LABELS)

except:

init_logger.critical("Error in %s.__init__" % (self.name))

init_logger.exception("Error!")

exc_type, exc_value, exc_traceback = sys.exc_info()

err_msg = str(''.join(traceback.format_exception(exc_type, exc_value, exc_traceback)))

UF.send_msg(msg=err_msg)

sys.exit(0)

# 4.2. Start thread

def start(self):

self.stopped = False

start_logger = logging.getLogger("Main.%s.start" % (self.name))

start_logger.info("Process starting")

self.t = Thread(target=self.update, name=self.name, args=())

self.t.daemon = True

self.t.start()

return self

# 4.3. Infinite image processing cycle

def update(self):

try:

update_logger = logging.getLogger("Main.%s.update" % (self.name))

update_logger.info("Process started")

i = 0

count = 0

err = 0

dellay = 0

with self.detection_graph.as_default():

with tf.Session(graph=self.detection_graph) as sess:

while True:

image = self.new_image

if self.stopped:

return

if not np.array_equal(image,self.old_image) and image is not None:

time_1 = time.time()

image_np_expanded = np.expand_dims(image, axis=0)

boxes = self.detection_graph.get_tensor_by_name('detection_boxes:0')

scores = self.detection_graph.get_tensor_by_name('detection_scores:0')

classes = self.detection_graph.get_tensor_by_name('detection_classes:0')

num_detections = self.detection_graph.get_tensor_by_name('num_detections:0')

self.image_tensor = self.detection_graph.get_tensor_by_name('image_tensor:0')

try:

(self.boxes, self.scores, self.classes, self.num_detections) = sess.run(

[boxes, scores, classes, num_detections],

feed_dict={self.image_tensor: image_np_expanded})

except:

update_logger.critical("Error with run tensor")

update_logger.exception("Error!")

exc_type, exc_value, exc_traceback = sys.exc_info()

err_msg = str(''.join(traceback.format_exception(exc_type, exc_value, exc_traceback)))

UF.send_msg(msg=err_msg)

if (self.visible == 'Yes'):

vis_util.visualize_boxes_and_labels_on_image_array(

image,

np.squeeze(self.boxes),

np.squeeze(self.classes).astype(np.int32),

np.squeeze(self.scores),

self.category_index,

use_normalized_coordinates=True,

line_thickness=8)

time_2 = time.time()

err = time_2 - time_1

dellay = dellay + err

count = count + 1

if count == 10:

self.dellay = dellay

print dellay

self.count = count

dellay = 0

count = 0

self.flag = True

self.old_image = image

else:

sleep(0.1)

except:

update_logger.critical("Error in process")

update_logger.exception("Error!")

exc_type, exc_value, exc_traceback = sys.exc_info()

err_msg = str(''.join(traceback.format_exception(exc_type, exc_value, exc_traceback)))

print err_msg

sys.exit(0)

def set_image(self, image):

self.new_image = image

def read(self):

if self.flag:

return self.old_image

else:

return None

def read_boxes(self):

if self.flag:

return self.boxes

else:

return None

def read_scores(self):

if self.flag:

return self.scores

else:

return None

def read_classes(self):

if self.flag:

return self.classes

else:

return None

def status(self):

try:

return self.t.isAlive()

except:

return False

def get_tps(self):

try:

result = (float(self.count) / self.dellay)

except:

result = 0

return result

def stop(self):

self.stopped = True

Листинг скрипта Move2.py

import os

import sys

import cv2

import configparser

from threading import Thread

from onvif import ONVIFCamera

import traceback

import time

from time import sleep

import numpy as np

import Utility_Functions as UF

import logging

################################

# 3. The process of taking a frame from a stream

################################

class Move:

# 3.1. Initialization

def __init__(self, length, hight,speed_coef, mycam_ip, mycam_port, mycam_login, mycam_password, mycam_wsdl_path, name="Move"):

try:

self.name = name

self.box = None

self.old_box = None

self.mycam_ip = mycam_ip

self.mycam_port = mycam_port

self.mycam_login = mycam_login

self.mycam_password = mycam_password

self.mycam_wsdl_path = mycam_wsdl_path

self.length = length

self.hight = hight

self.old_vec_x = 0

self.old_vec_y = 0

self.count_frame = 0

self.speed_coef = speed_coef

init_logger = logging.getLogger("Main.%s.init" % (self.name))

try:

mycam = ONVIFCamera(self.mycam_ip, self.mycam_port, self.mycam_login, self.mycam_password, self.mycam_wsdl_path)

init_logger.info("Successful conection ONVIFCamera")

except:

err_msg = "Error with conect ONVIFCamera..."

init_logger.critical(err_msg)

init_logger.info("Check the correctness of the entered data in the setings.ini (ip,port,login, password or wsdl_path)")

UF.send_msg(msg=err_msg)

sys.exit(0)

media = mycam.create_media_service()

profile = media.GetProfiles()[0]

self.ptz = mycam.create_ptz_service()

self.request = {k: self.ptz.create_type(k) for k in ['ContinuousMove', 'GotoHomePosition', 'SetHomePosition','GetConfigurationOptions', 'GetStatus']}

for _, r in self.request.items(): r.ProfileToken = profile._token

#ptz_configuration_options = self.ptz.GetConfigurationOptions(self.request['GetConfigurationOptions'])

except:

init_logger.critical("Error in %s.__init__" % (self.name))

init_logger.exception("Error!")

exc_type, exc_value, exc_traceback = sys.exc_info()

err_msg = str(''.join(traceback.format_exception(exc_type, exc_value, exc_traceback)))

UF.send_msg(msg=err_msg)

sys.exit(0)

# 3.2. Start thread

def start(self):

self.stopped = False

start_logger = logging.getLogger("Main.%s.start" % (self.name))

start_logger.info("Process starting")

self.t = Thread(target=self.update, name=self.name, args=())

self.t.daemon = True

self.t.start()

return self

# 3.3. Infinite loop of receiving frames from a stream

def update(self):

try:

update_logger = logging.getLogger("Main.%s.update" % (self.name))

update_logger.info("Process started")

while True:

if self.stopped:

return

box = self.box

old_box = self.old_box

#print box

if np.array_equal(box,old_box):

sleep(0.2)

elif box is not None:

to_x = int(abs(box[1] - box[3])/2.0 + box[1])

to_y = int(box[0])

if (to_x < self.length/3 - 40 or to_x > self.length/3 + 40):

if to_x > self.length/3:

vec_x = float(to_x - self.length/3)/(self.length)

else:

vec_x = float(to_x - self.length/3)/(self.length)*2

else:

vec_x = 0

if (to_y < self.hight/5 - 40 or to_y > self.hight/5 + 40):

vec_y = float(self.hight/5 - to_y)/(self.hight)

else:

vec_y = 0

self.count_frame = 0

vec_x = vec_x*self.speed_coef

vec_y = vec_y*self.speed_coef

if vec_x > 1:

vec_x = 1

if vec_y > 1:

vec_y = 1

self.request['ContinuousMove'].Velocity.PanTilt._x = vec_x

self.request['ContinuousMove'].Velocity.PanTilt._y = vec_y

try:

self.ptz.ContinuousMove(self.request['ContinuousMove'])

except:

update_logger.exception("Error!")

sleep(2)

try:

mycam = ONVIFCamera(self.mycam_ip, self.mycam_port, self.mycam_login, self.mycam_password, self.mycam_wsdl_path)

print "[INFO] Successful conection ONVIFCamera"

except:

err_msg = "[ERROR] Error with conect ONVIFCamera..."

print err_msg

print "[INFO] Check the correctness of the entered data in the setings.ini (ip,port,login, password or wsdl_path)"

UF.send_msg(msg=err_msg)

sys.exit(0)

old_box = box

elif box is None and old_box is not None:

self.request['ContinuousMove'].Velocity.PanTilt._x = vec_x

self.request['ContinuousMove'].Velocity.PanTilt._y = vec_y

if (self.count_frame == 30):

self.request['ContinuousMove'].Velocity.PanTilt._x = 0

self.request['ContinuousMove'].Velocity.PanTilt._y = 0

old_box = box

self.count_frame = 0

sleep (0.1)

try:

self.ptz.ContinuousMove(self.request['ContinuousMove'])

except:

update_logger.exception("Error!")

sleep(2)

try:

mycam = ONVIFCamera(self.mycam_ip, self.mycam_port, self.mycam_login, self.mycam_password, self.mycam_wsdl_path)

print "[INFO] Successful conection ONVIFCamera"

except:

err_msg = "[ERROR] Error with conect ONVIFCamera..."

print err_msg

print "[INFO] Check the correctness of the entered data in the setings.ini (ip,port,login, password or wsdl_path)"

UF.send_msg(msg=err_msg)

sys.exit(0)

self.count_frame = self.count_frame + 1

self.old_box = old_box

except:

update_logger.critical("Error in process")

update_logger.exception("Error!")

exc_type, exc_value, exc_traceback = sys.exc_info()

err_msg = str(''.join(traceback.format_exception(exc_type, exc_value, exc_traceback)))

print err_msg

sys.exit(0)

def status(self):

return self.t.isAlive()

def set_box(self, box):

self.box = box

def set_speed_coef(self, speed_coef):

self.speed_coef = speed_coef

def goto_home(self):

self.ptz.GotoHomePosition(self.request['GotoHomePosition'])

def get_zoom(self):

return self.ptz.GetStatus(self.request['GetStatus'])[0][1]._x

def stop(self):

self.stopped = True

Листинг скрипта Ping.py

import os

import logging

from time import sleep

from threading import Thread

class Ping:

def __init__(self, mycam_ip, name="Ping"):

self.name = name

self.mycam_ip = mycam_ip

self.stopped = False

self.r = os.system("timeout 0.4 ping -c 1 " + self.mycam_ip + " > /dev/null 2>&1")

def start(self):

start_logger = logging.getLogger("Main.Ping.start")

start_logger.info("Process starting")

t = Thread(target=self.update, name=self.name, args=())

t.daemon = True

t.start()

return self

# 3.3. Infinite loop of receiving frames from a stream

def update(self):

update_logger = logging.getLogger("Main.Ping.start")

update_logger.info("Process started")

while True:

if self.stopped:

return

self.r = os.system("timeout 0.4 ping -c 1 " + self.mycam_ip + " > /dev/null 2>&1")

sleep(1)

# 3.4. Get frame

def read(self):

return self.r

# 3.5. Stop frame

def stop(self):

self.stopped = True

Листинг скрипта Utility_Functions.py

import os

import sys

import smtplib

import base64

from time import sleep

from threading import Thread

import configparser

import numpy as np

import logging

import cv2

def send_msg(msg,SUBJECT="Error"):

email = get_setting("email")

send_msg_logger = logging.getLogger("Main.functions.send_msg")

HOST="smtp.gmail.com"

BODY = "\r\n".join(("From: %s" % FROM, "To: %s" % email, "Subject: %s" % SUBJECT , "", msg))

try:

server = smtplib.SMTP(HOST, 587)

except:

send_msg_logger.critical("Problem with internet connection.")

send_msg_logger.exception("Error!")

exit(0)

server.starttls()

server.login(FROM, base64.b64decode('VGVuc29yNTUyMQ=='))

server.sendmail(FROM, [email], BODY)

server.quit()

def get_pwd(dir=""):

pwd = os.getcwd()

if dir <> "":

lst = pwd.split('/')

count = len(lst)-3

string = ""

for i in range(count):

string = string + lst[i] + "/"

pwd = string + dir

return pwd

def get_setting(get_setting = ""):

get_setting_logger = logging.getLogger("Main.functions.get_setting")

if get_setting <> "":

config = configparser.ConfigParser()

pwd = get_pwd("conf")

config.read(pwd + "/settings.ini")

try:

setting = config.get("Settings",get_setting)

except:

get_setting_logger.warning("No option '%s' in section: 'Settings'" % (get_setting))

return ""

return setting

else:

return ""

def init_tracker(stream, tensor, move, length, hight, speed_coef):

print "[INFO] Start init"

flag = True

frame_count = 0

x1 = 0

x2 = 0

while flag:

image_np = stream.read()

image_np = cv2.resize(image_np, (length,hight))

tensor.set_image(image_np)

scores = tensor.read_scores()

image_np = tensor.read()

classes = tensor.read_classes()

boxes = tensor.read_boxes()

if image_np is not None:

scores[scores > 0] = 1

classes = classes*scores

persons = np.where(classes == 1)[1]

if (str(persons) <> '[]'):

classes = tensor.read_classes()

#print (persons_num, ': found person')

person = persons[0]

l_w = [hight,length,hight,length]

box = boxes[0][person]

#print box

if (box[1] > 0.05 and box[3] < 0.95):

frame_count = frame_count + 1

x1 = x1 + box[1]

x2 = x2 + box[3]

else:

frame_count = 0

x1 = 0

x2 = 0

if frame_count >= 50:

percent = round((x2/50 - x1/50) *100)

print percent

flag = False

return speed_coef*(1.1-move.get_zoom())*0.8

Размещено на Allbest.ru


Подобные документы

Работы в архивах красиво оформлены согласно требованиям ВУЗов и содержат рисунки, диаграммы, формулы и т.д.
PPT, PPTX и PDF-файлы представлены только в архивах.
Рекомендуем скачать работу.