python - 模块 'darknet' 没有属性 'load_network'

标签 python yolo darknet

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

module 'darknet' has no attribute '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

谁能告诉我如何解决这个错误?

最佳答案

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

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

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

关于python - 模块 'darknet' 没有属性 'load_network',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66544586/

相关文章:

opencv - 如何使用 OpenVINO 获得与 OpenCV 一样快的 YOLOv4 推理时间?

python - 属性错误 : 'PhotoImage' object has no attribute '_PhotoImage__photo'

opencv - cv2.dnn.readNetFromDarknet 错误 : (-212:Parsing error) Unsupported activation: relu in function 'cv::dnn::darknet::ReadDarknetFromCfgStream'

python - 如何提高YOLOv3检测时间? (OpenCV + Python)

python - 从 while 循环返回而不退出它 python

python - 手动标记的 Span 中带有 ENT_TYPE 的模式不起作用

python - 无法打开带有空格的train.txt "My Drive"He

python - 运行一小时的 GAE 任务?

python - 使用列表理解根据使用其他列表的条件替换重复项