-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
7 changed files
with
610 additions
and
166 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import cv2 | ||
import numpy as np # Tabalha com a parte cientifica (Vetores e Matrizes) | ||
import time # tempo de execução | ||
|
||
# ARQUIVOS | ||
input_file = 'arquivos/persons.jpeg' | ||
weights_path = 'arquivos/yolov4.weights' | ||
cfg_path = 'arquivos/yolov4.cfg' | ||
names_path = 'arquivos/coco.names' | ||
|
||
threshold = 0.8 # Nivel de confiança? | ||
threshold_NMS = 0.3 | ||
boxes = [] | ||
confidences = [] | ||
classIDs = [] | ||
|
||
# Carregar nome das classes | ||
LABELS = [] | ||
with open(names_path, 'r') as names: | ||
LABELS = [cname.strip() for cname in names.readlines()] | ||
# print(len(LABELS)) | ||
|
||
# SETANDO OS PARAMETROS DA REDE NEURAL | ||
net = cv2.dnn.readNetFromDarknet(cfg_path, weights_path) | ||
net.setPreferableBackend(cv2.dnn.DNN_BACKEND_OPENCV) | ||
|
||
# CAMADAS DE SAIDA E ENTRADA DO YOLO | ||
#print('CAMADAS DE SAIDA: ', net.getUnconnectedOutLayers()) | ||
layer_names = net.getLayerNames() | ||
layer_names = [layer_names[i - 1] | ||
for i in net.getUnconnectedOutLayers()] # SAIDA DE LAYER NAME | ||
#print("SAIDA ", len(layer_names)) | ||
|
||
# Gerar cores aleatorias | ||
np.random.seed(4) | ||
COLORS = np.random.randint(0, 255, size=(len(LABELS), 3), | ||
dtype="uint8") | ||
|
||
|
||
def showImage(img): | ||
cv2.imshow('img', img) | ||
# print(img.shape) # FORMATO DA IMAGEM | ||
cv2.waitKey(0) | ||
cv2.destroyAllWindows() | ||
|
||
|
||
image = cv2.imread(input_file) | ||
(H, W) = image.shape[:2] | ||
# H, W = image.shape[:2] # Altura e largura da imagem | ||
# CALCULAR PROCESSAMENTO | ||
blob = cv2.dnn.blobFromImage(image, 1 / 255.0, (416, 416), | ||
swapRB=True, crop=False) | ||
net.setInput(blob) | ||
start = time.time() | ||
layerOutputs = net.forward(layer_names) | ||
end = time.time() | ||
tempo = (end - start) | ||
# print(len(layerOutputs)) | ||
print("[INFO] YOLO took {:.2f} seconds".format(end - start)) | ||
|
||
# NIVEL DO CLASSIFICADOR E CONFIGURAÇÃO DAS VARIAVEIS | ||
for output in layerOutputs: | ||
for detection in output: | ||
scores = detection[5:] | ||
classID = np.argmax(scores) | ||
confidence = scores[classID] | ||
# NIVEL DE CONFIANÇA | ||
if confidence > threshold: | ||
box = detection[0:4] * np.array([W, H, W, H]) # CAIXA | ||
(centerX, centerY, width, height) = box.astype('int') | ||
x = int(centerX - (width/2)) | ||
y = int(centerY - (height/2)) | ||
boxes.append([x, y, int(width), int(height)]) | ||
confidences.append(float(confidence)) | ||
classIDs.append(classID) | ||
|
||
#print("CAIXAS: ", len(boxes)) | ||
#print("CONFIANCA: ", len(confidences)) | ||
|
||
|
||
def f_imagem(image, i, confidences, boxes, COLORS, LABELS, mostrar_texto=True): | ||
(x, y) = (boxes[i][0], boxes[i][1]) | ||
(w, h) = (boxes[i][2], boxes[i][3]) | ||
|
||
# extract the bounding box coordinates | ||
color = [int(c) for c in COLORS[classIDs[i]]] | ||
|
||
fundo = np.full((image.shape), (0, 0, 0), dtype=np.uint8) | ||
|
||
text = "{}: {:.4f}".format(LABELS[classIDs[i]], confidences[i]) | ||
|
||
if(mostrar_texto): | ||
print('> ' + text) | ||
print(x, y, w, h) | ||
|
||
cv2.putText(fundo, text, (x, y - 5), cv2.FONT_HERSHEY_SIMPLEX, | ||
0.5, (255, 255, 255), 2) | ||
fx, fy, fw, fh = cv2.boundingRect(fundo[:, :, 2]) | ||
cv2.rectangle(image, (x, y), (x + w, y + h), color, 2) | ||
|
||
cv2.rectangle(image, (fx, fy), (fx+fw, fy+fh), color, -1) | ||
cv2.rectangle(image, (fx, fy), (fx+fw, fy+fh), color, 3) | ||
cv2.putText(image, text, (x, y - 5), cv2.FONT_HERSHEY_SIMPLEX, | ||
0.5, (0, 0, 0), 1) | ||
return image, x, y, w, h | ||
|
||
|
||
idxs = cv2.dnn.NMSBoxes(boxes, confidences, threshold, threshold_NMS) | ||
|
||
if len(idxs) > 0: | ||
# loop over the indexes we are keeping | ||
for i in idxs.flatten(): | ||
|
||
if LABELS[classIDs[i]] == 'person': | ||
image, x, y, w, h = f_imagem( | ||
image, i, confidences, boxes, COLORS, LABELS, False) | ||
|
||
showImage(image) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
import cv2 # OpenCV | ||
import numpy as np # Tabalha com a parte cientifica (Vetores e Matrizes) | ||
import time | ||
import dlib | ||
import time | ||
import threading | ||
import math | ||
|
||
# ARQUIVOS | ||
input_file = 'arquivos/carros1.mp4' | ||
weights_path = 'arquivos/yolov4-tiny.weights' | ||
cfg_path = 'arquivos/yolov4-tiny.cfg' | ||
names_path = 'arquivos/coco.names' | ||
# CONFIG PRECISÃO | ||
threshold = 0.4 # Nivel de confiança? | ||
threshold_NMS = 0.4 | ||
font_smal, font_big = 0.4, 0.6 | ||
font_tipe = cv2.FONT_HERSHEY_SIMPLEX | ||
fontLine = 2 # inteiro | ||
amostrar_exibir = 20 | ||
amostra_atual = 0 | ||
|
||
|
||
# CARREGANDO NOME DAS CLASSES | ||
with open(names_path, 'r') as names: | ||
LABELS = [cname.strip() for cname in names.readlines()] | ||
# CARREGANDO ARQUIVOS | ||
net = cv2.dnn.readNetFromDarknet(cfg_path, weights_path) | ||
net.setPreferableBackend(cv2.dnn.DNN_BACKEND_OPENCV) | ||
# CORES DAS CLASSES | ||
np.random.seed(50) | ||
COLORS = np.random.randint(0, 255, size=(len(LABELS), 3), dtype='uint8') | ||
# CAMADAS DE SAIDA | ||
ln = net.getLayerNames() | ||
ln = [ln[i-1] for i in net.getUnconnectedOutLayers()] | ||
# CARREGAR VIDEO | ||
cap = cv2.VideoCapture(0) | ||
connected, video = cap.read() | ||
video_height = video.shape[0] | ||
video_width = video.shape[1] | ||
|
||
# TAMANHO DO VIDEO | ||
|
||
|
||
def reSizeX(_width, _height, _widthMax=600): | ||
if(_width > _widthMax): | ||
newSize = _width / _height | ||
video_width = _widthMax | ||
video_height = int(video_width/newSize) | ||
else: | ||
video_width = _width | ||
video_height = _height | ||
return video_width, video_height | ||
|
||
|
||
video_width, video_height = reSizeX(video_width, video_height) | ||
|
||
|
||
# MOSTRAR IMAGEM | ||
def imageShow(img): | ||
cv2.imshow('TELA DE CAPTURA', img) | ||
# print(img.shape) # FORMATO DA IMAGEM | ||
# cv2.waitKey(0) | ||
# cv2.destroyAllWindows() | ||
|
||
|
||
# CONSTUÇÃO DO BLOB | ||
def blobImage(net, img): | ||
start = time.time() | ||
blob = cv2.dnn.blobFromImage( | ||
img, 1/255.0, (416, 416), swapRB=True, crop=False) | ||
net.setInput(blob) | ||
layer_Outputs = net.forward(ln) | ||
end = time.time() | ||
# print("[TEMPO DE DETECÇÃO] {:.2f} seconds".format(end - start)) | ||
return net, img, layer_Outputs | ||
|
||
|
||
# REALIZAR DETECÇÃO | ||
def detectionImage(_detection, _threshold, _AllBoxes, _AllConfidences, _AllClassesID, _img): | ||
(H, W) = _img.shape[:2] | ||
scores = _detection[5:] | ||
classeID = np.argmax(scores) | ||
confidence = scores[classeID] | ||
if confidence > _threshold: | ||
box = detection[0:4] * np.array([W, H, W, H]) | ||
(centerX, centerY, width, height) = box.astype('int') | ||
x = int(centerX - (width/2)) | ||
y = int(centerY - (height/2)) | ||
_AllBoxes.append([x, y, int(width), int(height)]) | ||
_AllConfidences.append(float(confidence)) | ||
_AllClassesID.append(classeID) | ||
|
||
return _AllBoxes, _AllConfidences, _AllClassesID | ||
|
||
|
||
# CRIAR CAIXAS | ||
def createBoxes(_img, i, _confidences, _boxes, _COLORS, _LABELS, _AllClassesID): | ||
(x, y) = (_boxes[i][0], _boxes[i][1]) | ||
(w, h) = (_boxes[i][2], _boxes[i][3]) | ||
color = [int(c) for c in _COLORS[_AllClassesID[i]]] | ||
text = "{}: {:.4f}".format( | ||
_LABELS[_AllClassesID[i]], _confidences[i]) | ||
|
||
fundo = np.full((_img.shape), (0, 0, 0), dtype=np.uint8) | ||
|
||
cv2.putText(fundo, text, (x, y - 5), cv2.FONT_HERSHEY_SIMPLEX, | ||
0.5, (255, 255, 255), 2) | ||
fx, fy, fw, fh = cv2.boundingRect(fundo[:, :, 2]) | ||
cv2.rectangle(_img, (x, y), (x + w, y + h), color, 2) | ||
|
||
cv2.rectangle(_img, (fx, fy), (fx+fw, fy+fh), color, -1) | ||
cv2.rectangle(_img, (fx, fy), (fx+fw, fy+fh), color, 3) | ||
cv2.putText(_img, text, (x, y - 5), cv2.FONT_HERSHEY_SIMPLEX, | ||
0.5, (0, 0, 0), 1) | ||
|
||
return _img, x, y, w, h | ||
|
||
|
||
# CAPTURA FRAME A FRAME | ||
while(cv2.waitKey(1) < 0): # CLIQUE [1] EXIT | ||
_connected, _frame = cap.read() | ||
if not _connected and type(_frame) == type(None): | ||
print("VIDEO NULL") | ||
break | ||
t = time.time() | ||
_frame = cv2.resize(_frame, (video_width, video_height)) | ||
try: | ||
(H, W) = _frame.shape[:2] | ||
except: | ||
print('VIDEO SHAPE ERRO') | ||
continue | ||
|
||
net, _frame, layerOutputs = blobImage(net, _frame) | ||
AllBoxes = [] | ||
AllConfidences = [] | ||
AllClassesID = [] | ||
|
||
for output in layerOutputs: | ||
for detection in output: | ||
AllBoxes, AllConfidences, AllClassesID = detectionImage( | ||
detection, threshold, AllBoxes, AllConfidences, AllClassesID, _frame) | ||
|
||
objects = cv2.dnn.NMSBoxes(AllBoxes, AllConfidences, | ||
threshold, threshold_NMS) | ||
|
||
if len(objects) > 0: | ||
for i in objects.flatten(): | ||
_frame, x, y, w, h = createBoxes( | ||
_frame, i, AllConfidences, AllBoxes, COLORS, LABELS, AllClassesID) | ||
|
||
cv2.putText(_frame, "PROCSSAMENTO {:.2f}s".format(time.time()-t), | ||
(20, video_height-20), font_tipe, font_big, (255, 255, 255), fontLine, lineType=cv2.LINE_AA) | ||
|
||
imageShow(_frame) | ||
|
||
print("terminou") | ||
cv2.destroyAllWindows() |
Oops, something went wrong.