模块“darknet”没有属性“load_network”

问题描述 投票:0回答:3

我正在尝试运行来自 Google Colab 的 YOLO 掩模检测代码。 但是当我运行我的代码时出现此错误:

模块“darknet”没有属性“load_network”

知道为什么吗? 我确实导入了

darknet.py

我使用的代码:

from ctypes import *
import math
import random
import os
import cv2
import numpy as np
import time
import darknet

#OpenCV need the 4 corners
def convertBack(x, y, w, h):                                    #OpenCV uses top left and bottom right
    xmin = int(round(x - (w / 2)))                              #corner of rectangle box as input points
    xmax = int(round(x + (w / 2)))          
    ymin = int(round(y - (h / 2)))          
    ymax = int(round(y + (h / 2)))          
    return xmin, ymin, xmax, ymax


# drawing bounding boxes on image from detections
def DrawBBoxes(detections, img, dim, colors):                           #detections = detected objects, img= image of detected object, dim= dimensions of img
  i = 1                                                                 #for "array of 6 elements" in detections --> label= detected class name, confidence= accuracy of detection, bbox= contains 4 elements x,y,w,h.
  for label, confidence, bbox in detections:                             #x and y is coordinates of center of bbox, w and h is width and height of bbox 
   x, y, w, h = int((bbox[0]/dim)*width), #\                             #Saving the values of in array bbox into x,y,w,h. x in 0 index position of array and so on...
   int((bbox[1]/dim)*height), \
   int((bbox[2]/dim)*width), \
   int((bbox[3]/dim)*height)
   xmin, ymin, xmax, ymax = convertBack(float(x), float(y), float(w), float(h))    #calling function convertBack to get points for openCV
   pt1 = (xmin, ymin)
   pt2 = (xmax, ymax)
   cv2.rectangle(img, pt1, pt2, colors[label], 1)                                    #cv2.rectangle(image, start_point, end_point, color, thickness)
   if label == 'with_mask':
     string = label + str(i)
     i+=1
   else:
     string = label
     cv2.putText(img, string + ":" + str(round(confidence,2)),(pt1[0]-5, pt1[1] - 5), cv2.FONT_HERSHEY_SIMPLEX, 1,[0, 0, 255], 2)
     return img
###############################################################################################################

#define and load the trained models into GPU's"
invt_configPath = "darknet/cfg/yolov3_custom_train.cfg"
invt_weightPath = "darknet/backup/yolov3_custom_train_last.weights" 
invt_metaPath = "darknet/data/yolo.data"   


network, class_names, colors = darknet.load_network(invt_configPath,  invt_metaPath, invt_weightPath, batch_size=1)
invt_width = darknet.network_width(invt_network)                        #getting YOLO input image dimensions
invt_height = darknet.network_height(invt_network)

    image = DrawBBoxes(invt_detections,frame,416,colors)                                 #'name'_detections= detected class label, frame=detected image, 416=dimension of YOLO detection , colors of labels
    #image = DrawBBoxes(ppe_detections,image,416)

    cv2.imshow('Inventory Detections', image)             #cv2.imshow("display window name", "video frame feed")
    cv2.waitKey(10)                                       #Displaying video for time in milliseconds per frame


cap.release()
#out.release()
cv2.destroyAllWindows()
###############################################################################################################

darknet.py
是我从AlexeyAB的GitHub上得到的。

该文件包含函数

load_network
:

def load_network(config_file, data_file, weights, batch_size=1):
    network = load_net_custom(
        config_file.encode("ascii"),
        weights.encode("ascii"), 0, batch_size)
    metadata = load_meta(data_file.encode("ascii"))
    class_names = [metadata.names[i].decode("ascii") for i in range(metadata.classes)]
    colors = class_colors(class_names)
    return network, class_names, colors

任何人都可以告诉我如何解决这个错误吗?

python yolo darknet
3个回答
2
投票

添加 1 行 [sys.path.append()] 如下。

import time
sys.path.append(os.path.join('..', 'darknet', 'build', 'darknet', 'x64')) 
import darknet

根据你的暗网安装文件夹修改文件夹名称。


0
投票

尝试重命名darknet文件夹中的darknet.py,然后:

import darknet2


0
投票

导入时间 导入系统 导入操作系统

sys.path.append(os.path.join('..', 'darknet', 'built', 'darknet','x64')) 导入暗网

network, class_names, class_colors = darknet.load_network("cfg/yolov4-csp-cfg", "cfg/coco.data", "yolov4.weight")

宽度 = 网络宽度(网络) 高度=网络高度(网络)

我收到错误“模块 darknet 没有属性 load_network”。请帮忙解决一下。

© www.soinside.com 2019 - 2024. All rights reserved.