https://blog.csdn.net/dannisklx/article/details/140782542
https://modelscope.cn/models/goldsj/GroundingDINO/files
pip3 install autodistill autodistill-yolov8 autodistill-grounding-dino
import os
HOME = os.getcwd()
print(HOME)
IMAGE_DIR_PATH = f"{HOME}/public_data"
import supervision as sv
image_paths = sv.list_files_with_extensions(
directory=IMAGE_DIR_PATH,
extensions=["png", "jpg", "jpg"])
print('image count:', len(image_paths))
import cv2
SAMPLE_SIZE = 16
SAMPLE_GRID_SIZE = (4, 4)
SAMPLE_PLOT_SIZE = (16, 16)
titles = [
image_path.stem
for image_path
in image_paths[:SAMPLE_SIZE]]
images = [
cv2.imread(str(image_path))
for image_path
in image_paths[:SAMPLE_SIZE]]
sv.plot_images_grid(images=images, titles=titles, grid_size=SAMPLE_GRID_SIZE, size=SAMPLE_PLOT_SIZE)
from autodistill.detection import CaptionOntology
ontology=CaptionOntology({
"goods": "goods"
})
DATASET_DIR_PATH = f"{HOME}/public_dataset"
from autodistill_grounding_dino import GroundingDINO
base_model = GroundingDINO(ontology=ontology)
dataset = base_model.label(
input_folder=IMAGE_DIR_PATH,
extension=".jpg",
output_folder=DATASET_DIR_PATH)
ANNOTATIONS_DIRECTORY_PATH = f"{HOME}/public_dataset/train/labels"
IMAGES_DIRECTORY_PATH = f"{HOME}/public_dataset/train/images"
DATA_YAML_PATH = f"{HOME}/public_dataset/data.yaml"
print(ANNOTATIONS_DIRECTORY_PATH)
print(IMAGES_DIRECTORY_PATH)
print(DATA_YAML_PATH)
import supervision as sv
dataset = sv.DetectionDataset.from_yolo(
images_directory_path=IMAGES_DIRECTORY_PATH,
annotations_directory_path=ANNOTATIONS_DIRECTORY_PATH,
data_yaml_path=DATA_YAML_PATH)
len(dataset)
import supervision as sv
image_names = list(dataset.images.keys())[:SAMPLE_SIZE]
mask_annotator = sv.MaskAnnotator()
box_annotator = sv.BoxAnnotator()
images = []
for image_name in image_names:
image = dataset.images[image_name]
annotations = dataset.annotations[image_name]
labels = [
dataset.classes[class_id]
for class_id
in annotations.class_id]
annotates_image = mask_annotator.annotate(
scene=image.copy(),
detections=annotations)
annotates_image = box_annotator.annotate(
scene=annotates_image,
detections=annotations,
labels=labels)
images.append(annotates_image)
sv.plot_images_grid(
images=images,
titles=image_names,
grid_size=SAMPLE_GRID_SIZE,
size=SAMPLE_PLOT_SIZE)
from autodistill_yolov8 import YOLOv8
target_model = YOLOv8("yolov8n.pt")
target_model.train(DATA_YAML_PATH, epochs=100)