日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > Ubuntu >内容正文

Ubuntu

【yolo】ubuntu18.04 yolo打开摄像头实时检测框目标 转化pth文件为onnx

發(fā)布時(shí)間:2024/9/30 Ubuntu 120 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【yolo】ubuntu18.04 yolo打开摄像头实时检测框目标 转化pth文件为onnx 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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’

實(shí)時(shí)檢測框目標(biāo)

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') # 權(quán)重文件 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) # 加載網(wǎng)絡(luò)、配置權(quán)重 net = cv.dnn.readNetFromONNX(weightsPath) # # 利用下載的文件 print("[INFO] loading YOLO from disk...") # # 可以打印下信息 #打開攝像頭,讀取視頻 cv.namedWindow("Photo_Detect") #定義一個(gè)窗口 video=cv.VideoCapture(0) #捕獲攝像頭圖像 0位默認(rèn)的攝像頭 筆記本的自帶攝像頭 1為外界攝像頭 def object_dect(img):blobImg = cv.dnn.blobFromImage(img, 1.0 / 255.0, (416, 416), None, True,False) # # net需要的輸入是blob格式的,用blobFromImage這個(gè)函數(shù)來轉(zhuǎn)格式net.setInput(blobImg) # # 調(diào)用setInput函數(shù)將圖片送入輸入層# 獲取網(wǎng)絡(luò)輸出層信息(所有輸出層的名字),設(shè)定并前向傳播outInfo = net.getUnconnectedOutLayersNames() # # 前面的yolov3架構(gòu)也講了,yolo在每個(gè)scale都有輸出,outInfo是每個(gè)scale的名字信息,供net.forward使用layerOutputs = net.forward(outInfo) # 得到各個(gè)輸出層的、各個(gè)檢測框等信息,是二維結(jié)構(gòu)。(H, W) = img.shape[:2]boxes = [] # 所有邊界框(各層結(jié)果放一起)confidences = [] # 所有置信度classIDs = [] # 所有分類IDfor out in layerOutputs: # 各個(gè)輸出層for detection in out: # 各個(gè)框框# 拿到置信度scores = detection[5:] # 各個(gè)類別的置信度classID = np.argmax(scores) # 最高置信度的id即為分類idconfidence = scores[classID] # 拿到置信度# 根據(jù)置信度篩查if confidence > CONFIDENCE:box = detection[0:4] * np.array([W, H, W, H]) # 將邊界框放會(huì)圖片尺寸(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)應(yīng)用非最大值抑制(non-maxima suppression,nms)進(jìn)一步篩掉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')# 應(yīng)用檢測結(jié)果np.random.seed(42)COLORS = np.random.randint(0, 255, size=(len(labels), 3),dtype="uint8") # 框框顯示顏色,每一類有不同的顏色,每種顏色都是由RGB三個(gè)值組成的,所以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) # 線條粗細(xì)為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字體風(fēng)格、0.5字體大小、粗細(xì)2pxcv.imshow('detected image', img) #循環(huán)攝像頭的視頻 while(True): #值為1不斷讀取圖像ret, img = video.read() #視頻捕獲幀object_dect(img)if cv.waitKey(1) & 0xFF == ord('Q'): #按Q關(guān)閉所有窗口 一次沒反應(yīng)的話就多按幾下break #執(zhí)行完后釋放窗口 video.release() # 釋放捕獲 cv.destroyAllWindows() # 摧毀全部窗體

轉(zhuǎn)化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()

總結(jié)

以上是生活随笔為你收集整理的【yolo】ubuntu18.04 yolo打开摄像头实时检测框目标 转化pth文件为onnx的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。