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 = [] # 所有分类ID
for out in layerOutputs: # 各个输出层
for detection in out: # 各个框框
# 拿到置信度
scores = detection[5:] # 各个类别的置信度
classID = np.argmax(scores) # 最高置信度的id即为分类id
confidence = 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) # 线条粗细为2px
text = "{}: {:.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字体大小、粗细2px
cv.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 argparse
from PIL import Image
import torch
import torchvision.transforms as transforms
from torch.utils.data import DataLoader
from torchvision import datasets
from torch.autograd import Variable
import matplotlib.pyplot as plt
import matplotlib.patches as patches
from matplotlib.ticker import NullLocator
from 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 0
model = 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 division
from 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 argparse
from PIL import Image
import torch
import torchvision.transforms as transforms
from torch.utils.data import DataLoader
from torchvision import datasets
from torch.autograd import Variable
import matplotlib.pyplot as plt
import matplotlib.patches as patches
from matplotlib.ticker import NullLocator
import 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-frame
ret, frame = cap.read()
# Display the resulting frame
cv2.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'):
break
if(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 model
model = Darknet(opt.model_def, img_size=opt.img_size).to(device)
if opt.weights_path.endswith(".weights"):
# Load darknet weights
model.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 mode
dataloader = 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 file
Tensor = torch.cuda.FloatTensor if torch.cuda.is_available() else torch.FloatTensor
imgs = [] # Stores image paths
img_detections = [] # Stores detections for each image index
print("\nPerforming object detection:")
prev_time = time.time()
for batch_i, (img_paths, input_imgs) in enumerate(dataloader):
# Configure input
input_imgs = Variable(input_imgs.type(Tensor))
# Get detections
with torch.no_grad():
detections = model(input_imgs)
detections = non_max_suppression(detections, opt.conf_thres, opt.nms_thres)
# Log progress
current_time = time.time()
inference_time = datetime.timedelta(seconds=current_time - prev_time)
prev_time = current_time
print("\t+ Batch %d, Inference Time: %s" % (batch_i, inference_time))
# Save image and detections
imgs.extend(img_paths)
img_detections.extend(detections)
# Bounding-box colors
cmap = 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 detections
for img_i, (path, detections) in enumerate(zip(imgs, img_detections)):
print("(%d) Image: '%s'" % (img_i, path))
# Create plot
img = np.array(Image.open(path))
plt.figure()
fig, ax = plt.subplots(1)
ax.imshow(img)
# Draw bounding boxes and labels of detections
if detections is not None:
# Rescale boxes to original image
detections = 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 - x1
box_h = y2 - y1
color = bbox_colors[int(np.where(unique_labels == int(cls_pred))[0])]
# Create a Rectangle patch
bbox = 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 plot
ax.add_patch(bbox)
# Add label
plt.text(
x1,
y1,
s=classes[int(cls_pred)],
color="white",
verticalalignment="top",
bbox={"color": color, "pad": 0},
)
# Save generated image with detections
plt.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()