1. 程式人生 > >python指令碼實現給定標註bbox,landmark在原圖中顯示人臉框,人臉關鍵點

python指令碼實現給定標註bbox,landmark在原圖中顯示人臉框,人臉關鍵點

程式功能簡介:

利用給定的標註bbox,landmark在原圖中顯示人臉框,人臉關鍵點:

給定標註txt檔案資料格式,如下圖所示:

lfw_5590\Aaron_Eckhart_0001.jpg 84 161 92 169 106.250000 107.750000 146.750000 112.250000 125.250000 142.750000 105.250000 157.750000 139.750000 161.750000
lfw_5590\Aaron_Guiel_0001.jpg 85 172 93 181 100.250000 111.250000 145.750000 116.750000 124.250000 136.750000 92.750000 159.750000 138.750000 163.750000
lfw_5590\Aaron_Peirsol_0001.jpg 88 173 94 179 106.750000 113.250000 146.750000 113.250000 129.250000 139.750000 108.250000 153.250000 146.750000 152.750000
lfw_5590\Aaron_Pena_0001.jpg 67 176 83 192 101.750000 116.750000 145.250000 103.750000 125.250000 136.750000 119.750000 163.750000 146.250000 155.750000
lfw_5590\Aaron_Sorkin_0001.jpg 73 164 86 178 101.250000 112.250000 143.750000 111.750000 129.750000 137.750000 99.250000 155.250000 142.250000 154.750000
lfw_5590\Aaron_Tippin_0001.jpg 77 165 90 177 103.750000 111.750000 141.750000 111.250000 129.250000 133.750000 110.750000 156.250000 134.750000 155.250000
lfw_5590\Abba_Eban_0001.jpg 80 164 90 175 105.750000 113.750000 145.750000 110.250000 133.750000 136.250000 107.750000 156.250000 148.750000 155.250000
lfw_5590\Abdel_Aziz_Al-Hakim_0001.jpg 86 169 90 173 110.750000 110.250000 148.250000 113.750000 136.750000 133.250000 111.750000 152.250000 141.750000 154.250000
lfw_5590\Abdel_Nasser_Assidi_0001.jpg 80 167 87 174 103.750000 112.250000 148.250000 115.250000 124.250000 134.250000 106.750000 155.750000 136.250000 159.250000
lfw_5590\Abdulaziz_Kamilov_0001.jpg 76 162 90 177 108.250000 110.250000 146.750000 116.250000 128.750000 137.750000 104.250000 152.750000 133.250000 158.250000

python指令碼如下:

#!/usr/bin/env python2.7
# coding: utf-8
#第一行用於指定編譯器位置,先在環境變數中查詢python編譯器; 第二行申明中文檔案編碼的註釋

import cv2

annot_dir = '/home/xiao/code/deep-landmark/dataset/train'

def join(str1, str2):
    str = str1 + '/' + str2
    return str


# 判斷這是否為一個主程式,其他python程式無法呼叫
if __name__ == '__main__':

    txt = join(annot_dir, 'trainImageList.txt')
    with open(txt, 'r') as fd:
        lines = fd.readlines()

    i=0
    for line in lines:
        i=i+1
        print i
        line = line.strip()
        components = line.split(' ')
        img_path =  join(annot_dir, components[0].replace('\\','/'))
        img = cv2.imread(img_path, -1)
        cv2.rectangle(img, (int(components[1]),int(components[3])), (int(components[2]),int(components[4])), (0,0,255), 2)
        # 強制型別轉換,從string轉換為float,再轉換為int
        cv2.circle(img, (int(float(components[5])),int(float(components[6]))), 2, (0,255,0), -1)
        cv2.circle(img, (int(float(components[7])),int(float(components[8]))), 2, (0,255,0), -1)
        cv2.circle(img, (int(float(components[9])),int(float(components[10]))), 2, (0,255,0), -1)
        cv2.circle(img, (int(float(components[11])),int(float(components[12]))), 2, (0,255,0), -1)
        cv2.circle(img, (int(float(components[13])),int(float(components[14]))), 2, (0,255,0), -1)
        cv2.imshow('image',img)
        cv2.waitKey(1000)