https://blog.csdn.net/Keep_Trying_Go/article/details/125503943
#獲取網絡模型
model=cv2.dnn_DetectionModel(net)
#設置網絡的輸入參數
model.setInputParams(scale=1/255,size=(416,416))
#進行預測
class_id,scores,boxes=model.detect(frame,confThreshold=Confidence_thresh,
nmsThreshold=Nms_thresh)
#返回預測的類別和坐標
return class_id,scores,boxes
不需要存儲預測框坐標的轉換,需要進行相關 (坐標,高寬,置信度,概率值以及類別)進行重新的遍歷和存儲,直接得到輸出的結果。
#讀取YOLO-V3權重文件和網絡配置文件
net=cv2.dnn.readNet(model='dnn_model/yolov3.weights',config='dnn_model/yolov3.cfg')
#設置置信度阈值和非極大值抑制的阈值
Confidence_thresh=0.2
Nms_thresh=0.35
#讀取coco.names文件中的類別
with open('dnn_model/coco.names','r') as fp:
classes=fp.read().splitlines()
#yolo-v3檢測
def detect(frame):
#獲取網絡模型
model=cv2.dnn_DetectionModel(net)
#設置網絡的輸入參數
model.setInputParams(scale=1/255,size=(416,416))
#進行預測
class_id,scores,boxes=model.detect(frame,confThreshold=Confidence_thresh,
nmsThreshold=Nms_thresh)
#返回預測的類別和坐標
return class_id,scores,boxes
#進行預測
class_ids,scores,boxes=detect(frame)
#繪制矩形框
for (class_id,box) in enumerate(boxes):
(x,y,w,h)=box
class_name=classes[class_ids[class_id]]
confidence=scores[class_id]
confidence=str(round(confidence,2))
cv2.rectangle(img=frame,pt1=(x,y),pt2=(x+w,y+h),
color=(0,255,0),thickness=2)
text=class_name+' '+confidence
cv2.putText(img=frame,text=text,
org=(x,y-10),fontFace=cv2.FONT_HERSHEY_SIMPLEX,
fontScale=1.0,color=(0,255,0),thickness=2)
import os
import cv2
import numpy as np
#創建窗口
# cv2.namedWindow(winname='detect',flags=cv2.WINDOW_AUTOSIZE)
# cv2.resizeWindow(winname='detect',width=750,height=600)
#讀取YOLO-V3權重文件和網絡配置文件
net=cv2.dnn.readNet(model='dnn_model/yolov3.weights',config='dnn_model/yolov3.cfg')
#設置置信度阈值和非極大值抑制的阈值
Confidence_thresh=0.2
Nms_thresh=0.35
#讀取coco.names文件中的類別
with open('dnn_model/coco.names','r') as fp:
classes=fp.read().splitlines()
#yolo-v3檢測
def detect(frame):
#獲取網絡模型
model=cv2.dnn_DetectionModel(net)
#設置網絡的輸入參數
model.setInputParams(scale=1/255,size=(416,416))
#進行預測
class_id,scores,boxes=model.detect(frame,confThreshold=Confidence_thresh,
nmsThreshold=Nms_thresh)
#返回預測的類別和坐標
return class_id,scores,boxes
#實時的檢測
def detect_time():
#開啟攝像頭 'video/los_angeles.mp4' or 'video/soccer.mp4'
cap=cv2.VideoCapture(0)
while cap.isOpened():
OK,frame=cap.read()
if not OK:
break
frame=cv2.flip(src=frame,flipCode=2)
# frame=cv2.resize(src=frame,dsize=(416,416))
#進行預測
class_ids,scores,boxes=detect(frame)
#繪制矩形框
for (class_id,box) in enumerate(boxes):
(x,y,w,h)=box
class_name = classes[class_ids[class_id]]
confidence = scores[class_id]
confidence=str(round(confidence,2))
cv2.rectangle(img=frame,pt1=(x,y),pt2=(x+w,y+h),
color=(0,255,0),thickness=2)
text=class_name+' '+confidence
cv2.putText(img=frame,text=text,
org=(x,y-10),fontFace=cv2.FONT_HERSHEY_SIMPLEX,
fontScale=1.0,color=(0,255,0),thickness=2)
cv2.imshow('detect',frame)
key=cv2.waitKey(1)
if key==27:
break
cap.release()
#單張圖片的檢測
def signal_detect(image_path='images/face1.jpg'):
frame=cv2.imread(image_path)
frame = cv2.resize(src=frame, dsize=(416, 416))
# 進行預測
class_ids, scores, boxes = detect(frame)
# 繪制矩形框
for (class_id, box) in enumerate(boxes):
(x, y, w, h) = box
class_name = classes[class_ids[class_id]]
confidence = scores[class_ids[class_id]]
confidence = str(round(confidence, 2))
cv2.rectangle(img=frame, pt1=(x, y), pt2=(x + w, y + h),
color=(0, 255, 0), thickness=2)
text = class_name + ' ' + confidence
cv2.putText(img=frame, text=text,
org=(x, y - 10), fontFace=cv2.FONT_HERSHEY_SIMPLEX,
fontScale=1.0, color=(0, 255, 0), thickness=2)
cv2.imshow('detect', frame)
cv2.waitKey(0)
cv2.destroyAllWindows()
if __name__ == '__main__':
print('Pycharm')
# signal_detect()
detect_time()
yolov3視頻演示1