测试yolov3 Python接口时会报错,看看如何解决。
1.下载yolov3。
地址: git clone https://github.com/pjreddie/darknet.git
2.修改makefile,编译。
3.将python文件夹下darknet.py放到和libdarknet.so同目录,执行python darknet.py 报错。
ycc@ycc:~/darknet$ python darknet.py
File "darknet.py", line 154
print r
^
SyntaxError: Missing parentheses in call to 'print'. Did you mean print(r)?
解决:把 print r 改成print(r)。
ycc@ycc:~/darknet$ python darknet.py
Traceback (most recent call last):
File "darknet.py", line 48, in <module>
lib = CDLL("libdarknet.so", RTLD_GLOBAL)
File "/home/ycc/anaconda3/lib/python3.7/ctypes/__init__.py", line 356, in __init__
self._handle = _dlopen(self._name, mode)
OSError: libdarknet.so: cannot open shared object file: No such file or directory
解决:将libdarknet.so的路径改成绝对路径。
class METADATA(Structure):
_fields_ = [("classes", c_int),
("names", POINTER(c_char_p))]
#lib = CDLL("/home/pjreddie/documents/darknet/libdarknet.so", RTLD_GLOBAL)
lib = CDLL("/home/ycc/darknet/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
ycc@ycc:~/darknet$ python darknet.py
Traceback (most recent call last):
File "darknet.py", line 151, in <module>
net = load_net("cfg/tiny-yolo.cfg", "tiny-yolo.weights", 0)
ctypes.ArgumentError: argument 1: <class 'TypeError'>: wrong type
解决:出错的字符串前加b
net = load_net(b"cfg/yolov3.cfg", b"yolov3.weights", 0)
meta = load_meta(b"cfg/coco.data")
r = detect(net, meta, b"data/dog.jpg")
print (r)
97 upsample 2x 38 x 38 x 128 -> 76 x 76 x 128
98 route 97 36
99 conv 128 1 x 1 / 1 76 x 76 x 384 -> 76 x 76 x 128 0.568 BFLOPs
100 conv 256 3 x 3 / 1 76 x 76 x 128 -> 76 x 76 x 256 3.407 BFLOPs
101 conv 128 1 x 1 / 1 76 x 76 x 256 -> 76 x 76 x 128 0.379 BFLOPs
102 conv 256 3 x 3 / 1 76 x 76 x 128 -> 76 x 76 x 256 3.407 BFLOPs
103 conv 128 1 x 1 / 1 76 x 76 x 256 -> 76 x 76 x 128 0.379 BFLOPs
104 conv 256 3 x 3 / 1 76 x 76 x 128 -> 76 x 76 x 256 3.407 BFLOPs
105 conv 255 1 x 1 / 1 76 x 76 x 256 -> 76 x 76 x 255 0.754 BFLOPs
106 yolo
Loading weights from yolov3.weights...Done!
[(b'dog', 0.9993329048156738, (224.17959594726562, 378.47900390625, 178.7544708251953, 328.2962341308594)), (b'bicycle', 0.991621732711792, (344.5289306640625, 286.759765625, 486.1888427734375, 321.36590576171875)), (b'truck', 0.9165933728218079, (580.9117431640625, 125.05439758300781, 208.13427734375, 87.27815246582031))]
成功运行。