笔记补充:yolo识别、框选图片物体的python代码实现

前记:

(不知道怎么第一篇发出去的博客似乎一直搜不到,在此补充一封)

作用说明:学习笔记,主要用于自我记录。

(PS:本人菜鸟,文章仅供参考;如有错误,欢迎各位大神批评指正!)

最近刚刚接触yolo,由于yolo官网和网上各种资料几乎都是基于C语言的,本人觉得python比较简洁,故用python实现了C可实现的部分功能,包括(1)图片检测及画框、(2)本地视频检测、(3)webcam检测、(4)yolo结合ROS检测摄像头(webcam)视频。

该文此下只介绍(1)yolo在python下图片检测及画框。其余部分后续介绍。

【本系列文章代码全基于原作者代码,文中代码由于TAB缩进量可能会出现缩进问题】


1、使用环境和平台:

ubuntu 14.04+ python2.7+opencv2.4+yolo

关于yolo/darknet的安装:

git clone https://github.com/pjreddie/darknet
cd darknet
make

有GPU的可自行安装CUDA(识别速度会快很多),修改相关参数。

yolo/darknet官网链接:

https://pjreddie.com/darknet/yolo/

2、检测图片的python代码

以下代码只是对源程序(darknet/python/darknet.py)的简单修改。

#!coding=utf-8
#modified by Leo at 2018.04.25
#function: 1,NOT overwrite the origin picture  
#          2,print info BEFORE showing the display_window

from ctypes import *
import math
#import module named cv2 to draw
import cv2

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/username/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

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_detections
free_detections.argtypes = [POINTER(DETECTION), c_int]

free_ptrs = lib.free_ptrs
free_ptrs.argtypes = [POINTER(c_void_p), c_int]

network_predict = lib.network_predict
network_predict.argtypes = [c_void_p, POINTER(c_float)]

reset_rnn = lib.reset_rnn
reset_rnn.argtypes = [c_void_p]

load_net = lib.load_network
load_net.argtypes = [c_char_p, c_char_p, c_int]
load_net.restype = c_void_p

do_nms_obj = lib.do_nms_obj
do_nms_obj.argtypes = [POINTER(DETECTION), c_int, c_int, c_float]

do_nms_sort = lib.do_nms_sort
do_nms_sort.argtypes = [POINTER(DETECTION), c_int, c_int, c_float]

free_image = lib.free_image
free_image.argtypes = [IMAGE]

letterbox_image = lib.letterbox_image
letterbox_image.argtypes = [IMAGE, c_int, c_int]
letterbox_image.restype = IMAGE

load_meta = lib.get_metadata
lib.get_metadata.argtypes = [c_char_p]
lib.get_metadata.restype = METADATA

load_image = lib.load_image_color
load_image.argtypes = [c_char_p, c_int, c_int]
load_image.restype = IMAGE

rgbgr_image = lib.rgbgr_image
rgbgr_image.argtypes = [IMAGE]

predict_image = lib.network_predict_image
predict_image.argtypes = [c_void_p, IMAGE]
predict_image.restype = POINTER(c_float)

def classify(net, meta, im):
    out = predict_image(net, im)
    res = []
    for i in range(meta.classes):
        res.append((meta.names[i], out[i]))
    res = sorted(res, key=lambda x: -x[1])
    return res

def detect(net, meta, image, thresh=.5, hier_thresh=.5, nms=.45):
    im = load_image(image, 0, 0)
    num = c_int(0)
    pnum = pointer(num)
    predict_image(net, im)
    dets = get_network_boxes(net, im.w, im.h, thresh, hier_thresh, None, 0, pnum)
    num = pnum[0]
    if (nms): do_nms_obj(dets, num, meta.classes, nms);

    res = []
    for j in range(num):
        for i in range(meta.classes):
            if dets[j].prob[i] > 0:
                b = dets[j].bbox
                res.append((meta.names[i], dets[j].prob[i], (b.x, b.y, b.w, b.h)))
    res = sorted(res, key=lambda x: -x[1])
    free_image(im)
    free_detections(dets, num)
    return res

# display the pic after detecting. 2018.04.25
def showPicResult(image):
    img = cv2.imread(image)
    cv2.imwrite(out_img, img)
    for i in range(len(r)):
        x1=r[i][2][0]-r[i][2][2]/2
        y1=r[i][2][1]-r[i][2][3]/2
        x2=r[i][2][0]+r[i][2][2]/2
        y2=r[i][2][1]+r[i][2][3]/2
        im = cv2.imread(out_img)
        cv2.rectangle(im,(int(x1),int(y1)),(int(x2),int(y2)),(0,255,0),3)
        #This is a method that works well. 
        cv2.imwrite(out_img, im)
    cv2.imshow('yolo_image_detector', cv2.imread(out_img))
    cv2.waitKey(0)
    cv2.destroyAllWindows()

    
if __name__ == "__main__":
    #notice the path of every file
    net = load_net("/home/username/darknet/cfg/yolov3.cfg", "/home/username/darknet/weights/yolov3.weights", 0)
    meta = load_meta("/home/username/darknet/cfg/coco.data")
    origin_img = "/home/username/darknet/data/copy_dog.jpg"
    out_img = "/home/username/darknet/data/test_result.jpg"
    r = detect(net, meta, origin_img)
    print r
    #display the rectangle of the objects in window
    showPicResult(origin_img)


yolo.weights文件如果没有的话请自行去yolo官网或Github上下载。

Notice:

1、注意cfg、weights等各个文件的路径,将程序中的各种文件路径全改为自己的(另外可能需要修改coco.data的内容,因为文件中涉及到coco.names的路径);

2、此程序中对图片中识别到的物体画框的代码使用了opencv工具,没有安装的请自行安装(这里使用的是opencv2.4);

3、注意cfg和weights文件的对应,如不对应可能导致识别图片不准确或错误。

3、代码解释

本程序主要增加部分:

import cv2

# display the pic after detecting. 
def showPicResult(image):
    img = cv2.imread(image)
    cv2.imwrite(out_img, img)
    for i in range(len(r)):
        x1=r[i][2][0]-r[i][2][2]/2
        y1=r[i][2][1]-r[i][2][3]/2
        x2=r[i][2][0]+r[i][2][2]/2
        y2=r[i][2][1]+r[i][2][3]/2
        im = cv2.imread(out_img)
        cv2.rectangle(im,(int(x1),int(y1)),(int(x2),int(y2)),(0,255,0),3)
        #This is a method that works well. 
        cv2.imwrite(out_img, im)
    cv2.imshow('yolo_image_detector', cv2.imread(out_img))
    cv2.waitKey(0)
    cv2.destroyAllWindows()

  for i in range(len(r)):

r是一个list,其中存放了检测图片的信息,包括class(检测到的物体类别)、置信度、坐标,其中坐标包括x、y、w、h。

x、y:从左上角到物体中心点距离(float类型);w、h:目标区域的宽和高(float类型)。

cv2.rectangle(im,(int(x1),int(y1)),(int(x2),int(y2)),(0,255,0),3)

此函数实在目标区域绘制一个矩形,参数1:原图片,参数2:绘制矩形的左上角坐标,参数3: 右下角坐标,参数4: 矩形线条颜色,参数5: 线条粗细。(由于cv2中坐标为整数,所以程序中进行了类型转换float-->int)

对OpenCV不太了解的可以参考:

https://docs.opencv.org/3.0-beta/doc/py_tutorials/py_tutorials.html


后记:

程序改动不大,理解起来很简单,在我电脑上是完全可以实现的,如有问题欢迎批评指正!

联系邮箱:[email protected]

(PS:后续部分,陆续奉上。)

你可能感兴趣的:(人工智能之yolo机器视觉)