Upload 13 files
Browse filesFiles changed (13)
- Scripts/__pycache__/detection.cpython-310.pycAdded
- Scripts/__pycache__/image_processing.cpython-310.pycAdded
- Scripts/__pycache__/utils.cpython-310.pycAdded
- Scripts/__pycache__/utils_spud.cpython-310.pycAdded
- Scripts/dataset_classes.txtAdded+9
- Scripts/detection.pyAdded+157
- Scripts/detection_classAdded+23
- Scripts/image_processing.pyAdded+27
- Scripts/notacion.pyAdded+30
- Scripts/requirements.txtAdded
- Scripts/utils.pyAdded+118
- Scripts/utils_spud.pyAdded+51
- Scripts/yolo_classes.txtAdded+80
123456789Car
Cyclist
DontCare
Misc
Pedestrian
Person_sitting
Tram
Truck
Van123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
from ultralytics import YOLOv10
import os
import torch
def yolov10_inference(image, model_id, image_size, conf_threshold):
model = YOLOv10.from_pretrained(f'jameslahm/{model_id}')
results = model.predict(source=image, imgsz=image_size, conf=conf_threshold)
detections = []
if results and len(results) > 0:
for result in results:
for box in result.boxes:
detections.append({
"coords": box.xyxy.cpu().numpy(),
"class": result.names[int(box.cls.cpu())],
"conf": box.conf.cpu().numpy()
#"bbox": box.xyxy.cpu().numpy().tolist()
})
return results[0].plot() if results and len(results) > 0 else image, detections
def calculate_iou(boxA, boxB):
xA = max(boxA[0], boxB[0])
yA = max(boxA[1], boxB[1])
xB = min(boxA[2], boxB[2])
yB = min(boxA[3], boxB[3])
interArea = max(0, xB - xA) * max(0, yB - yA)
boxAArea = (boxA[2] - boxA[0]) * (boxA[3] - boxA[1])
boxBArea = (boxB[2] - boxB[0]) * (boxB[3] - boxB[1])
iou = interArea / float(boxAArea + boxBArea - interArea)
return iou
def calculate_detection_metrics(detections_true, detections_pred):
true_positives = 0
pred_positives = len(detections_pred)
real_positives = len(detections_true)
ious = []
for pred in detections_pred:
for real in detections_true:
if pred['class'] == real['class']:
iou = calculate_iou(pred['coords'].flatten(), real['coords'].flatten())
if iou >= 0.5:
true_positives += 1
ious.append(iou)
break
precision = true_positives / pred_positives if pred_positives > 0 else 0
recall = true_positives / real_positives if real_positives > 0 else 0
f1_score = 2 * (precision * recall) / (precision + recall) if (precision + recall) > 0 else 0
average_iou = sum(ious) / len(ious) if ious else 0
return {"Precision": precision, "Recall": recall, "F1-Score": f1_score, "IOU": average_iou}
def read_kitti_annotations(file_path):
ground_truths = []
with open(file_path, 'r') as file:
for line in file:
parts = line.strip().split()
if parts[0] != 'DontCare': # Ignorar 'DontCare'
class_label = parts[0].lower() # Clase en minúscula
bbox = [float(parts[4]), float(parts[5]), float(parts[6]), float(parts[7])]
ground_truths.append({'class': class_label, 'bbox': bbox})
return ground_truths
def save_detections(detections, output_dir, filename='detections.txt'):
if not os.path.exists(output_dir):
os.makedirs(output_dir)
with open(os.path.join(output_dir, filename), 'w') as file:
for detection in detections:
class_label = detection['class']
bbox = ','.join(map(str, detection['bbox']))
file.write(f"{class_label},[{bbox}]\n")
def yolov10_inference_1(image, model_id, image_size, conf_threshold):
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = YOLOv10.from_pretrained(f'jameslahm/{model_id}').to(device)
results = model.predict(source=image, imgsz=image_size, conf=conf_threshold)
detections = []
if results and len(results) > 0:
for result in results:
for box in result.boxes:
detections.append({
#"coords": box.xyxy.cpu().numpy(),
"class": result.names[int(box.cls.cpu())],
#"conf": box.conf.cpu().numpy()
"bbox": box.xyxy.cpu().numpy().tolist()
})
return results[0].plot() if results and len(results) > 0 else image, detections
def calculate_iou_1(boxA, boxB):
# Asegúrate de que boxA y boxB son listas planas de flotantes
# Esto es necesario porque la función max() y min() requieren comparar elementos directamente
boxA = [float(num) for sublist in boxA for num in sublist] if isinstance(boxA[0], list) else [float(num) for num in boxA]
boxB = [float(num) for sublist in boxB for num in sublist] if isinstance(boxB[0], list) else [float(num) for num in boxB]
# Determina las coordenadas de la intersección
xA = max(boxA[0], boxB[0])
yA = max(boxA[1], boxB[1])
xB = min(boxA[2], boxB[2])
yB = min(boxA[3], boxB[3])
# Calcula el área de la intersección
interArea = max(0, xB - xA) * max(0, yB - yA)
# Calcula el área de cada cuadro delimitador y la unión
boxAArea = (boxA[2] - boxA[0]) * (boxA[3] - boxA[1])
boxBArea = (boxB[2] - boxB[0]) * (boxB[3] - boxB[1])
unionArea = boxAArea + boxBArea - interArea
# Calcula el IoU
iou = interArea / float(unionArea)
return iou
def calculate_detection_metrics_1(detections_true, detections_pred):
true_positives = 0
pred_positives = len(detections_pred)
real_positives = len(detections_true)
ious = []
for pred in detections_pred:
pred_bbox = pred['bbox']
pred_class = pred['class']
for real in detections_true:
real_bbox = real['bbox']
real_class = real['class']
if pred_class == real_class:
iou = calculate_iou_1(pred_bbox, real_bbox)
if iou >= 0.5:
true_positives += 1
ious.append(iou)
break
precision = true_positives / pred_positives if pred_positives > 0 else 0
recall = true_positives / real_positives if real_positives > 0 else 0
f1_score = 2 * (precision * recall) / (precision + recall) if (precision + recall) > 0 else 0
average_iou = sum(ious) / len(ious) if ious else 0
return {"Precision": precision, "Recall": recall, "F1-Score": f1_score, "IOU": average_iou}
1234567891011121314151617181920212223import os
def extract_classes_from_txt(directory, start, end, output_file):
classes = set()
for i in range(start, end + 1):
file_path = os.path.join(directory, f"{i:06}.txt")
if os.path.exists(file_path):
with open(file_path, 'r') as file:
for line in file:
class_label = line.split()[0]
classes.add(class_label)
else:
print(f"File {file_path} does not exist.")
# Save the unique classes to a text file
with open(output_file, 'w') as f:
for class_label in sorted(classes):
f.write(class_label + '\n')
print(f"All unique classes have been saved to {output_file}.")
# Example usage
directory = r"C:\Users\USUARIO\Documents\GitHub\Yolov10\kitti\label_2"
extract_classes_from_txt(directory, 0, 7480, "unique_classes.txt")123456789101112131415161718192021222324252627
import cv2
import numpy as np
def modulo(x, L):
positive = x > 0
x = x % L
x = np.where( ( x == 0) & positive, L, x)
return x
def apply_blur(image, kernel_size):
"""Aplica desenfoque gaussiano a la imagen."""
return cv2.GaussianBlur(image, (kernel_size, kernel_size), 0)
def clip_image(image, correction, sat_factor):
"""Clips image with saturation factor and correction."""
processed_image = np.power(image, correction) * sat_factor
clipped_image = np.clip(processed_image, 0, 1)
return clipped_image
def wrap_image(image, correction, sat_factor):
"""Wraps image with saturation factor and correction."""
processed_image = np.power(image, 1.0) * sat_factor
wrapped_image = modulo(processed_image, 1.0)
return wrapped_image
123456789101112131415161718192021222324252627282930import numpy as np
from PIL import Image
import torch
from torchvision import transforms
# Mapeo de clases de YOLOv10 a las clases de tu base de datos
def map_yolo_classes_to_db(yolo_detections):
"""
Ajusta las clases detectadas por YOLO a las clases de la base de datos según el mapeo proporcionado.
"""
mapping = {
'person': 'pedestrian',
'bicycle': 'cyclist',
'car': 'car',
'motorbike': 'cisc',
'aeroplane': 'cisc',
'bus': 'cisc',
'train': 'tram',
'truck': 'truck',
'van': 'van'
}
mapped_detections = []
for det in yolo_detections:
if det['class'] in mapping:
det['class'] = mapping[det['class']]
mapped_detections.append(det)
return mapped_detections
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118import torch
import torch_dct as dct
from torchvision.transforms import functional as F
def flip_odd_lines(matrix):
"""
Flip odd lines of a matrix
"""
matrix = matrix.clone()
matrix[..., 1::2, :] = matrix[..., 1::2, :].flip(-1)
return matrix
rotate = lambda m, r: torch.rot90(m, r, [-2, -1])
sequency_vec = lambda m: flip_odd_lines(rotate(m, 0)).flatten(start_dim= m.dim()-2)
sequency_mat = lambda v, s: rotate(flip_odd_lines(v.unflatten(-1, s)), 0)
def modulo(x, L):
positive = x > 0
x = x % L
x = torch.where( ( x == 0) & positive, L, x)
return x
def center_modulo(x, L):
return modulo(x + L/2, L) - L/2
def unmodulo(psi):
psi = torch.nn.functional.pad(psi, (1,1), mode='constant', value=0)
psi = torch.diff(psi, 1)
psi = dct.dct(psi, norm='ortho')
N = psi.shape[-1]
k = torch.arange(0, N)
denom = 2*( torch.cos( torch.pi * k / N ) - 1 )
denom[0] = 1.0
denom = denom.unsqueeze(0).unsqueeze(0) + 1e-7
psi = psi / denom
psi[..., 0] = 0.0
psi = dct.idct(psi, norm='ortho')
return psi
RD = lambda x, L: torch.round( x / L) * L
def hard_thresholding(x, t):
return x * (torch.abs(x) > t)
def stripe_estimation(psi, t=0.15):
dx = torch.diff(psi, 1, dim=-1)
dy = torch.diff(psi, 1, dim=-2)
dx = hard_thresholding(dx, t)
dy = hard_thresholding(dy, t)
dx = F.pad(dx, (1, 0, 1, 0))
dy = F.pad(dy, (0, 1, 0, 1))
rho = torch.diff(dx, 1, dim=-1) + torch.diff(dy, 1, dim=-2)
dct_rho = dct.dct_2d(rho, norm='ortho')
MX = rho.shape[-2]
NX = rho.shape[-1]
I, J = torch.meshgrid(torch.arange(0, MX), torch.arange(0, NX), indexing="ij")
I = I.to(rho.device)
J = J.to(rho.device)
denom = 2 * (torch.cos(torch.pi * I / MX ) + torch.cos(torch.pi * J / NX ) - 2)
denom = denom.unsqueeze(0).unsqueeze(0)
denom = denom.to(rho.device)
dct_phi = dct_rho / denom
dct_phi[..., 0, 0] = 0
phi = dct.idct_2d(dct_phi, norm='ortho')
phi = phi - torch.min(phi)
# phi = phi - torch.amin(phi, dim=(-1, -2), keepdim=True)
# phi = RD(phi, 1.0)
return phi
def recons(m_t, DO=1, L=1.0, vertical=False, t=0.3):
if vertical:
m_t = m_t.permute(0, 1, 3, 2)
shape = m_t.shape[-2:]
modulo_vec = sequency_vec(m_t)
res = center_modulo( torch.diff(modulo_vec, n=DO), L) - torch.diff(modulo_vec, n=DO)
bl = res
for i in range(DO):
bl = unmodulo(bl)
bl = RD(bl, L)
x_est = bl
x_est = sequency_mat(x_est, shape)
x_est = x_est + m_t
if vertical:
x_est = x_est.permute(0, 1, 3, 2)
stripes = stripe_estimation(x_est, t=t)
x_est = x_est - stripes
# if vertical:
# x_est = x_est.permute(0, 1, 3, 2)
x_est = x_est - x_est.min()
x_est = x_est / x_est.max()
return x_est 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051import torch
import torch_dct as dct
import torch.nn.functional as F
def modulo(x, L):
positive = x > 0
x = x % L
x = torch.where( ( x == 0) & positive, L, x)
return x
def center_modulo(x, L):
return modulo(x + L/2, L) - L/2
def hard_threshold(x, threshold):
return torch.where(torch.abs(x) > threshold, x, torch.zeros_like(x))
def recons_spud(y, threshold=0.1, mx=1.0):
# Mdx_y = M( Delta_x @ y ) , Mdy_y = M( Delta_y @ y )
Mdx_y = F.pad( center_modulo(torch.diff(y, 1, dim=-1), mx), (1, 0), mode='constant')
Mdy_y = F.pad( center_modulo(torch.diff(y, 1, dim=-2), mx), (0, 0, 1, 0), mode='constant')
# DTMDy = D^T ( Mdx_y, Mdy_y )
rho = - ( torch.diff(F.pad(Mdx_y, (0, 1)), 1, dim=-1) + torch.diff(F.pad(Mdy_y, (0,0, 0, 1)), 1, dim=-2) )
dct_rho = dct.dct_2d(rho, norm='ortho')
NX, MX = rho.shape[-1], rho.shape[-2]
I, J = torch.meshgrid(torch.arange(0, MX), torch.arange(0, NX), indexing="ij")
I, J = I.to(rho.device), J.to(rho.device)
I, J = I.unsqueeze(0).unsqueeze(0), J.unsqueeze(0).unsqueeze(0)
denom = 2 * ( 2 - ( torch.cos(torch.pi * I / MX ) + torch.cos(torch.pi * J / NX ) ) )
denom = denom.to(rho.device)
dct_phi = dct_rho / denom
dct_phi[..., 0, 0] = 0
dct_phi = hard_threshold(dct_phi, threshold)
phi = dct.idct_2d(dct_phi, norm='ortho')
phi = phi - phi.min()
phi = phi / phi.max()
return phi1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980person
bicycle
car
motorbike
aeroplane
bus
train
truck
boat
traffic light
fire hydrant
stop sign
parking meter
bench
bird
cat
dog
horse
sheep
cow
elephant
bear
zebra
giraffe
backpack
umbrella
handbag
tie
suitcase
frisbee
skis
snowboard
sports ball
kite
baseball bat
baseball glove
skateboard
surfboard
tennis racket
bottle
wine glass
cup
fork
knife
spoon
bowl
banana
apple
sandwich
orange
broccoli
carrot
hot dog
pizza
donut
cake
chair
sofa
pottedplant
bed
diningtable
toilet
tvmonitor
laptop
mouse
remote
keyboard
cell phone
microwave
oven
toaster
sink
refrigerator
book
clock
vase
scissors
teddy bear
hair drier
toothbrushScripts/__pycache__/detection.cpython-310.pycAdded
Scripts/__pycache__/image_processing.cpython-310.pycAdded
Scripts/__pycache__/utils.cpython-310.pycAdded
Scripts/__pycache__/utils_spud.cpython-310.pycAdded
Scripts/dataset_classes.txtAdded+9
Scripts/detection.pyAdded+157
Scripts/detection_classAdded+23
Scripts/image_processing.pyAdded+27
Scripts/notacion.pyAdded+30
Scripts/requirements.txtAdded
Scripts/utils.pyAdded+118
Scripts/utils_spud.pyAdded+51
Scripts/yolo_classes.txtAdded+80