过程记录 yolov3目标检测输出目标坐标和原图物体像素坐标比较
生活随笔
收集整理的這篇文章主要介紹了
过程记录 yolov3目标检测输出目标坐标和原图物体像素坐标比较
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
設備:jetsonnano B01 一個usb廣角攝像頭
語言為python 庫為opencv 框架pytorch
大家都知道yolov3的坐標變換過程比較復雜,大概要經過圖片縮放,對數空間變換等等,然后在pytorch中是用一個tensor類型來表示張量的,所以一直認為目標檢測的坐標是一個奇奇怪怪的值,需要經過變換才能得到原圖目標的像素坐標
之前一直糾結怎么坐標轉化,但今天實驗之后懷疑其實檢測結果的坐標轉化為int數值以后跟實際原圖像素坐標是一樣的?
先上結果:
這是原始圖片上的坐標點
左下角為像素坐標
(鼠標放在中間油漬的位置時顯示的坐標
檢測結果:
結果輸出的坐標(轉化為整數之后輸出的)
兩個坐標幾乎一樣,感覺其實它們兩個是一個數值,而tensor類型后面多了一串小數點
完整代碼
用opencv打開攝像頭查看圖片某點像素坐標:
檢測部分顯示結果坐標:
pytorch python
關于目標檢測的完整檢測代碼:(很長,熟悉的人不必看)
from __future__ import division import serial as ser import time se=ser.Serial("/dev/ttyUSB0",115200,timeout=1) from models import * from utils.utils import * from utils.datasets import * from utils.augmentations import * from utils.transforms import * import cv2 import os import sys import time import datetime import argparsefrom PIL import Imageimport torch import torchvision.transforms as transforms from torch.utils.data import DataLoader from torchvision import datasets from torch.autograd import Variableimport matplotlib.pyplot as plt import matplotlib.patches as patches from matplotlib.ticker import NullLocator i = 1 cap = cv2.VideoCapture(0) array_of_img = [] start = time.time() directory_name=r'output'while(cap.isOpened()):ret, frame = cap.read()cv2.imshow('frame',frame)if cv2.waitKey(30) == ord('q'):#ret, frame = cap.read()cv2.imwrite('data/custom/dd/'+str(i)+".jpg",frame) frame=cv2.imread('data/custom/dd/'+str(i)+".jpg")break # When everything done, release the capturecap.release()cv2.destroyAllWindows()washtime = [] thex = [] they = [] area = 0 str1="" if __name__ == "__main__":parser = argparse.ArgumentParser()parser.add_argument("--image_folder", type=str, default="data/samples", help="path to dataset")parser.add_argument("--model_def", type=str, default="config/yolov3.cfg", help="path to model definition file")parser.add_argument("--weights_path", type=str, default="weights/yolov3.weights", help="path to weights file")parser.add_argument("--class_path", type=str, default="data/coco.names", help="path to class label file")parser.add_argument("--conf_thres", type=float, default=0.8, help="object confidence threshold")parser.add_argument("--nms_thres", type=float, default=0.4, help="iou thresshold for non-maximum suppression")parser.add_argument("--batch_size", type=int, default=1, help="size of the batches")parser.add_argument("--n_cpu", type=int, default=0, help="number of cpu threads to use during batch generation")parser.add_argument("--img_size", type=int, default=416, help="size of each image dimension")parser.add_argument("--checkpoint_model", type=str, help="path to checkpoint model")opt = parser.parse_args()print(opt)device = torch.device("cuda" if torch.cuda.is_available() else "cpu")os.makedirs("output", exist_ok=True)# Set up modellamodel = Darknet(opt.model_def, img_size=opt.img_size).to(device)if opt.weights_path.endswith(".weights"):# Load darknet weightsmodel.load_darknet_weights(opt.weights_path)else:# Load checkpoint weightsmodel.load_state_dict(torch.load(opt.weights_path))model.eval() # Set in evaluation modedataloader = DataLoader(ImageFolder(opt.image_folder, transform= \transforms.Compose([DEFAULT_TRANSFORMS, Resize(opt.img_size)])),batch_size=opt.batch_size,shuffle=False,num_workers=opt.n_cpu,)classes = load_classes(opt.class_path) # Extracts class labels from fileTensor = torch.cuda.FloatTensor if torch.cuda.is_available() else torch.FloatTensorimgs = [] # Stores image pathsimg_detections = [] # Stores detections for each image indexprint("\nPerforming object detection:")prev_time = time.time()for batch_i, (img_paths, input_imgs) in enumerate(dataloader):# Configure inputinput_imgs = Variable(input_imgs.type(Tensor))# Get detectionswith torch.no_grad():detections = model(input_imgs)detections = non_max_suppression(detections, opt.conf_thres, opt.nms_thres)# Log progresscurrent_time = time.time()inference_time = datetime.timedelta(seconds=current_time - prev_time)prev_time = current_timeprint("\t+ Batch %d, Inference Time: %s" % (batch_i, inference_time))# Save image and detectionsimgs.extend(img_paths)img_detections.extend(detections)# Bounding-box colorscmap = plt.get_cmap("tab20b")colors = [cmap(i) for i in np.linspace(0, 1, 20)]print("\nSaving images:")# Iterate through images and save plot of detectionsfor img_i, (path, detections) in enumerate(zip(imgs, img_detections)):print("(%d) Image: '%s'" % (img_i, path))# Create plotimg = np.array(Image.open(path))plt.figure()fig, ax = plt.subplots(1)ax.imshow(img)# Draw bounding boxes and labels of detectionsif detections is not None:# Rescale boxes to original imagedetections = rescale_boxes(detections, opt.img_size, img.shape[:2])unique_labels = detections[:, -1].cpu().unique()n_cls_preds = len(unique_labels)bbox_colors = random.sample(colors, n_cls_preds)for x1, y1, x2, y2, conf, cls_conf, cls_pred in detections:print("\t+ Label: %s, Conf: %.5f" % (classes[int(cls_pred)], cls_conf.item())) box_w = x2 - x1box_h = y2 - y1centx = int((x1+x2)*0.1/2)centy = int((y1+y2)*0.1/2)thex.append(centx)they.append(centy)str1+="x:"str1+=str(centx)str1+=",y:"str1+=str(centy)area = int(box_w*0.1)*int(box_h*0.1)if area > 600: time = 30print("x和y"+str((x2+x1)/2)+","+str((y1+y2)/2))print("x:")print(int((x1+x2)/2))print("y:")print(int((y1+y2)/2))print(int(box_w*0.1))print(int(box_h*0.1))color = bbox_colors[int(np.where(unique_labels == int(cls_pred))[0])]# Create a Rectangle patchbbox = patches.Rectangle((x1, y1), box_w, box_h, linewidth=2, edgecolor=color, facecolor="none")print(int(box_w)*int(box_h)*0.01)print(str1)if(box_w*box_h>50):se.write("1".encode())time.sleep(3)se.write("0".encode())se.write(str1.encode())data_read = se.readline()print(data_read.decode())# Add the bbox to the plotax.add_patch(bbox)# Add labelplt.text(x1,y1,s=classes[int(cls_pred)],color="white",verticalalignment="top",bbox={"color": color, "pad": 0},)# Save generated image with detectionsplt.axis("off")plt.gca().xaxis.set_major_locator(NullLocator())plt.gca().yaxis.set_major_locator(NullLocator())filename = os.path.basename(path).split(".")[0]#output_path = os.path.join("output", f"{filename}.png")#output_path = os.path.join("/data/custom/dd", f"{filename}.png")plt.savefig(filename, bbox_inches="tight", pad_inches=0.0)plt.close()串口:
se.write("50".encode()) print("緩存中的字節數") print(se.inWaiting()) time.sleep(2) se.write("100".encode()) print("緩存中的字節數") print(se.inWaiting())#:返回接收緩存中的字節數 data = se.readline() print(data.decode()) data = se.readline() print(data.decode()) print(se.inWaiting()) se.write("my87u".encode()) print("緩存中的字節數") print(se.inWaiting()) se.flush() #等待所有數據寫出。 data = se.readline() print(data.decode()) data = se.readline() print(data.decode())參數:
parser = argparse.ArgumentParser()parser.add_argument("--image_folder", type=str, default="data/custom/dd2", help="path to dataset")parser.add_argument("--model_def", type=str, default="config/yolov3-custom.cfg", help="path to model definition file")parser.add_argument("--weights_path", type=str, default="checkpoints/ckpt_86.pth", help="path to weights file")parser.add_argument("--class_path", type=str, default="data/custom/classes.names", help="path to class label file")parser.add_argument("--conf_thres", type=float, default=0.8, help="object confidence threshold")parser.add_argument("--nms_thres", type=float, default=0.4, help="iou thresshold for non-maximum suppression")parser.add_argument("--batch_size", type=int, default=1, help="size of the batches")parser.add_argument("--n_cpu", type=int, default=0, help="number of cpu threads to use during batch generation")parser.add_argument("--img_size", type=int, default=416, help="size of each image dimension")parser.add_argument("--checkpoint_model",type=str,default="checkpoints/ckpt_86.pth", help="path to checkpoint model")opt = parser.parse_args()print(opt)總結
以上是生活随笔為你收集整理的过程记录 yolov3目标检测输出目标坐标和原图物体像素坐标比较的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【过程记录】aop学习·实现动态代理的j
- 下一篇: 【java学习笔记】java se8的流