需要注意的是:更改完源程序.c文件,需要对整个项目重新编译、make install,对已经生成的文件进行更新,类似于之前VS中在一个类中增加新函数重新编译封装dll,而python接口的调用主要使用的是libdarknet.so文件,其余在配置文件中的修改不必重新进行编译安装。
之前训练好的模型,在模型调用的时候,总是在
lib = CDLL("/home/*****/*******/darknet/libdarknet.so", RTLD_GLOBAL)这里读不到darknet编译生成的.so文件,导致直接的报错;之前以为是文件路径的问题,稀里糊涂的;由于很久不写c文件了,所以最后直接在python接口后在py文件中修改的画框、标置信度等操作,一次次的尝试后终于成功
(1)将项目中python文件下的darknet.py文件拷贝到根目录,和/libdarknet.so在同一个目录下
(2)整个demo程序都是用绝对路径;
实现yolov3模型加载,批量读取文件夹下的照片到库函数变量,最终处理结果存入在另外新建文件夹
###2019.04.03 by ylxb def showPicResult(image,peoplecar,outimage): img = cv2.imread(image) out_img =outimage cv2.imwrite(out_img, img) for i in range(len(peoplecar)): x1=peoplecar[i][2][0]-peoplecar[i][2][2]/2 y1=peoplecar[i][2][1]-peoplecar[i][2][3]/2 x2=peoplecar[i][2][0]+peoplecar[i][2][2]/2 y2=peoplecar[i][2][1]+peoplecar[i][2][3]/2 im = cv2.imread(out_img) cv2.rectangle(im,(int(x1),int(y1)),(int(x2),int(y2)),(255,255,0),3) text = listpeoplecar[i][0] # 在图片上添加文字信息 if(text=="people"): carcol=(55, 55, 255)#颜色显示 else: carcol = (255, 55, 55) cv2.putText(im, text, (int(x1), int(y1)), cv2.FONT_HERSHEY_SIMPLEX, 0.8, carcol, 1, cv2.LINE_AA) #This is a method that works well. cv2.imwrite(out_img, im)
###2019.04.03 by ylxb filenames = os.listdir(picDir) i = 0 num = 0#目标个数 car_num = 0#car个数 people_num = 0#people个数 car = "car" # car元素 people = "people" # people元素 for name in filenames: filename=os.path.join(picDir,name) #print(filename) listpeoplecar = detect(net, meta, filename) print(listpeoplecar) i = i + 1 #save_picpath = out_img+str(filename).split("/")[-1].split(".")[0] + ".png" out_img=out_img1+str(i)+'.png' showPicResult(filename,listpeoplecar,out_img) for item in listpeoplecar: #print(item) car_num = car_num + item[0].count(car)#car个数 people_num = people_num + item[0].count(people)#people个数 num = num + 1#目标个数 print('car个数: ' + str(car_num)) print('people个数: ' + str(people_num)) print('共检测出目标个数: ' + str(num)) print('共检测照片个数:'+ str(i))
放其中一个照片测试照片:
以上这篇对YOLOv3模型调用时候的python接口详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。