Professional Documents
Culture Documents
Code cutvideo.py
import cv2
def FrameCapture(Path):
vidObj = cv2.VideoCapture(Path)
count = 0
success = 1
while success:
success, image = vidObj.read()
if count % 10 == 0:
cv2.imwrite("dataset\\frame%d.jpg"%(count/10),image)
count+=1
print("0")
#if __name__ == '__name__':
FrameCapture("D:\XLA\YOLO\WIN_20240229_16_03_15_Pro.mp4")
print("1")
1. Thư viện
khai báo thư viện cv2 trong OpenCV
import cv2
# khởi tạo hàm FrameCapture nhận đường dẫn video theo Path
def FrameCapture(Path):
# gán cv2.VideoCapture đường dẫn Path vào vidObj
vidObj = cv2.VideoCapture(Path)
#đếm số khung hình trích xuất
count = 0
#biến kiểm tra việc đọc và mở video thành công hay ko
success = 1
2. Vòng lặp chạy nhận diện và hiển thị
# vòng lặp while sẽ thực thi success khi không còn khung hình để đọc
while success:
success, image = vidObj.read()
if count % 10 == 0:
cv2.imwrite("dataset\\frame%d.jpg"%(count/10),image)
count+=1
# vidObj.read() sẽ đọc khung hình từ video và gán nó vào biến image và cập nhật giá trị
của success cho biết việc đọc thành công hay không
# vòng lặp if nếu sau mỗi 10 khung hình thì sẽ lưu ảnh
# hàm cv2.imwrite dùng để lưu ảnh với tên file theo .jpg vào thư mục dataset và đánh số
thứ tự hình ảnh
# biến count+=1 sau mỗi lần lặp thì count sẽ tăng lên 1 đếm số khung hình đã xử lý
3. Gọi hàm và in thông báo
print("0")
#if __name__ == '__name__':
FrameCapture("D:\XLA\YOLO\WIN_20240229_16_03_15_Pro.mp4")
print("1")
print("0")
print in giá trị 0
FrameCapture("D:\XLA\YOLO\WIN_20240229_16_03_15_Pro.mp4")
# hàm FrameCapture gọi đường dẫn video
print("1")
# print giá tri 1 khi hàm FrameCapture hoàn thành
Code my_train.py
# Train du lieu anh
from ultralytics import YOLO
# Load a model
model = YOLO('yolov8n.yaml') # build a new model from YAML
model = YOLO('yolov8n.pt') # load a pretrained model (recommended for training)
goc:yolov8n.pt
if __name__ == '__main__': # can khi chay bang GPU
model.train(data='D:\\XLA\\YOLO\\dataset.yaml', epochs=30, imgsz=640, batch=16,
optimizer='Adam')
metrics = model.val()
4. Nhập thư viện
#khai báo thư viện ultralytics vào lớp YOLO
from ultralytics import YOLO
5. Load mô hình YOLO
# Load a model
model = YOLO('yolov8n.yaml') # build a new model from YAML
model = YOLO('yolov8n.pt') # load a pretrained model (recommended for training)
goc:yolov8n.pt
# tạo mô hình YOLO mới từ tệp .yaml được cung cấp
# tải mô hình YOLO đã được huấn luyện từ tệp .pt
6. Huấn luyện mô hình
if __name__ == '__main__': # can khi chay bang GPU
model.train(data='D:\\XLA\\YOLO\\dataset.yaml', epochs=30, imgsz=640, batch=16,
optimizer='Adam')
metrics = model.val()
# hàm model.train bắt đầu quá trình huấn luyện với data theo tệp .yaml
# epchs số lần lặp lại toàn bộ dữ lieu
# imgsz kích thước đầu vào là 640x640 pixel
# batch số lượng ảnh sử dụng qua mỗi lần cập nhật trọng số
# optimizer thuật toán tối ưu hóa
# hàm model.val() đánh giá mô hình theo tập dữ liệu trong thư mục val
Giải thích code “detect.py”.
import cv2
from ultralytics import YOLO
model = YOLO(r'E:\Dai_Hoc\XuLyAnh-TH\YoLo\runs\detect\train3\weights\best.pt')
vid = cv2.VideoCapture(0)
vid = cv2.VideoCapture('G:/Hk_1_nam_4/thi_giac_may_tinh/YOLO8/test1.mp4')
while (True):
ret, frame = vid.read()
frame = cv2.flip(frame, 1) #mirror
results = model(frame)
annotated_image = results[0].plot() #
vid.release()
cv2.destroyAllWindows()
1.Nhập thư viện:
import cv2
from ultralytics import YOLO
import cv2: Nhập thư viện OpenCV để thực hiện các tác vụ về thị giác máy tính như
chụp ảnh, xử lý ảnh và hiển thị ảnh.
from ultralytics import YOLO: Nhập lớp YOLO từ thư viện ultralytics cung cấp các
chức năng để tải và sử dụng mô hình YOLO để phát hiện đối tượng.
2.Tải mô hình YOLO.
model = YOLO(r'E:\Dai_Hoc\XuLyAnh-TH\YoLo\runs\detect\train3\weights\best.pt')
Tạo một thể hiện đối tượng YOLO. Tham số chỉ ra đường dẫn đến tập tin trọng số mô
hình YOLOv5 được đào tạo trước (best.pt). Tệp này chứa các tham số mô hình đã được
đào tạo cho phép phát hiện đối tượng.
3. Khởi tạo Video Capture:
vid = cv2.VideoCapture(0)
Mở webcam mặc định (chỉ mục 0) và lưu trữ thông tin vào biến vid.
Nói cách khác, nó "bật" webcam và cho phép chương trình truy cập hình ảnh từ camera.
vid = cv2.VideoCapture('G:/Hk_1_nam_4/thi_giac_may_tinh/YOLO8/test1.mp4')
Mở tập tin video có tên test1.mp4 và lưu trữ thông tin vào biến vid.
Nói cách khác, nó "mở" video và cho phép chương trình truy cập hình ảnh từ video.
4. Vòng lặp chính:
while (True):
ret, frame = vid.read()
frame = cv2.flip(frame, 1)
results = model(frame)
annotated_image = results[0].plot()
model = YOLO(r'E:\Dai_Hoc\XuLyAnh-TH\YoLo\runs\detect\train\weights\best.pt')
vid = cv2.VideoCapture(0)
while (True):
ret, frame = vid.read()
frame = cv2.flip(frame, 1)
results = model(frame)
annotated_image = results[0].plot()
for r in results:
acls = r.boxes.cls
for a in acls:
cls_value = int(a.item())
if cls_value == 0:
detected_classes['Sang'] = True
elif cls_value == 1:
detected_classes['Phuong'] = True
elif cls_value == 2:
detected_classes['ChaiNuoc'] = True
ser.write(str(cls_value).encode
for cls_name, detected in detected_classes.items():
if not detected:
if cls_name == 'Sang':
ser.write(b'4')
elif cls_name == 'Phuong':
ser.write(b'5')
elif cls_name == 'ChaiNuoc':
ser.write(b'6')
vid.release()
cv2.destroyAllWindows()
while(1)
{
// kiểm tra xem có dữ liệu nhận được từ uart hay không
if(UART1_Data_Ready() == 1)
{
ReceiveData = UART1_Read();
// nếu biến nhận là 0 tiến hành bật led ở chân RE0
if(ReceiveData == '0')
{
LATE0_bit = 0;
}
// nếu nhận được 4 thì tiến hành tắt led ở chân REO
else if(ReceiveData == '4')
{
LATE0_bit = 1;
}
// nếu biến nhận là 1 tiến hành bật led ở chân RE1
if(ReceiveData == '1')
{
LATE1_bit = 0;
}
// nếu biến nhận là 5 tiến hành tắt led ở chân RE1
else if (ReceiveData == '5')
{
LATE1_bit = 1;
}
// nếu biến nhận là 2 tiến hành bật led ở chân RE2
if(ReceiveData == '2')
{
LATE2_bit = 0;
}
// nếu biến nhận là 6 tiến hành tắt led ở chân RE2
else if (ReceiveData == '6')
{
LATE2_bit = 1;
}
}
}
Mô phỏng
Nhận 1 đối tượng