VideoAnalysis/main.py
2023-05-20 13:23:06 +04:00

78 lines
2.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import os
import sys
import cv2 as cv
import numpy as np
import requests
import torch
import imageWorking
import neuralNetwork
import ontologyWorking
url = 'http://kb.athene.tech/api/1.0/ontology/'
img_path = 'data'
img_size = (1280, 720)
def analyse_file(uid, image_path):
if ontologyWorking.is_ontology_exists(uid, url):
raise Exception(f'Онтология с uid {uid} не существует')
if not os.path.isfile(image_path):
raise Exception(f'Изображение {image_path} не существует')
model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True)
model.names = neuralNetwork.rename_entity(model.names)
list_sqwrl = ontologyWorking.get_list_sqwrl(uid, url)
results = model(imageWorking.get_image_as_array(image_path))
object_properties = list()
data_properties = list()
for i, res in enumerate(results.pred):
results_ndarray = np.array(res)
request = ontologyWorking.get_request_data(model.names, results_ndarray)
object_properties += request[0]
data_properties += request[1]
data = {
'data':
{
'objectPropertyAssertions': object_properties,
'dataPropertyAssertions': data_properties
}
}
result = {
'QueryGetNotEmpty': '',
'QueryGetCheck': '',
'QueryGetEmpty': ''
}
for sqwrl in list_sqwrl['response']:
response = requests.post(url + f'{uid}/query/{sqwrl["name"]}', json=data).json()
if response['error']:
raise Exception(response['error'])
result[sqwrl['name']] = [resultSQWRL['name']['value'] for resultSQWRL in response['response']['rows']]
print(f'Запрос {sqwrl["name"]} выполнен')
print()
print('Результаты')
if result['QueryGetNotEmpty']:
print('Аудитория занята')
elif result['QueryGetCheck']:
print('Аудиторию необходимо проверить')
elif result['QueryGetEmpty']:
print('Аудитория пустая')
else:
print('Неизвестное состояние')
# Вывод изображения
cv.imshow('result', results.render()[0][:, :, ::-1])
cv.waitKey(0)
cv.destroyAllWindows()
if __name__ == '__main__':
if len(sys.argv) != 3:
print(f'Запуск: {sys.argv[0]} <Ontology UID> <Image path>')
exit(1)
analyse_file(sys.argv[1], sys.argv[2])