使用python和yolo方法實現(xiàn)yolo標簽自動標注
yolo代碼自動標注
1.引言
在圖像處理與機器視覺領(lǐng)域,標注數(shù)據(jù)的質(zhì)量和數(shù)量對于模型的訓練至關(guān)重要。然而,手動標注大量圖像是一項繁瑣且耗時的工作。為了解決這個問題,自動標注技術(shù)應(yīng)運而生。本文將介紹如何使用YOLOv10結(jié)合傳統(tǒng)圖像處理算法進行驗證碼圖像的自動標注,從初步的自動標注到高階的基于檢測結(jié)果的自動標注。
2.初階“自動標注”,給每個圖像都生成一個固定的標注文件,進而在labglimg中對矩形框進行微調(diào),減少標注的工作量
在初步的自動標注階段,我們首先為每張圖像生成一個固定的標注文件,這些文件的內(nèi)容通常是預設(shè)的,矩形框的位置并不精確,因此適合用作標注的初步框架。
import os # 圖像文件夾路徑 image_folder = 'D:' # 標注文件即txt文件路徑 txt_folder = 'D:' # 基于上述兩個路徑檢索圖像和txt即yolo格式的標注文件 image_files = [f.split('.')[0] for f in os.listdir(image_folder) if f.lower().endswith(('jpg', 'jpeg', 'png', 'bmp'))] txt_files = [f.split('.')[0] for f in os.listdir(txt_folder) if f.lower().endswith('txt')] # 遍歷圖像文件名 for img_name in image_files: # 如果某圖像沒有對應(yīng)的標簽文件即txt文件 if img_name not in txt_files: # 寫入txt文件 txt_content = """0 0.808621 0.945652 0.072414 0.073913 0 0.250000 0.604348 0.093103 0.078261 0 0.660345 0.584783 0.086207 0.100000 0 0.613793 0.160870 0.117241 0.139130 0 0.293103 0.184783 0.103448 0.117391""" with open(os.path.join(txt_folder, img_name + '.txt'), 'w') as f: f.write(txt_content) print("Process complete.")
由于我們對每張圖都生成的是一份固定的標注txt文件,所以生成的文件一般都是這種的,矩形框 不在正確的位置上,雖然能夠降低一定的標注工作量,但是我們還是想讓它更精準一點,那就需要使用訓練好的模型了。
局限性:
矩形框位置不準確,標注結(jié)果可能與實際物體位置存在偏差。
無法自動識別圖像中的所有物體,可能遺漏檢測。
3.高階自動標注,利用我們訓練好的(但是沒有特別精準的)yolo文件先對每張圖進行檢測,再手動微調(diào)
為了提高自動標注的精度,我們可以使用YOLOv10模型對每張圖像進行目標檢測,并根據(jù)檢測結(jié)果生成標注文件。這種方式能夠提供較為準確的初步標注,用戶只需要對檢測框進行微調(diào)即可,減少了大量的手動標注工作。
import os from ultralytics import YOLOv10 # Folder paths image_folder = 'D:' txt_folder = 'D:' # Load a pretrained YOLOv10n model model = YOLOv10("\weights\\best.pt") image_files = [f for f in os.listdir(image_folder) if f.lower().endswith(('jpg', 'jpeg', 'png', 'bmp'))] # Perform object detection on each image file for img_name in image_files: img_path = os.path.join(image_folder, img_name) # Perform prediction results = model.predict(img_path) # Extract detection results (assuming results[0] contains the detection) detections = results[0].boxes.xywh # Get bounding box coordinates (xywh format) # If no .txt file exists, create a new one txt_path = os.path.join(txt_folder, img_name.split('.')[0] + '.txt') if not os.path.exists(txt_path): # Check if .txt file already exists # Write detection results to the .txt file with open(txt_path, 'w') as f: for detection in detections: # Assuming you want the format: class_id x_center y_center width height (normalized) # Convert coordinates from pixels to normalized values by dividing by image width/height x_center, y_center, width, height = detection x_center /= results[0].orig_img.shape[1] # Normalize by image width y_center /= results[0].orig_img.shape[0] # Normalize by image height width /= results[0].orig_img.shape[1] # Normalize by image width height /= results[0].orig_img.shape[0] # Normalize by image height # Write to file (Assuming class id is 0 here, change based on your model) f.write(f"0 {x_center} {y_center} {width} {height}\n") print("Process complete.")
代碼詳解:
1.加載YOLOv10模型:使用YOLOv10模型進行物體檢測。通過model.predict()函數(shù)對每張圖像進行檢測。
2.提取檢測結(jié)果:模型返回的結(jié)果中包含了檢測到的目標的坐標信息,采用boxes.xywh提取出目標的位置。
3.坐標歸一化:將檢測結(jié)果的坐標從像素值轉(zhuǎn)換為相對圖像大小的比例(即歸一化值),以符合YOLO標注格式。
4.生成標注文件:根據(jù)模型檢測結(jié)果生成標注文件,格式為class_id x_center y_center width height。
這種方式得到的數(shù)據(jù)一般是下圖這樣的,1.會有一些框不準,2.有一些框是重復的,3.有一些代碼沒有檢測到。此時我們只需要微調(diào)這三種情況的框即可,顯著降低了工作量。
4.總結(jié)
本文介紹了基于YOLOv10的自動標注方法,從初階的固定標注到高階的基于YOLO檢測結(jié)果的自動標注。兩者相比,高階方法顯著提高了標注的準確性,并減少了人工操作的時間。
初階方法:快速生成標注文件,適用于初步標注,但需要人工微調(diào)和修正。
高階方法:通過YOLOv10檢測得到較為精準的標注結(jié)果,適合更高精度要求的任務(wù),但仍需要人工微調(diào)。
希望這些代碼能幫助大家提升標注工作的效率和精度,降低人工標注的成本。在未來的工作中,我們可以繼續(xù)優(yōu)化和擴展該方法,進一步提高自動標注的精度和適用范圍。
到此這篇關(guān)于使用python和yolo方法實現(xiàn)yolo標簽自動標注的文章就介紹到這了,更多相關(guān)python yolo標簽自動標注內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python中ndarray數(shù)組的索引和切片的使用
本文主要介紹了python中ndarray數(shù)組的索引和切片的使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-07-07Python使用logging實現(xiàn)多進程安全的日志模塊
這篇文章主要為大家詳細介紹了Python如何使用標準庫logging實現(xiàn)多進程安全的日志模塊,文中的示例代碼講解詳細,感興趣的小伙伴可以了解下2024-01-01