





# 라이브러리 설치
!pip install ultralytics
# 모델 불러오기
from ultralytics import YOLO
model = YOLO('yolov8n-seg.pt') # 사전에 학습된 YOLOv8 segmentation 모델 로딩
# 예측 수행하기
results = model.predict(source="https://ultralytics.com/images/bus.jpg", # 예측할 이미지
save=True, # 예측 결과를 이미지로 저장
conf=0.15) # 예측에 대한 확신도 임계치

# 예측 수행하기
results = model.predict(source="https://ultralytics.com/images/bus.jpg", # 예측할 이미지
save=True, # 예측 결과를 이미지로 저장
conf=0.40) # 예측에 대한 확신도 임계치
# 예측에 사용 된 이미지 출력
import matplotlib.pyplot as plt
import cv2
img = cv2.cvtColor(results[0].plot(), cv2.COLOR_BGR2RGB)
plt.figure(figsize=(10, 10))
plt.imshow(img)
plt.show()



AP : 평균
mAP : 여러 Object에 대한 AP를 평균

!pip install roboflow
from roboflow import Roboflow
rf = Roboflow(api_key="Itm8WkbVFGNUfRDOErTQ")
project = rf.workspace("none-n1imd").project("car-jxbzt")
version = project.version(1)
dataset = version.download("yolov8-obb")
# 전이학습을 진행할 사전학습 모델 생성
model = YOLO('yolov8n-seg.yaml').load('yolov8n-seg.pt')
# 모델학습
results = model.train(data="./car-1/data.yaml", # 데이터셋 설정파일 경로
epochs=100, # 학습횟수 설정
imgsz=640) # 입력 이미지 사이즈 설정
# 학습 가중치 파일경로 설정
best_model_path = "/content/drive/MyDrive/Colab Notebooks/24.08.29 DeepLearning/data/best.pt"
# 모델로딩
best_model = YOLO(best_model_path)
# 모델예측
results = best_model.predict(source="/content/drive/MyDrive/Colab Notebooks/24.08.29 DeepLearning/car-1/valid/images/16-rear-side_jpg.rf.a2cb70870143ce9d081d8ddc9d13760c.jpg", # 이미지 경로
save=True, # 예측 결과 이미지로 저장
conf=0.15) # 모델의 확실도 임계치 설정
# 예측에 사용 된 이미지 출력
import matplotlib.pyplot as plt
import cv2
img = cv2.cvtColor(results[0].plot(), cv2.COLOR_BGR2RGB)
plt.figure(figsize=(10, 10))
plt.imshow(img)
plt.show()
영상을 활용해 차량파손범위 예측
참고 : https://docs.ultralytics.com/modes/predict/#streaming-source-for-loop
local jupyternotebook으로 이동

OpenCV, Ultralytics 설치 확인
# 라이브러리 설치
!pip install opencv-python ultralytics
import cv2
from ultralytics import YOLO
# Load the YOLOv8 model
model = YOLO("./best.pt")
# Open the video file
video_path = "./car.mp4"
cap = cv2.VideoCapture(video_path)
# Loop through the video frames
while cap.isOpened():
# Read a frame from the video
success, frame = cap.read()
if success:
# Run YOLOv8 inference on the frame
results = model(frame)
# Visualize the results on the frame
annotated_frame = results[0].plot()
# Display the annotated frame
cv2.imshow("YOLOv8 Inference", annotated_frame)
# Break the loop if 'q' is pressed
if cv2.waitKey(1) & 0xFF == ord("q"):
break
else:
# Break the loop if the end of the video is reached
break
# Release the video capture object and close the display window
cap.release()
cv2.destroyAllWindows()
