update upstaged changes

This commit is contained in:
ІО-23 Шмуляр Олег 2025-12-04 13:19:41 +02:00
parent 830c3a0571
commit 41e1571c4d
6 changed files with 110 additions and 4 deletions

12
aug.py
View File

@ -23,14 +23,18 @@ tagt_path = '../videos/total-exp'
p = 1
t = a.Compose([
a.SafeRotate(limit=80, p=0.8),
a.BBoxSafeRandomCrop(),
a.HorizontalFlip(p=0.5),
a.RandomBrightnessContrast(p=0.2)
a.VerticalFlip(p=0.5),
a.RandomBrightnessContrast(p=0.9, brightness_limit = (-0.2, 0.7), contrast_limit = (-0.6, 0.4)),
a.ColorJitter(brightness=0.2, contrast=0.2, saturation=0.2, hue=0.1, p=0.8),
a.ChromaticAberration(p=0.2),
], bbox_params=a.BboxParams(format='yolo', label_fields=['class_labels']))
fs = set([i.rsplit(".", 1)[0] for i in ls(base_path) if i != "classes.txt"])
for i in list(fs)[:1]:
for i in list(fs):
img = cv.cvtColor(cv.imread(f"{base_path}/{i}.jpg"), cv.COLOR_BGR2RGB)
box = [tuple(map(float, j.split()[1:])) for j in open(f"{base_path}/{i}.txt").read().split("\n") if j]
@ -44,9 +48,9 @@ for i in list(fs)[:1]:
out_box = r['bboxes']
out_lbl = r['class_labels']
#print(f"open({tagt_path}/{p}-{i}-{k}.xml)")
print(f"open({tagt_path}/{p}-{i}-{k}.txt)")
#print("\n".join([" ".join(list(map(str, ['0'] + list(y)))) for y in out_box]) + "\n")
open(f"{tagt_path}/{p}-{i}-{k}.xml", 'w').write("\n".join([" ".join(list(map(str, ['0'] + list(y)))) for y in out_box]) + "\n")
open(f"{tagt_path}/{p}-{i}-{k}.txt", 'w').write("\n".join([" ".join(list(map(str, ['0'] + list(y)))) for y in out_box]) + "\n")
cv.imwrite(f"{tagt_path}/{p}-{i}-{k}.jpg", cv.cvtColor(out_img, cv.COLOR_RGB2BGR))
#sq(out_img, out_box)

10
detect.py Normal file
View File

@ -0,0 +1,10 @@
from ultralytics import YOLO
from matplotlib import pyplot as plt
m = YOLO("best.pt")
res = m("../dataset/images/train/1-1-0000001-1.jpg")
res[0].show()
#print(res)
#plt.imshow(res)
#plt.show()

24
live.py Normal file
View File

@ -0,0 +1,24 @@
from ultralytics import YOLO
import json
import cv2 as cv
m = YOLO("best.pt")
c = cv.VideoCapture(0)
while True:
_, frame = c.read()
res = m(frame)
for i in json.loads(res[0].to_json()):
i = i['box']
cv.rectangle(frame, (round(i['x1']), round(i['y1'])), (round(i['x2']), round(i['y2'])), (0, 0, 255), 3)
#print(i)
cv.imshow('frame', frame)
if cv.waitKey(1) == ord('q'):
break
c.release()
cv.destroyAllWindows()

33
show-box.py Normal file
View File

@ -0,0 +1,33 @@
import albumentations as a
import numpy as np
from os import listdir as ls
import cv2 as cv
from sys import argv, exit
if len(argv) != 2:
exit(1)
def b_c(b, s = (1920, 1080)):
return [
round(b[0] * s[0] - b[2] * s[0] / 2),
round(b[0] * s[0] + b[2] * s[0] / 2),
round(b[1] * s[1] - b[3] * s[1] / 2),
round(b[1] * s[1] + b[3] * s[1] / 2),
]
def sq(img, bbox):
for i in bbox:
b = b_c(i, s = (img.shape[1], img.shape[0]))
#print(b)
cv.rectangle(img, (b[0], b[2]), (b[1], b[3]), (255, 0, 0), 2)
img = cv.cvtColor(cv.imread(f"{argv[1]}.jpg"), cv.COLOR_BGR2RGB)
box = [tuple(map(float, j.split()[1:])) for j in open(f"{argv[1]}.txt").read().split("\n") if j]
box = np.array(box)
sq(img, box)
from matplotlib import pyplot as plt
plt.imshow(img)
plt.show()

4
train.py Normal file
View File

@ -0,0 +1,4 @@
from ultralytics import YOLO
m = YOLO("m.yaml")
m.train(data = "../dataset/d.yaml", epochs = 100, imgsz = 300)

31
video-cruncher.py Normal file
View File

@ -0,0 +1,31 @@
from ultralytics import YOLO
import json
import cv2 as cv
from sys import argv, exit
if len(argv) != 2:
exit(1)
m = YOLO("best.pt")
ci = cv.VideoCapture(argv[1])
target_dir = argv[1].rsplit(".", 1)[0] + "-p"
k = 0
while True:
ret, frame = ci.read()
if not ret:
break
res = m(frame)
for i in json.loads(res[0].to_json()):
i = i['box']
cv.rectangle(frame, (round(i['x1']), round(i['y1'])), (round(i['x2']), round(i['y2'])), (0, 0, 255), 3)
cv.imwrite(f"{target_dir}/{k:07d}.jpg", frame)
k += 1
ci.release()