当前位置:网站首页>The GUI interface of yolov3 (3) -- solve the out of memory problem and add camera detection function
The GUI interface of yolov3 (3) -- solve the out of memory problem and add camera detection function
2022-07-25 23:57:00 【perfectdisaster】
I have written two blogs before , The first article writes a very simple GUI Interface , The second part is after a few days PyQt After studying , Optimize and beautify the previous page , At the same time, the function of output identifying object name and corresponding quantity is added . After a few days of thinking and learning , I wrote the Third Edition , Solved the problem left over for the first time out of memory problem , From then on, there is no limit on the number of tests , At the same time, the camera detection function is added ( The principle of video streaming is the same , If necessary, you can modify it in the code I provide next ). Here are also the addresses of the previous two blogs , For everyone to learn .
yolov3 Of GUI Interface ( Simple and easy , Picture detection )
Native configuration :
pyCharm:PyCharm 2022.1.3 (Community Edition)
python 3.6.9
PyQt: Qt5 Version Number is: 5.9.5
PyQt5 Version is: 5.10.1
Sip Version is: 4.19.7
This example applies to PyQt5
see PyQt The version can refer to the blogger's Last blog
One . Interface design : open Qt-designer, Add controls as shown , The name of this example is yolov3Gui.ui, Save in darknet Under the folder :

Modify the control name as follows :( here status_text dial the wrong number , In fact, this is the error of the Second Edition , But I'm too lazy to revise )

take ui File to py file
The input terminal :
pyuic5 -o yolov3Gui.py yolov3Gui. ui
Or use PyUIC conversion Transformed py The documents are as follows :( modify : take imagebtn,videobtn and closebtn The width is changed to 71 To complete the subsequent operation of turning it into a circle )
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'yolov3Gui4.ui'
#
# Created by: PyQt5 UI code generator 5.9.2
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(1317, 907)
self.output_text = QtWidgets.QTextEdit(Form)
self.output_text.setGeometry(QtCore.QRect(840, 90, 441, 591))
self.output_text.setObjectName("output_text")
self.status_tetx = QtWidgets.QTextEdit(Form)
self.status_tetx.setGeometry(QtCore.QRect(870, 550, 381, 111))
self.status_tetx.setObjectName("status_tetx")
self.imagebtn = QtWidgets.QPushButton(Form)
self.imagebtn.setGeometry(QtCore.QRect(842, 760, 71, 71))
self.imagebtn.setObjectName("imagebtn")
self.label = QtWidgets.QLabel(Form)
self.label.setGeometry(QtCore.QRect(850, 50, 111, 16))
self.label.setObjectName("label")
self.image_show_label = QtWidgets.QLabel(Form)
self.image_show_label.setGeometry(QtCore.QRect(40, 90, 751, 771))
self.image_show_label.setObjectName("image_show_label")
self.closebtn = QtWidgets.QPushButton(Form)
self.closebtn.setGeometry(QtCore.QRect(1132, 760, 71, 71))
self.closebtn.setObjectName("closebtn")
self.videobtn = QtWidgets.QPushButton(Form)
self.videobtn.setGeometry(QtCore.QRect(987, 760, 71, 71))
self.videobtn.setObjectName("videobtn")
self.retranslateUi(Form)
self.closebtn.clicked.connect(Form.close)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.status_tetx.setHtml(_translate("Form", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'Ubuntu\'; font-size:11pt; font-weight:400; font-style:normal;\">\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"> Identify program status output </p></body></html>"))
self.imagebtn.setText(_translate("Form", " Picture detection "))
self.label.setText(_translate("Form", " Identify the result output area "))
self.image_show_label.setText(_translate("Form", " Image display "))
self.closebtn.setText(_translate("Form", " sign out "))
self.videobtn.setText(_translate("Form", " Camera detection "))Two . Logic writing : stay darknet New under folder Callyolov3.py file , Copy the following , Respectively in image_demo and video_demo Function to change the loading path to your own path :
import sys
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from yolov3Gui4 import Ui_Form
import cv2
from PIL import Image
import random
import string
import darknet
from collections import Counter
import numpy as np
import os
import time
class yolov3Gui(QMainWindow, Ui_Form):
def __init__(self):
super(yolov3Gui, self).__init__()
self.openfile_name_image = ''
self.image = None
self.setupUi(self)
self.imagebtn.clicked.connect(self.select_image)
self.imagebtn.clicked.connect(self.image_demo)
#self.imagebtn.clicked.connect(self.status)
self.imagebtn.clicked.connect(self.status_image)
self.videobtn.clicked.connect(self.video_demo)
#self.videobtn.clicked.connect(self.status)
self.videobtn.clicked.connect(self.status_video)
self.closebtn.clicked.connect(self.stop)
def status(self):
self.status_tetx.clear()
self.status_tetx.setText("start to output\n")
def status_image(self):
# if self.startbtn.isChecked():
self.status_tetx.setText("image detection")
def status_video(self):
# if self.startbtn.isChecked():
self.status_tetx.setText("video detection")
def detect(self):
# self.image_show_label.clear()
if self.image is None:
print(' No pictures ')
elif self.image is not None:
# Test pictures
# self.image_show_label.clear()
predictions = run_detect(self.openfile_name_image)
# Read the pictures after detection
img = cv2.imread('img/result/' + self.openfile_name_image.split('/')[-1])
# img = cv2.resize(img, (400, 300), interpolation=cv2.INTER_AREA)
img = cv2.resize(img, (751, 771), interpolation=cv2.INTER_AREA)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# cv2.imshow('test', img)
# cv2.waitKey(20)
# Put the picture on the label self.img_show_label in
a = QImage(img.data, img.shape[1], img.shape[0], img.shape[1] * 3, QImage.Format_RGB888)
self.image_show_label.setPixmap(QPixmap.fromImage(a))
self.output_text.clear()
'''for label, confidence, bbox in preditions:
self.output_text.setText(label + ' numbers: ' + confidence )
print(preditions)
print(label)'''
i = len(predictions)
labels = []
for n in range(i):
labels.append(predictions[n][0])
result = Counter(labels)
# listVal = list(result)
text = ''
for key, value in result.items():
# print("label: " + key + " number: " + str(value) + '\n')
text = text + "label: " + key + " number: " + str(value) + '\n'
# print(text)
self.output_text.setText(text)
# print(result)
# print(labels)
# self.output_text.setText(predictions[n][o] + "\n")
# print(predictions[n][0])
pass
def stop(self):
sys.exit(0)
def select_image(self):
# temp Select the path of the file for What opens here is this main.py Function under the same level directory img Folder
temp, _ = QFileDialog.getOpenFileName(self, " Select photo file ", r"./img/")
if temp is not None:
self.openfile_name_image = temp
# Read the selected picture
self.image = cv2.imread(self.openfile_name_image)
# print(self.openfile_name_image)
'''a = QImage(self.image.data, self.image.shape[1], self.image.shape[0], self.image.shape[1] * 3, QImage.Format_RGB888)
self.image_show_label.setPixmap(QPixmap.fromImage(a))'''
def image_demo(self):
# Load the trained model path , It can be absolute path or relative path
weightsPath = "myData/backup/my_yolov3_last.weights"
configPath = "cfg/my_yolov3.cfg"
# labelsPath = "coco.names"
labelsPath = "myData/myData.names"
boxes = []
confidences = []
classIDs = []
# Initialize some parameters
image = cv2.imread(self.openfile_name_image)
LABELS = open(labelsPath).read().strip().split("\n") # Object category
COLORS = np.random.randint(0, 255, size=(len(LABELS), 3), dtype="uint8") # Color
net = cv2.dnn.readNetFromDarknet(configPath, weightsPath)
# Read in the image to be detected
# 0 Is the camera number , If there is only one, the default is 0
'''temp, _ = QFileDialog.getOpenFileName(self, " Select photo file ", r"./img/")
if temp is not None:
self.openfile_name_image = temp
# Read the selected picture '''
(H, W) = image.shape[:2]
# obtain YOLO Required output layer
ln = net.getLayerNames()
ln = [ln[i[0] - 1] for i in net.getUnconnectedOutLayers()]
# Construct a... From the input image blob, Then through the loaded model , Give us the bounding box and the associated probability
blob = cv2.dnn.blobFromImage(image, 1 / 255.0, (416, 416), swapRB=True, crop=False)
net.setInput(blob)
try:
layerOutputs = net.forward(ln)
except KeyboardInterrupt:
print("The End")
# Loop over each layer of output
for output in layerOutputs:
# Cycle each test
for detection in output:
scores = detection[5:]
classID = np.argmax(scores)
confidence = scores[classID]
# Filter out the test results with low confidence
if confidence > 0.5:
# The width and height of the frame after the frame
box = detection[0:4] * np.array([W, H, W, H])
(centerX, centerY, width, height) = box.astype("int")
# The top left corner of the border
x = int(centerX - (width / 2))
y = int(centerY - (height / 2))
# Update the detected box
boxes.append([x, y, int(width), int(height)])
confidences.append(float(confidence))
classIDs.append(classID)
# Maximum suppression
idxs = cv2.dnn.NMSBoxes(boxes, confidences, 0.2, 0.3)
predictions = []
if len(idxs) > 0:
for i in idxs.flatten():
(x, y) = (boxes[i][0], boxes[i][1])
(w, h) = (boxes[i][2], boxes[i][3])
# Draw borders and categories on the original drawing
color = [int(c) for c in COLORS[classIDs[i]]]
cv2.rectangle(image, (x, y), (x + w, y + h), color, 2)
text = "{}: {:.4f}".format(LABELS[classIDs[i]], confidences[i])
cv2.putText(image, text, (x, y - 5), cv2.FONT_HERSHEY_SIMPLEX, 2, color, 3)
predictions.append("{}".format(LABELS[classIDs[i]]))
# print(text)
# cv2.imshow("Image", image)
#print(predictions)
i = len(predictions)
labels = []
for n in range(i):
labels.append(predictions[n])
result = Counter(labels)
# listVal = list(result)
text = ''
for key, value in result.items():
# print("label: " + key + " number: " + str(value) + '\n')
text = text + "label: " + key + " number: " + str(value) + '\n'
# print(text)
self.output_text.setText(text)
image = BGR_to_RGB(image)
image = cv2.resize(image, (751, 771), interpolation=cv2.INTER_AREA)
a = QImage(image.data, image.shape[1], image.shape[0], image.shape[1] * 3, QImage.Format_RGB888)
# a = QImage(image,QImage.Format_RGB888)
self.image_show_label.setPixmap(QPixmap.fromImage(a))
# wait for 30ms Display images , If you press “ESC” sign out
cv2.waitKey(30)
def video_demo(self):
# Load the trained model path , It can be absolute path or relative path
weightsPath = "myData/backup/my_yolov3_last.weights"
configPath = "cfg/my_yolov3.cfg"
# labelsPath = "coco.names"
labelsPath = "myData/myData.names"
# Initialize some parameters
LABELS = open(labelsPath).read().strip().split("\n") # Object category
COLORS = np.random.randint(0, 255, size=(len(LABELS), 3), dtype="uint8") # Color
net = cv2.dnn.readNetFromDarknet(configPath, weightsPath)
# Read in the image to be detected
# 0 Is the camera number , If there is only one, the default is 0
'''temp, _ = QFileDialog.getOpenFileName(self, " Select photo file ", r"./img/")
if temp is not None:
self.openfile_name_image = temp
# Read the selected picture '''
# MP4= cv2.imread(self.openfile_name_image)
#MP4 = 'test.webm'
capture = cv2.VideoCapture(0)
while (True):
boxes = []
confidences = []
classIDs = []
ref, image = capture.read()
(H, W) = image.shape[:2]
# obtain YOLO Required output layer
ln = net.getLayerNames()
ln = [ln[i[0] - 1] for i in net.getUnconnectedOutLayers()]
# Construct a... From the input image blob, Then through the loaded model , Give us the bounding box and the associated probability
blob = cv2.dnn.blobFromImage(image, 1 / 255.0, (416, 416), swapRB=True, crop=False)
net.setInput(blob)
try:
layerOutputs = net.forward(ln)
except KeyboardInterrupt:
print("The End")
# Loop over each layer of output
for output in layerOutputs:
# Cycle each test
for detection in output:
scores = detection[5:]
classID = np.argmax(scores)
confidence = scores[classID]
# Filter out the test results with low confidence
if confidence > 0.5:
# The width and height of the frame after the frame
box = detection[0:4] * np.array([W, H, W, H])
(centerX, centerY, width, height) = box.astype("int")
# The top left corner of the border
x = int(centerX - (width / 2))
y = int(centerY - (height / 2))
# Update the detected box
boxes.append([x, y, int(width), int(height)])
confidences.append(float(confidence))
classIDs.append(classID)
# Maximum suppression
idxs = cv2.dnn.NMSBoxes(boxes, confidences, 0.2, 0.3)
predictions = []
if len(idxs) > 0:
for i in idxs.flatten():
(x, y) = (boxes[i][0], boxes[i][1])
(w, h) = (boxes[i][2], boxes[i][3])
# Draw borders and categories on the original drawing
color = [int(c) for c in COLORS[classIDs[i]]]
cv2.rectangle(image, (x, y), (x + w, y + h), color, 2)
text = "{}: {:.4f}".format(LABELS[classIDs[i]], confidences[i])
cv2.putText(image, text, (x, y - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
predictions.append("{}".format(LABELS[classIDs[i]]))
#print(predictions)
i = len(predictions)
labels = []
for n in range(i):
labels.append(predictions[n])
result = Counter(labels)
# listVal = list(result)
text = ''
for key, value in result.items():
# print("label: " + key + " number: " + str(value) + '\n')
text = text + "label: " + key + " number: " + str(value) + '\n'
# print(text)
self.output_text.setText(text)
# cv2.imshow("Image", image)
image = BGR_to_RGB(image)
image = cv2.resize(image, (751, 771), interpolation=cv2.INTER_AREA)
a = QImage(image.data, image.shape[1], image.shape[0], image.shape[1] * 3, QImage.Format_RGB888)
# a = QImage(image,QImage.Format_RGB888)
self.image_show_label.setPixmap(QPixmap.fromImage(a))
# wait for 30ms Display images
cv2.waitKey(30)
def BGR_to_RGB(cvimg):
pilimg = cvimg.copy()
pilimg[:, :, 0] = cvimg[:, :, 2]
pilimg[:, :, 2] = cvimg[:, :, 0]
return pilimg
if __name__ == '__main__':
app = QApplication(sys.argv)
yG = yolov3Gui()
yG.setWindowTitle("yolov3Gui")
qssStyle = """
#imagebtn{
background-color:orange;
border-radius:35px;
border:2px groover gray;
}
#closebtn{
background-color:orange;
border-radius:35px;
border:2px groover gray;
}
#videobtn{
background-color:orange;
border-radius:35px;
border:2px groover gray;
}
#output_text{
background-color:gray;
}
#image_show_label{
background-color:gray;
}
#status_tetx{
background-color:gray;
}
"""
yG.setStyleSheet(qssStyle)
try:
yG.show()
sys.exit(app.exec_())
except KeyboardInterrupt:
print("The end")
1. solve out of memory problem : In these days of modification and debugging , I found that every time the first version of the program detects images, it needs to reload the model file , This should be the cause out of memory The culprit of , So that when I write the function of camera detection with the original idea, the program can run without any output , So we need to change our thinking , restart .
In the first version of the program , My idea is to call detect In the document image_detection Function to detect pictures , Save the test results to result Folder , Then read and display to GUI In the interface ,detect The documents are as follows ( Real batch inspection ):
import argparse
import os
import glob
import random
import darknet
import time
import cv2
import numpy as np
import darknet
'''def parser():
parser = argparse.ArgumentParser(description="YOLO Object Detection")
parser.add_argument("--input", type=str, default="",
help="image source. It can be a single image, a"
"txt with paths to them, or a folder. Image valid"
" formats are jpg, jpeg or png."
"If no input is given, ")
parser.add_argument("--batch_size", default=1, type=int,
help="number of images to be processed at the same time")
parser.add_argument("--weights", default="yolov4.weights",
help="yolo weights path")
parser.add_argument("--dont_show", action='store_true',
help="windown inference display. For headless systems")
parser.add_argument("--ext_output", action='store_true',
help="display bbox coordinates of detected objects")
parser.add_argument("--save_labels", action='store_true',
help="save detections bbox for each image in yolo format")
parser.add_argument("--config_file", default="./cfg/yolov4.cfg",
help="path to config file")
parser.add_argument("--data_file", default="./cfg/coco.data",
help="path to data file")
parser.add_argument("--thresh", type=float, default=.25,
help="remove detections with lower confidence")
return parser.parse_args()'''
def parser():
parser = argparse.ArgumentParser(description="YOLO Object Detection")
parser.add_argument("--input", type=str, default="",
help="image source. It can be a single image, a"
"txt with paths to them, or a folder. Image valid"
" formats are jpg, jpeg or png."
"If no input is given, ")
parser.add_argument("--batch_size", default=1, type=int,
help="number of images to be processed at the same time")
parser.add_argument("--weights", default="myData/backup/my_yolov3_last.weights",# Change to your own path
help="yolo weights path")
parser.add_argument("--dont_show", action='store_true',
help="windown inference display. For headless systems")
parser.add_argument("--ext_output", action='store_true',
help="display bbox coordinates of detected objects")
parser.add_argument("--save_labels", action='store_true',
help="save detections bbox for each image in yolo format")
parser.add_argument("--config_file", default="./cfg/my_yolov3.cfg",
help="path to config file")
parser.add_argument("--data_file", default="./cfg/my_data.data",
help="path to data file")
parser.add_argument("--thresh", type=float, default=.25,
help="remove detections with lower confidence")
return parser.parse_args()
def check_arguments_errors(args):
assert 0 < args.thresh < 1, "Threshold should be a float between zero and one (non-inclusive)"
if not os.path.exists(args.config_file):
raise(ValueError("Invalid config path {}".format(os.path.abspath(args.config_file))))
if not os.path.exists(args.weights):
raise(ValueError("Invalid weight path {}".format(os.path.abspath(args.weights))))
if not os.path.exists(args.data_file):
raise(ValueError("Invalid data file path {}".format(os.path.abspath(args.data_file))))
if args.input and not os.path.exists(args.input):
raise(ValueError("Invalid image path {}".format(os.path.abspath(args.input))))
def check_batch_shape(images, batch_size):
"""
Image sizes should be the same width and height
"""
shapes = [image.shape for image in images]
if len(set(shapes)) > 1:
raise ValueError("Images don't have same shape")
if len(shapes) > batch_size:
raise ValueError("Batch size higher than number of images")
return shapes[0]
def load_images(images_path):
"""
If image path is given, return it directly
For txt file, read it and return each line as image path
In other case, it's a folder, return a list with names of each
jpg, jpeg and png file
"""
input_path_extension = images_path.split('.')[-1]
if input_path_extension in ['jpg', 'jpeg', 'png']:
return [images_path]
elif input_path_extension == "txt":
with open(images_path, "r") as f:
return f.read().splitlines()
else:
return glob.glob(
os.path.join(images_path, "*.jpg")) + \
glob.glob(os.path.join(images_path, "*.png")) + \
glob.glob(os.path.join(images_path, "*.jpeg"))
def prepare_batch(images, network, channels=3):
width = darknet.network_width(network)
height = darknet.network_height(network)
darknet_images = []
for image in images:
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image_resized = cv2.resize(image_rgb, (width, height),
interpolation=cv2.INTER_LINEAR)
custom_image = image_resized.transpose(2, 0, 1)
darknet_images.append(custom_image)
batch_array = np.concatenate(darknet_images, axis=0)
batch_array = np.ascontiguousarray(batch_array.flat, dtype=np.float32)/255.0
darknet_images = batch_array.ctypes.data_as(darknet.POINTER(darknet.c_float))
return darknet.IMAGE(width, height, channels, darknet_images)
def image_detection(image_path,network, class_names, class_colors, thresh):
# Darknet doesn't accept numpy images.
# Create one with image we reuse for each detect
width = darknet.network_width(network)
height = darknet.network_height(network)
darknet_image = darknet.make_image(width, height, 3)
image = cv2.imread(image_path)
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image_resized = cv2.resize(image_rgb, (width, height),
interpolation=cv2.INTER_LINEAR)
darknet.copy_image_from_bytes(darknet_image, image_resized.tobytes())
detections = darknet.detect_image(network, class_names, darknet_image, thresh=thresh)
darknet.free_image(darknet_image)
image = darknet.draw_boxes(detections, image_resized, class_colors)
return cv2.cvtColor(image, cv2.COLOR_BGR2RGB), detections
#return cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
def batch_detection(network, images, class_names, class_colors,
thresh=0.25, hier_thresh=.5, nms=.45, batch_size=4):
image_height, image_width, _ = check_batch_shape(images, batch_size)
darknet_images = prepare_batch(images, network)
batch_detections = darknet.network_predict_batch(network, darknet_images, batch_size, image_width,
image_height, thresh, hier_thresh, None, 0, 0)
batch_predictions = []
for idx in range(batch_size):
num = batch_detections[idx].num
detections = batch_detections[idx].dets
if nms:
darknet.do_nms_obj(detections, num, len(class_names), nms)
predictions = darknet.remove_negatives(detections, class_names, num)
images[idx] = darknet.draw_boxes(predictions, images[idx], class_colors)
batch_predictions.append(predictions)
darknet.free_batch_detections(batch_detections, batch_size)
return images, batch_predictions
def image_classification(image, network, class_names):
width = darknet.network_width(network)
height = darknet.network_height(network)
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image_resized = cv2.resize(image_rgb, (width, height),
interpolation=cv2.INTER_LINEAR)
darknet_image = darknet.make_image(width, height, 3)
darknet.copy_image_from_bytes(darknet_image, image_resized.tobytes())
detections = darknet.predict_image(network, darknet_image)
predictions = [(name, detections[idx]) for idx, name in enumerate(class_names)]
darknet.free_image(darknet_image)
return sorted(predictions, key=lambda x: -x[1])
def convert2relative(image, bbox):
"""
YOLO format use relative coordinates for annotation
"""
x, y, w, h = bbox
height, width, _ = image.shape
return x/width, y/height, w/width, h/height
def save_annotations(name, image, detections, class_names):
"""
Files saved with image_name.txt and relative coordinates
"""
file_name = name.split(".")[:-1][0] + ".txt"
with open(file_name, "w") as f:
for label, confidence, bbox in detections:
x, y, w, h = convert2relative(image, bbox)
label = class_names.index(label)
f.write("{} {:.4f} {:.4f} {:.4f} {:.4f}\n".format(label, x, y, w, h))
def batch_detection_example():
args = parser()
check_arguments_errors(args)
batch_size = 3
random.seed(3) # deterministic bbox colors
network, class_names, class_colors = darknet.load_network(
args.config_file,
args.data_file,
args.weights,
batch_size=batch_size
)
image_names = ['data/horses.jpg', 'data/horses.jpg', 'data/eagle.jpg']
images = [cv2.imread(image) for image in image_names]
images, detections, = batch_detection(network, images, class_names,
class_colors, batch_size=batch_size)
for name, image in zip(image_names, images):
cv2.imwrite(name.replace("data/", ""), image)
print(detections)
def get_files(dir, suffix):
res = []
for root, directory, files in os.walk(dir):
for filename in files:
name, suf = os.path.splitext(filename)
if suf == suffix:
#res.append(filename)
res.append(os.path.join(root, filename))
return res
def bbox2points_zs(bbox):
"""
From bounding box yolo format
to corner points cv2 rectangle
"""
x, y, w, h = bbox
xmin = int(round(x - (w / 2)))
xmax = int(round(x + (w / 2)))
ymin = int(round(y - (h / 2)))
ymax = int(round(y + (h / 2)))
return xmin, ymin, xmax, ymax
def main():
args = parser()
check_arguments_errors(args)
input_dir = '/home/yourdarknet'
config_file = '/home/your/darknet/cfg/my_yolov3.cfg'# Change to your own path
data_file = '/home/your/darknet/cfg/my_data.data'
weights = '/home/your/darknet/myData/backup/my_yolov3_last.weights'
random.seed(3) # deterministic bbox colors
network, class_names, class_colors = darknet.load_network(
config_file,
data_file,
weights,
batch_size=args.batch_size
)
src_width = darknet.network_width(network)
src_height = darknet.network_height(network)
# Generate a folder to save the path of pictures
save_dir = os.path.join(input_dir, 'object_result')
# Remove the first space
save_dir=save_dir.strip()
# Remove the tail \ Symbol
save_dir=save_dir.rstrip("\\")
# Determine if the path exists # There is True # non-existent False
isExists=os.path.exists(save_dir)
# Judge the result
if not isExists:
# Create a directory if it doesn't exist # Create directory manipulation functions
os.makedirs(save_dir)
print(save_dir+' Create success ')
else:
# If directory exists Do not create , And prompt that the directory already exists
print(save_dir + ' directory already exists ')
image_list = get_files(input_dir, '.jpg')
total_len = len(image_list)
index = 0
#while True:
for i in range(0, total_len):
image_name = image_list[i]
src_image = cv2.imread(image_name)
cv2.imshow('src_image', src_image)
cv2.waitKey(1)
prev_time = time.time()
image, detections = image_detection(
image_name, network, class_names, class_colors, args.thresh)
#'''
file_name, type_name = os.path.splitext(image_name)
#print(file_name)
#print(file_name.split(r'/'))
print(''.join(file_name.split(r'/')[-1]) + 'bbbbbbbbb')
cut_image_name_list = file_name.split(r'/')[-1:] #cut_image_name_list is list
save_dir_image = os.path.join(save_dir ,cut_image_name_list[0])
if not os.path.exists(save_dir_image):
os.makedirs(save_dir_image)
cut_image_name = ''.join(cut_image_name_list) #list to str
object_count = 0
for label, confidence, bbox in detections:
cut_image_name_temp = cut_image_name + "_{}.jpg".format(object_count)
object_count += 1
xmin, ymin, xmax, ymax = bbox2points_zs(bbox)
print("aaaaaaaaa x,{} y,{} w,{} h{}".format(xmin, ymin, xmax, ymax))
xmin_coordinary = (int)(xmin * src_image.shape[1] / src_width-0.5)
ymin_coordinary = (int)(ymin * src_image.shape[0] / src_height-0.5)
xmax_coordinary = (int)(xmax * src_image.shape[1] / src_width+0.5)
ymax_coordinary = (int)(ymax * src_image.shape[0] / src_height+0.5)
if xmin_coordinary>src_image.shape[1]:
xmin_coordinary = src_image.shape[1]
if ymin_coordinary>src_image.shape[0]:
ymin_coordinary = src_image.shape[0]
if xmax_coordinary>src_image.shape[1]:
xmax_coordinary = src_image.shape[1]
if ymax_coordinary>src_image.shape[0]:
ymax_coordinary = src_image.shape[0]
if xmin_coordinary < 0:
xmin_coordinary = 0
if ymin_coordinary < 0:
ymin_coordinary = 0
if xmax_coordinary < 0:
xmax_coordinary = 0
if ymax_coordinary < 0:
ymax_coordinary = 0
print("qqqqqqqq x,{} y,{} w,{} h{}".format(xmin_coordinary, ymin_coordinary, xmax_coordinary, ymax_coordinary))
out_iou_img = np.full((ymax_coordinary - ymin_coordinary, xmax_coordinary - xmin_coordinary, src_image.shape[2]), 114, dtype=np.uint8)
out_iou_img[:,:] = src_image[ymin_coordinary:ymax_coordinary,xmin_coordinary:xmax_coordinary]
cv2.imwrite(os.path.join(save_dir_image,cut_image_name_temp),out_iou_img)
#'''
#if args.save_labels:
#if True:
#save_annotations(image_name, image, detections, class_names)
darknet.print_detections(detections, args.ext_output)
fps = int(1/(time.time() - prev_time))
print("FPS: {}".format(fps))
if not args.dont_show:
#cv2.imshow('Inference', image)
cv2.waitKey(1)
#if cv2.waitKey() & 0xFF == ord('q'):
#break
index += 1
if __name__ == "__main__":
# unconmment next line for an example of batch processing
# batch_detection_example()
main()
But this will lead to the need to reload the model for each test , So much so that out of memory And then the camera detection has no output . The solution is to statically load the model , Specifically, image_demo and vedio_demo Function .
2. Camera detection ideas : utilize opencv Of VeidoCapture Function reads every frame of the video for detection , But this led to some jamming in the video display during the detection . The idea of video stream detection is exactly the same as camera detection , Just need to add a function to read the video path .
3、 ... and . Detection effect : Before running this example, you need to check the directory , Even if darknet New in folder img Folder , stay img New under folder result Folder , Then return darknet Folder run this example , The input terminal :( Some parameters such as the size of the detection box and the detection font can be modified before detection )
mkdir img
cd img
mkdir result
cd ..
python3 Callyolov3.py Final effect :1. Picture detection :


2. Camera detection :
yolov3 Of GUI The interface realizes camera detection
Next possible modifications :
1. Add and save output detection items label and number To txt file
2. Solve the slightly stuck problem of camera detection
边栏推荐
- 结对编程实践心得
- @Autowired注解的底层原理
- redis-扩展数据类型(跳跃表/BitMaps/HyperLogLog/GeoSpatial)
- 什么是奇偶校验?如何用C语言实现?
- Qpprogressbar for QT style (QSS) application
- What is multithreading
- ArcGIS cuts TIF images (grid data) according to the vector range, merges shp files in batches, cuts vectors in the region according to the vector range, outputs the geographic coordinate system, conve
- Leetcode200-查找岛屿数量详解
- 疫情之下的好消息
- What is inode? It will help you understand inode and what help inode provides when creating and deleting files.
猜你喜欢

String functions and memory operation functions

S4/hana ME21N create Po output control message button missing solution (switch EDI output mode brf+ to Nast mode)

The GUI interface of yolov3 (simple, image detection)

Zhiniu stock -- 09

A long detailed explanation of C language operators

多御安全浏览器手机版将增加新功能,使用户浏览更个性化

Loading process such as reflection
![[learning notes] solid works operation record](/img/f7/0535b473755643ce32996f00fa5554.png)
[learning notes] solid works operation record

SQLZOO——Nobel Quiz

二叉树——222. 完全二叉树的节点个数
随机推荐
Seventy second: pedestrian detection
Exercise (3) create a list set (both ArrayList and LinkedList)
Song of statistics lyrics
@Autowired注解的底层原理
Lua脚本编写Wireshark插件解析第三方私有协议
Basic syntax of MySQL DDL, DML and DQL
Bubble sort idea and Implementation
二叉树——104. 二叉树的最大深度
注解@Autowired源码解析
Part 66: monocular 3D reconstruction point cloud
Key and difficult points of C language pointer
R语言安装教程 | 图文介绍超详细
Leetcode200-查找岛屿数量详解
牛客/洛谷——[NOIP2003 普及组]栈
The process of finding free screen recording software - I didn't expect win10 to come with this function
What is inode? It will help you understand inode and what help inode provides when creating and deleting files.
Promise resolve callback hell, async await modifier
二叉树——112. 路径总和
获取马蜂窝酒店数据
QT smart pointer error prone point