使用YOLO训练后如何裁剪检测到的物体?
How to crop the detected object after training using YOLO?
我正在使用 YOLO 进行模型训练。我想裁剪检测到的对象。
对于 Darknet 存储库,我使用的是:https://github.com/AlexeyAB/darknet/
为了检测输出坐标并将其存储在文本文件中,我使用的是:
!./darknet detector test data_for_colab/obj.data data_for_colab/yolov3-tiny-obj.cfg yolov3-tiny-obj_10000.weights -dont_show -ext_output < TEST.txt > result.txt
Result.jpg
考虑到 TEST.txt 文件中您有详细信息作为示例图像。
您可以使用 python 的 re 模块进行文本模式检测,即您的 "class_name".
正在解析 .txt 文件
import re
path='/content/darknet/result.txt'
myfile=open(path,'r')
lines=myfile.readlines()
pattern= "class_name"
for line in lines:
if re.search(pattern,line):
Cord_Raw=line
Cord=Cord_Raw.split("(")[1].split(")")[0].split(" ")
现在我们将获取列表中的坐标。
坐标计算
x_min=int(Cord[1])
x_max=x_min + int(Cord[5])
y_min=int(Cord[3])
y_max=y_min+ int(Cord[7])
从实际图像裁剪
import cv2
img = cv2.imread("Image.jpg")
crop_img = img[y_min:y_max, x_min:x_max]
cv2.imwrite("Object.jpg",crop_img)
我正在使用 YOLO 进行模型训练。我想裁剪检测到的对象。 对于 Darknet 存储库,我使用的是:https://github.com/AlexeyAB/darknet/
为了检测输出坐标并将其存储在文本文件中,我使用的是: !./darknet detector test data_for_colab/obj.data data_for_colab/yolov3-tiny-obj.cfg yolov3-tiny-obj_10000.weights -dont_show -ext_output < TEST.txt > result.txt Result.jpg
考虑到 TEST.txt 文件中您有详细信息作为示例图像。 您可以使用 python 的 re 模块进行文本模式检测,即您的 "class_name".
正在解析 .txt 文件
import re
path='/content/darknet/result.txt'
myfile=open(path,'r')
lines=myfile.readlines()
pattern= "class_name"
for line in lines:
if re.search(pattern,line):
Cord_Raw=line
Cord=Cord_Raw.split("(")[1].split(")")[0].split(" ")
现在我们将获取列表中的坐标。
坐标计算
x_min=int(Cord[1])
x_max=x_min + int(Cord[5])
y_min=int(Cord[3])
y_max=y_min+ int(Cord[7])
从实际图像裁剪
import cv2
img = cv2.imread("Image.jpg")
crop_img = img[y_min:y_max, x_min:x_max]
cv2.imwrite("Object.jpg",crop_img)