常用函数
读取文件,读取路径函数
import os
import json
import matplotlib.pyplot as plt
import numpy as np
import cv2
import matplotlib
import matplotlib.pyplot as plt
import random
import shutil
from matplotlib import pyplot as plt
from pycocotools.coco import COCO
from tqdm import tqdm
def save_json(list_file,path):
with open(path,'w') as f:
json.dump(list_file,f,indent=4)
return 0
def load_jsonfile(json_path):
with open(json_path, 'r', encoding="UTF-8") as f:
json_file = json.load(f)
return json_file
def get_all_path(dir_root):
file_list = os.walk(dir_root)
all_name = []
for root, dirs, files in file_list:
if len(files) > 0:
for file_name in files:
if file_name == 'summary.json':
continue
file_path = root + '/' + file_name
all_name.append(file_path)
return all_name
def mkdir(path):
if not os.path.exists(path):
os.makedirs(path)
return 0
def getmd5(file):
m = hashlib.md5()
with open(file,'rb') as f:
for line in f:
m.update(line)
md5code = m.hexdigest()
return md5code
对检测框处理函数
import os
import json
import matplotlib.pyplot as plt
import numpy as np
import cv2
import matplotlib
import matplotlib.pyplot as plt
import random
import shutil
from matplotlib import pyplot as plt
from pycocotools.coco import COCO
def xyxy_to_xywh(box):
x1,y1,x2,y2 = box
return [x1,y1,x2 - x1,y2 - y1]
def xywh_to_xyxy(box):
x,y,w,h = box
return [x,y,x+w,y+h]
def iou(boxA, boxB):
# box : (x,y,w,h)
boxA = xywh_to_xyxy(boxA)
boxB = xywh_to_xyxy(boxB)
# box: (x1, y1, x2, y2)
# determine the (x, y)-coordinates of the intersection rectangle
xA = max(boxA[0], boxB[0])
yA = max(boxA[1], boxB[1])
xB = min(boxA[2], boxB[2])
yB = min(boxA[3], boxB[3])
# compute the area of intersection rectangle
interArea = max(0, xB - xA + 1) * max(0, yB - yA + 1)
# compute the area of both the prediction and ground-truth
# rectangles
boxAArea = (boxA[2] - boxA[0] + 1) * (boxA[3] - boxA[1] + 1)
boxBArea = (boxB[2] - boxB[0] + 1) * (boxB[3] - boxB[1] + 1)
# compute the intersection over union by taking the intersection
# area and dividing it by the sum of prediction + ground-truth
# areas - the interesection area
iou = interArea / float(boxAArea + boxBArea - interArea)
# return the intersection over union value
return iou
常用简单的可视化代码:
```python
RED = (0, 0, 255)
GREEN = (0, 255, 0)
BLUE = (255, 0, 0)
CYAN = (255, 255, 0)
YELLOW = (0, 255, 255)
ORANGE = (0, 165, 255)
PURPLE = (255, 0, 255)
color_list = [RED, GREEN, BLUE, CYAN, YELLOW, ORANGE, PURPLE]
def rectangle(img,box,color = (0,0,0)):
x,y,w,h = box
if w > 0 and h > 0:
pos1 = (int(x),int(y))
pos2 = (int(x + w),int(y + h))
cv2.rectangle(img, pos1, pos2, color, 3)
return img
def show_landmark(img, kpts, color=(128, 65, 0),thr = 0.00):
kpts = np.array(kpts).reshape(-1, 3)
for idx in range(kpts.shape[0]):
x = kpts[idx, 0]
y = kpts[idx, 1]
score = kpts[idx,2]
if x > 0 and y > 0 and score > thr:
pos = (int(x), int(y))
cv2.circle(img, pos, 3, color, -1)
return img
def show_coco_skelenton(img,kpts,color = (255,128,128),thr = 0.01):
# 0:“鼻子”,
# 1:“左眼”,
# 2:“右眼”,
# 3:“左耳”,
# 4:“右耳”,
# 5:“左肩”
# 6:“右肩”
# 7:“左肘”,
# 8:“右肘”
# 9:“左手腕”
# 10:“刚性手腕”
# 11:“左臀部”
# 12:“右臀部”
# 13:“左膝盖”
# 14:“右膝盖”
# 15:“左脚踝”
# 16:“右脚踝”
kpts = np.array(kpts).reshape(-1, 3)
skelenton = [[16, 14], [14, 12], [17, 15], [15, 13], [12, 13], [6, 12], [7, 13], [6, 7], [6, 8], [7, 9], [8, 10],
[9, 11], [2, 3], [1, 2], [1, 3], [2, 4], [3, 5], [4, 6], [5, 7]]
for sk in skelenton:
pos1 = (int(kpts[sk[0] - 1, 0]), int(kpts[sk[0] - 1, 1]))
pos2 = (int(kpts[sk[1] - 1, 0]), int(kpts[sk[1] - 1, 1]))
if pos1[0]>0 and pos1[1] >0 and pos2[0] >0 and pos2[1] > 0 and kpts[sk[0] - 1, 2] > thr and kpts[sk[1] - 1, 0] > thr:
cv2.line(img, pos1, pos2, color, 2, 8)
return img
def show_mpii_skelenton(img,kpts,color = (255,128,128),thr = 0.01):
# 0 右脚
# 1 右膝盖
# 2 右腰
# 3 左腰
# 4 左膝盖
# 5 左脚
# 6 腰部中间
# 7 胸前
# 8 脖子
# 9 头顶
# 10右手腕
# 11右手肘
# 12右肩
# 13左肩
# 14左手肘
# 15左手腕
kpts = np.array(kpts).reshape(-1, 3)
skelenton = [[1, 2], [2, 3], [3, 7], [7, 4], [4, 5], [5, 6], [7, 8], [8, 9], [9, 10], [8, 13], [13, 12],
[12, 11], [8, 14], [14, 15], [15, 16]]
for sk in skelenton:
pos1 = (int(kpts[sk[0] - 1, 0]), int(kpts[sk[0] - 1, 1]))
pos2 = (int(kpts[sk[1] - 1, 0]), int(kpts[sk[1] - 1, 1]))
if pos1[0] > 0 and pos1[1] > 0 and pos2[0] > 0 and pos2[1] > 0 and kpts[sk[0] - 1, 2] > thr and kpts[
sk[1] - 1, 0] > thr:
cv2.line(img, pos1, pos2, color, 2, 8)
return img
def show_body(img, kpts, color=(128, 65, 0),thr = 0.00):
kpts = np.array(kpts).reshape(-1, 3)
for idx in range(kpts.shape[0]):
x = kpts[idx, 0]
y = kpts[idx, 1]
score = kpts[idx,2]
if x > 0 and y > 0 and score > thr:
pos = (int(x), int(y))
cv2.circle(img, pos, 3, color, -1)
# font = cv2.FONT_HERSHEY_SIMPLEX
# cv2.putText(img, str(idx + 1), pos, font, 1.2, (255, 255, 255), 2)
return img
def show_hand_21kpts(img,hand_list,thr = 0):
hand_list = np.array(hand_list).reshape(-1,3)
hand_kpt = hand_list[:,0:2]
score = hand_list[:,2:]
# hand_kpt = np.array(hand_kpt).reshape(-1,2)
# score = np.array(score).reshape(-1)
index_x1 = [0, 1, 2, 3, 0, 5, 6, 7, 0, 9, 10, 11, 0, 13, 14, 15, 0, 17, 18, 19]
index_x2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
# index_x1 = [0, 1, 2, 0, 4, 5, 6, 0, 8, 9, 10, 0, 12, 13, 14, 0, 16, 17, 18]
# index_x2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
color_index = [0, 0, 0,0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4]
# color_index = [0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4]
color_list = [(255, 0, 0), (75, 75, 35), (96, 0, 128), (0, 255, 0), (0, 0, 255)]
for idx in range(len(index_x1)):
x1 = index_x1[idx]
x2 = index_x2[idx]
color = color_list[color_index[idx]]
if hand_kpt[x1][0] > 0 and hand_kpt[x1][1] and hand_kpt[x2][0] > 0 and hand_kpt[x2][1] \
and score[x1] > thr and score[x2] > thr:
pos_x1 = (int(hand_kpt[x1][0]), int(hand_kpt[x1][1]))
pos_x2 = (int(hand_kpt[x2][0]), int(hand_kpt[x2][1]))
cv2.line(img, pos_x1, pos_x2, color, 1, 8)
return img