程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Implementation of yolo-v3 real-time detection (opencv+python Implementation) -- Improvement -- more understandable

編輯:Python

List of articles

    • 1. Last article YOLO-V3 Real time detection implementation
    • 2. Improvements to the previous article
    • 3. Code combat
      • (1) The network file (.cfg) And read the weight file
      • (2) Reading of class file
      • (3) Loading of network model
      • (4) Get the predicted results
      • (5) Drawing graphics
      • (6) The whole code
      • (7) Prediction of a single picture
      • (8) Real time detection

1. Last article YOLO-V3 Real time detection implementation

https://blog.csdn.net/Keep_Trying_Go/article/details/125503943


2. Improvements to the previous article

# Get network model
model=cv2.dnn_DetectionModel(net)
# Set the input parameters of the network
model.setInputParams(scale=1/255,size=(416,416))
# To make predictions
class_id,scores,boxes=model.detect(frame,confThreshold=Confidence_thresh,
nmsThreshold=Nms_thresh)
# Returns the category and coordinates of the forecast
return class_id,scores,boxes

There is no need to store the conversion of the prediction frame coordinates , Relevant ( coordinate , Height and width , Degree of confidence , Probability values and categories ) Re traverse and store , Get the output directly .


3. Code combat

(1) The network file (.cfg) And read the weight file

# Read YOLO-V3 Weight file and network configuration file
net=cv2.dnn.readNet(model='dnn_model/yolov3.weights',config='dnn_model/yolov3.cfg')

(2) Reading of class file

# Set the confidence threshold and the threshold of non maximum suppression
Confidence_thresh=0.2
Nms_thresh=0.35
# Read coco.names Categories in files
with open('dnn_model/coco.names','r') as fp:
classes=fp.read().splitlines()

(3) Loading of network model

#yolo-v3 testing 
def detect(frame):
# Get network model
model=cv2.dnn_DetectionModel(net)
# Set the input parameters of the network
model.setInputParams(scale=1/255,size=(416,416))
# To make predictions
class_id,scores,boxes=model.detect(frame,confThreshold=Confidence_thresh,
nmsThreshold=Nms_thresh)
# Returns the category and coordinates of the forecast
return class_id,scores,boxes

(4) Get the predicted results

# To make predictions
class_ids,scores,boxes=detect(frame)

(5) Drawing graphics

# Draw rectangle
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)

(6) The whole code

import os
import cv2
import numpy as np
# create a window
# cv2.namedWindow(winname='detect',flags=cv2.WINDOW_AUTOSIZE)
# cv2.resizeWindow(winname='detect',width=750,height=600)
# Read YOLO-V3 Weight file and network configuration file
net=cv2.dnn.readNet(model='dnn_model/yolov3.weights',config='dnn_model/yolov3.cfg')
# Set the confidence threshold and the threshold of non maximum suppression
Confidence_thresh=0.2
Nms_thresh=0.35
# Read coco.names Categories in files
with open('dnn_model/coco.names','r') as fp:
classes=fp.read().splitlines()
#yolo-v3 testing 
def detect(frame):
# Get network model
model=cv2.dnn_DetectionModel(net)
# Set the input parameters of the network
model.setInputParams(scale=1/255,size=(416,416))
# To make predictions
class_id,scores,boxes=model.detect(frame,confThreshold=Confidence_thresh,
nmsThreshold=Nms_thresh)
# Returns the category and coordinates of the forecast
return class_id,scores,boxes
# Real time detection
def detect_time():
# Turn on camera '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))
# To make predictions
class_ids,scores,boxes=detect(frame)
# Draw rectangle
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()
# Single picture detection
def signal_detect(image_path='images/face1.jpg'):
frame=cv2.imread(image_path)
frame = cv2.resize(src=frame, dsize=(416, 416))
# To make predictions
class_ids, scores, boxes = detect(frame)
# Draw rectangle
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()

(7) Prediction of a single picture

(8) Real time detection

yolov3 Video demo 1


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved