【yolo】ubuntu18.04 yolo打开摄像头实时检测框目标 转化pth文件为onnx
生活随笔
收集整理的這篇文章主要介紹了
【yolo】ubuntu18.04 yolo打开摄像头实时检测框目标 转化pth文件为onnx
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
heziyi@heziyi-ZenBook-UX425IA-U4700IA:~/桌面/PyTorch-YOLOv3$ python3 video.py
yolov3_ckpt_69.onnx
Traceback (most recent call last):
File “video.py”, line 18, in
net = cv.dnn.readNetFromONNX(weightsPath) # # 利用下載的文件
cv2.error: OpenCV(4.1.2) /io/opencv/modules/dnn/src/dnn.cpp:525: error: (-2:Unspecified error) Can’t create layer “106” of type “ConstantOfShape” in function ‘getLayerInstance’
實時檢測框目標
import numpy as np import cv2 as cv import os import time path = r'weights' path2=r'config' path3=r'data/custom' #weightsPath = os.path.join(path2, 'yolov3_ckpt_69.pth') # 權重文件 weightsPath = os.path.join(path,"yolov3.weights") configPath = os.path.join(path2, 'yolov3.cfg') # 配置文件 labelsPath = os.path.join(path3, 'classes.names') # label名稱 CONFIDENCE = 0.5 # 過濾弱檢測的最小概率 THRESHOLD = 0.4 # 非最大值抑制閾值 print(weightsPath) # 加載網絡、配置權重 net = cv.dnn.readNetFromONNX(weightsPath) # # 利用下載的文件 print("[INFO] loading YOLO from disk...") # # 可以打印下信息 #打開攝像頭,讀取視頻 cv.namedWindow("Photo_Detect") #定義一個窗口 video=cv.VideoCapture(0) #捕獲攝像頭圖像 0位默認的攝像頭 筆記本的自帶攝像頭 1為外界攝像頭 def object_dect(img):blobImg = cv.dnn.blobFromImage(img, 1.0 / 255.0, (416, 416), None, True,False) # # net需要的輸入是blob格式的,用blobFromImage這個函數來轉格式net.setInput(blobImg) # # 調用setInput函數將圖片送入輸入層# 獲取網絡輸出層信息(所有輸出層的名字),設定并前向傳播outInfo = net.getUnconnectedOutLayersNames() # # 前面的yolov3架構也講了,yolo在每個scale都有輸出,outInfo是每個scale的名字信息,供net.forward使用layerOutputs = net.forward(outInfo) # 得到各個輸出層的、各個檢測框等信息,是二維結構。(H, W) = img.shape[:2]boxes = [] # 所有邊界框(各層結果放一起)confidences = [] # 所有置信度classIDs = [] # 所有分類IDfor out in layerOutputs: # 各個輸出層for detection in out: # 各個框框# 拿到置信度scores = detection[5:] # 各個類別的置信度classID = np.argmax(scores) # 最高置信度的id即為分類idconfidence = scores[classID] # 拿到置信度# 根據置信度篩查if confidence > CONFIDENCE:box = detection[0:4] * np.array([W, H, W, H]) # 將邊界框放會圖片尺寸(centerX, centerY, width, height) = box.astype("int")x = int(centerX - (width / 2))y = int(centerY - (height / 2))boxes.append([x, y, int(width), int(height)])confidences.append(float(confidence))classIDs.append(classID)# # 2)應用非最大值抑制(non-maxima suppression,nms)進一步篩掉idxs = cv.dnn.NMSBoxes(boxes, confidences, CONFIDENCE, THRESHOLD) # boxes中,保留的box的索引index存入idxs# 得到labels列表with open(labelsPath, 'rt') as f:labels = f.read().rstrip('\n').split('\n')# 應用檢測結果np.random.seed(42)COLORS = np.random.randint(0, 255, size=(len(labels), 3),dtype="uint8") # 框框顯示顏色,每一類有不同的顏色,每種顏色都是由RGB三個值組成的,所以size為(len(labels), 3)if len(idxs) > 0:for i in idxs.flatten(): # indxs是二維的,第0維是輸出層,所以這里把它展平成1維(x, y) = (boxes[i][0], boxes[i][1])(w, h) = (boxes[i][2], boxes[i][3])color = [int(c) for c in COLORS[classIDs[i]]]cv.rectangle(img, (x, y), (x + w, y + h), color, 2) # 線條粗細為2pxtext = "{}: {:.4f}".format(labels[classIDs[i]], confidences[i])cv.putText(img, text, (x, y - 5), cv.FONT_HERSHEY_SIMPLEX, 0.5, color,2) # cv.FONT_HERSHEY_SIMPLEX字體風格、0.5字體大小、粗細2pxcv.imshow('detected image', img) #循環攝像頭的視頻 while(True): #值為1不斷讀取圖像ret, img = video.read() #視頻捕獲幀object_dect(img)if cv.waitKey(1) & 0xFF == ord('Q'): #按Q關閉所有窗口 一次沒反應的話就多按幾下break #執行完后釋放窗口 video.release() # 釋放捕獲 cv.destroyAllWindows() # 摧毀全部窗體轉化pth為onnx
from __future__ import division import torch import torch.onnx#from conf import settings import os 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 NullLocatorfrom models import * from utils.utils import * from utils.datasets import * from utils.augmentations import * from utils.transforms import *import time from time import strftime import cv2 def pth_to_onnx(input, checkpoint, onnx_path, input_names=['input'], output_names=['output'], device='cpu'):parser = argparse.ArgumentParser()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="yolov3_ckpt_69.pth", help="path to weights 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", default="yolov3_ckpt_69.pth",type=str, help="path to checkpoint model")opt = parser.parse_args()if not onnx_path.endswith('.onnx'):print('Warning! The onnx model name is not correct,\please give a name that ends with \'.onnx\'!')return 0model = Darknet(opt.model_def, img_size=opt.img_size)state_dict = torch.load('yolov3_ckpt_69.pth',map_location=torch.device('cpu') )model.load_state_dict(state_dict)model.eval()# model.to(device)torch.onnx.export(model, input, onnx_path, verbose=True, input_names=input_names, output_names=output_names,opset_version=11)print("Exporting .pth model to onnx model has been successful!")def read():#os.environ['CUDA_VISIBLE_DEVICES']='2'checkpoint = r'yolov3_ckpt_69.pth'onnx_path = r'yolov3_ckpt_69.onnx'input = torch.randn(1,3,416,416)# device = torch.device("cuda:2" if torch.cuda.is_available() else 'cpu')pth_to_onnx(input, checkpoint, onnx_path) if __name__ == "__main__":read()讀取顯示圖片
from __future__ import divisionfrom models import * from utils.utils import * from utils.datasets import * from utils.augmentations import * from utils.transforms import *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 NullLocatorimport time from time import strftime import cv2 url = 'http://192.168.1.108:8080/video' i=0 cap = cv2.VideoCapture(url) start = time.time() while(cap.isOpened()):i=i+1# Capture frame-by-frameret, frame = cap.read()# Display the resulting framecv2.imshow('frame',frame)end = time.time()cv2.imwrite('/home/heziyi/桌面/PyTorch-YOLOv3/data/custom/dd/'+"my"+".jpg",frame)if cv2.waitKey(1) & 0xFF == ord('q'):breakif(end - start)==1:break # When everything done, release the capture cap.release() cv2.destroyAllWindows()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("cpu")os.makedirs("output", exist_ok=True)# Set up modelmodel = 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 = torch.load(model_path, map_location='cpu')model.load_state_dict(torch.load(opt.weights_path,map_location='cpu'))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 - y1color = 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(x1))print(int(x2))# 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")plt.savefig(output_path, bbox_inches="tight", pad_inches=0.0)bb=cv2.imread(output_path)cv2.putText(bb, strftime("%H:%M:%S"), (10,70), cv2.FONT_HERSHEY_SIMPLEX, 2,(0,255,0),2,cv2.LINE_AA)cv2.imshow("after",bb)cv2.waitKey(0)plt.close()總結
以上是生活随笔為你收集整理的【yolo】ubuntu18.04 yolo打开摄像头实时检测框目标 转化pth文件为onnx的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【pytorch】pytorch-yol
- 下一篇: 【jetsonnano】jetsonna