人機(jī)視角葡萄早期發(fā)育階段計(jì)數(shù)系統(tǒng) 葡萄早期發(fā)育階段計(jì)數(shù)數(shù)據(jù)集 葡萄數(shù)據(jù)集的應(yīng)用)
無(wú)人機(jī)視角葡萄早期發(fā)育階段計(jì)數(shù)系統(tǒng) 葡萄早期發(fā)育階段計(jì)數(shù)數(shù)據(jù)集 葡萄數(shù)據(jù)集的應(yīng)用無(wú)人機(jī)視角葡萄早期發(fā)育階段計(jì)數(shù)葡萄串的數(shù)量為葡萄種植者提供了有關(guān)潛在收獲產(chǎn)量的相關(guān)信息。然而在田間進(jìn)行手工計(jì)數(shù)是費(fèi)時(shí)費(fèi)力的。配備RGB或多光譜攝像頭的無(wú)人機(jī)能夠快速而準(zhǔn)確地完成這項(xiàng)任務(wù)。 該數(shù)據(jù)集包含15GB無(wú)人機(jī)拍攝圖像與可見(jiàn)葡萄串的掩碼標(biāo)簽。使用的RGB相機(jī)傾斜角為60度。每次飛行記錄了葡萄園一行的一側(cè)。葡萄漿果處于豌豆大小到串封閉階段即在收獲前兩個(gè)月拍攝。以下是完整的代碼示例包括數(shù)據(jù)準(zhǔn)備、模型訓(xùn)練和生成預(yù)測(cè)結(jié)果。importosimportcv2importnumpy as np from sklearn.model_selectionimporttrain_test_splitimportshutil from ultralyticsimportYOLO# Define pathsdata_pathpath_to_your_datasetimages_pathos.path.join(data_path,images)masks_pathos.path.join(data_path,masks)train_images_pathos.path.join(data_path,images,train)train_labels_pathos.path.join(data_path,labels,train)val_images_pathos.path.join(data_path,images,val)val_labels_pathos.path.join(data_path,labels,val)# Create directories if they dont existos.makedirs(train_images_path,exist_okTrue)os.makedirs(train_labels_path,exist_okTrue)os.makedirs(val_images_path,exist_okTrue)os.makedirs(val_labels_path,exist_okTrue)# Load masks and convert to bounding boxesdef load_masks_and_convert(masks_path): annotations[]forfilenameinos.listdir(masks_path):iffilename.endswith(.png)or filename.endswith(.jpg): mask_filenamefilename image_filenamefilename mask_pathos.path.join(masks_path, mask_filename)maskcv2.imread(mask_path, cv2.IMREAD_GRAYSCALE)contours, _cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)bboxes[]forcontourincontours: x, y, w, hcv2.boundingRect(contour)bboxes.append([x, y, x w, y h])annotations.append({image:image_filename,bboxes:bboxes})returnannotations annotationsload_masks_and_convert(masks_path)# Split data into train and validation setstrain_annots, val_annotstrain_test_split(annotations,test_size0.2,random_state42)# Save images and annotations to respective foldersdef save_data(annots, images_save_path, labels_save_path):forannotinannots: image_filenameannot[image]image_pathos.path.join(images_path, image_filename)shutil.copy(image_path, images_save_path)with open(os.path.join(labels_save_path, os.path.splitext(image_filename)[0].txt),w)as f:forbboxinannot[bboxes]: x_center(bbox[0] bbox[2])/2.0/1920# Assuming image width is 1920y_center(bbox[1] bbox[3])/2.0/1080# Assuming image height is 1080width(bbox[2]- bbox[0])/1920# Assuming image width is 1920height(bbox[3]- bbox[1])/1080# Assuming image height is 1080class_id0# Only one class grape_clusterf.write(f{class_id} {x_center} {y_center} {width} {height}\n)save_data(train_annots, train_images_path, train_labels_path)save_data(val_annots, val_images_path, val_labels_path)# Create dataset.yaml file for YOLOv8dataset_yaml_content train: ./images/train val: ./images/val nc:1names:[grape_cluster] with open(os.path.join(data_path,dataset.yaml),w)as f: f.write(dataset_yaml_content)# Step 3: Train YOLOv8 Model# Load a pre-trained YOLOv8 modelmodelYOLO(yolov8n.pt)# You can choose other sizes like yolov8s, yolov8m, yolov8l, yolov8x# Modify the number of classes in the final layermodel.nc1# Training commandresultsmodel.train(dataos.path.join(data_path,dataset.yaml),imgsz640,epochs50,batch16,devicecudaiftorch.cuda.is_available()elsecpu,cacheTrue)# Evaluate the modelmetricsmodel.val()# Export the trained modelmodel.export(formatonnx)運(yùn)行腳本在終端中運(yùn)行以下命令來(lái)執(zhí)行整個(gè)流程python main.py總結(jié)以上文檔包含了從數(shù)據(jù)加載、預(yù)處理、模型構(gòu)建到訓(xùn)練的所有步驟。希望這些詳細(xì)的信息和代碼能夠幫助你順利實(shí)施和優(yōu)化你的無(wú)人機(jī)視角葡萄早期發(fā)育階段計(jì)數(shù)系統(tǒng)。自定義說(shuō)明數(shù)據(jù)文件路徑: 修改data_path變量以指向你的數(shù)據(jù)文件。圖像分辨率: 根據(jù)你的實(shí)際圖像分辨率調(diào)整x_center,y_center,width,height的計(jì)算公式。超參數(shù)調(diào)整: 根據(jù)需要調(diào)整訓(xùn)練參數(shù)如imgsz,epochs,batch等。模型選擇: 你可以選擇不同的 YOLOv8 模型大小yolov8n,yolov8s,yolov8m,yolov8l,yolov8x以適應(yīng)你的需求。通過(guò)這些步驟使用 YOLOv8 進(jìn)行無(wú)人機(jī)視角葡萄早期發(fā)育階段計(jì)數(shù)任務(wù)。