目标
使用 darknet (https://github.com/pjreddie/darknet) 自带的 python 接口处理图片和视频。
project 下载
git clone https://github.com/pjreddie/darknet
cd darknet
#改一些配置 ,具体操作见 我的上一份博客的结尾部分
#(https://blog.csdn.net/qq_20241587/article/details/111176541)
make
处理单张图片
即:指定一张图片的路径,指定检测结果新图片的存放位置,进行 model检测+画框+另存为新图片.
下图中,左边是处理前的样子,右边是处理后的样子,两张图位置在代码中指定
代码如下。
核心改动处 :
1 # lib = CDLL("libdarknet.so", RTLD_GLOBAL) , 改成自己的项目的具体地址
2 使用 cv2.rectangle 画框框, 使用cv2.putText 放文字,为了避免框框和文字交叉,我加了一丢丢的偏移量。
其中 yolov3的输出是 :
label_i = box_i[0] #标签
prob_i = box_i[1] #标签置信度
x_ = box_i[2][0]
y_ = box_i[2][1]
w_ = box_i[2][2]
h_ = box_i[2][3] # bbox信息(x,y,w,h)为物体的中心位置相对格子位置的偏移及宽度和高度,
cv2.rectangle(image, (int(x_ - w_ / 2), int(y_ - h_ / 2)),
(int(x_ + w_ / 2), int(y_ + h_ / 2)),
color, line_type)
cv2.putText(image, text_, (int(x_ - w_ / 2 - 5), int(y_ - h_ / 2 - 5)), cv2.FONT_HERSHEY_DUPLEX, 0.7, color,
2)
from ctypes import *
import math
import random
import cv2
import os
def sample(probs):
s = sum(probs)
probs = [a / s for a in probs]
r = random.uniform(0, 1)
for i in range(len(probs)):
r = r - probs[i]
if r <= 0:
return i
return len(probs) - 1
def c_array(ctype, values):
arr = (ctype * len(values))()
arr[:] = values
return arr
class BOX(Structure):
_fields_ = [("x", c_float),
("y", c_float),
("w", c_float),
("h", c_float)]
class DETECTION(Structure):
_fields_ = [("bbox", BOX),
("classes", c_int),
("prob", POINTER(c_float)),
("mask", POINTER(c_float)),
("objectness", c_float),
("sort_class", c_int)]
class IMAGE(Structure):
_fields_ = [("w", c_int),
("h", c_int),
("c", c_int),
("data", POINTER(c_float))]
class METADATA(Structure):
_fields_ = [("classes", c_int),
("names", POINTER(c_char_p))]
lib = CDLL("/home/jiantang/桌面/enn/workcode/yoloV3/github/darknet/libdarknet.so", RTLD_GLOBAL)
# lib = CDLL("libdarknet.so", RTLD_GLOBAL)
lib.network_width.argtypes = [c_void_p]
lib.network_width.restype = c_int
lib.network_height.argtypes = [c_void_p]
lib.network_height.restype = c_int
predict = lib.network_predict
predict.argtypes = [c_void_p, POINTER(c_float)]
predict.restype = POINTER(c_float)
set_gpu = lib.cuda_set_device
set_gpu.argtypes = [c_int]
make_image = lib.make_image
make_image.argtypes = [c_int, c_int, c_int]
make_image.restype = IMAGE
get_network_boxes = lib.get_network_boxes
get_network_boxes.argtypes = [c_void_p, c_int, c_int, c_float, c_float, POINTER(c_int), c_int, POINTER(c_int)]
get_network_boxes.restype = POINTER(DETECTION)
make_network_boxes = lib.make_network_boxes
make_network_boxes.argtypes = [c_void_p]
make_network_boxes.restype = POINTER(DETECTION)
free_detections = lib.free_detecti