欢迎访问 生活随笔!

凯发k8官方网

当前位置: 凯发k8官方网 > 运维知识 > ubuntu >内容正文

ubuntu

【yolo】ubuntu18.04 yolo打开摄像头实时检测框目标 转化pth文件为onnx -凯发k8官方网

发布时间:2024/9/30 ubuntu 9 豆豆
凯发k8官方网 收集整理的这篇文章主要介绍了 【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=i1# 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()

总结

以上是凯发k8官方网为你收集整理的【yolo】ubuntu18.04 yolo打开摄像头实时检测框目标 转化pth文件为onnx的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得凯发k8官方网网站内容还不错,欢迎将凯发k8官方网推荐给好友。

  • 上一篇:
  • 下一篇:
网站地图