본문 바로가기

프로젝트

프로젝트ㅣ- 시니어 건강관리 음식인식(5) 머신러닝

728x90

Django 웹 프레임워크를 사용하여 저장된 사진을 YOLOv5 가중치 파일로 학습시키 는 법

1.데이터셋 준비 
저장된 사진 박스좌표와 객체 클래스 라벨이 있는 파일을 XML이나 JSON형식으로 저장

2. yolov5설치 -레포지토리 클론후 필요 종속성설치
git clone https://github.com/ultralytics/yolov5.git
cd yolov5
pip install -r requirements.txt

3.가중치파일 추가 
YOLOv5 웹 앱 프로젝트 내에 가중치 파일을 추가해야함. 일반적으로 weights 디렉토리를 생성하고 거기에 가중치 파일을 넣음

4.django view 작성 
Django 웹 앱의 View 함수를 작성하여 yolov5 가중치 파일로 학습시키기 가능함 .

import torch
from torchvision import transforms

def process_image(image):
    # 이미지 전처리를 위한 변환 작업
    transform = transforms.Compose([
        transforms.ToTensor(),
        transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
    ])
    image = transform(image).unsqueeze(0)
    return image

def detect_objects(image_path):
    # 가중치 파일 로드
    weights_path = 'path_to_weights/your_weights.pt'
    model = torch.hub.load('ultralytics/yolov5', 'custom', path=weights_path)
    model.conf = 0.5  # 객체 탐지의 임계값 설정

    # 이미지 로드 및 전처리
    image = Image.open(image_path)
    image = process_image(image)

    # 객체 탐지 수행
    results = model(image)

    # 결과 분석 및 저장

def your_view(request):
    if request.method == 'POST' and request.FILES['image

 저장된 사진을 불러와서 YOLOv5를 사용하여 객체 탐지를 수행하고, 결과를 분석하고 저장

728x90